|
| 1 | +import { isEqual } from "../utils/isEqual"; |
| 2 | + |
| 3 | +describe("isEqual", () => { |
| 4 | + test("correctly compares against undefined", () => { |
| 5 | + expect(isEqual(undefined, undefined)).toBe(true); |
| 6 | + expect(isEqual(undefined, null)).toBe(false); |
| 7 | + expect(isEqual(undefined, 1)).toBe(false); |
| 8 | + expect(isEqual(undefined, "foo")).toBe(false); |
| 9 | + expect(isEqual(undefined, {})).toBe(false); |
| 10 | + expect(isEqual(undefined, [])).toBe(false); |
| 11 | + }); |
| 12 | + |
| 13 | + test("correct compares against nulls", () => { |
| 14 | + expect(isEqual(null, null)).toBe(true); |
| 15 | + expect(isEqual(null, 1)).toBe(false); |
| 16 | + expect(isEqual(null, "foo")).toBe(false); |
| 17 | + expect(isEqual(null, {})).toBe(false); |
| 18 | + expect(isEqual(null, [])).toBe(false); |
| 19 | + }); |
| 20 | + |
| 21 | + test("compares simple values", () => { |
| 22 | + expect(isEqual(1, 1)).toBe(true); |
| 23 | + expect(isEqual(1, 2)).toBe(false); |
| 24 | + expect(isEqual("foo", "foo")).toBe(true); |
| 25 | + expect(isEqual("foo", "bar")).toBe(false); |
| 26 | + }); |
| 27 | + |
| 28 | + test("compares objects", () => { |
| 29 | + expect(isEqual({}, {})).toBe(true); |
| 30 | + expect(isEqual({ foo: "bar" }, { foo: "bar" })).toBe(true); |
| 31 | + expect(isEqual({ foo: "bar" }, { foo: "baz" })).toBe(false); |
| 32 | + expect(isEqual({ foo: "bar" }, { bar: "baz" })).toBe(false); |
| 33 | + expect(isEqual({ foo: "bar" }, { foo: "bar", bar: "baz" })).toBe(false); |
| 34 | + }); |
| 35 | + |
| 36 | + test("deep compares objects", () => { |
| 37 | + expect(isEqual({ foo: { bar: "baz" } }, { foo: { bar: "baz" } })).toBe(true); |
| 38 | + expect(isEqual({ foo: { bar: "baz" } }, { foo: { bar: "qux" } })).toBe(false); |
| 39 | + expect(isEqual({ foo: { bar: "baz" } }, { foo: { baz: "qux" } })).toBe(false); |
| 40 | + expect(isEqual({ foo: { bar: "baz" } }, { foo: { bar: "baz", baz: "qux" } })).toBe(false); |
| 41 | + }); |
| 42 | + |
| 43 | + test("compares arrays", () => { |
| 44 | + expect(isEqual([], [])).toBe(true); |
| 45 | + expect(isEqual([1, 2, 3], [1, 2, 3])).toBe(true); |
| 46 | + expect(isEqual([1, 2, 3], [1, 2, 4])).toBe(false); |
| 47 | + expect(isEqual([1, 2, 3], [1, 2])).toBe(false); |
| 48 | + expect(isEqual([1, 2, 3], [1, 2, 3, 4])).toBe(false); |
| 49 | + }); |
| 50 | + |
| 51 | + test("compares maps", () => { |
| 52 | + const map1 = new Map(); |
| 53 | + map1.set("foo", "bar"); |
| 54 | + const map2 = new Map(); |
| 55 | + |
| 56 | + expect(isEqual(map1, map2)).toBe(false); |
| 57 | + }); |
| 58 | + |
| 59 | + test("compares dates", () => { |
| 60 | + expect(isEqual(new Date(1), new Date(1))).toBe(true); |
| 61 | + expect(isEqual(new Date(1), new Date(2))).toBe(false); |
| 62 | + }); |
| 63 | + |
| 64 | + test("compares nested arrays", () => { |
| 65 | + expect(isEqual([[1], [2], [3]], [[1], [2], [3]])).toBe(true); |
| 66 | + expect(isEqual([[1], [2], [3]], [[1], [2], [4]])).toBe(false); |
| 67 | + }); |
| 68 | + |
| 69 | + test("compares nested objects and arrays", () => { |
| 70 | + expect(isEqual({ a: { b: [1] } }, { a: { b: [1] } })).toBe(true); |
| 71 | + expect(isEqual({ a: { b: [1] } }, { a: { b: [2] } })).toBe(false); |
| 72 | + }); |
| 73 | + |
| 74 | + const testFunction = () => { return 1 }; |
| 75 | + test("functions", () => { |
| 76 | + expect(isEqual(() => { return 1 }, () => { return 2 })).toBe(false); |
| 77 | + expect(isEqual(testFunction, testFunction)).toBe(true); |
| 78 | + }); |
| 79 | + |
| 80 | + test("objects with functions", () => { |
| 81 | + expect(isEqual({ a: () => 1 }, { a: () => 1 })).toBe(false); |
| 82 | + expect(isEqual({ a: testFunction }, { a: testFunction })).toBe(true); |
| 83 | + }); |
| 84 | + |
| 85 | + test("regExp", () => { |
| 86 | + expect(isEqual(/a(.*)/, /a(.*)/)).toBe(true); |
| 87 | + expect(isEqual(/a/, /b.*/)).toBe(false); |
| 88 | + }); |
| 89 | + |
| 90 | + test("deepEquals with Error objects", () => { |
| 91 | + const error1 = new Error("test error"); |
| 92 | + const error2 = new Error("test error"); |
| 93 | + expect(isEqual(error1, error1)).toBe(true); |
| 94 | + expect(isEqual(error1, error2)).toBe(false); |
| 95 | + }); |
| 96 | + |
| 97 | + test("array buffers", () => { |
| 98 | + const buffer1 = new ArrayBuffer(2); |
| 99 | + const buffer1View = new Uint8Array(buffer1); |
| 100 | + buffer1View.set([42, 43]); |
| 101 | + |
| 102 | + const buffer2 = new ArrayBuffer(2); |
| 103 | + const buffer2View = new Uint8Array(buffer2); |
| 104 | + buffer2View.set([42, 43]); |
| 105 | + |
| 106 | + const buffer3 = new ArrayBuffer(2); |
| 107 | + const buffer3View = new Uint8Array(buffer3); |
| 108 | + buffer3View.set([42, 44]); |
| 109 | + |
| 110 | + const buffer4 = new ArrayBuffer(3); |
| 111 | + |
| 112 | + expect(isEqual(buffer1, buffer2)).toBe(true); |
| 113 | + expect(isEqual(buffer1, buffer3)).toBe(false); |
| 114 | + expect(isEqual(buffer1, buffer4)).toBe(false); |
| 115 | + }); |
| 116 | + |
| 117 | + test("typed arrays", () => { |
| 118 | + expect(isEqual(new Uint8Array([1, 2, 3]), new Uint8Array([1, 2, 3]))).toBe(true); |
| 119 | + expect(isEqual(new Uint8Array([1, 2, 3]), new Uint8Array([1, 2]))).toBe(false); |
| 120 | + expect(isEqual(new Uint8Array([1, 2, 3]), new Uint8Array([1, 2, 4]))).toBe(false); |
| 121 | + }); |
| 122 | + |
| 123 | + test("data views", () => { |
| 124 | + const buffer1 = new ArrayBuffer(2); |
| 125 | + const buffer2 = new ArrayBuffer(2); |
| 126 | + const buffer3 = new ArrayBuffer(3); |
| 127 | + |
| 128 | + const view1 = new DataView(buffer1); |
| 129 | + const view2 = new DataView(buffer2); |
| 130 | + const view3 = new DataView(buffer3); |
| 131 | + |
| 132 | + view1.setUint8(0, 42); |
| 133 | + view1.setUint8(1, 43); |
| 134 | + |
| 135 | + view2.setUint8(0, 42); |
| 136 | + view2.setUint8(1, 43); |
| 137 | + |
| 138 | + expect(isEqual(view1, view2)).toBe(true); |
| 139 | + expect(isEqual(view1, view3)).toBe(false); |
| 140 | + }); |
| 141 | +}); |
0 commit comments