delete.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. var Similar = require('../src/similar');
  2. describe("simple key", () => {
  3. var similar = new Similar(),
  4. entry = { key: 'stringkey', val: 'stringval' };
  5. similar.set(entry.key, entry.val);
  6. similar.delete(entry.key);
  7. it("has proper length", () => { expect(similar.list.length).toEqual(0); });
  8. it("has proper size", () => { expect(similar.size).toEqual(0); });
  9. it("has proper lastItem", () => { expect(similar.lastItem).toEqual(undefined); });
  10. it("resolves has() correctly", () => { expect(similar.has(entry.key)).toEqual(false); expect(similar.has(undefined)).toEqual(false); });
  11. it("resolves get() correctly", () => { expect(similar.get(entry.key)).toEqual(undefined); });
  12. });
  13. describe("NaN key", () => {
  14. var similar = new Similar(),
  15. entry1 = { key: NaN, val: { prop: 'propval' } },
  16. entry2 = { key: ['x','y'], val: { prop: 'propval2' } };
  17. similar.set(entry1.key, entry1.val);
  18. similar.set(entry2.key, entry2.val);
  19. similar.delete(entry1.key);
  20. it("has proper length", () => { expect(similar.list.length).toEqual(1); });
  21. it("has proper size", () => { expect(similar.size).toEqual(1); });
  22. it("has proper lastItem", () => { expect(similar.lastItem.key).toEqual(entry2.key); });
  23. it("resolves entry1 has() correctly", () => { expect(similar.has(entry1.key)).toEqual(false); });
  24. it("resolves entry1 get() correctly", () => { expect(similar.get(entry1.key)).toEqual(undefined); });
  25. it("resolves entry2 has() correctly", () => { expect(similar.has(entry2.key)).toEqual(true); });
  26. it("resolves entry2 get() correctly", () => { expect(similar.get(entry2.key)).toEqual(entry2.val); });
  27. if (!!Map) {
  28. var map = new Map();
  29. map.set(entry1.key, entry1.val);
  30. map.set(entry2.key, entry2.val);
  31. map.delete(entry1.key);
  32. it("matches Map entry1 has() correctly", () => { expect(similar.has(entry1.key)).toEqual(map.has(entry1.key)); });
  33. it("matches Map entry1 get() correctly", () => { expect(similar.get(entry1.key)).toEqual(map.get(entry1.key)); });
  34. it("matches Map entry2 has() correctly", () => { expect(similar.has(entry2.key)).toEqual(similar.has(entry2.key)); });
  35. it("matches Map entry2 get() correctly", () => { expect(similar.get(entry2.key)).toEqual(similar.get(entry2.key)); });
  36. }
  37. });
  38. describe("complex key", () => {
  39. var similar = new Similar(),
  40. entry1 = { key: ['a','b'], val: { prop: 'propval' } },
  41. entry2 = { key: ['x','y'], val: { prop: 'propval2' } };
  42. similar.set(entry1.key, entry1.val);
  43. similar.set(entry2.key, entry2.val);
  44. similar.delete(entry1.key);
  45. it("has proper length", () => { expect(similar.list.length).toEqual(1); });
  46. it("has proper size", () => { expect(similar.size).toEqual(1); });
  47. it("has proper lastItem", () => { expect(similar.lastItem.key).toEqual(entry2.key); });
  48. it("resolves entry1 has() correctly", () => { expect(similar.has(entry1.key)).toEqual(false); });
  49. it("resolves entry1 get() correctly", () => { expect(similar.get(entry1.key)).toEqual(undefined); });
  50. it("resolves entry2 has() correctly", () => { expect(similar.has(entry2.key)).toEqual(true); });
  51. it("resolves entry2 get() correctly", () => { expect(similar.get(entry2.key)).toEqual(entry2.val); });
  52. if (!!Map) {
  53. var map = new Map();
  54. map.set(entry1.key, entry1.val);
  55. map.set(entry2.key, entry2.val);
  56. map.delete(entry1.key);
  57. it("matches Map entry1 has() correctly", () => { expect(similar.has(entry1.key)).toEqual(map.has(entry1.key)); });
  58. it("matches Map entry1 get() correctly", () => { expect(similar.get(entry1.key)).toEqual(map.get(entry1.key)); });
  59. it("matches Map entry2 has() correctly", () => { expect(similar.has(entry2.key)).toEqual(similar.has(entry2.key)); });
  60. it("matches Map entry2 get() correctly", () => { expect(similar.get(entry2.key)).toEqual(similar.get(entry2.key)); });
  61. }
  62. });
  63. describe("from empty cache", () => {
  64. var similar = new Similar(),
  65. entry = { key: 'stringkey', val: 'stringval' };
  66. similar.delete(entry.key);
  67. it("has proper length", () => { expect(similar.list.length).toEqual(0); });
  68. it("has proper size", () => { expect(similar.size).toEqual(0); });
  69. it("has proper lastItem", () => { expect(similar.lastItem).toEqual(undefined); });
  70. it("resolves has() correctly", () => { expect(similar.has(entry.key)).toEqual(false); expect(similar.has(undefined)).toEqual(false); });
  71. it("resolves get() correctly", () => { expect(similar.get(entry.key)).toEqual(undefined); });
  72. if (!!Map) {
  73. var map = new Map();
  74. map.delete(entry.key);
  75. it("resolves has() correctly", () => { expect(map.has(entry.key)).toEqual(false); expect(map.has(undefined)).toEqual(false); });
  76. it("resolves get() correctly", () => { expect(map.get(entry.key)).toEqual(undefined); });
  77. }
  78. });