foreach.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. var Similar = require('../src/similar');
  2. describe("Foreach with one entry", () => {
  3. var similar = new Similar(),
  4. entry = { key: 'stringkey', val: 'stringval' };
  5. similar.list.push(entry);
  6. similar.size = 1;
  7. it("iterates keys", () => {
  8. similar.forEach((val, key) => {
  9. expect(key).toEqual(entry.key);
  10. });
  11. });
  12. it("iterates vals", () => {
  13. similar.forEach((val, key) => {
  14. expect(val).toEqual(entry.val);
  15. });
  16. });
  17. });
  18. describe("Foreach with NaN entries", () => {
  19. var similar = new Similar(),
  20. entries = [
  21. { key: NaN, val: 'stringval' },
  22. { key: undefined, val: { b: 2 } },
  23. { key: NaN, val: null },
  24. { key: [NaN, 6, '%', { l: 'x'}], val: ['_', 4, { a: 1 }] }
  25. ],
  26. i = 0;
  27. entries.forEach(entry => similar.list.push(entry));
  28. similar.size = entries.length;
  29. it("iterates keys", () => {
  30. i = 0;
  31. similar.forEach((val, key) => {
  32. expect(key).toEqual(entries[i].key);
  33. i++;
  34. });
  35. });
  36. it("iterates vals", () => {
  37. i = 0;
  38. similar.forEach((val, key) => {
  39. expect(val).toEqual(entries[i].val);
  40. i++;
  41. });
  42. });
  43. });
  44. describe("Foreach with multiple complex entries", () => {
  45. var similar = new Similar(),
  46. entries = [
  47. { key: 'stringkey', val: 'stringval' },
  48. { key: { a: 1 }, val: { b: 2 } },
  49. { key: undefined, val: null },
  50. { key: ['a', 6, '%', { l: 'x'}], val: ['_', 4, { a: 1 }] }
  51. ],
  52. i = 0;
  53. entries.forEach(entry => similar.list.push(entry));
  54. similar.size = entries.length;
  55. it("iterates keys", () => {
  56. i = 0;
  57. similar.forEach((val, key) => {
  58. expect(key).toEqual(entries[i].key);
  59. i++;
  60. });
  61. });
  62. it("iterates vals", () => {
  63. i = 0;
  64. similar.forEach((val, key) => {
  65. expect(val).toEqual(entries[i].val);
  66. i++;
  67. });
  68. });
  69. });