|
| 1 | +import { shallow } from '../shallow'; |
| 2 | + |
| 3 | +describe('shallow', () => { |
| 4 | + it('returns true for identical primitives', () => { |
| 5 | + expect(shallow(1, 1)).toBe(true); |
| 6 | + expect(shallow('a', 'a')).toBe(true); |
| 7 | + expect(shallow(true, true)).toBe(true); |
| 8 | + expect(shallow(null, null)).toBe(true); |
| 9 | + expect(shallow(undefined, undefined)).toBe(true); |
| 10 | + }); |
| 11 | + |
| 12 | + it('returns false for different primitives', () => { |
| 13 | + expect(shallow(1, 2)).toBe(false); |
| 14 | + expect(shallow('a', 'b')).toBe(false); |
| 15 | + expect(shallow(true, false)).toBe(false); |
| 16 | + }); |
| 17 | + |
| 18 | + it('returns true for same object reference', () => { |
| 19 | + const obj = { a: 1 }; |
| 20 | + expect(shallow(obj, obj)).toBe(true); |
| 21 | + }); |
| 22 | + |
| 23 | + it('returns true for objects with same top-level values', () => { |
| 24 | + expect(shallow({ a: 1, b: 2 }, { a: 1, b: 2 })).toBe(true); |
| 25 | + }); |
| 26 | + |
| 27 | + it('returns false for objects with different top-level values', () => { |
| 28 | + expect(shallow({ a: 1 }, { a: 2 })).toBe(false); |
| 29 | + expect(shallow({ a: 1 }, { a: 1, b: 2 })).toBe(false); |
| 30 | + }); |
| 31 | + |
| 32 | + it('does not deeply compare nested objects', () => { |
| 33 | + const nested1 = { a: { b: 1 } }; |
| 34 | + const nested2 = { a: { b: 1 } }; |
| 35 | + expect(shallow(nested1, nested2)).toBe(false); |
| 36 | + }); |
| 37 | + |
| 38 | + it('returns true for same nested object reference', () => { |
| 39 | + const inner = { b: 1 }; |
| 40 | + expect(shallow({ a: inner }, { a: inner })).toBe(true); |
| 41 | + }); |
| 42 | + |
| 43 | + it('returns true for equal arrays', () => { |
| 44 | + expect(shallow([1, 2, 3], [1, 2, 3])).toBe(true); |
| 45 | + }); |
| 46 | + |
| 47 | + it('returns false for arrays with different values', () => { |
| 48 | + expect(shallow([1, 2], [1, 3])).toBe(false); |
| 49 | + expect(shallow([1, 2], [1, 2, 3])).toBe(false); |
| 50 | + }); |
| 51 | + |
| 52 | + it('returns true for equal Maps', () => { |
| 53 | + const mapA = new Map([ |
| 54 | + ['a', 1], |
| 55 | + ['b', 2], |
| 56 | + ]); |
| 57 | + const mapB = new Map([ |
| 58 | + ['a', 1], |
| 59 | + ['b', 2], |
| 60 | + ]); |
| 61 | + expect(shallow(mapA, mapB)).toBe(true); |
| 62 | + }); |
| 63 | + |
| 64 | + it('returns false for Maps with different values', () => { |
| 65 | + const mapA = new Map([['a', 1]]); |
| 66 | + const mapB = new Map([['a', 2]]); |
| 67 | + expect(shallow(mapA, mapB)).toBe(false); |
| 68 | + }); |
| 69 | + |
| 70 | + it('returns true for equal Sets', () => { |
| 71 | + const setA = new Set([1, 2, 3]); |
| 72 | + const setB = new Set([1, 2, 3]); |
| 73 | + expect(shallow(setA, setB)).toBe(true); |
| 74 | + }); |
| 75 | + |
| 76 | + it('returns false for Sets with different values', () => { |
| 77 | + const setA = new Set([1, 2]); |
| 78 | + const setB = new Set([1, 3]); |
| 79 | + expect(shallow(setA, setB)).toBe(false); |
| 80 | + }); |
| 81 | + |
| 82 | + it('returns false for different prototypes', () => { |
| 83 | + class A { |
| 84 | + x = 1; |
| 85 | + } |
| 86 | + class B { |
| 87 | + x = 1; |
| 88 | + } |
| 89 | + expect(shallow(new A(), new B())).toBe(false); |
| 90 | + }); |
| 91 | + |
| 92 | + it('returns false when comparing object to null', () => { |
| 93 | + expect(shallow({ a: 1 }, null)).toBe(false); |
| 94 | + expect(shallow(null, { a: 1 })).toBe(false); |
| 95 | + }); |
| 96 | +}); |
0 commit comments