From d6f288221d3550ca1a6302c6b6e11875287f36e6 Mon Sep 17 00:00:00 2001 From: grzdev Date: Sun, 11 Oct 2026 18:59:15 +0100 Subject: [PATCH] test(store): add unit tests for shallow equality utility --- packages/store/tests/shallow.test.ts | 84 ++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 packages/store/tests/shallow.test.ts diff --git a/packages/store/tests/shallow.test.ts b/packages/store/tests/shallow.test.ts new file mode 100644 index 00000000..548e5eab --- /dev/null +++ b/packages/store/tests/shallow.test.ts @@ -0,0 +1,84 @@ +import { describe, expect, test } from 'vitest' +import { shallow } from '../src/shallow' + +describe('shallow', () => { + test('returns true for identical primitives and references', () => { + expect(shallow(1, 1)).toBe(true) + expect(shallow('test', 'test')).toBe(true) + expect(shallow(true, true)).toBe(true) + expect(shallow(null, null)).toBe(true) + expect(shallow(undefined, undefined)).toBe(true) + expect(shallow(NaN, NaN)).toBe(true) + + const obj = { a: 1 } + expect(shallow(obj, obj)).toBe(true) + }) + + test('returns false for mismatched primitives and null/objects', () => { + expect(shallow(1, 2)).toBe(false) + expect(shallow('a', 'b')).toBe(false) + expect(shallow(null, {})).toBe(false) + expect(shallow({}, null)).toBe(false) + expect(shallow(undefined, {})).toBe(false) + expect(shallow(1 as any, {})).toBe(false) + }) + + test('compares shallow objects correctly', () => { + expect(shallow({ a: 1, b: '2' }, { a: 1, b: '2' })).toBe(true) + expect(shallow({ a: 1, b: '2' }, { a: 1, b: '3' })).toBe(false) + expect(shallow({ a: 1 }, { a: 1, b: 2 })).toBe(false) + expect(shallow({ a: 1, b: 2 }, { a: 1 })).toBe(false) + }) + + test('compares objects with symbol keys', () => { + const sym = Symbol('key') + expect(shallow({ [sym]: 1, a: 2 }, { [sym]: 1, a: 2 })).toBe(true) + expect(shallow({ [sym]: 1, a: 2 }, { [sym]: 2, a: 2 })).toBe(false) + }) + + test('compares arrays shallowly', () => { + expect(shallow([1, 2, 3], [1, 2, 3])).toBe(true) + expect(shallow([1, 2, 3], [1, 2, 4])).toBe(false) + expect(shallow([1, 2], [1, 2, 3])).toBe(false) + }) + + test('compares Map instances', () => { + const mapA = new Map([ + ['a', 1], + ['b', 2], + ]) + const mapB = new Map([ + ['a', 1], + ['b', 2], + ]) + const mapC = new Map([ + ['a', 1], + ['b', 3], + ]) + const mapD = new Map([['a', 1]]) + + expect(shallow(mapA, mapB)).toBe(true) + expect(shallow(mapA, mapC)).toBe(false) + expect(shallow(mapA, mapD)).toBe(false) + }) + + test('compares Set instances', () => { + const setA = new Set([1, 2, 3]) + const setB = new Set([1, 2, 3]) + const setC = new Set([1, 2, 4]) + const setD = new Set([1, 2]) + + expect(shallow(setA, setB)).toBe(true) + expect(shallow(setA, setC)).toBe(false) + expect(shallow(setA, setD)).toBe(false) + }) + + test('compares Date instances', () => { + const d1 = new Date('2026-01-01T00:00:00.000Z') + const d2 = new Date('2026-01-01T00:00:00.000Z') + const d3 = new Date('2026-01-02T00:00:00.000Z') + + expect(shallow(d1, d2)).toBe(true) + expect(shallow(d1, d3)).toBe(false) + }) +})