|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { isEqual } from "./is-equal"; |
| 3 | + |
| 4 | +describe("isEqual helper", () => { |
| 5 | + it("should return true for identical primitive values", () => { |
| 6 | + expect(isEqual(1, 1)).toBe(true); |
| 7 | + expect(isEqual("string", "string")).toBe(true); |
| 8 | + expect(isEqual(true, true)).toBe(true); |
| 9 | + expect(isEqual(null, null)).toBe(true); |
| 10 | + expect(isEqual(undefined, undefined)).toBe(true); |
| 11 | + }); |
| 12 | + |
| 13 | + it("should return false for different primitive values", () => { |
| 14 | + expect(isEqual(1, 2)).toBe(false); |
| 15 | + expect(isEqual("string", "different")).toBe(false); |
| 16 | + expect(isEqual(true, false)).toBe(false); |
| 17 | + expect(isEqual(null, undefined)).toBe(false); |
| 18 | + expect(isEqual(0, null)).toBe(false); |
| 19 | + }); |
| 20 | + |
| 21 | + it("should handle NaN values correctly", () => { |
| 22 | + expect(isEqual(NaN, NaN)).toBe(true); |
| 23 | + expect(isEqual(NaN, 1)).toBe(false); |
| 24 | + expect(isEqual(1, NaN)).toBe(false); |
| 25 | + }); |
| 26 | + |
| 27 | + it("should compare arrays correctly", () => { |
| 28 | + expect(isEqual([1, 2, 3], [1, 2, 3])).toBe(true); |
| 29 | + expect(isEqual([1, 2, 3], [1, 2, 4])).toBe(false); |
| 30 | + expect(isEqual([1, 2, 3], [1, 2])).toBe(false); |
| 31 | + expect(isEqual([], [])).toBe(true); |
| 32 | + expect(isEqual([1, [2, 3]], [1, [2, 3]])).toBe(true); |
| 33 | + expect(isEqual([1, [2, 3]], [1, [2, 4]])).toBe(false); |
| 34 | + }); |
| 35 | + |
| 36 | + it("should compare objects correctly", () => { |
| 37 | + expect(isEqual({}, {})).toBe(true); |
| 38 | + expect(isEqual({ a: 1, b: 2 }, { a: 1, b: 2 })).toBe(true); |
| 39 | + expect(isEqual({ a: 1, b: 2 }, { b: 2, a: 1 })).toBe(true); |
| 40 | + expect(isEqual({ a: 1, b: 2 }, { a: 1, b: 3 })).toBe(false); |
| 41 | + expect(isEqual({ a: 1, b: 2 }, { a: 1 })).toBe(false); |
| 42 | + expect(isEqual({ a: 1 }, { a: 1, b: 2 })).toBe(false); |
| 43 | + }); |
| 44 | + |
| 45 | + it("should handle nested objects", () => { |
| 46 | + expect(isEqual({ a: { b: 1 } }, { a: { b: 1 } })).toBe(true); |
| 47 | + expect(isEqual({ a: { b: 1 } }, { a: { b: 2 } })).toBe(false); |
| 48 | + expect(isEqual({ a: { b: { c: 3 } } }, { a: { b: { c: 3 } } })).toBe(true); |
| 49 | + expect(isEqual({ a: { b: { c: 3 } } }, { a: { b: { c: 4 } } })).toBe(false); |
| 50 | + }); |
| 51 | + |
| 52 | + it("should handle objects with array values", () => { |
| 53 | + expect(isEqual({ a: [1, 2, 3] }, { a: [1, 2, 3] })).toBe(true); |
| 54 | + expect(isEqual({ a: [1, 2, 3] }, { a: [1, 2, 4] })).toBe(false); |
| 55 | + expect(isEqual({ a: [1, { b: 2 }] }, { a: [1, { b: 2 }] })).toBe(true); |
| 56 | + expect(isEqual({ a: [1, { b: 2 }] }, { a: [1, { b: 3 }] })).toBe(false); |
| 57 | + }); |
| 58 | + |
| 59 | + it("should handle arrays with object elements", () => { |
| 60 | + expect(isEqual([{ a: 1 }, { b: 2 }], [{ a: 1 }, { b: 2 }])).toBe(true); |
| 61 | + expect(isEqual([{ a: 1 }, { b: 2 }], [{ a: 1 }, { b: 3 }])).toBe(false); |
| 62 | + }); |
| 63 | + |
| 64 | + it("should handle RegExp objects", () => { |
| 65 | + expect(isEqual(/abc/g, /abc/g)).toBe(true); |
| 66 | + expect(isEqual(/abc/g, /abc/i)).toBe(false); |
| 67 | + expect(isEqual(/abc/g, /def/g)).toBe(false); |
| 68 | + }); |
| 69 | + |
| 70 | + it("should handle objects with custom valueOf method", () => { |
| 71 | + const date1 = new Date("2023-01-01"); |
| 72 | + const date2 = new Date("2023-01-01"); |
| 73 | + const date3 = new Date("2023-01-02"); |
| 74 | + |
| 75 | + expect(isEqual(date1, date2)).toBe(true); |
| 76 | + expect(isEqual(date1, date3)).toBe(false); |
| 77 | + }); |
| 78 | + |
| 79 | + it("should handle objects with custom toString method", () => { |
| 80 | + const obj1 = { |
| 81 | + toString() { |
| 82 | + return "custom string"; |
| 83 | + }, |
| 84 | + }; |
| 85 | + |
| 86 | + const obj2 = { |
| 87 | + toString() { |
| 88 | + return "custom string"; |
| 89 | + }, |
| 90 | + }; |
| 91 | + |
| 92 | + const obj3 = { |
| 93 | + toString() { |
| 94 | + return "different string"; |
| 95 | + }, |
| 96 | + }; |
| 97 | + |
| 98 | + expect(isEqual(obj1, obj2)).toBe(true); |
| 99 | + expect(isEqual(obj1, obj3)).toBe(false); |
| 100 | + }); |
| 101 | + |
| 102 | + it("should handle objects with different constructors", () => { |
| 103 | + class A {} |
| 104 | + class B {} |
| 105 | + |
| 106 | + expect(isEqual(new A(), new A())).toBe(true); |
| 107 | + expect(isEqual(new A(), new B())).toBe(false); |
| 108 | + }); |
| 109 | +}); |
0 commit comments