1
+ import Vue from 'vue' ;
1
2
import TreeSelect from '..' ;
2
3
import { mount } from '../../../test/utils' ;
3
4
5
+ Vue . use ( TreeSelect ) ;
6
+
4
7
test ( 'empty list' , ( ) => {
5
8
expect ( mount ( TreeSelect ) ) . toMatchSnapshot ( ) ;
6
9
} ) ;
7
10
11
+ const mockItem = {
12
+ text : 'city1' ,
13
+ id : 1
14
+ } ;
15
+
16
+ const mockItem2 = {
17
+ text : 'city2' ,
18
+ id : 2
19
+ } ;
20
+
21
+ const mockItems = [
22
+ {
23
+ text : 'group1' ,
24
+ children : [ mockItem ]
25
+ }
26
+ ] ;
27
+
8
28
test ( 'click-nav event' , ( ) => {
9
29
const onNavClick = jest . fn ( ) ;
10
30
const onClickNav = jest . fn ( ) ;
11
- const item = {
12
- text : 'city1' ,
13
- id : 1
14
- } ;
15
31
16
32
const wrapper = mount ( TreeSelect , {
17
33
propsData : {
18
- items : [
19
- {
20
- text : 'group1' ,
21
- children : [ item ]
22
- }
23
- ]
34
+ items : mockItems
24
35
} ,
25
36
context : {
26
37
on : {
@@ -30,28 +41,20 @@ test('click-nav event', () => {
30
41
}
31
42
} ) ;
32
43
33
- const items = wrapper . findAll ( '.van-tree-select__nav-item' ) ;
34
- items . at ( 0 ) . trigger ( 'click' ) ;
44
+ const navItems = wrapper . findAll ( '.van-tree-select__nav-item' ) ;
45
+ navItems . at ( 0 ) . trigger ( 'click' ) ;
46
+
35
47
expect ( onNavClick ) . toHaveBeenCalledWith ( 0 ) ;
36
48
expect ( onClickNav ) . toHaveBeenCalledWith ( 0 ) ;
37
49
} ) ;
38
50
39
51
test ( 'click-item event' , ( ) => {
40
52
const onItemClick = jest . fn ( ) ;
41
53
const onClickItem = jest . fn ( ) ;
42
- const item = {
43
- text : 'city1' ,
44
- id : 1
45
- } ;
46
54
47
55
const wrapper = mount ( TreeSelect , {
48
56
propsData : {
49
- items : [
50
- {
51
- text : 'group1' ,
52
- children : [ item ]
53
- }
54
- ]
57
+ items : mockItems
55
58
} ,
56
59
context : {
57
60
on : {
@@ -63,23 +66,19 @@ test('click-item event', () => {
63
66
64
67
const items = wrapper . findAll ( '.van-tree-select__item' ) ;
65
68
items . at ( 0 ) . trigger ( 'click' ) ;
66
- expect ( onItemClick ) . toHaveBeenCalledWith ( item ) ;
67
- expect ( onClickItem ) . toHaveBeenCalledWith ( item ) ;
69
+ expect ( onItemClick ) . toHaveBeenCalledWith ( mockItem ) ;
70
+ expect ( onClickItem ) . toHaveBeenCalledWith ( mockItem ) ;
68
71
} ) ;
69
72
70
73
test ( 'click disabled nav' , ( ) => {
71
74
const onClickNav = jest . fn ( ) ;
72
- const item = {
73
- text : 'city1' ,
74
- id : 1
75
- } ;
76
75
77
76
const wrapper = mount ( TreeSelect , {
78
77
propsData : {
79
78
items : [
80
79
{
81
80
text : 'group1' ,
82
- children : [ item ] ,
81
+ children : [ mockItem ] ,
83
82
disabled : true
84
83
}
85
84
]
@@ -105,8 +104,7 @@ test('click disabled item', () => {
105
104
text : 'group1' ,
106
105
children : [
107
106
{
108
- text : 'city1' ,
109
- id : 1 ,
107
+ ...mockItem ,
110
108
disabled : true
111
109
}
112
110
]
@@ -151,3 +149,87 @@ test('height prop', () => {
151
149
152
150
expect ( wrapper ) . toMatchSnapshot ( ) ;
153
151
} ) ;
152
+
153
+ test ( 'use sync modifier in main-active-index' , ( ) => {
154
+ const wrapper = mount ( {
155
+ template : `
156
+ <van-tree-select
157
+ :items="items"
158
+ :main-active-index.sync="mainActiveIndex"
159
+ />
160
+ ` ,
161
+ data ( ) {
162
+ return {
163
+ mainActiveIndex : - 1 ,
164
+ items : mockItems
165
+ } ;
166
+ }
167
+ } ) ;
168
+
169
+ const navItems = wrapper . findAll ( '.van-tree-select__nav-item' ) ;
170
+ navItems . at ( 0 ) . trigger ( 'click' ) ;
171
+
172
+ expect ( wrapper . vm . mainActiveIndex ) . toEqual ( 0 ) ;
173
+ } ) ;
174
+
175
+ test ( 'use sync modifier in active-id' , ( ) => {
176
+ const wrapper = mount ( {
177
+ template : `
178
+ <van-tree-select
179
+ :items="items"
180
+ :main-active-index="0"
181
+ :active-id.sync="activeId"
182
+ />
183
+ ` ,
184
+ data ( ) {
185
+ return {
186
+ activeId : mockItem . id ,
187
+ mainActiveIndex : 0 ,
188
+ items : [
189
+ {
190
+ text : 'group1' ,
191
+ children : [ mockItem , mockItem2 ]
192
+ }
193
+ ]
194
+ } ;
195
+ }
196
+ } ) ;
197
+
198
+ const items = wrapper . findAll ( '.van-tree-select__item' ) ;
199
+ items . at ( 1 ) . trigger ( 'click' ) ;
200
+
201
+ expect ( wrapper . vm . activeId ) . toEqual ( mockItem2 . id ) ;
202
+ } ) ;
203
+
204
+ test ( 'multiple select' , ( ) => {
205
+ const wrapper = mount ( {
206
+ template : `
207
+ <van-tree-select
208
+ :items="items"
209
+ :main-active-index="0"
210
+ :active-id.sync="activeId"
211
+ />
212
+ ` ,
213
+ data ( ) {
214
+ return {
215
+ activeId : [ ] ,
216
+ mainActiveIndex : 0 ,
217
+ items : [
218
+ {
219
+ text : 'group1' ,
220
+ children : [ mockItem , mockItem2 ]
221
+ }
222
+ ]
223
+ } ;
224
+ }
225
+ } ) ;
226
+
227
+ const items = wrapper . findAll ( '.van-tree-select__item' ) ;
228
+ items . at ( 0 ) . trigger ( 'click' ) ;
229
+ items . at ( 1 ) . trigger ( 'click' ) ;
230
+ expect ( wrapper . vm . activeId ) . toEqual ( [ mockItem . id , mockItem2 . id ] ) ;
231
+
232
+ items . at ( 0 ) . trigger ( 'click' ) ;
233
+ items . at ( 1 ) . trigger ( 'click' ) ;
234
+ expect ( wrapper . vm . activeId ) . toEqual ( [ ] ) ;
235
+ } ) ;
0 commit comments