forked from vuejs/vue-test-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwrapper-array.js
224 lines (170 loc) · 5.25 KB
/
wrapper-array.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// @flow
import type Wrapper from './wrapper'
import type VueWrapper from './vue-wrapper'
import { throwError, warn } from 'shared/util'
export default class WrapperArray implements BaseWrapper {
+wrappers: Array<Wrapper | VueWrapper>;
+length: number;
constructor (wrappers: Array<Wrapper | VueWrapper>) {
const length = wrappers.length
// $FlowIgnore
Object.defineProperty(this, 'wrappers', {
get: () => wrappers,
set: () => throwError('wrapperArray.wrappers is read-only')
})
// $FlowIgnore
Object.defineProperty(this, 'length', {
get: () => length,
set: () => throwError('wrapperArray.length is read-only')
})
}
at (index: number): Wrapper | VueWrapper {
if (index > this.length - 1) {
throwError(`no item exists at ${index}`)
}
return this.wrappers[index]
}
attributes (): void {
this.throwErrorIfWrappersIsEmpty('attributes')
throwError(
`attributes must be called on a single wrapper, use ` +
`at(i) to access a wrapper`
)
}
classes (): void {
this.throwErrorIfWrappersIsEmpty('classes')
throwError(
`classes must be called on a single wrapper, use ` +
`at(i) to access a wrapper`
)
}
contains (selector: Selector): boolean {
this.throwErrorIfWrappersIsEmpty('contains')
return this.wrappers.every(wrapper => wrapper.contains(selector))
}
exists (): boolean {
return this.length > 0 && this.wrappers.every(wrapper => wrapper.exists())
}
filter (predicate: Function): WrapperArray {
return new WrapperArray(this.wrappers.filter(predicate))
}
emitted (): void {
this.throwErrorIfWrappersIsEmpty('emitted')
throwError(
`emitted must be called on a single wrapper, use ` +
`at(i) to access a wrapper`
)
}
emittedByOrder (): void {
this.throwErrorIfWrappersIsEmpty('emittedByOrder')
throwError(
`emittedByOrder must be called on a single wrapper, ` +
`use at(i) to access a wrapper`
)
}
findAll (): void {
this.throwErrorIfWrappersIsEmpty('findAll')
throwError(
`findAll must be called on a single wrapper, use ` +
`at(i) to access a wrapper`
)
}
find (): void {
this.throwErrorIfWrappersIsEmpty('find')
throwError(
`find must be called on a single wrapper, use at(i) ` +
`to access a wrapper`
)
}
html (): void {
this.throwErrorIfWrappersIsEmpty('html')
throwError(
`html must be called on a single wrapper, use at(i) ` +
`to access a wrapper`
)
}
is (selector: Selector): boolean {
this.throwErrorIfWrappersIsEmpty('is')
return this.wrappers.every(wrapper => wrapper.is(selector))
}
isEmpty (): boolean {
this.throwErrorIfWrappersIsEmpty('isEmpty')
return this.wrappers.every(wrapper => wrapper.isEmpty())
}
isVisible (): boolean {
this.throwErrorIfWrappersIsEmpty('isVisible')
return this.wrappers.every(wrapper => wrapper.isVisible())
}
isVueInstance (): boolean {
this.throwErrorIfWrappersIsEmpty('isVueInstance')
return this.wrappers.every(wrapper => wrapper.isVueInstance())
}
name (): void {
this.throwErrorIfWrappersIsEmpty('name')
throwError(
`name must be called on a single wrapper, use at(i) ` +
`to access a wrapper`
)
}
props (): void {
this.throwErrorIfWrappersIsEmpty('props')
throwError(
`props must be called on a single wrapper, use ` +
`at(i) to access a wrapper`
)
}
text (): void {
this.throwErrorIfWrappersIsEmpty('text')
throwError(
`text must be called on a single wrapper, use at(i) ` +
`to access a wrapper`
)
}
throwErrorIfWrappersIsEmpty (method: string): void {
if (this.wrappers.length === 0) {
throwError(`${method} cannot be called on 0 items`)
}
}
setData (data: Object): void {
this.throwErrorIfWrappersIsEmpty('setData')
this.wrappers.forEach(wrapper => wrapper.setData(data))
}
setMethods (props: Object): void {
this.throwErrorIfWrappersIsEmpty('setMethods')
this.wrappers.forEach(wrapper => wrapper.setMethods(props))
}
setProps (props: Object): void {
this.throwErrorIfWrappersIsEmpty('setProps')
this.wrappers.forEach(wrapper => wrapper.setProps(props))
}
setValue (value: any): void {
this.throwErrorIfWrappersIsEmpty('setValue')
this.wrappers.forEach(wrapper => wrapper.setValue(value))
}
setChecked (checked: boolean = true): void {
this.throwErrorIfWrappersIsEmpty('setChecked')
this.wrappers.forEach(wrapper => wrapper.setChecked(checked))
}
setSelected (): void {
this.throwErrorIfWrappersIsEmpty('setSelected')
throwError(
`setSelected must be called on a single wrapper, ` +
`use at(i) to access a wrapper`
)
}
trigger (event: string, options: Object): void {
this.throwErrorIfWrappersIsEmpty('trigger')
this.wrappers.forEach(wrapper => wrapper.trigger(event, options))
}
update (): void {
this.throwErrorIfWrappersIsEmpty('update')
warn(
`update has been removed. All changes are now ` +
`synchrnous without calling update`
)
}
destroy (): void {
this.throwErrorIfWrappersIsEmpty('destroy')
this.wrappers.forEach(wrapper => wrapper.destroy())
}
}