Skip to content

Commit

Permalink
Sync mode cleanup (#1671)
Browse files Browse the repository at this point in the history
* docs(createwrapper): remove sync mode from RU docs

* chore(flow): remove sync option from config options

* improvement(tests): write Tests to fit async API signature

Update tests, where applicable, to be awaited. This includes trigger, setValue, setSelected,
setProps, setData, setChecked
  • Loading branch information
AtofStryker committed Sep 10, 2020
1 parent e91effe commit bd91c9f
Show file tree
Hide file tree
Showing 16 changed files with 111 additions and 123 deletions.
1 change: 0 additions & 1 deletion docs/ru/api/createWrapper.md
Expand Up @@ -4,7 +4,6 @@

- `{vm|HTMLElement} node`
- `{Object} options`
- `{Boolean} sync`
- `{Boolean} attachedToDocument`

- **Возвращает:**
Expand Down
1 change: 0 additions & 1 deletion flow/options.flow.js
Expand Up @@ -32,7 +32,6 @@ declare type NormalizedOptions = {
attrs?: { [key: string]: string },
listeners?: { [key: string]: Function | Array<Function> },
parentComponent?: Object,
sync: boolean,
shouldProxy?: boolean
}

Expand Down
7 changes: 3 additions & 4 deletions test/specs/create-local-vue.spec.js
Expand Up @@ -49,8 +49,7 @@ describeWithShallowAndMount('createLocalVue', mountingMethod => {
const wrapper = mountingMethod(ComponentWithVuex, { localVue, store })
expect(wrapper.vm.$store).toBeTruthy()
expect(wrapper.text()).toEqual('0 1')
wrapper.trigger('click')
await Vue.nextTick()
await wrapper.trigger('click')
expect(wrapper.text()).toEqual('1 1')
})

Expand All @@ -70,7 +69,7 @@ describeWithShallowAndMount('createLocalVue', mountingMethod => {
itDoNotRunIf(
mountingMethod.name === 'shallowMount',
'Router should work properly with local Vue',
() => {
async () => {
const localVue = createLocalVue()
localVue.use(VueRouter)
const routes = [
Expand All @@ -95,7 +94,7 @@ describeWithShallowAndMount('createLocalVue', mountingMethod => {

expect(wrapper.text()).toContain('home')

wrapper.find('a').trigger('click')
await wrapper.find('a').trigger('click')
expect(wrapper.text()).toContain('foo')

const freshWrapper = mountingMethod(Component)
Expand Down
4 changes: 2 additions & 2 deletions test/specs/mounting-options/propsData.spec.js
Expand Up @@ -20,9 +20,9 @@ describeRunIf(process.env.TEST_ENV !== 'node', 'propsData', () => {
})

describe('should not modify propsData between tests', () => {
it('should have the correct props after modifying', () => {
it('should have the correct props after modifying', async () => {
expect(wrapper.vm.prop1).toHaveLength(2)
wrapper.setProps({ prop1: [] })
await wrapper.setProps({ prop1: [] })
expect(wrapper.vm.prop1).toHaveLength(0)
})

Expand Down
46 changes: 21 additions & 25 deletions test/specs/mounting-options/scopedSlots.spec.js
Expand Up @@ -241,34 +241,30 @@ describeWithShallowAndMount('scopedSlots', mountingMethod => {
}
)

itDoNotRunIf(
vueVersion < 2.5,
'renders scoped slots in sync mode by default',
async () => {
const TestComponent = {
template: '<div />',
data() {
return {
val: null
}
},
mounted() {
this.val = 123
},
render() {
return this.$scopedSlots.default(this.val)
itDoNotRunIf(vueVersion < 2.5, 'renders scoped slots', async () => {
const TestComponent = {
template: '<div />',
data() {
return {
val: null
}
},
mounted() {
this.val = 123
},
render() {
return this.$scopedSlots.default(this.val)
}
const stub = jest.fn()
mountingMethod(TestComponent, {
scopedSlots: {
default: stub
}
})
await Vue.nextTick()
expect(stub).toHaveBeenCalledWith(123)
}
)
const stub = jest.fn()
mountingMethod(TestComponent, {
scopedSlots: {
default: stub
}
})
await Vue.nextTick()
expect(stub).toHaveBeenCalledWith(123)
})

itDoNotRunIf(
vueVersion < 2.6,
Expand Down
4 changes: 1 addition & 3 deletions test/specs/wrapper-array/setChecked.spec.js
@@ -1,5 +1,4 @@
import { describeWithShallowAndMount } from '~resources/utils'
import Vue from 'vue'

describeWithShallowAndMount('setChecked', mountingMethod => {
it('sets value to the input elements of type checkbox or radio', async () => {
Expand All @@ -20,8 +19,7 @@ describeWithShallowAndMount('setChecked', mountingMethod => {
const wrapperArray = wrapper.findAll('.foo')
expect(wrapper.vm.t1).toEqual(false)
expect(wrapper.vm.t2).toEqual('')
wrapperArray.setChecked()
await Vue.nextTick()
await wrapperArray.setChecked()
expect(wrapper.vm.t1).toEqual(true)
expect(wrapper.vm.t2).toEqual('foo')
expect(wrapperArray.at(0).element.checked).toEqual(true)
Expand Down
5 changes: 2 additions & 3 deletions test/specs/wrapper-array/setData.spec.js
@@ -1,15 +1,14 @@
import { compileToFunctions } from 'vue-template-compiler'
import ComponentWithVIf from '~resources/components/component-with-v-if.vue'
import { describeWithShallowAndMount } from '~resources/utils'
import Vue from 'vue'

describeWithShallowAndMount('setData', mountingMethod => {
it('sets component data and updates nested vm nodes', async () => {
const wrapper = mountingMethod(ComponentWithVIf)
const componentArr = wrapper.findAll(ComponentWithVIf)
expect(componentArr.at(0).findAll('.child.ready').length).toEqual(0)
componentArr.setData({ ready: true })
await Vue.nextTick()
await componentArr.setData({ ready: true })

expect(componentArr.at(0).findAll('.child.ready').length).toEqual(1)
})

Expand Down
9 changes: 4 additions & 5 deletions test/specs/wrapper-array/setProps.spec.js
@@ -1,16 +1,15 @@
import { compileToFunctions } from 'vue-template-compiler'
import ComponentWithProps from '~resources/components/component-with-props.vue'
import { describeWithShallowAndMount } from '~resources/utils'
import Vue from 'vue'

describeWithShallowAndMount('setProps', mountingMethod => {
it('sets component props and updates DOM when called on Vue instance', async () => {
const prop1 = 'prop 1'
const prop2 = 'prop 2'
const propsData = { prop1: 'a prop', prop2 }
const wrapper = mountingMethod(ComponentWithProps, { propsData })
wrapper.findAll(ComponentWithProps).setProps({ prop1 })
await Vue.nextTick()
await wrapper.findAll(ComponentWithProps).setProps({ prop1 })

expect(wrapper.find('.prop-1').element.textContent).toEqual(prop1)
expect(wrapper.find('.prop-2').element.textContent).toEqual(prop2)
})
Expand All @@ -19,8 +18,8 @@ describeWithShallowAndMount('setProps', mountingMethod => {
const prop1 = 'prop 1'
const prop2 = 'prop s'
const wrapper = mountingMethod(ComponentWithProps)
wrapper.findAll(ComponentWithProps).setProps({ prop1, prop2 })
await Vue.nextTick()
await wrapper.findAll(ComponentWithProps).setProps({ prop1, prop2 })

expect(wrapper.find('.prop-1').element.textContent).toEqual(prop1)
expect(wrapper.find('.prop-2').element.textContent).toEqual(prop2)
})
Expand Down
4 changes: 2 additions & 2 deletions test/specs/wrapper-array/setValue.spec.js
@@ -1,7 +1,7 @@
import { describeWithShallowAndMount } from '~resources/utils'

describeWithShallowAndMount('setValue', mountingMethod => {
it('sets value to the text-control input elements', () => {
it('sets value to the text-control input elements', async () => {
const wrapper = mountingMethod({
data() {
return {
Expand All @@ -18,7 +18,7 @@ describeWithShallowAndMount('setValue', mountingMethod => {
const wrapperArray = wrapper.findAll('.foo')
expect(wrapper.vm.t1).toEqual('')
expect(wrapper.vm.t2).toEqual('')
wrapperArray.setValue('foo')
await wrapperArray.setValue('foo')
expect(wrapper.vm.t1).toEqual('foo')
expect(wrapper.vm.t2).toEqual('foo')
expect(wrapperArray.at(0).element.value).toEqual('foo')
Expand Down
12 changes: 6 additions & 6 deletions test/specs/wrapper-array/trigger.spec.js
Expand Up @@ -3,33 +3,33 @@ import ComponentWithEvents from '~resources/components/component-with-events.vue
import { describeWithShallowAndMount } from '~resources/utils'

describeWithShallowAndMount('trigger', mountingMethod => {
it('causes click handler to fire when wrapper.trigger("click") is called on a Component', () => {
it('causes click handler to fire when wrapper.trigger("click") is called on a Component', async () => {
const clickHandler = jest.fn()
const wrapper = mountingMethod(ComponentWithEvents, {
propsData: { clickHandler }
})
const buttonArr = wrapper.findAll('.click')
buttonArr.trigger('click')
await buttonArr.trigger('click')

expect(clickHandler).toHaveBeenCalled()
})

it('causes keydown handler to fire when wrapper.trigger("keydown") is fired on a Component', () => {
it('causes keydown handler to fire when wrapper.trigger("keydown") is fired on a Component', async () => {
const keydownHandler = jest.fn()
const wrapper = mountingMethod(ComponentWithEvents, {
propsData: { keydownHandler }
})
wrapper.findAll('.keydown').trigger('keydown')
await wrapper.findAll('.keydown').trigger('keydown')

expect(keydownHandler).toHaveBeenCalled()
})

it('causes keydown handler to fire when wrapper.trigger("keydown.enter") is fired on a Component', () => {
it('causes keydown handler to fire when wrapper.trigger("keydown.enter") is fired on a Component', async () => {
const keydownHandler = jest.fn()
const wrapper = mountingMethod(ComponentWithEvents, {
propsData: { keydownHandler }
})
wrapper.findAll('.keydown-enter').trigger('keydown.enter')
await wrapper.findAll('.keydown-enter').trigger('keydown.enter')

expect(keydownHandler).toHaveBeenCalled()
})
Expand Down
26 changes: 13 additions & 13 deletions test/specs/wrapper/setChecked.spec.js
Expand Up @@ -12,22 +12,22 @@ describeWithShallowAndMount('setChecked', mountingMethod => {
await response
expect(wrapper.text()).toContain('checkbox checked')
})
it('sets element checked true with no option passed', () => {
it('sets element checked true with no option passed', async () => {
const wrapper = mountingMethod(ComponentWithInput)
const input = wrapper.find('input[type="checkbox"]')
input.setChecked()
await input.setChecked()

expect(input.element.checked).toEqual(true)
})

it('sets element checked equal to param passed', () => {
it('sets element checked equal to param passed', async () => {
const wrapper = mountingMethod(ComponentWithInput)
const input = wrapper.find('input[type="checkbox"]')

input.setChecked(true)
await input.setChecked(true)
expect(input.element.checked).toEqual(true)

input.setChecked(false)
await input.setChecked(false)
expect(input.element.checked).toEqual(false)
})

Expand Down Expand Up @@ -56,10 +56,10 @@ describeWithShallowAndMount('setChecked', mountingMethod => {
expect(wrapper.find('.counter').text()).toEqual('4')
})

it('triggers a change event when called on a checkbox', () => {
it('triggers a change event when called on a checkbox', async () => {
const listener = jest.fn()

mountingMethod({
await mountingMethod({
// For compatibility with earlier versions of Vue that use the `click`
// event for updating `v-model`.
template: `
Expand All @@ -75,10 +75,10 @@ describeWithShallowAndMount('setChecked', mountingMethod => {
expect(listener).toHaveBeenCalled()
})

it('does not trigger a change event if the checkbox is already checked', () => {
it('does not trigger a change event if the checkbox is already checked', async () => {
const listener = jest.fn()

mountingMethod({
await mountingMethod({
template: `
<input
type="checkbox"
Expand Down Expand Up @@ -118,10 +118,10 @@ describeWithShallowAndMount('setChecked', mountingMethod => {
expect(wrapper.find('.counter').text()).toEqual('4')
})

it('triggers a change event when called on a radio button', () => {
it('triggers a change event when called on a radio button', async () => {
const listener = jest.fn()

mountingMethod({
await mountingMethod({
template: `
<input
type="radio"
Expand All @@ -135,10 +135,10 @@ describeWithShallowAndMount('setChecked', mountingMethod => {
expect(listener).toHaveBeenCalled()
})

it('does not trigger a change event if the radio button is already checked', () => {
it('does not trigger a change event if the radio button is already checked', async () => {
const listener = jest.fn()

mountingMethod({
await mountingMethod({
template: `
<input
type="radio"
Expand Down
16 changes: 8 additions & 8 deletions test/specs/wrapper/setData.spec.js
Expand Up @@ -153,7 +153,7 @@ describeWithShallowAndMount('setData', mountingMethod => {
expect(wrapper.text()).toEqual('There is no message yet')
})

it('updates an existing property in a data object', () => {
it('updates an existing property in a data object', async () => {
const TestComponent = {
data: () => ({
anObject: {
Expand All @@ -166,7 +166,7 @@ describeWithShallowAndMount('setData', mountingMethod => {
render: () => {}
}
const wrapper = mountingMethod(TestComponent)
wrapper.setData({
await wrapper.setData({
anObject: {
propA: {
prop1: 'c'
Expand All @@ -177,7 +177,7 @@ describeWithShallowAndMount('setData', mountingMethod => {
expect(wrapper.vm.anObject.propA.prop1).toEqual('c')
})

it('should append a new property to an object without removing existing properties', () => {
it('should append a new property to an object without removing existing properties', async () => {
const TestComponent = {
data: () => ({
anObject: {
Expand All @@ -190,7 +190,7 @@ describeWithShallowAndMount('setData', mountingMethod => {
render: () => {}
}
const wrapper = mountingMethod(TestComponent)
wrapper.setData({
await wrapper.setData({
anObject: {
propA: {
prop2: 'b'
Expand Down Expand Up @@ -238,7 +238,7 @@ describeWithShallowAndMount('setData', mountingMethod => {
}
})
expect(wrapper.text()).toContain('bar')
wrapper.setData({
await wrapper.setData({
nullProperty: {
another: {
obj: true
Expand All @@ -261,7 +261,7 @@ describeWithShallowAndMount('setData', mountingMethod => {
})
}
const wrapper = mountingMethod(TestComponent)
wrapper.setData({
await wrapper.setData({
items: [3]
})
expect(wrapper.vm.items).toEqual([3])
Expand Down Expand Up @@ -304,7 +304,7 @@ describeWithShallowAndMount('setData', mountingMethod => {
expect(wrapper.html()).toEqual('<div>propA,propB,propC</div>')
})

it('allows setting data of type Date synchronously', () => {
it('allows setting data of type Date synchronously', async () => {
const TestComponent = {
template: `
<div>
Expand All @@ -317,7 +317,7 @@ describeWithShallowAndMount('setData', mountingMethod => {
}
const testDate = new Date()
const wrapper = mountingMethod(TestComponent)
wrapper.setData({ selectedDate: testDate })
await wrapper.setData({ selectedDate: testDate })
expect(wrapper.vm.selectedDate).toEqual(testDate)
})
})

0 comments on commit bd91c9f

Please sign in to comment.