completion.spec.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import { replaceWord, addPositionToWords, wordAtPosition, splitByWhitespaceBoundary } from '../../../../../src/services/completion/completion.js'
  2. describe('addPositiontoWords', () => {
  3. it('adds the position to a word list', () => {
  4. const words = ['hey', ' ', 'this', ' ', 'is', ' ', 'fun']
  5. const expected = [
  6. {
  7. word: 'hey',
  8. start: 0,
  9. end: 3
  10. },
  11. {
  12. word: ' ',
  13. start: 3,
  14. end: 4
  15. },
  16. {
  17. word: 'this',
  18. start: 4,
  19. end: 8
  20. },
  21. {
  22. word: ' ',
  23. start: 8,
  24. end: 9
  25. },
  26. {
  27. word: 'is',
  28. start: 9,
  29. end: 11
  30. },
  31. {
  32. word: ' ',
  33. start: 11,
  34. end: 12
  35. },
  36. {
  37. word: 'fun',
  38. start: 12,
  39. end: 15
  40. }
  41. ]
  42. const res = addPositionToWords(words)
  43. expect(res).to.eql(expected)
  44. })
  45. })
  46. describe('splitByWhitespaceBoundary', () => {
  47. it('splits at whitespace boundaries', () => {
  48. const str = 'This is a #nice @test for you, @idiot@idiot.com'
  49. const expected = ['This', ' ', 'is', ' ', 'a', ' ', '#nice', ' ', '@test', ' ', 'for', ' ', 'you,', ' ', '@idiot@idiot.com']
  50. const res = splitByWhitespaceBoundary(str)
  51. expect(res).to.eql(expected)
  52. })
  53. })
  54. describe('wordAtPosition', () => {
  55. it('returns the word for a given string and postion, plus the start and end position of that word', () => {
  56. const str = 'Hey this is fun'
  57. const { word, start, end } = wordAtPosition(str, 4)
  58. expect(word).to.eql('this')
  59. expect(start).to.eql(4)
  60. expect(end).to.eql(8)
  61. })
  62. })
  63. describe('replaceWord', () => {
  64. it('replaces a word (with start and end) with another word in a given string', () => {
  65. const str = 'hey @take , how are you'
  66. const wordsWithPosition = addPositionToWords(splitByWhitespaceBoundary(str))
  67. const toReplace = wordsWithPosition[2]
  68. expect(toReplace.word).to.eql('@take')
  69. const expected = 'hey @takeshitakenji , how are you'
  70. const res = replaceWord(str, toReplace, '@takeshitakenji')
  71. expect(res).to.eql(expected)
  72. })
  73. })