-
Notifications
You must be signed in to change notification settings - Fork 130
Expand file tree
/
Copy pathsnapshot-diff.test.ts
More file actions
99 lines (88 loc) · 4.24 KB
/
snapshot-diff.test.ts
File metadata and controls
99 lines (88 loc) · 4.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import test from 'node:test';
import assert from 'node:assert/strict';
import { attachRefs, type RawSnapshotNode } from '../../utils/snapshot.ts';
import { buildSnapshotDiff } from '../snapshot-diff.ts';
function nodes(raw: RawSnapshotNode[]) {
return attachRefs(raw);
}
test('buildSnapshotDiff reports unchanged lines when snapshots are equal', () => {
const previous = nodes([
{ index: 0, depth: 0, type: 'XCUIElementTypeWindow' },
{ index: 1, depth: 1, type: 'XCUIElementTypeButton', label: 'Increment' },
]);
const current = nodes([
{ index: 0, depth: 0, type: 'XCUIElementTypeWindow' },
{ index: 1, depth: 1, type: 'XCUIElementTypeButton', label: 'Increment' },
]);
const diff = buildSnapshotDiff(previous, current);
assert.equal(diff.summary.additions, 0);
assert.equal(diff.summary.removals, 0);
assert.equal(diff.summary.unchanged, 2);
assert.deepEqual(diff.lines.map((line) => line.kind), ['unchanged', 'unchanged']);
});
test('buildSnapshotDiff reports added and removed lines', () => {
const previous = nodes([
{ index: 0, depth: 0, type: 'XCUIElementTypeWindow' },
{ index: 1, depth: 1, type: 'XCUIElementTypeStaticText', label: '67' },
{ index: 2, depth: 1, type: 'XCUIElementTypeButton', label: 'Increment' },
]);
const current = nodes([
{ index: 0, depth: 0, type: 'XCUIElementTypeWindow' },
{ index: 1, depth: 1, type: 'XCUIElementTypeStaticText', label: '134' },
{ index: 2, depth: 1, type: 'XCUIElementTypeButton', label: 'Increment' },
]);
const diff = buildSnapshotDiff(previous, current);
assert.equal(diff.summary.additions, 1);
assert.equal(diff.summary.removals, 1);
assert.equal(diff.summary.unchanged, 2);
assert.deepEqual(diff.lines.map((line) => line.kind), ['unchanged', 'removed', 'added', 'unchanged']);
});
test('buildSnapshotDiff treats value changes as remove plus add', () => {
const previous = nodes([{ index: 0, depth: 0, type: 'XCUIElementTypeTextField', label: 'Amount', value: '67' }]);
const current = nodes([{ index: 0, depth: 0, type: 'XCUIElementTypeTextField', label: 'Amount', value: '134' }]);
const diff = buildSnapshotDiff(previous, current);
assert.equal(diff.summary.additions, 1);
assert.equal(diff.summary.removals, 1);
assert.equal(diff.summary.unchanged, 0);
assert.deepEqual(diff.lines.map((line) => line.kind), ['removed', 'added']);
});
test('buildSnapshotDiff preserves surrounding context ordering', () => {
const previous = nodes([
{ index: 0, depth: 0, type: 'XCUIElementTypeWindow' },
{ index: 1, depth: 1, type: 'XCUIElementTypeStaticText', label: 'Count' },
{ index: 2, depth: 1, type: 'XCUIElementTypeStaticText', label: '67' },
{ index: 3, depth: 1, type: 'XCUIElementTypeButton', label: 'Increment' },
]);
const current = nodes([
{ index: 0, depth: 0, type: 'XCUIElementTypeWindow' },
{ index: 1, depth: 1, type: 'XCUIElementTypeStaticText', label: 'Count' },
{ index: 2, depth: 1, type: 'XCUIElementTypeStaticText', label: '134' },
{ index: 3, depth: 1, type: 'XCUIElementTypeButton', label: 'Increment' },
]);
const diff = buildSnapshotDiff(previous, current);
assert.equal(diff.lines[0]?.kind, 'unchanged');
assert.equal(diff.lines[1]?.kind, 'unchanged');
assert.equal(diff.lines[2]?.kind, 'removed');
assert.equal(diff.lines[3]?.kind, 'added');
assert.equal(diff.lines[4]?.kind, 'unchanged');
});
test('buildSnapshotDiff flatten option uses flat snapshot line shape', () => {
const previous = nodes([
{ index: 0, depth: 0, type: 'XCUIElementTypeWindow' },
{ index: 1, depth: 1, type: 'XCUIElementTypeOther', label: '335' },
{ index: 2, depth: 2, type: 'XCUIElementTypeStaticText', label: '335' },
]);
const current = nodes([
{ index: 0, depth: 0, type: 'XCUIElementTypeWindow' },
{ index: 1, depth: 1, type: 'XCUIElementTypeOther', label: '402' },
{ index: 2, depth: 2, type: 'XCUIElementTypeStaticText', label: '402' },
]);
const diff = buildSnapshotDiff(previous, current, { flatten: true });
assert.equal(diff.summary.additions, 2);
assert.equal(diff.summary.removals, 2);
const changed = diff.lines.filter((line) => line.kind !== 'unchanged');
assert.equal(changed.length, 4);
for (const line of changed) {
assert.equal(line.text.startsWith(' '), false);
}
});