-
Notifications
You must be signed in to change notification settings - Fork 0
/
purity.test.ts
206 lines (201 loc) Β· 6.76 KB
/
purity.test.ts
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import {afterEach, beforeEach, describe, expect, it, vi} from "vitest"
import {init, render} from "./purity.js"
import {delay} from "./delay.js"
import type {App} from "./purity.js"
export type AnyObject = {[key: string]: any}
describe("purity", () => {
let app: App<AnyObject>, defaultState: AnyObject
beforeEach(() => {
document.body.innerHTML = '<div id="root"></div>'
console.warn = vi.fn()
})
afterEach(() => {
document.body.innerHTML = ""
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
app = undefined
vi.restoreAllMocks()
})
describe("counter app", () => {
let CounterApp: () => string
beforeEach(() => {
defaultState = {title: "COUNTER", counter: 0}
app = init(defaultState)
CounterApp = () => render`
<div id="root">
<h1>${app.getState().title}</h1>
<span id="count">${app.getState().count}</span>
</div>
`
})
it("should match default state after created", () => {
expect(app.getState()).toEqual(defaultState)
})
it("should mount a component depending on the default state", () => {
app.mount(CounterApp)
expect(document.body.innerHTML).toEqual(CounterApp())
})
it("should change state after updates depending on previous state", () => {
app.mount(CounterApp)
app.setState(state => ({counter: state.counter + 1}))
expect(app.getState()).toEqual({title: "COUNTER", counter: 1})
})
})
it("should bind an event", async () => {
const eventHandler = vi.fn()
const ClickableComponent = () => render`
<button id="root" ::click=${eventHandler}>Click Me</button>
`
app = init({})
app.mount(ClickableComponent)
// Awaiting for the eventHandler to be set in setTimeout
await delay(0)
expect(document.body.innerHTML).toEqual(
`<button id="root" data-purity_flag="">Click Me</button>`
)
;(document.querySelector("button#root") as HTMLElement).click()
expect(eventHandler).toHaveBeenCalledTimes(1)
})
it("should bind multiple events", async () => {
const clickHandler = vi.fn()
const blurHandler = vi.fn()
const ClickableComponent = () => render`
<input type="text" id="root" ::click=${clickHandler} ::blur=${blurHandler} />
`
app = init({})
app.mount(ClickableComponent)
// Awaiting for the eventHandler to be set in setTimeout
await delay(0)
expect(document.body.innerHTML).toEqual(
'<input type="text" id="root" data-purity_flag="">'
)
;(document.querySelector("input#root") as HTMLElement).click()
expect(clickHandler).toHaveBeenCalledTimes(1)
;(document.querySelector("input#root") as HTMLElement).focus()
;(document.querySelector("input#root") as HTMLElement).blur()
expect(blurHandler).toHaveBeenCalledTimes(1)
})
it(`
should handle binding an event to element which is not a purity node
(does not have an id defined on it)
and ignore other data-* attributes defined on that element
`, async () => {
const clickHandler = vi.fn()
const RootWithClickableElement = () => render`
<div id="root">
<button ::click=${clickHandler} data-other="something">
Click Me Not a Node
</button>
</div>
`
app = init({})
app.mount(RootWithClickableElement)
await delay(0)
expect(document.body.innerHTML).toEqual(
`<div id="root"><button data-purity_flag="" data-other="something">Click Me Not a Node</button></div>`
)
})
it("should add and remove components in DOM", async () => {
const Child1 = () => render`
<h1 id="first">First</h1>
`
const Child2 = () => render`
<h2 id="second">Second</h2>
`
const Parent = () => {
const {selected} = app.getState()
return render`
<div id="root">
${
{first: Child1(), second: Child2()}[
selected as "first" | "second"
] as string | undefined
}
</div>
`
}
app = init({})
app.mount(Parent)
await delay(0)
expect(document.body.innerHTML).toEqual(`<div id="root"></div>`)
app.setState(() => ({selected: "first"}))
await delay(0)
expect(document.body.innerHTML).toEqual(`<div id="root">${Child1()}</div>`)
app.setState(() => ({selected: "second"}))
await delay(0)
expect(document.body.innerHTML).toEqual(`<div id="root">${Child2()}</div>`)
})
it(`
should handle the case when the App root id differs from a defined one in html
or is not specified
`, () => {
const WrongRoot = () => render`
<div id="wrong-root"></div>
`
const NoRoot = () => render`
<div></div>
`
app = init({})
expect(() => {
app.mount(WrongRoot)
}).toThrow(
`Root DOM element's id does not correspond to the defined application root id "wrong-root".`
)
expect(() => {
app.mount(NoRoot)
}).toThrow(
`Root DOM element's id does not correspond to the defined application root id "undefined".`
)
})
it(`
should not change innerHTML when only attributes have changed in the wrapper tag
(input's value should remain the same)
`, async () => {
app = init({})
const StaticComponent = () => render`
<div id="root">
<input id="color" style="color: ${app.getState().something};" />
<button
::click=${() => {
app.setState(() => ({
something: (
document.querySelector("#color") as HTMLInputElement
).value,
}))
}}
>
Apply color
</button>
</div>
`
app.mount(StaticComponent)
await delay(0)
;(document.querySelector("#color") as HTMLInputElement).value = "red"
;(document.querySelector("button") as HTMLElement).click()
expect(
(document.querySelector("#color") as HTMLInputElement).value
).toEqual("red")
expect(
(document.querySelector("#color") as HTMLInputElement).style.color
).toEqual("red")
expect(document.body.innerHTML).toEqual(
'<div id="root"><input style="color: red;" id="color"><button data-purity_flag="">Apply color</button></div>'
)
})
it("should handle conditional rendering & process arrays", () => {
const ConditionalComponent = ({maybeArr}: {maybeArr?: any[]}) => render`
<div id="root">
<ul>
${maybeArr?.map(item => render`<li>${item}</li>`)}
</ul>
</div>
`
app = init({})
app.mount(() => ConditionalComponent({}))
expect(document.body.innerHTML).toEqual(`<div id="root"><ul></ul></div>`)
app.mount(() => ConditionalComponent({maybeArr: ["π", "π", "π°"]}))
expect(document.body.innerHTML).toEqual(
`<div id="root"><ul><li>π</li><li>π</li><li>π°</li></ul></div>`
)
})
})