-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
/
class.spec.ts
193 lines (162 loc) 路 5.24 KB
/
class.spec.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
// https://github.com/vuejs/vue/blob/dev/test/unit/features/directives/class.spec.js
import { h, render, createComponent } from '../../src'
type ClassItem = {
value: string | object | string[]
}
function assertClass(assertions: Array<Array<any>>) {
const root = document.createElement('div')
const dynamic = { value: '' }
const wrapper = () => h('div', { class: ['foo', dynamic.value] })
for (const [input, expected] of assertions) {
if (typeof input === 'function') {
input(dynamic.value)
} else {
dynamic.value = input
}
render(wrapper(), root)
expect(root.children[0].className).toBe(expected)
}
}
describe('class', () => {
test('plain string', () => {
assertClass([
['bar', 'foo bar'],
['baz qux', 'foo baz qux'],
['qux', 'foo qux'],
[undefined, 'foo']
])
})
test('object value', () => {
assertClass([
[{ bar: true, baz: false }, 'foo bar'],
[{ baz: true }, 'foo baz'],
[null, 'foo'],
[{ 'bar baz': true, qux: false }, 'foo bar baz'],
[{ qux: true }, 'foo qux']
])
})
test('array value', () => {
assertClass([
[['bar', 'baz'], 'foo bar baz'],
[['qux', 'baz'], 'foo qux baz'],
[['w', 'x y z'], 'foo w x y z'],
[undefined, 'foo'],
[['bar'], 'foo bar'],
[(val: Array<any>) => val.push('baz'), 'foo bar baz']
])
})
test('array of mixed values', () => {
assertClass([
[['x', { y: true, z: true }], 'foo x y z'],
[['x', { y: true, z: false }], 'foo x y'],
[['f', { z: true }], 'foo f z'],
[['l', 'f', { n: true, z: true }], 'foo l f n z'],
[['x', {}], 'foo x'],
[undefined, 'foo']
])
})
test('class merge between parent and child', () => {
const root = document.createElement('div')
const childClass: ClassItem = { value: 'd' }
const child = {
props: {},
render: () => h('div', { class: ['c', childClass.value] })
}
const parentClass: ClassItem = { value: 'b' }
const parent = {
props: {},
render: () => h(child, { class: ['a', parentClass.value] })
}
render(h(parent), root)
expect(root.children[0].className).toBe('c d a b')
parentClass.value = 'e'
// the `foo` here is just for forcing parent to be updated
// (otherwise it's skipped since its props never change)
render(h(parent, { foo: 1 }), root)
expect(root.children[0].className).toBe('c d a e')
parentClass.value = 'f'
render(h(parent, { foo: 2 }), root)
expect(root.children[0].className).toBe('c d a f')
parentClass.value = { foo: true }
childClass.value = ['bar', 'baz']
render(h(parent, { foo: 3 }), root)
expect(root.children[0].className).toBe('c bar baz a foo')
})
test('class merge between multiple nested components sharing same element', () => {
const component1 = createComponent({
props: {},
render() {
return this.$slots.default()[0]
}
})
const component2 = createComponent({
props: {},
render() {
return this.$slots.default()[0]
}
})
const component3 = createComponent({
props: {},
render() {
return h(
'div',
{
class: 'staticClass'
},
[this.$slots.default()]
)
}
})
const root = document.createElement('div')
const componentClass1 = { value: 'componentClass1' }
const componentClass2 = { value: 'componentClass2' }
const componentClass3 = { value: 'componentClass3' }
const wrapper = () =>
h(component1, { class: componentClass1.value }, () => [
h(component2, { class: componentClass2.value }, () => [
h(component3, { class: componentClass3.value }, () => ['some text'])
])
])
render(wrapper(), root)
expect(root.children[0].className).toBe(
'staticClass componentClass3 componentClass2 componentClass1'
)
componentClass1.value = 'c1'
render(wrapper(), root)
expect(root.children[0].className).toBe(
'staticClass componentClass3 componentClass2 c1'
)
componentClass2.value = 'c2'
render(wrapper(), root)
expect(root.children[0].className).toBe('staticClass componentClass3 c2 c1')
componentClass3.value = 'c3'
render(wrapper(), root)
expect(root.children[0].className).toBe('staticClass c3 c2 c1')
})
test('deep update', () => {
const root = document.createElement('div')
const test = {
a: true,
b: false
}
const wrapper = () => h('div', { class: test })
render(wrapper(), root)
expect(root.children[0].className).toBe('a')
test.b = true
render(wrapper(), root)
expect(root.children[0].className).toBe('a b')
})
// a vdom patch edge case where the user has several un-keyed elements of the
// same tag next to each other, and toggling them.
test('properly remove staticClass for toggling un-keyed children', () => {
const root = document.createElement('div')
const ok = { value: true }
const wrapper = () =>
h('div', [ok.value ? h('div', { class: 'a' }) : h('div')])
render(wrapper(), root)
expect(root.children[0].children[0].className).toBe('a')
ok.value = false
render(wrapper(), root)
expect(root.children[0].children[0].className).toBe('')
})
})