completion.spec.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import { replaceWord, addPositionToWords, wordAtPosition, splitIntoWords } 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: 'this',
  13. start: 3,
  14. end: 7
  15. },
  16. {
  17. word: 'is',
  18. start: 7,
  19. end: 9
  20. },
  21. {
  22. word: 'fun',
  23. start: 9,
  24. end: 12
  25. }
  26. ]
  27. const res = addPositionToWords(words)
  28. expect(res).to.eql(expected)
  29. })
  30. })
  31. describe('splitIntoWords', () => {
  32. it('splits at whitespace boundaries', () => {
  33. const str = 'This is a #nice @test for you, @idiot.'
  34. const expected = ['This', ' ', 'is', ' ', 'a', ' ', '#nice', ' ', '@test', ' ', 'for', ' ', 'you', ', ', '@idiot', '.']
  35. const res = splitIntoWords(str)
  36. expect(res).to.eql(expected)
  37. })
  38. })
  39. describe('wordAtPosition', () => {
  40. it('returns the word for a given string and postion, plus the start and end position of that word', () => {
  41. const str = 'Hey this is fun'
  42. const { word, start, end } = wordAtPosition(str, 4)
  43. expect(word).to.eql('this')
  44. expect(start).to.eql(4)
  45. expect(end).to.eql(8)
  46. })
  47. })
  48. describe('replaceWord', () => {
  49. it('replaces a word (with start and end) with another word in a given string', () => {
  50. const str = 'hey @take, how are you'
  51. const wordsWithPosition = addPositionToWords(splitIntoWords(str))
  52. const toReplace = wordsWithPosition[2]
  53. expect(toReplace.word).to.eql('@take')
  54. const expected = 'hey @takeshitakenji, how are you'
  55. const res = replaceWord(str, toReplace, '@takeshitakenji')
  56. expect(res).to.eql(expected)
  57. })
  58. })