Skip to content

Commit c2de268

Browse files
committed
chore: improve deprecations
1 parent 6f0a41a commit c2de268

File tree

4 files changed

+125
-4
lines changed

4 files changed

+125
-4
lines changed

Diff for: packages/shared/merge-options.js

+8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// @flow
22
import { normalizeStubs, normalizeProvide } from './normalize'
3+
import { warn } from 'shared/util'
34

45
function getOption(option, config?: Object): any {
56
if (option === false) {
@@ -33,6 +34,13 @@ export function mergeOptions(
3334
const methods = (getOption(options.methods, config.methods): {
3435
[key: string]: Function
3536
})
37+
38+
if (config.methods && Object.keys(config.methods).length) {
39+
warn(
40+
`config.methods has been deprecated. It will be removed in a future release`
41+
)
42+
}
43+
3644
const provide = (getOption(options.provide, config.provide): Object)
3745
const stubs = (getStubs(options.stubs, config.stubs): Object)
3846
// $FlowIgnore

Diff for: packages/test-utils/src/wrapper.js

+86-4
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,16 @@
33
import Vue from 'vue'
44
import pretty from 'pretty'
55
import getSelector from './get-selector'
6-
import { REF_SELECTOR, FUNCTIONAL_OPTIONS, VUE_VERSION } from 'shared/consts'
6+
import {
7+
REF_SELECTOR,
8+
FUNCTIONAL_OPTIONS,
9+
VUE_VERSION,
10+
DOM_SELECTOR
11+
} from 'shared/consts'
712
import config from './config'
813
import WrapperArray from './wrapper-array'
914
import ErrorWrapper from './error-wrapper'
10-
import { throwError, getCheckedEvent, isPhantomJS } from 'shared/util'
15+
import { throwError, getCheckedEvent, isPhantomJS, warn } from 'shared/util'
1116
import find from './find'
1217
import createWrapper from './create-wrapper'
1318
import { recursivelySetData } from './recursively-set-data'
@@ -117,6 +122,9 @@ export default class Wrapper implements BaseWrapper {
117122
* Checks if wrapper contains provided selector.
118123
*/
119124
contains(rawSelector: Selector): boolean {
125+
warn(
126+
'contains is deprecated and will be removed in a future release. Use `wrapper.find`, `wrapper.findComponent` or `wrapper.get` instead'
127+
)
120128
const selector = getSelector(rawSelector, 'contains')
121129
const nodes = find(this.rootNode, this.vm, selector)
122130
return nodes.length > 0
@@ -163,6 +171,9 @@ export default class Wrapper implements BaseWrapper {
163171
* Returns an Array containing custom events emitted by the Wrapper vm
164172
*/
165173
emittedByOrder(): Array<{ name: string, args: Array<any> }> {
174+
warn(
175+
'emittedByOrder is deprecated and will be removed in a future release. Use `wrapper.emitted` instead'
176+
)
166177
if (!this._emittedByOrder && !this.vm) {
167178
throwError(
168179
`wrapper.emittedByOrder() can only be called on a Vue instance`
@@ -190,6 +201,9 @@ export default class Wrapper implements BaseWrapper {
190201
* matches the provided selector.
191202
*/
192203
get(rawSelector: Selector): Wrapper {
204+
warn(
205+
'get is deprecated and will be removed in a future release. Use `find` or `findComponent` instead'
206+
)
193207
const found = this.find(rawSelector)
194208
if (found instanceof ErrorWrapper) {
195209
throw new Error(`Unable to find ${rawSelector} within: ${this.html()}`)
@@ -198,11 +212,16 @@ export default class Wrapper implements BaseWrapper {
198212
}
199213

200214
/**
201-
* Finds first node in tree of the current wrapper that
215+
* Finds first DOM node in tree of the current wrapper that
202216
* matches the provided selector.
203217
*/
204218
find(rawSelector: Selector): Wrapper | ErrorWrapper {
205219
const selector = getSelector(rawSelector, 'find')
220+
if (selector.type !== DOM_SELECTOR) {
221+
warn(
222+
'finding components with `find` is deprecated and will be removed in a future release. Use `findComponent` instead'
223+
)
224+
}
206225
const node = find(this.rootNode, this.vm, selector)[0]
207226

208227
if (!node) {
@@ -215,11 +234,63 @@ export default class Wrapper implements BaseWrapper {
215234
}
216235

217236
/**
218-
* Finds node in tree of the current wrapper that matches
237+
* Finds DOM elements in tree of the current wrapper that matches
219238
* the provided selector.
220239
*/
221240
findAll(rawSelector: Selector): WrapperArray {
222241
const selector = getSelector(rawSelector, 'findAll')
242+
if (selector.type !== DOM_SELECTOR) {
243+
warn(
244+
'finding components with `findAll` is deprecated and will be removed in a future release. Use `findAllComponents` instead'
245+
)
246+
}
247+
const nodes = find(this.rootNode, this.vm, selector)
248+
const wrappers = nodes.map(node => {
249+
// Using CSS Selector, returns a VueWrapper instance if the root element
250+
// binds a Vue instance.
251+
const wrapper = createWrapper(node, this.options)
252+
wrapper.selector = rawSelector
253+
return wrapper
254+
})
255+
256+
const wrapperArray = new WrapperArray(wrappers)
257+
wrapperArray.selector = rawSelector
258+
return wrapperArray
259+
}
260+
261+
/**
262+
* Finds first component in tree of the current wrapper that
263+
* matches the provided selector.
264+
*/
265+
findComponent(rawSelector: Selector): Wrapper | ErrorWrapper {
266+
const selector = getSelector(rawSelector, 'findComponent')
267+
if (selector.type === DOM_SELECTOR) {
268+
throwError(
269+
'findComponent requires a Vue constructor or valid find object. If you are searching for DOM nodes, use `find` instead'
270+
)
271+
}
272+
const node = find(this.rootNode, this.vm, selector)[0]
273+
274+
if (!node) {
275+
return new ErrorWrapper(rawSelector)
276+
}
277+
278+
const wrapper = createWrapper(node, this.options)
279+
wrapper.selector = rawSelector
280+
return wrapper
281+
}
282+
283+
/**
284+
* Finds components in tree of the current wrapper that matches
285+
* the provided selector.
286+
*/
287+
findAllComponents(rawSelector: Selector): WrapperArray {
288+
const selector = getSelector(rawSelector, 'findAll')
289+
if (selector.type === DOM_SELECTOR) {
290+
throwError(
291+
'findAllComponent requires a Vue constructor or valid find object. If you are searching for DOM nodes, use `find` instead'
292+
)
293+
}
223294
const nodes = find(this.rootNode, this.vm, selector)
224295
const wrappers = nodes.map(node => {
225296
// Using CSS Selector, returns a VueWrapper instance if the root element
@@ -245,6 +316,9 @@ export default class Wrapper implements BaseWrapper {
245316
* Checks if node matches selector
246317
*/
247318
is(rawSelector: Selector): boolean {
319+
warn(
320+
`is is deprecated and will be removed in a future release. Use element.tagName instead`
321+
)
248322
const selector = getSelector(rawSelector, 'is')
249323

250324
if (selector.type === REF_SELECTOR) {
@@ -258,6 +332,10 @@ export default class Wrapper implements BaseWrapper {
258332
* Checks if node is empty
259333
*/
260334
isEmpty(): boolean {
335+
warn(
336+
`isEmpty is deprecated and will be removed in a future release. ` +
337+
`Consider a custom matcher such as those provided in jest-dom: https://github.com/testing-library/jest-dom#tobeempty`
338+
)
261339
if (!this.vnode) {
262340
return this.element.innerHTML === ''
263341
}
@@ -282,6 +360,8 @@ export default class Wrapper implements BaseWrapper {
282360
* Checks if node is visible
283361
*/
284362
isVisible(): boolean {
363+
warn(`isEmpty is deprecated and will be removed in a future release.
364+
Consider a custom matcher such as those provided in jest-dom: https://github.com/testing-library/jest-dom#tobevisible`)
285365
let element = this.element
286366
while (element) {
287367
if (
@@ -302,6 +382,7 @@ export default class Wrapper implements BaseWrapper {
302382
* Checks if wrapper is a vue instance
303383
*/
304384
isVueInstance(): boolean {
385+
warn(`isVueInstance is deprecated and will be removed in a future release`)
305386
return !!this.vm
306387
}
307388

@@ -529,6 +610,7 @@ export default class Wrapper implements BaseWrapper {
529610
* Sets vm methods
530611
*/
531612
setMethods(methods: Object): void {
613+
warn(`setMethods is deprecated and will be removed in a future release`)
532614
if (!this.isVueInstance()) {
533615
throwError(`wrapper.setMethods() can only be called on a Vue instance`)
534616
}

Diff for: test/specs/wrapper/find.spec.js

+15
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,21 @@ describeWithShallowAndMount('find', mountingMethod => {
137137
expect(wrapper.find(Component).vnode).to.be.an('object')
138138
})
139139

140+
it('returns Wrapper of Vue Components matching component using findComponent', () => {
141+
const wrapper = mountingMethod(ComponentWithChild)
142+
expect(wrapper.findComponent(Component).vnode).to.be.an('object')
143+
})
144+
145+
it('throws an error if findComponent selector is a CSS selector', () => {
146+
const wrapper = mountingMethod(Component)
147+
const message =
148+
'[vue-test-utils]: findComponent requires a Vue constructor or valid find object. If you are searching for DOM nodes, use `find` instead'
149+
const fn = () => wrapper.findComponent('#foo')
150+
expect(fn)
151+
.to.throw()
152+
.with.property('message', message)
153+
})
154+
140155
itSkipIf(isRunningPhantomJS, 'returns Wrapper of class component', () => {
141156
const TestComponent = {
142157
template: `

Diff for: test/specs/wrapper/findAll.spec.js

+16
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,22 @@ 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', () => {
149+
const wrapper = mountingMethod(ComponentWithChild)
150+
const componentArr = wrapper.findAllComponents(Component)
151+
expect(componentArr.length).to.equal(1)
152+
})
153+
154+
it('throws an error if findComponent selector is a CSS selector', () => {
155+
const wrapper = mountingMethod(Component)
156+
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'
158+
const fn = () => wrapper.findAllComponents('#foo')
159+
expect(fn)
160+
.to.throw()
161+
.with.property('message', message)
162+
})
163+
148164
it('returns correct number of Vue Wrapper when component has a v-for', () => {
149165
const items = [{ id: 1 }, { id: 2 }, { id: 3 }]
150166
const wrapper = mountingMethod(ComponentWithVFor, { propsData: { items } })

0 commit comments

Comments
 (0)