Skip to content

Commit

Permalink
refactor: remove deprecated methods (#1109)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: hasStyle, hasAttribute, hasClass, hasProp, visible, and setComputed removed
  • Loading branch information
eddyerburgh authored Jan 20, 2019
1 parent 1f160df commit 1d1d003
Show file tree
Hide file tree
Showing 18 changed files with 4 additions and 960 deletions.
6 changes: 0 additions & 6 deletions flow/wrapper.flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,6 @@ declare interface BaseWrapper {
emittedByOrder(): Array<{ name: string, args: Array<any> }> | void;
exists(): boolean;
filter(predicate: Function): WrapperArray | void;
visible(): boolean | void;
hasAttribute(attribute: string, value: string): boolean | void;
hasClass(className: string): boolean | void;
hasProp(prop: string, value: string): boolean | void;
hasStyle(style: string, value: string): boolean | void;
find(selector: Selector): Wrapper | void;
findAll(selector: Selector): WrapperArray | void;
html(): string | void;
Expand All @@ -34,7 +29,6 @@ declare interface BaseWrapper {
props(key?: string): { [name: string]: any } | any | void;
text(): string | void;
setData(data: Object): void;
setComputed(computed: Object): void;
setMethods(methods: Object): void;
setValue(value: any): void;
setChecked(checked?: boolean): void;
Expand Down
38 changes: 0 additions & 38 deletions packages/test-utils/src/wrapper-array.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,6 @@ export default class WrapperArray implements BaseWrapper {
return new WrapperArray(this.wrappers.filter(predicate))
}

visible (): boolean {
this.throwErrorIfWrappersIsEmpty('visible')

return this.length > 0 && this.wrappers.every(wrapper => wrapper.visible())
}

emitted (): void {
this.throwErrorIfWrappersIsEmpty('emitted')

Expand All @@ -85,32 +79,6 @@ export default class WrapperArray implements BaseWrapper {
)
}

hasAttribute (attribute: string, value: string): boolean {
this.throwErrorIfWrappersIsEmpty('hasAttribute')

return this.wrappers.every(wrapper =>
wrapper.hasAttribute(attribute, value)
)
}

hasClass (className: string): boolean {
this.throwErrorIfWrappersIsEmpty('hasClass')

return this.wrappers.every(wrapper => wrapper.hasClass(className))
}

hasProp (prop: string, value: string): boolean {
this.throwErrorIfWrappersIsEmpty('hasProp')

return this.wrappers.every(wrapper => wrapper.hasProp(prop, value))
}

hasStyle (style: string, value: string): boolean {
this.throwErrorIfWrappersIsEmpty('hasStyle')

return this.wrappers.every(wrapper => wrapper.hasStyle(style, value))
}

findAll (): void {
this.throwErrorIfWrappersIsEmpty('findAll')

Expand Down Expand Up @@ -195,12 +163,6 @@ export default class WrapperArray implements BaseWrapper {
}
}

setComputed (computed: Object): void {
this.throwErrorIfWrappersIsEmpty('setComputed')

this.wrappers.forEach(wrapper => wrapper.setComputed(computed))
}

setData (data: Object): void {
this.throwErrorIfWrappersIsEmpty('setData')

Expand Down
230 changes: 0 additions & 230 deletions packages/test-utils/src/wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,139 +234,6 @@ export default class Wrapper implements BaseWrapper {
return new WrapperArray(wrappers)
}

/**
* Checks if wrapper has an attribute with matching value
*/
hasAttribute (attribute: string, value: string): boolean {
warn(
`hasAttribute() has been deprecated and will be ` +
`removed in version 1.0.0. Use attributes() ` +
`instead—https://vue-test-utils.vuejs.org/api/wrapper/attributes.html`
)

if (typeof attribute !== 'string') {
throwError(
`wrapper.hasAttribute() must be passed attribute as a string`
)
}

if (typeof value !== 'string') {
throwError(
`wrapper.hasAttribute() must be passed value as a string`
)
}

return !!(this.element.getAttribute(attribute) === value)
}

/**
* Asserts wrapper has a class name
*/
hasClass (className: string): boolean {
warn(
`hasClass() has been deprecated and will be removed ` +
`in version 1.0.0. Use classes() ` +
`instead—https://vue-test-utils.vuejs.org/api/wrapper/classes.html`
)
let targetClass = className

if (typeof targetClass !== 'string') {
throwError('wrapper.hasClass() must be passed a string')
}

// if $style is available and has a matching target, use that instead.
if (this.vm && this.vm.$style && this.vm.$style[targetClass]) {
targetClass = this.vm.$style[targetClass]
}

const containsAllClasses = targetClass
.split(' ')
.every(target => this.element.classList.contains(target))

return !!(this.element && containsAllClasses)
}

/**
* Asserts wrapper has a prop name
*/
hasProp (prop: string, value: string): boolean {
warn(
`hasProp() has been deprecated and will be removed ` +
`in version 1.0.0. Use props() ` +
`instead—https://vue-test-utils.vuejs.org/api/wrapper/props.html`
)

if (!this.isVueInstance()) {
throwError('wrapper.hasProp() must be called on a Vue instance')
}
if (typeof prop !== 'string') {
throwError('wrapper.hasProp() must be passed prop as a string')
}

// $props object does not exist in Vue 2.1.x, so use
// $options.propsData instead
if (
this.vm &&
this.vm.$options &&
this.vm.$options.propsData &&
this.vm.$options.propsData[prop] === value
) {
return true
}

return !!this.vm && !!this.vm.$props && this.vm.$props[prop] === value
}

/**
* Checks if wrapper has a style with value
*/
hasStyle (style: string, value: string): boolean {
warn(
`hasStyle() has been deprecated and will be removed ` +
`in version 1.0.0. Use wrapper.element.style ` +
`instead`
)

if (typeof style !== 'string') {
throwError(`wrapper.hasStyle() must be passed style as a string`)
}

if (typeof value !== 'string') {
throwError('wrapper.hasClass() must be passed value as string')
}

/* istanbul ignore next */
if (
navigator.userAgent.includes &&
(navigator.userAgent.includes('node.js') ||
navigator.userAgent.includes('jsdom'))
) {
warn(
`wrapper.hasStyle is not fully supported when ` +
`running jsdom - only inline styles are supported`
)
}
const body = document.querySelector('body')
const mockElement = document.createElement('div')

if (!(body instanceof Element)) {
return false
}
const mockNode = body.insertBefore(mockElement, null)
// $FlowIgnore : Flow thinks style[style] returns a number
mockElement.style[style] = value

if (!this.options.attachedToDocument && (this.vm || this.vnode)) {
// $FlowIgnore : Possible null value, will be removed in 1.0.0
const vm = this.vm || this.vnode.context.$root
body.insertBefore(vm.$root._vnode.elm, null)
}

const elStyle = window.getComputedStyle(this.element)[style]
const mockNodeStyle = window.getComputedStyle(mockNode)[style]
return !!(elStyle && mockNodeStyle && elStyle === mockNodeStyle)
}

/**
* Returns HTML of element as a string
*/
Expand Down Expand Up @@ -562,79 +429,6 @@ export default class Wrapper implements BaseWrapper {
throwError(`wrapper.setSelected() cannot be called on this element`)
}

/**
* Sets vm computed
*/
setComputed (computed: Object): void {
if (!this.isVueInstance()) {
throwError(
`wrapper.setComputed() can only be called on a Vue ` +
`instance`
)
}

warn(
`setComputed() has been deprecated and will be ` +
`removed in version 1.0.0. You can overwrite ` +
`computed properties by passing a computed object ` +
`in the mounting options`
)

Object.keys(computed).forEach(key => {
if (VUE_VERSION > 2.1) {
// $FlowIgnore : Problem with possibly null this.vm
if (!this.vm._computedWatchers[key]) {
throwError(
`wrapper.setComputed() was passed a value that ` +
`does not exist as a computed property on the ` +
`Vue instance. Property ${key} does not exist ` +
`on the Vue instance`
)
}
// $FlowIgnore : Problem with possibly null this.vm
this.vm._computedWatchers[key].value = computed[key]
// $FlowIgnore : Problem with possibly null this.vm
this.vm._computedWatchers[key].getter = () => computed[key]
} else {
let isStore = false
// $FlowIgnore : Problem with possibly null this.vm
this.vm._watchers.forEach(watcher => {
if (watcher.getter.vuex && key in watcher.vm.$options.store.getters) {
watcher.vm.$options.store.getters = {
...watcher.vm.$options.store.getters
}
Object.defineProperty(watcher.vm.$options.store.getters, key, {
get: function () {
return computed[key]
}
})
isStore = true
}
})

// $FlowIgnore : Problem with possibly null this.vm
if (!isStore && !this.vm._watchers.some(w => w.getter.name === key)) {
throwError(
`wrapper.setComputed() was passed a value that does ` +
`not exist as a computed property on the Vue instance. ` +
`Property ${key} does not exist on the Vue instance`
)
}
// $FlowIgnore : Problem with possibly null this.vm
this.vm._watchers.forEach(watcher => {
if (watcher.getter.name === key) {
watcher.value = computed[key]
watcher.getter = () => computed[key]
}
})
}
})
// $FlowIgnore : Problem with possibly null this.vm
this.vm._watchers.forEach(watcher => {
watcher.run()
})
}

/**
* Sets vm data
*/
Expand Down Expand Up @@ -830,28 +624,4 @@ export default class Wrapper implements BaseWrapper {
`updates are now synchronous by default`
)
}

/**
* Utility to check wrapper is visible. Returns false if a parent
* element has display: none or visibility: hidden style.
*/
visible (): boolean {
warn(
`visible has been deprecated and will be removed in ` +
`version 1, use isVisible instead`
)
let element = this.element
while (element) {
if (
element.style &&
(element.style.visibility === 'hidden' ||
element.style.display === 'none')
) {
return false
}
element = element.parentElement
}

return true
}
}
7 changes: 0 additions & 7 deletions packages/test-utils/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ interface BaseWrapper {
contains (selector: Selector): boolean
exists (): boolean
isVisible (): boolean
visible (): boolean

attributes(): { [name: string]: string }
attributes(key: string): string | void
Expand All @@ -62,16 +61,10 @@ interface BaseWrapper {
props(): { [name: string]: any }
props(key: string): any | void

hasAttribute (attribute: string, value: string): boolean
hasClass (className: string): boolean
hasProp (prop: string, value: any): boolean
hasStyle (style: string, value: string): boolean

is (selector: Selector): boolean
isEmpty (): boolean
isVueInstance (): boolean

setComputed (computed: object): void
setData (data: object): void
setMethods (data: object): void
setProps (props: object): void
Expand Down
5 changes: 0 additions & 5 deletions packages/test-utils/types/test/wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,8 @@ bool = wrapper.contains(ClassComponent)

bool = wrapper.exists()

bool = wrapper.hasAttribute('foo', 'bar')
bool = wrapper.attributes().foo === 'bar'
bool = wrapper.hasClass('foo-class')
bool = wrapper.hasProp('checked', true)
bool = wrapper.props().checked
bool = wrapper.hasStyle('color', 'red')
bool = wrapper.classes('foo')

bool = wrapper.is(normalOptions)
Expand All @@ -33,7 +29,6 @@ let o: string = wrapper.emitted('hello')[0]
const emittedByOrder = wrapper.emittedByOrder()
const name: string = emittedByOrder[0].name

wrapper.setComputed({computedProp: true})
wrapper.setData({ foo: 'bar' })
wrapper.setMethods({checked: true})
wrapper.setProps({ checked: true })
Expand Down
6 changes: 3 additions & 3 deletions test/specs/components/TransitionStub.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ describeWithShallowAndMount('TransitionStub', mountingMethod => {
transition: TransitionStub
}
})
expect(wrapper.find('nav').visible()).to.equal(false)
expect(wrapper.find('nav').isVisible()).to.equal(false)
wrapper.find('button').trigger('click')
expect(wrapper.find('nav').visible()).to.equal(true)
expect(wrapper.find('nav').isVisible()).to.equal(true)
wrapper.find('button').trigger('click')
expect(wrapper.find('nav').visible()).to.equal(false)
expect(wrapper.find('nav').isVisible()).to.equal(false)
})

it('logs error when has multiple children', () => {
Expand Down
Loading

0 comments on commit 1d1d003

Please sign in to comment.