@flighthq/snapshot 0.2.0 → 0.2.1-next.410.a23f189
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@flighthq/snapshot",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.1-next.410.a23f189",
|
|
4
4
|
"repository": {
|
|
5
5
|
"type": "git",
|
|
6
6
|
"url": "https://github.com/flighthq/flight.git",
|
|
@@ -32,8 +32,8 @@
|
|
|
32
32
|
"clean:dist": "tsx ../../scripts/clean-package-dist.ts"
|
|
33
33
|
},
|
|
34
34
|
"dependencies": {
|
|
35
|
-
"@flighthq/math": "0.2.
|
|
36
|
-
"@flighthq/types": "0.2.
|
|
35
|
+
"@flighthq/math": "0.2.1-next.410.a23f189",
|
|
36
|
+
"@flighthq/types": "0.2.1-next.410.a23f189"
|
|
37
37
|
},
|
|
38
38
|
"devDependencies": {
|
|
39
39
|
"typescript": "^5.3.0"
|
|
@@ -32,6 +32,24 @@ describe('equalsSnapshot', () => {
|
|
|
32
32
|
expect(equalsSnapshot(captureSnapshot({}), captureSnapshot({}))).toBe(true);
|
|
33
33
|
});
|
|
34
34
|
|
|
35
|
+
it('is false for an array compared to an object with the same indexed values', () => {
|
|
36
|
+
const a = captureSnapshot([1, 2] as unknown as object);
|
|
37
|
+
const b = captureSnapshot({ 0: 1, 1: 2 } as unknown as object);
|
|
38
|
+
expect(equalsSnapshot(a, b)).toBe(false);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it('is false when objects differ three levels deep', () => {
|
|
42
|
+
const a = captureSnapshot({ level1: { level2: { level3: { value: 1 } } } });
|
|
43
|
+
const b = captureSnapshot({ level1: { level2: { level3: { value: 2 } } } });
|
|
44
|
+
expect(equalsSnapshot(a, b)).toBe(false);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it('is true when objects are equal three levels deep', () => {
|
|
48
|
+
const a = captureSnapshot({ level1: { level2: { level3: { value: 42 } } } });
|
|
49
|
+
const b = captureSnapshot({ level1: { level2: { level3: { value: 42 } } } });
|
|
50
|
+
expect(equalsSnapshot(a, b)).toBe(true);
|
|
51
|
+
});
|
|
52
|
+
|
|
35
53
|
it('distinguishes null from a value at the same key', () => {
|
|
36
54
|
const a = captureSnapshot({ tag: null as number | null });
|
|
37
55
|
const b = captureSnapshot({ tag: 0 as number | null });
|
|
@@ -23,6 +23,16 @@ describe('interpolateSnapshots', () => {
|
|
|
23
23
|
expect(atEnd).toEqual({ x: 10, y: 0 });
|
|
24
24
|
});
|
|
25
25
|
|
|
26
|
+
it('interpolates only dotted-path schema-listed nested fields', () => {
|
|
27
|
+
const a = captureSnapshot({ pos: { x: 0, y: 0 }, hp: 100 });
|
|
28
|
+
const b = captureSnapshot({ pos: { x: 100, y: 200 }, hp: 0 });
|
|
29
|
+
const out = { pos: { x: 0, y: 0 }, hp: 0 };
|
|
30
|
+
interpolateSnapshots(a, b, 0.5, out, ['pos.x']);
|
|
31
|
+
expect(out.pos.x).toBe(50);
|
|
32
|
+
expect(out.pos.y).toBe(200);
|
|
33
|
+
expect(out.hp).toBe(0);
|
|
34
|
+
});
|
|
35
|
+
|
|
26
36
|
it('interpolates only schema-listed paths and snaps the rest to b', () => {
|
|
27
37
|
const a = captureSnapshot({ x: 0, y: 10 });
|
|
28
38
|
const b = captureSnapshot({ x: 10, y: 0 });
|
|
@@ -39,6 +49,16 @@ describe('interpolateSnapshots', () => {
|
|
|
39
49
|
expect(out.pos).toEqual({ x: 25, y: 10 });
|
|
40
50
|
});
|
|
41
51
|
|
|
52
|
+
it('assigns keys present only in b and ignores keys present only in a', () => {
|
|
53
|
+
const a = captureSnapshot({ x: 10, onlyA: 99 } as Record<string, unknown>);
|
|
54
|
+
const b = captureSnapshot({ x: 20, onlyB: 'hello' } as Record<string, unknown>);
|
|
55
|
+
const out = { x: 0 } as Record<string, unknown>;
|
|
56
|
+
interpolateSnapshots(a, b, 0.5, out);
|
|
57
|
+
expect(out.x).toBe(15);
|
|
58
|
+
expect(out.onlyB).toBe('hello');
|
|
59
|
+
expect(out).not.toHaveProperty('onlyA');
|
|
60
|
+
});
|
|
61
|
+
|
|
42
62
|
it('clamps t below 0 and above 1', () => {
|
|
43
63
|
const a = captureSnapshot({ x: 0 });
|
|
44
64
|
const b = captureSnapshot({ x: 10 });
|
|
@@ -58,6 +78,14 @@ describe('interpolateSnapshots', () => {
|
|
|
58
78
|
expect(out.points).toEqual([5, 50]);
|
|
59
79
|
});
|
|
60
80
|
|
|
81
|
+
it('snaps to b when a has a number but b has a non-number at the same key', () => {
|
|
82
|
+
const a = captureSnapshot({ x: 5 } as Record<string, unknown>);
|
|
83
|
+
const b = captureSnapshot({ x: 'hello' } as Record<string, unknown>);
|
|
84
|
+
const out = { x: 0 } as Record<string, unknown>;
|
|
85
|
+
interpolateSnapshots(a, b, 0.5, out);
|
|
86
|
+
expect(out.x).toBe('hello');
|
|
87
|
+
});
|
|
88
|
+
|
|
61
89
|
it('snaps a null field to b', () => {
|
|
62
90
|
const a = captureSnapshot({ value: 1, tag: null as number | null });
|
|
63
91
|
const b = captureSnapshot({ value: 3, tag: null as number | null });
|
|
@@ -19,6 +19,13 @@ describe('restoreSnapshot', () => {
|
|
|
19
19
|
expect(target.items).toEqual([1, 2, 3]);
|
|
20
20
|
});
|
|
21
21
|
|
|
22
|
+
it('is a no-op for a primitive top-level snapshot', () => {
|
|
23
|
+
const snapshot = captureSnapshot(42 as unknown as Record<string, unknown>);
|
|
24
|
+
const target: Record<string, unknown> = { x: 1 };
|
|
25
|
+
restoreSnapshot(snapshot, target);
|
|
26
|
+
expect(target).toEqual({ x: 1 });
|
|
27
|
+
});
|
|
28
|
+
|
|
22
29
|
it('keeps the target object identity, mutating in place', () => {
|
|
23
30
|
const snapshot = captureSnapshot({ x: 1 });
|
|
24
31
|
const target = { x: 0 };
|
|
@@ -45,6 +52,31 @@ describe('restoreSnapshot', () => {
|
|
|
45
52
|
expect(snapshot.nested.a).toBe(1);
|
|
46
53
|
});
|
|
47
54
|
|
|
55
|
+
it('preserves extra keys in the target that are not in the snapshot', () => {
|
|
56
|
+
const snapshot = captureSnapshot({ x: 10 } as Record<string, number>);
|
|
57
|
+
const target = { x: 0, y: 5, z: 99 } as Record<string, number>;
|
|
58
|
+
restoreSnapshot(snapshot, target);
|
|
59
|
+
expect(target.x).toBe(10);
|
|
60
|
+
expect(target.y).toBe(5);
|
|
61
|
+
expect(target.z).toBe(99);
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it('replaces a nested container when the type changes from object to array', () => {
|
|
65
|
+
const snapshot = captureSnapshot({ data: [1, 2, 3] as unknown });
|
|
66
|
+
const target = { data: { a: 1 } as unknown };
|
|
67
|
+
restoreSnapshot(snapshot, target);
|
|
68
|
+
expect(target.data).toEqual([1, 2, 3]);
|
|
69
|
+
expect(Array.isArray(target.data)).toBe(true);
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it('replaces a nested container when the type changes from array to object', () => {
|
|
73
|
+
const snapshot = captureSnapshot({ data: { a: 1 } as unknown });
|
|
74
|
+
const target = { data: [1, 2, 3] as unknown };
|
|
75
|
+
restoreSnapshot(snapshot, target);
|
|
76
|
+
expect(target.data).toEqual({ a: 1 });
|
|
77
|
+
expect(Array.isArray(target.data)).toBe(false);
|
|
78
|
+
});
|
|
79
|
+
|
|
48
80
|
it('resizes the target array to the snapshot array length', () => {
|
|
49
81
|
const snapshot = captureSnapshot({ items: [1, 2] });
|
|
50
82
|
const target = { items: [9, 9, 9, 9] };
|