-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccordion.spec.js
182 lines (159 loc) · 4.4 KB
/
accordion.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
/* eslint-disable max-lines-per-function */
import { expect } from 'chai'
import Vue from 'vue'
import { shallowMount } from '@vue/test-utils'
import UiAccordion from './accordion.vue'
describe('Component accordion', () => {
const css = `
.ui-accordion .ui-accordion__head {
display: block;
width: 100%;
text-align: left;
cursor: pointer;
}
.ui-accordion .ui-accordion__body {
overflow: hidden;
max-height: 0;
opacity: 0;
transition: opacity 300ms ease;
}
.ui-accordion.ui-accordion--is-open .ui-accordion__body {
opacity: 1;
}
.ui-accordion.ui-accordion--is-animating .ui-accordion__body {
transition: opacity 300ms ease, max-height 300ms ease;
}
`
const styleNode = document.createElement('style')
before(() => {
styleNode.innerHTML = css
document.head.appendChild(styleNode)
})
after(() => {
document.head.removeChild(styleNode)
})
it('The ui-accordion is an object', () => {
expect(UiAccordion).to.be.an('object')
expect(UiAccordion).to.be.not.empty
})
it('It can be properly opened when the isPassive mode is disabled', () => {
const wrapper = shallowMount(UiAccordion, {
scopedSlots: {
head: '<h1 class="label">Head</h1>',
body: '<p>Body</p>',
},
})
const button = wrapper.find('.ui-accordion__head')
button.trigger('click')
expect(wrapper.vm.state.isOpen).to.be.ok
})
it('It can be properly opened when the isPassive mode is enabled', async () => {
const wrapper = shallowMount(UiAccordion, {
propsData: {
isPassive: true,
isOpen: false,
},
scopedSlots: {
head: '<h1 class="label">Head</h1>',
body: '<p>Body</p>',
},
})
wrapper.setProps({ isOpen: true })
await Vue.nextTick()
expect(wrapper.vm.state.isOpen).to.be.ok
})
it('Dom Events do not change the component state in the passive mode', () => {
const wrapper = shallowMount(UiAccordion, {
propsData: {
isPassive: true,
isOpen: false,
},
scopedSlots: {
head: '<h1 class="label">Head</h1>',
body: '<p>Body</p>',
},
})
const button = wrapper.find('.ui-accordion__head')
button.trigger('click')
expect(wrapper.vm.state.isOpen).to.be.not.ok
})
it('The component change event will be dispatched', (done) => {
const wrapper = shallowMount(UiAccordion, {
propsData: {
isPassive: true,
isOpen: false,
},
scopedSlots: {
head: '<h1 class="label">Head</h1>',
body: '<p>Body</p>',
},
listeners: {
change() {
expect(wrapper.vm.state.isOpen).to.be.ok
done()
},
},
})
wrapper.vm.$nextTick(() => {
wrapper.setProps({ isOpen: true })
})
})
it('The component changed event will be properly dispatched', (done) => {
const wrapper = shallowMount(UiAccordion, {
propsData: {
isPassive: true,
isOpen: false,
},
scopedSlots: {
head: '<h1 class="label">Head</h1>',
body: '<p>Body</p>',
},
listeners: {
changed(isOpen) {
if (isOpen) {
expect(wrapper.vm.state.isOpen).to.be.equal(true)
wrapper.setProps({ isOpen: false })
} else {
// wait the closing animation to consider this test done
expect(wrapper.vm.state.isOpen).to.be.equal(false)
done()
wrapper.destroy()
}
},
},
attachTo: document.body,
})
wrapper.vm.$nextTick(() => {
wrapper.setProps({ isOpen: true })
})
})
it('The body slot will receive the isVisible property', (done) => {
const wrapper = shallowMount(UiAccordion, {
propsData: {
isPassive: true,
isOpen: false,
},
scopedSlots: {
head: '<h1 class="label">Head</h1>',
body: '<p slot-scope="props">{{ props.isVisible }}</p>',
},
listeners: {
change() {
// it will be true only when the transition will be complete
expect(wrapper.find('p').html()).to.be.equal('<p>false</p>')
},
changed() {
wrapper.vm.$nextTick(() => {
expect(wrapper.find('p').html()).to.be.equal('<p>true</p>')
done()
wrapper.destroy()
})
},
},
attachTo: document.body,
})
wrapper.vm.$nextTick(() => {
wrapper.setProps({ isOpen: true })
})
})
})