-
Notifications
You must be signed in to change notification settings - Fork 0
/
binding.test.tsx
188 lines (174 loc) · 5.5 KB
/
binding.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import { JSDOM } from 'jsdom'
import { deepEqual, equal, fail } from 'node:assert/strict'
import { describe, it } from 'node:test'
import { Subscription, firstValueFrom } from 'rxjs'
import { TestScheduler } from 'rxjs/testing'
import {
bindElement,
bufferEntries,
makeEntries,
schedulable,
scheduledKey,
} from './binding.js'
import { ElementDescription } from './component.js'
import { jsx } from './jsx.js'
import { ObservableEvent, makeEventProxy } from './events.js'
import buildDomStrategy from './wiring-dom-build.js'
describe('binding', () => {
it("doesn't schedule immediates", () => {
const actual = schedulable('example', true)
const expected = false
deepEqual(actual, expected)
})
it("doesn't schedule 'value'", () => {
const actual = schedulable('value', false)
const expected = false
deepEqual(actual, expected)
})
it('schedules bfDelayValue as value', () => {
const key = 'bfDelayValue'
const actual = schedulable(key, false)
const expected = true
deepEqual(actual, expected)
const actualKey = scheduledKey(key)
const expectedKey = 'value'
deepEqual(actualKey, expectedKey)
})
it('considers other keys schedulable', () => {
const key = 'example'
const actual = schedulable(key, false)
const expected = true
deepEqual(actual, expected)
const actualKey = scheduledKey(key)
deepEqual(actualKey, key)
})
it('makes entry observables', () => {
const testScheduler = new TestScheduler((actual, expected) =>
deepEqual(actual, expected),
)
testScheduler.run(({ cold, expectObservable }) => {
const example = cold(' a--bc--d---', { a: 1, b: 2, c: 3, d: 4 })
const expected = ' e--fg--h---'
const expectedValues = {
e: ['example', 1],
f: ['example', 2],
g: ['example', 3],
h: ['example', 4],
}
const observed = makeEntries('example', example)
expectObservable(observed).toBe(expected, expectedValues)
})
})
it('buffers entries', () => {
const testScheduler = new TestScheduler((actual, expected) =>
deepEqual(actual, expected),
)
testScheduler.run(({ animate, cold, expectObservable }) => {
animate(' --x--x--x--')
const example = cold('a--bc--d---', {
a: ['example1', 1] as [string, unknown],
b: ['example1', 2] as [string, unknown],
c: ['example2', true] as [string, unknown],
d: ['example1', 3] as [string, unknown],
})
const expected = ' --e--f--g--'
const expectedValues = {
e: { example1: 1 },
f: {
example1: 2,
example2: true,
},
g: { example1: 3 },
}
const observed = bufferEntries(example)
expectObservable(observed).toBe(expected, expectedValues)
})
})
it('binds a no-bind element', () => {
const { window } = new JSDOM()
const { document } = window
const element = document.createElement('div')
const error = (error: unknown) => fail(error as Error | string)
const complete = () => {}
const subscription = new Subscription()
bindElement(element, (<div />) as ElementDescription, {
domStrategy: buildDomStrategy,
error,
complete,
componentRunner(_container, description, _context, _placeholder) {
throw new Error(
`attempted to run component ${
'name' in description
? description.name
: description.component.name
}`,
)
},
componentWirer(description, _context) {
throw new Error(
`attempted to wire component ${
'name' in description
? description.name
: description.component.name
}`,
)
},
eventBinder: {
applyEvent(_event, _element, eventName) {
throw new Error(
`not setup for event binding testing; attempted to bind ${eventName}`,
)
},
},
subscription,
isStaticComponent: true,
isStaticTree: true,
})
subscription.unsubscribe()
})
it('binds a dom attachment', async () => {
const { window } = new JSDOM()
const { document } = window
const element = document.createElement('div')
const error = (error: unknown) => fail(error as Error | string)
const complete = () => {}
const subscription = new Subscription()
const { events, handler } = makeEventProxy('test component')
const bfDomAttach = events.attach as ObservableEvent<HTMLElement>
const elementPromise = firstValueFrom(bfDomAttach)
bindElement(
element,
(<div events={{ bfDomAttach }} />) as ElementDescription,
{
domStrategy: buildDomStrategy,
error,
complete,
componentRunner(_container, description, _context, _placeholder) {
throw new Error(
`attempted to run component ${
'name' in description
? description.name
: description.component.name
}`,
)
},
componentWirer(description, _context) {
throw new Error(
`attempted to wire component ${
'name' in description
? description.name
: description.component.name
}`,
)
},
eventBinder: handler,
subscription,
isStaticComponent: true,
isStaticTree: true,
},
)
const actual = await elementPromise
equal(actual, element)
subscription.unsubscribe()
})
})