Skip to content

Commit 66c5038

Browse files
refactor: cleanup find methods and tests (#1525)
* refactor: cleanup find methods * refactor: add deprecation jsdoc tags * refactor: cleanup tests * fix: fix order of errors
1 parent aa7b76d commit 66c5038

File tree

3 files changed

+71
-38
lines changed

3 files changed

+71
-38
lines changed

packages/test-utils/src/wrapper.js

+48-35
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ export default class Wrapper implements BaseWrapper {
126126

127127
/**
128128
* Checks if wrapper contains provided selector.
129+
* @deprecated
129130
*/
130131
contains(rawSelector: Selector): boolean {
131132
warnDeprecated(
@@ -176,6 +177,7 @@ export default class Wrapper implements BaseWrapper {
176177

177178
/**
178179
* Returns an Array containing custom events emitted by the Wrapper vm
180+
* @deprecated
179181
*/
180182
emittedByOrder(): Array<{ name: string, args: Array<any> }> {
181183
warnDeprecated('emittedByOrder', 'Use `wrapper.emitted` instead')
@@ -225,41 +227,8 @@ export default class Wrapper implements BaseWrapper {
225227
'Use `findComponent` instead'
226228
)
227229
}
228-
const node = find(this.rootNode, this.vm, selector)[0]
229-
230-
if (!node) {
231-
return new ErrorWrapper(rawSelector)
232-
}
233-
234-
const wrapper = createWrapper(node, this.options)
235-
wrapper.selector = rawSelector
236-
return wrapper
237-
}
238-
239-
/**
240-
* Finds DOM elements in tree of the current wrapper that matches
241-
* the provided selector.
242-
*/
243-
findAll(rawSelector: Selector): WrapperArray {
244-
const selector = getSelector(rawSelector, 'findAll')
245-
if (selector.type !== DOM_SELECTOR) {
246-
warnDeprecated(
247-
'finding components with `findAll`',
248-
'Use `findAllComponents` instead'
249-
)
250-
}
251-
const nodes = find(this.rootNode, this.vm, selector)
252-
const wrappers = nodes.map(node => {
253-
// Using CSS Selector, returns a VueWrapper instance if the root element
254-
// binds a Vue instance.
255-
const wrapper = createWrapper(node, this.options)
256-
wrapper.selector = rawSelector
257-
return wrapper
258-
})
259230

260-
const wrapperArray = new WrapperArray(wrappers)
261-
wrapperArray.selector = rawSelector
262-
return wrapperArray
231+
return this.__find(rawSelector, selector)
263232
}
264233

265234
/**
@@ -268,11 +237,22 @@ export default class Wrapper implements BaseWrapper {
268237
*/
269238
findComponent(rawSelector: Selector): Wrapper | ErrorWrapper {
270239
const selector = getSelector(rawSelector, 'findComponent')
240+
if (!this.vm) {
241+
throwError(
242+
'You cannot chain findComponent off a DOM element. It can only be used on Vue Components.'
243+
)
244+
}
245+
271246
if (selector.type === DOM_SELECTOR) {
272247
throwError(
273248
'findComponent requires a Vue constructor or valid find object. If you are searching for DOM nodes, use `find` instead'
274249
)
275250
}
251+
252+
return this.__find(rawSelector, selector)
253+
}
254+
255+
__find(rawSelector: Selector, selector: Object): Wrapper | ErrorWrapper {
276256
const node = find(this.rootNode, this.vm, selector)[0]
277257

278258
if (!node) {
@@ -284,17 +264,41 @@ export default class Wrapper implements BaseWrapper {
284264
return wrapper
285265
}
286266

267+
/**
268+
* Finds DOM elements in tree of the current wrapper that matches
269+
* the provided selector.
270+
*/
271+
findAll(rawSelector: Selector): WrapperArray {
272+
const selector = getSelector(rawSelector, 'findAll')
273+
if (selector.type !== DOM_SELECTOR) {
274+
warnDeprecated(
275+
'finding components with `findAll`',
276+
'Use `findAllComponents` instead'
277+
)
278+
}
279+
return this.__findAll(rawSelector, selector)
280+
}
281+
287282
/**
288283
* Finds components in tree of the current wrapper that matches
289284
* the provided selector.
290285
*/
291286
findAllComponents(rawSelector: Selector): WrapperArray {
292287
const selector = getSelector(rawSelector, 'findAll')
288+
if (!this.vm) {
289+
throwError(
290+
'You cannot chain findAllComponents off a DOM element. It can only be used on Vue Components.'
291+
)
292+
}
293293
if (selector.type === DOM_SELECTOR) {
294294
throwError(
295-
'findAllComponent requires a Vue constructor or valid find object. If you are searching for DOM nodes, use `find` instead'
295+
'findAllComponents requires a Vue constructor or valid find object. If you are searching for DOM nodes, use `find` instead'
296296
)
297297
}
298+
return this.__findAll(rawSelector, selector)
299+
}
300+
301+
__findAll(rawSelector: Selector, selector: Object): WrapperArray {
298302
const nodes = find(this.rootNode, this.vm, selector)
299303
const wrappers = nodes.map(node => {
300304
// Using CSS Selector, returns a VueWrapper instance if the root element
@@ -318,6 +322,7 @@ export default class Wrapper implements BaseWrapper {
318322

319323
/**
320324
* Checks if node matches selector
325+
* @deprecated
321326
*/
322327
is(rawSelector: Selector): boolean {
323328
warnDeprecated('is', 'Use element.tagName instead')
@@ -332,6 +337,7 @@ export default class Wrapper implements BaseWrapper {
332337

333338
/**
334339
* Checks if node is empty
340+
* @deprecated
335341
*/
336342
isEmpty(): boolean {
337343
warnDeprecated(
@@ -360,6 +366,7 @@ export default class Wrapper implements BaseWrapper {
360366

361367
/**
362368
* Checks if node is visible
369+
* @deprecated
363370
*/
364371
isVisible(): boolean {
365372
warnDeprecated(
@@ -384,6 +391,7 @@ export default class Wrapper implements BaseWrapper {
384391

385392
/**
386393
* Checks if wrapper is a vue instance
394+
* @deprecated
387395
*/
388396
isVueInstance(): boolean {
389397
warnDeprecated(`isVueInstance`)
@@ -392,6 +400,7 @@ export default class Wrapper implements BaseWrapper {
392400

393401
/**
394402
* Returns name of component, or tag name if node is not a Vue component
403+
* @deprecated
395404
*/
396405
name(): string {
397406
warnDeprecated(`name`)
@@ -414,6 +423,7 @@ export default class Wrapper implements BaseWrapper {
414423
/**
415424
* Prints a simple overview of the wrapper current state
416425
* with useful information for debugging
426+
* @deprecated
417427
*/
418428
overview(): void {
419429
warnDeprecated(`overview`)
@@ -517,6 +527,7 @@ export default class Wrapper implements BaseWrapper {
517527

518528
/**
519529
* Checks radio button or checkbox element
530+
* @deprecated
520531
*/
521532
setChecked(checked: boolean = true): Promise<*> {
522533
warnDeprecated(
@@ -568,6 +579,7 @@ export default class Wrapper implements BaseWrapper {
568579

569580
/**
570581
* Selects <option></option> element
582+
* @deprecated
571583
*/
572584
setSelected(): Promise<void> {
573585
warnDeprecated(
@@ -625,6 +637,7 @@ export default class Wrapper implements BaseWrapper {
625637

626638
/**
627639
* Sets vm methods
640+
* @deprecated
628641
*/
629642
setMethods(methods: Object): void {
630643
warnDeprecated(`setMethods`)

test/specs/wrapper/find.spec.js

+10
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,16 @@ describeWithShallowAndMount('find', mountingMethod => {
152152
.with.property('message', message)
153153
})
154154

155+
it('throws an error if findComponent is chained off a DOM element', () => {
156+
const wrapper = mountingMethod(ComponentWithChild)
157+
const message =
158+
'[vue-test-utils]: You cannot chain findComponent off a DOM element. It can only be used on Vue Components.'
159+
const fn = () => wrapper.find('span').findComponent('#foo')
160+
expect(fn)
161+
.to.throw()
162+
.with.property('message', message)
163+
})
164+
155165
itSkipIf(isRunningPhantomJS, 'returns Wrapper of class component', () => {
156166
const TestComponent = {
157167
template: `

test/specs/wrapper/findAll.spec.js

+13-3
Original file line numberDiff line numberDiff line change
@@ -145,22 +145,32 @@ describeWithShallowAndMount('findAll', mountingMethod => {
145145
expect(componentArr.length).to.equal(1)
146146
})
147147

148-
it('returns an array of VueWrappers of Vue Components matching componentusing findAllComponents', () => {
148+
it('returns an array of VueWrappers of Vue Components matching components using findAllComponents', () => {
149149
const wrapper = mountingMethod(ComponentWithChild)
150150
const componentArr = wrapper.findAllComponents(Component)
151151
expect(componentArr.length).to.equal(1)
152152
})
153153

154-
it('throws an error if findComponent selector is a CSS selector', () => {
154+
it('throws an error if findAllComponents selector is a CSS selector', () => {
155155
const wrapper = mountingMethod(Component)
156156
const message =
157-
'[vue-test-utils]: findAllComponent requires a Vue constructor or valid find object. If you are searching for DOM nodes, use `find` instead'
157+
'[vue-test-utils]: findAllComponents requires a Vue constructor or valid find object. If you are searching for DOM nodes, use `find` instead'
158158
const fn = () => wrapper.findAllComponents('#foo')
159159
expect(fn)
160160
.to.throw()
161161
.with.property('message', message)
162162
})
163163

164+
it('throws an error if chaining findAllComponents off a DOM element', () => {
165+
const wrapper = mountingMethod(ComponentWithChild)
166+
const message =
167+
'[vue-test-utils]: You cannot chain findAllComponents off a DOM element. It can only be used on Vue Components.'
168+
const fn = () => wrapper.find('span').findAllComponents('#foo')
169+
expect(fn)
170+
.to.throw()
171+
.with.property('message', message)
172+
})
173+
164174
it('returns correct number of Vue Wrapper when component has a v-for', () => {
165175
const items = [{ id: 1 }, { id: 2 }, { id: 3 }]
166176
const wrapper = mountingMethod(ComponentWithVFor, { propsData: { items } })

0 commit comments

Comments
 (0)