Map.groupBy.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. test("length is 2", () => {
  2. expect(Map.groupBy).toHaveLength(2);
  3. });
  4. describe("errors", () => {
  5. test("callback must be a function", () => {
  6. expect(() => {
  7. Map.groupBy([], undefined);
  8. }).toThrowWithMessage(TypeError, "undefined is not a function");
  9. });
  10. test("null or undefined items value", () => {
  11. expect(() => {
  12. Map.groupBy();
  13. }).toThrowWithMessage(TypeError, "undefined cannot be converted to an object");
  14. expect(() => {
  15. Map.groupBy(undefined);
  16. }).toThrowWithMessage(TypeError, "undefined cannot be converted to an object");
  17. expect(() => {
  18. Map.groupBy(null);
  19. }).toThrowWithMessage(TypeError, "null cannot be converted to an object");
  20. });
  21. });
  22. describe("normal behavior", () => {
  23. test("basic functionality", () => {
  24. const array = [1, 2, 3, 4, 5, 6];
  25. const visited = [];
  26. const trueObject = { true: true };
  27. const falseObject = { false: false };
  28. const firstResult = Map.groupBy(array, value => {
  29. visited.push(value);
  30. return value % 2 === 0 ? trueObject : falseObject;
  31. });
  32. expect(visited).toEqual([1, 2, 3, 4, 5, 6]);
  33. expect(firstResult).toBeInstanceOf(Map);
  34. expect(firstResult.size).toBe(2);
  35. expect(firstResult.get(trueObject)).toEqual([2, 4, 6]);
  36. expect(firstResult.get(falseObject)).toEqual([1, 3, 5]);
  37. const secondResult = Map.groupBy(array, (_, index) => {
  38. return index < array.length / 2 ? trueObject : falseObject;
  39. });
  40. expect(secondResult).toBeInstanceOf(Map);
  41. expect(secondResult.size).toBe(2);
  42. expect(secondResult.get(trueObject)).toEqual([1, 2, 3]);
  43. expect(secondResult.get(falseObject)).toEqual([4, 5, 6]);
  44. });
  45. test("never calls callback with empty array", () => {
  46. var callbackCalled = 0;
  47. const result = Map.groupBy([], () => {
  48. callbackCalled++;
  49. });
  50. expect(result).toBeInstanceOf(Map);
  51. expect(result.size).toBe(0);
  52. expect(callbackCalled).toBe(0);
  53. });
  54. test("calls callback once for every item", () => {
  55. var callbackCalled = 0;
  56. const result = Map.groupBy([1, 2, 3], () => {
  57. callbackCalled++;
  58. });
  59. expect(result).toBeInstanceOf(Map);
  60. expect(result.size).toBe(1);
  61. expect(result.get(undefined)).toEqual([1, 2, 3]);
  62. expect(callbackCalled).toBe(3);
  63. });
  64. test("still returns a Map even if the global Map constructor was changed", () => {
  65. const mapGroupBy = Map.groupBy;
  66. globalThis.Map = null;
  67. const result = mapGroupBy([1, 2], value => {
  68. return value % 2 === 0;
  69. });
  70. expect(result.size).toBe(2);
  71. expect(result.get(true)).toEqual([2]);
  72. expect(result.get(false)).toEqual([1]);
  73. });
  74. });