-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass.spec.js
208 lines (197 loc) · 5.74 KB
/
class.spec.js
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
207
208
import Vue from 'vue'
function assertClass (assertions, done) {
const vm = new Vue({
template: '<div class="foo" :class="value"></div>',
data: { value: '' }
}).$mount()
const chain = waitForUpdate()
assertions.forEach(([value, expected], i) => {
chain.then(() => {
if (typeof value === 'function') {
value(vm.value)
} else {
vm.value = value
}
}).then(() => {
expect(vm.$el.className).toBe(expected)
if (i >= assertions.length - 1) {
done()
}
})
})
chain.then(done)
}
describe('Directive v-bind:class', () => {
it('plain string', done => {
assertClass([
['bar', 'foo bar'],
['baz qux', 'foo baz qux'],
['qux', 'foo qux'],
[undefined, 'foo']
], done)
})
it('object value', done => {
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']
], done)
})
it('array value', done => {
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 => val.push('baz'), 'foo bar baz']
], done)
})
it('array of mixed values', done => {
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']
], done)
})
it('class merge between parent and child', done => {
const vm = new Vue({
template: '<child class="a" :class="value"></child>',
data: { value: 'b' },
components: {
child: {
template: '<div class="c" :class="value"></div>',
data: () => ({ value: 'd' })
}
}
}).$mount()
const child = vm.$children[0]
expect(vm.$el.className).toBe('c a d b')
vm.value = 'e'
waitForUpdate(() => {
expect(vm.$el.className).toBe('c a d e')
}).then(() => {
child.value = 'f'
}).then(() => {
expect(vm.$el.className).toBe('c a f e')
}).then(() => {
vm.value = { foo: true }
child.value = ['bar', 'baz']
}).then(() => {
expect(vm.$el.className).toBe('c a bar baz foo')
}).then(done)
})
it('class merge between multiple nested components sharing same element', done => {
const vm = new Vue({
template: `
<component1 :class="componentClass1">
<component2 :class="componentClass2">
<component3 :class="componentClass3">
some text
</component3>
</component2>
</component1>
`,
data: {
componentClass1: 'componentClass1',
componentClass2: 'componentClass2',
componentClass3: 'componentClass3'
},
components: {
component1: {
render () {
return this.$slots.default[0]
}
},
component2: {
render () {
return this.$slots.default[0]
}
},
component3: {
template: '<div class="staticClass"><slot></slot></div>'
}
}
}).$mount()
expect(vm.$el.className).toBe('staticClass componentClass3 componentClass2 componentClass1')
vm.componentClass1 = 'c1'
waitForUpdate(() => {
expect(vm.$el.className).toBe('staticClass componentClass3 componentClass2 c1')
vm.componentClass2 = 'c2'
}).then(() => {
expect(vm.$el.className).toBe('staticClass componentClass3 c2 c1')
vm.componentClass3 = 'c3'
}).then(() => {
expect(vm.$el.className).toBe('staticClass c3 c2 c1')
}).then(done)
})
it('deep update', done => {
const vm = new Vue({
template: '<div :class="test"></div>',
data: {
test: { a: true, b: false }
}
}).$mount()
expect(vm.$el.className).toBe('a')
vm.test.b = true
waitForUpdate(() => {
expect(vm.$el.className).toBe('a b')
}).then(done)
})
// css static classes should only contain a single space in between,
// as all the text inside of classes is shipped as a JS string
// and this could lead to useless spacing in static classes
it('condenses whitespace in staticClass', done => {
const vm = new Vue({
template: '<div class=" test1\ntest2\ttest3 test4 test5 \n \n \ntest6\t"></div>',
}).$mount()
expect(vm.$el.className).toBe('test1 test2 test3 test4 test5 test6')
done()
})
it('condenses whitespace in staticClass merge in a component', done => {
const vm = new Vue({
template: `
<component1 class="\n\t staticClass \t\n" :class="componentClass1">
</component1>
`,
data: {
componentClass1: 'componentClass1',
},
components: {
component1: {
template: '<div class="\n\t test \t\n"></div>'
},
}
}).$mount()
expect(vm.$el.className).toBe('test staticClass componentClass1')
vm.componentClass1 = 'c1'
waitForUpdate(() => {
expect(vm.$el.className).toBe('test staticClass c1')
}).then(done)
})
// a vdom patch edge case where the user has several un-keyed elements of the
// same tag next to each other, and toggling them.
it('properly remove staticClass for toggling un-keyed children', done => {
const vm = new Vue({
template: `
<div>
<div v-if="ok" class="a"></div>
<div v-if="!ok"></div>
</div>
`,
data: {
ok: true
}
}).$mount()
expect(vm.$el.children[0].className).toBe('a')
vm.ok = false
waitForUpdate(() => {
expect(vm.$el.children[0].className).toBe('')
}).then(done)
})
})