-
Notifications
You must be signed in to change notification settings - Fork 0
/
stamp-collection.test.tsx
104 lines (97 loc) · 3.1 KB
/
stamp-collection.test.tsx
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
100
101
102
103
104
import { JSDOM } from 'jsdom'
import { equal } from 'node:assert/strict'
import { describe, it } from 'node:test'
import { ContextComponent } from './component.js'
import { jsx } from './jsx.js'
import { buildStamp } from './stamp-builder.js'
import { StampCollection } from './stamp-collection.js'
describe('stamp collection', () => {
const { window } = new JSDOM()
const { document } = window
it('registers complex stamps', () => {
interface SneakyComponentProps {
warn: boolean
faces: number
}
const Sneaky = ({ warn, faces }: SneakyComponentProps) => {
if (warn) {
const even = faces % 2 == 0
return <div class="red">{even ? 'Even' : 'Odd'}</div>
}
const faceName = faces > 0 && faces < 3 ? faces.toString() : 'Many'
return <div>{faceName}</div>
}
const SneakyComponent: ContextComponent<SneakyComponentProps> = Sneaky
const redOdd = Sneaky({ warn: true, faces: 1 })
const redEven = Sneaky({ warn: true, faces: 2 })
const face1 = Sneaky({ warn: false, faces: 1 })
const face2 = Sneaky({ warn: false, faces: 2 })
const face3 = Sneaky({ warn: false, faces: 3 })
const facesMany = Sneaky({ warn: false, faces: 4 })
const redOddStamp = buildStamp(redOdd, document)
const redEvenStamp = buildStamp(redEven, document)
const face1Stamp = buildStamp(face1, document)
const face2Stamp = buildStamp(face2, document)
const face3Stamp = buildStamp(face3, document)
const facesManyStamp = buildStamp(facesMany, document)
const stamps = new StampCollection()
.registerStampAlternative(
SneakyComponent,
({ warn, faces }) => warn && faces % 2 === 0,
redEvenStamp,
)
.registerStampAlternative(
SneakyComponent,
({ warn, faces }) => warn && faces % 2 !== 0,
redOddStamp,
)
.registerStampAlternative(
SneakyComponent,
({ warn, faces }) => !warn && faces === 1,
face1Stamp,
)
.registerStampAlternative(
SneakyComponent,
({ warn, faces }) => !warn && faces === 2,
face2Stamp,
)
.registerStampAlternative(
SneakyComponent,
({ warn, faces }) => !warn && faces === 3,
face3Stamp,
)
.registerStampAlternative(
SneakyComponent,
({ warn, faces }) => !warn && (faces < 0 || faces > 3),
facesManyStamp,
)
equal(
stamps.getStamp(SneakyComponent, { warn: true, faces: 32 }),
redEvenStamp,
)
equal(
stamps.getStamp(SneakyComponent, { warn: true, faces: 49 }),
redOddStamp,
)
equal(
stamps.getStamp(SneakyComponent, { warn: false, faces: -34 }),
facesManyStamp,
)
equal(
stamps.getStamp(SneakyComponent, { warn: false, faces: 99 }),
facesManyStamp,
)
equal(
stamps.getStamp(SneakyComponent, { warn: false, faces: 1 }),
face1Stamp,
)
equal(
stamps.getStamp(SneakyComponent, { warn: false, faces: 2 }),
face2Stamp,
)
equal(
stamps.getStamp(SneakyComponent, { warn: false, faces: 3 }),
face3Stamp,
)
})
})