serialization_test.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. suite('serialization', function () {
  2. setup(function () {
  3. var documents = [{
  4. id: 'a',
  5. title: 'Mr. Green kills Colonel Mustard',
  6. body: 'Mr. Green killed Colonel Mustard in the study with the candlestick. Mr. Green is not a very nice fellow.',
  7. wordCount: 19
  8. },{
  9. id: 'b',
  10. title: 'Plumb waters plant',
  11. body: 'Professor Plumb has a green plant in his study',
  12. wordCount: 9
  13. },{
  14. id: 'c',
  15. title: 'Scarlett helps Professor',
  16. body: 'Miss Scarlett watered Professor Plumbs green plant while he was away from his office last week.',
  17. wordCount: 16
  18. },{
  19. id: 'd',
  20. title: 'All about JavaScript',
  21. body: 'JavaScript objects have a special __proto__ property',
  22. wordCount: 7
  23. }]
  24. this.idx = lunr(function () {
  25. this.ref('id')
  26. this.field('title')
  27. this.field('body')
  28. documents.forEach(function (document) {
  29. this.add(document)
  30. }, this)
  31. })
  32. this.serializedIdx = JSON.stringify(this.idx)
  33. this.loadedIdx = lunr.Index.load(JSON.parse(this.serializedIdx))
  34. })
  35. test('search', function () {
  36. var idxResults = this.idx.search('green'),
  37. serializedResults = this.loadedIdx.search('green')
  38. assert.deepEqual(idxResults, serializedResults)
  39. })
  40. test('__proto__ double serialization', function () {
  41. var doubleLoadedIdx = lunr.Index.load(JSON.parse(JSON.stringify(this.loadedIdx))),
  42. idxResults = this.idx.search('__proto__'),
  43. doubleSerializedResults = doubleLoadedIdx.search('__proto__')
  44. assert.deepEqual(idxResults, doubleSerializedResults)
  45. })
  46. })