|
| 1 | +import type { ReadonlyJsonValue } from '../typesConstants'; |
| 2 | +import { |
| 3 | + StateSnapshotManager, |
| 4 | + defaultDidSnapshotsChange, |
| 5 | +} from './StateSnapshotManager'; |
| 6 | + |
| 7 | +describe(`${defaultDidSnapshotsChange.name}`, () => { |
| 8 | + type SampleInput = Readonly<{ |
| 9 | + snapshotA: ReadonlyJsonValue; |
| 10 | + snapshotB: ReadonlyJsonValue; |
| 11 | + }>; |
| 12 | + |
| 13 | + it('Will detect when two JSON primitives are the same', () => { |
| 14 | + const samples = [ |
| 15 | + { snapshotA: true, snapshotB: true }, |
| 16 | + { snapshotA: 'cat', snapshotB: 'cat' }, |
| 17 | + { snapshotA: 2, snapshotB: 2 }, |
| 18 | + { snapshotA: null, snapshotB: null }, |
| 19 | + ] as const satisfies readonly SampleInput[]; |
| 20 | + |
| 21 | + for (const { snapshotA, snapshotB } of samples) { |
| 22 | + expect(defaultDidSnapshotsChange(snapshotA, snapshotB)).toBe(false); |
| 23 | + } |
| 24 | + }); |
| 25 | + |
| 26 | + it('Will detect when two JSON primitives are different', () => { |
| 27 | + const samples = [ |
| 28 | + { snapshotA: true, snapshotB: false }, |
| 29 | + { snapshotA: 'cat', snapshotB: 'dog' }, |
| 30 | + { snapshotA: 2, snapshotB: 789 }, |
| 31 | + { snapshotA: null, snapshotB: 'blah' }, |
| 32 | + ] as const satisfies readonly SampleInput[]; |
| 33 | + |
| 34 | + for (const { snapshotA, snapshotB } of samples) { |
| 35 | + expect(defaultDidSnapshotsChange(snapshotA, snapshotB)).toBe(true); |
| 36 | + } |
| 37 | + }); |
| 38 | + |
| 39 | + it('Will detect when a value flips from a primitive to an object (or vice versa)', () => { |
| 40 | + expect(defaultDidSnapshotsChange(null, {})).toBe(true); |
| 41 | + expect(defaultDidSnapshotsChange({}, null)).toBe(true); |
| 42 | + }); |
| 43 | + |
| 44 | + it('Will reject numbers that changed by a very small floating-point epsilon', () => { |
| 45 | + expect(defaultDidSnapshotsChange(3, 3 / 1.00000001)).toBe(false); |
| 46 | + }); |
| 47 | + |
| 48 | + it('Will check array values one level deep', () => { |
| 49 | + const snapshotA = [1, 2, 3]; |
| 50 | + |
| 51 | + const snapshotB = [...snapshotA]; |
| 52 | + expect(defaultDidSnapshotsChange(snapshotA, snapshotB)).toBe(false); |
| 53 | + |
| 54 | + const snapshotC = [...snapshotA, 4]; |
| 55 | + expect(defaultDidSnapshotsChange(snapshotA, snapshotC)).toBe(true); |
| 56 | + |
| 57 | + const snapshotD = [...snapshotA, {}]; |
| 58 | + expect(defaultDidSnapshotsChange(snapshotA, snapshotD)).toBe(true); |
| 59 | + }); |
| 60 | + |
| 61 | + it('Will check object values one level deep', () => { |
| 62 | + const snapshotA = { cat: true, dog: true }; |
| 63 | + |
| 64 | + const snapshotB = { ...snapshotA, dog: true }; |
| 65 | + expect(defaultDidSnapshotsChange(snapshotA, snapshotB)).toBe(false); |
| 66 | + |
| 67 | + const snapshotC = { ...snapshotA, bird: true }; |
| 68 | + expect(defaultDidSnapshotsChange(snapshotA, snapshotC)).toBe(true); |
| 69 | + |
| 70 | + const snapshotD = { ...snapshotA, value: {} }; |
| 71 | + expect(defaultDidSnapshotsChange(snapshotA, snapshotD)).toBe(true); |
| 72 | + }); |
| 73 | +}); |
| 74 | + |
| 75 | +describe(`${StateSnapshotManager.name}`, () => { |
| 76 | + it('Lets external systems subscribe and unsubscribe to internal snapshot changes', () => { |
| 77 | + type SampleData = Readonly<{ |
| 78 | + snapshotA: ReadonlyJsonValue; |
| 79 | + snapshotB: ReadonlyJsonValue; |
| 80 | + }>; |
| 81 | + |
| 82 | + const samples = [ |
| 83 | + { snapshotA: false, snapshotB: true }, |
| 84 | + { snapshotA: 0, snapshotB: 1 }, |
| 85 | + { snapshotA: 'cat', snapshotB: 'dog' }, |
| 86 | + { snapshotA: null, snapshotB: 'neat' }, |
| 87 | + { snapshotA: {}, snapshotB: { different: true } }, |
| 88 | + { snapshotA: [], snapshotB: ['I have a value now!'] }, |
| 89 | + ] as const satisfies readonly SampleData[]; |
| 90 | + |
| 91 | + for (const { snapshotA, snapshotB } of samples) { |
| 92 | + const subscriptionCallback = jest.fn(); |
| 93 | + const manager = new StateSnapshotManager({ |
| 94 | + initialSnapshot: snapshotA, |
| 95 | + didSnapshotsChange: defaultDidSnapshotsChange, |
| 96 | + }); |
| 97 | + |
| 98 | + const unsubscribe = manager.subscribe(subscriptionCallback); |
| 99 | + manager.updateSnapshot(snapshotB); |
| 100 | + expect(subscriptionCallback).toHaveBeenCalledTimes(1); |
| 101 | + expect(subscriptionCallback).toHaveBeenCalledWith(snapshotB); |
| 102 | + |
| 103 | + unsubscribe(); |
| 104 | + manager.updateSnapshot(snapshotA); |
| 105 | + expect(subscriptionCallback).toHaveBeenCalledTimes(1); |
| 106 | + } |
| 107 | + }); |
| 108 | + |
| 109 | + it('Lets user define a custom comparison algorithm during instantiation', () => { |
| 110 | + type SampleData = Readonly<{ |
| 111 | + snapshotA: ReadonlyJsonValue; |
| 112 | + snapshotB: ReadonlyJsonValue; |
| 113 | + compare: (A: ReadonlyJsonValue, B: ReadonlyJsonValue) => boolean; |
| 114 | + }>; |
| 115 | + |
| 116 | + const exampleDeeplyNestedJson: ReadonlyJsonValue = { |
| 117 | + value1: { |
| 118 | + value2: { |
| 119 | + value3: 'neat', |
| 120 | + }, |
| 121 | + }, |
| 122 | + |
| 123 | + value4: { |
| 124 | + value5: [{ valueX: true }, { valueY: false }], |
| 125 | + }, |
| 126 | + }; |
| 127 | + |
| 128 | + const samples = [ |
| 129 | + { |
| 130 | + snapshotA: exampleDeeplyNestedJson, |
| 131 | + snapshotB: { |
| 132 | + ...exampleDeeplyNestedJson, |
| 133 | + value4: { |
| 134 | + value5: [{ valueX: false }, { valueY: false }], |
| 135 | + }, |
| 136 | + }, |
| 137 | + compare: (A, B) => JSON.stringify(A) !== JSON.stringify(B), |
| 138 | + }, |
| 139 | + { |
| 140 | + snapshotA: { tag: 'snapshot-993', value: 1 }, |
| 141 | + snapshotB: { tag: 'snapshot-2004', value: 1 }, |
| 142 | + compare: (A, B) => { |
| 143 | + const recastA = A as Record<string, unknown>; |
| 144 | + const recastB = B as Record<string, unknown>; |
| 145 | + return recastA.tag !== recastB.tag; |
| 146 | + }, |
| 147 | + }, |
| 148 | + ] as const satisfies readonly SampleData[]; |
| 149 | + |
| 150 | + for (const { snapshotA, snapshotB, compare } of samples) { |
| 151 | + const subscriptionCallback = jest.fn(); |
| 152 | + const manager = new StateSnapshotManager({ |
| 153 | + initialSnapshot: snapshotA, |
| 154 | + didSnapshotsChange: compare, |
| 155 | + }); |
| 156 | + |
| 157 | + void manager.subscribe(subscriptionCallback); |
| 158 | + manager.updateSnapshot(snapshotB); |
| 159 | + expect(subscriptionCallback).toHaveBeenCalledWith(snapshotB); |
| 160 | + } |
| 161 | + }); |
| 162 | + |
| 163 | + it('Rejects new snapshots that are equivalent to old ones, and does NOT notify subscribers', () => { |
| 164 | + type SampleData = Readonly<{ |
| 165 | + snapshotA: ReadonlyJsonValue; |
| 166 | + snapshotB: ReadonlyJsonValue; |
| 167 | + }>; |
| 168 | + |
| 169 | + const samples = [ |
| 170 | + { snapshotA: true, snapshotB: true }, |
| 171 | + { snapshotA: 'kitty', snapshotB: 'kitty' }, |
| 172 | + { snapshotA: null, snapshotB: null }, |
| 173 | + { snapshotA: [], snapshotB: [] }, |
| 174 | + { snapshotA: {}, snapshotB: {} }, |
| 175 | + ] as const satisfies readonly SampleData[]; |
| 176 | + |
| 177 | + for (const { snapshotA, snapshotB } of samples) { |
| 178 | + const subscriptionCallback = jest.fn(); |
| 179 | + const manager = new StateSnapshotManager({ |
| 180 | + initialSnapshot: snapshotA, |
| 181 | + didSnapshotsChange: defaultDidSnapshotsChange, |
| 182 | + }); |
| 183 | + |
| 184 | + void manager.subscribe(subscriptionCallback); |
| 185 | + manager.updateSnapshot(snapshotB); |
| 186 | + expect(subscriptionCallback).not.toHaveBeenCalled(); |
| 187 | + } |
| 188 | + }); |
| 189 | + |
| 190 | + it("Uses the default comparison algorithm if one isn't specified at instantiation", () => { |
| 191 | + const snapshotA = { value: 'blah' }; |
| 192 | + const snapshotB = { value: 'blah' }; |
| 193 | + |
| 194 | + const manager = new StateSnapshotManager({ |
| 195 | + initialSnapshot: snapshotA, |
| 196 | + }); |
| 197 | + |
| 198 | + const subscriptionCallback = jest.fn(); |
| 199 | + void manager.subscribe(subscriptionCallback); |
| 200 | + manager.updateSnapshot(snapshotB); |
| 201 | + |
| 202 | + expect(subscriptionCallback).not.toHaveBeenCalled(); |
| 203 | + }); |
| 204 | +}); |
0 commit comments