cache.test.ts 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import { WordCache } from '@/features/source-editor/extensions/spelling/cache'
  2. import { expect } from 'chai'
  3. import { Word } from '@/features/source-editor/extensions/spelling/spellchecker'
  4. describe('WordCache', function () {
  5. describe('basic operations', function () {
  6. let cache: WordCache, lang: string
  7. beforeEach(function () {
  8. cache = new WordCache()
  9. lang = 'xx'
  10. })
  11. it('should store values in cache', function () {
  12. let word = 'foo'
  13. expect(cache.get(lang, word)).to.not.exist
  14. cache.set(lang, word, true)
  15. expect(cache.get(lang, word)).to.equal(true)
  16. word = 'bar'
  17. expect(cache.get(lang, word)).to.not.exist
  18. cache.set(lang, word, false)
  19. expect(cache.get(lang, word)).to.equal(false)
  20. })
  21. it('should store words in separate languages', function () {
  22. const word = 'foo'
  23. const otherLang = 'zz'
  24. cache.set(lang, word, true)
  25. expect(cache.get(lang, word)).to.equal(true)
  26. expect(cache.get(otherLang, word)).to.not.exist
  27. cache.set(otherLang, word, false)
  28. expect(cache.get(lang, word)).to.equal(true)
  29. expect(cache.get(otherLang, word)).to.equal(false)
  30. })
  31. it('should check words against cache', function () {
  32. cache.set(lang, 'foo', false)
  33. cache.set(lang, 'bar', true)
  34. cache.set(lang, 'baz', true)
  35. const wordsToCheck = [
  36. { text: 'foo', from: 0 },
  37. { text: 'baz', from: 1 },
  38. { text: 'quux', from: 2 },
  39. { text: 'foo', from: 3 },
  40. { text: 'zaz', from: 4 },
  41. ] as Word[]
  42. const result = cache.checkWords(lang, wordsToCheck)
  43. expect(result).to.have.keys('knownMisspelledWords', 'unknownWords')
  44. expect(result.knownMisspelledWords).to.deep.equal([
  45. { text: 'foo', from: 0 },
  46. { text: 'foo', from: 3 },
  47. ])
  48. expect(result.unknownWords).to.deep.equal([
  49. { text: 'quux', from: 2 },
  50. { text: 'zaz', from: 4 },
  51. ])
  52. })
  53. })
  54. })