Skip to content

Commit f689640

Browse files
authoredMay 3, 2018
refactor: rename shallow to shallowMount (#576)
1 parent f3dfb1e commit f689640

File tree

5 files changed

+254
-2
lines changed

5 files changed

+254
-2
lines changed
 

‎packages/test-utils/src/index.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,23 @@
1-
import shallow from './shallow'
1+
import shallowMount from './shallow-mount'
22
import mount from './mount'
33
import createLocalVue from './create-local-vue'
44
import TransitionStub from './components/TransitionStub'
55
import TransitionGroupStub from './components/TransitionGroupStub'
66
import RouterLinkStub from './components/RouterLinkStub'
77
import config from './config'
8+
import { warn } from 'shared/util'
9+
10+
function shallow (component, options) {
11+
warn('shallow has been renamed to shallowMount and will be deprecated in 1.0.0')
12+
return shallowMount(component, options)
13+
}
814

915
export default {
1016
createLocalVue,
1117
config,
1218
mount,
1319
shallow,
20+
shallowMount,
1421
TransitionStub,
1522
TransitionGroupStub,
1623
RouterLinkStub
+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// @flow
2+
3+
import './warn-if-no-window'
4+
import Vue from 'vue'
5+
import mount from './mount'
6+
import type VueWrapper from './vue-wrapper'
7+
import {
8+
createComponentStubsForAll,
9+
createStubs
10+
} from 'shared/stub-components'
11+
import { camelize,
12+
capitalize,
13+
hyphenate
14+
} from 'shared/util'
15+
16+
export default function shallowMount (
17+
component: Component,
18+
options: Options = {}
19+
): VueWrapper {
20+
const vue = options.localVue || Vue
21+
22+
// remove any recursive components added to the constructor
23+
// in vm._init from previous tests
24+
if (component.name && component.components) {
25+
delete component.components[capitalize(camelize(component.name))]
26+
delete component.components[hyphenate(component.name)]
27+
}
28+
29+
return mount(component, {
30+
...options,
31+
components: {
32+
...createStubs(vue.options.components),
33+
...createComponentStubsForAll(component)
34+
}
35+
})
36+
}

‎test/setup/mocha.setup.js

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ if (process.env.TEST_ENV !== 'node') {
44

55
const chai = require('chai')
66
const sinon = require('sinon')
7+
const sinonChai = require('sinon-chai')
8+
9+
chai.use(sinonChai)
710

811
global.expect = chai.expect
912
global.sinon = sinon

‎test/specs/components/TransitionStub.spec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ describeWithShallowAndMount('TransitionStub', (mountingMethod) => {
5454
'transition': TransitionStub
5555
}
5656
})
57-
expect(error.args[0][0]).to.equal(msg)
57+
expect(error).calledWith(msg)
5858
error.restore()
5959
})
6060

‎test/specs/shallow-mount.spec.js

+206
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
import { compileToFunctions } from 'vue-template-compiler'
2+
import Vue from 'vue'
3+
import { mount, shallowMount } from '~vue/test-utils'
4+
import Component from '~resources/components/component.vue'
5+
import ComponentWithChild from '~resources/components/component-with-child.vue'
6+
import ComponentWithNestedChildren from '~resources/components/component-with-nested-children.vue'
7+
import ComponentWithLifecycleHooks from '~resources/components/component-with-lifecycle-hooks.vue'
8+
import ComponentWithoutName from '~resources/components/component-without-name.vue'
9+
import ComponentAsAClassWithChild from '~resources/components/component-as-a-class-with-child.vue'
10+
import RecursiveComponent from '~resources/components/recursive-component.vue'
11+
import { vueVersion, describeIf } from '~resources/utils'
12+
13+
describeIf(process.env.TEST_ENV !== 'node',
14+
'shallow', () => {
15+
let info
16+
17+
beforeEach(() => {
18+
info = sinon.stub(console, 'info')
19+
})
20+
21+
afterEach(() => {
22+
info.restore()
23+
})
24+
25+
it('returns new VueWrapper of Vue localVue if no options are passed', () => {
26+
const compiled = compileToFunctions('<div><input /></div>')
27+
const wrapper = shallowMount(compiled)
28+
expect(wrapper.isVueComponent).to.equal(true)
29+
expect(wrapper.vm).to.be.an('object')
30+
})
31+
32+
it('returns new VueWrapper of Vue localVue with all children stubbed', () => {
33+
const wrapper = shallowMount(ComponentWithNestedChildren)
34+
expect(wrapper.isVueComponent).to.equal(true)
35+
expect(wrapper.findAll(Component).length).to.equal(0)
36+
expect(wrapper.findAll(ComponentWithChild).length).to.equal(1)
37+
})
38+
39+
it('returns new VueWrapper of Vue localVue with all children stubbed', () => {
40+
const wrapper = shallowMount(ComponentWithNestedChildren)
41+
expect(wrapper.isVueComponent).to.equal(true)
42+
expect(wrapper.findAll(Component).length).to.equal(0)
43+
expect(wrapper.findAll(ComponentWithChild).length).to.equal(1)
44+
})
45+
46+
it('does not modify component directly', () => {
47+
const wrapper = shallowMount(ComponentWithNestedChildren)
48+
expect(wrapper.findAll(Component).length).to.equal(0)
49+
const mountedWrapper = mount(ComponentWithNestedChildren)
50+
expect(mountedWrapper.findAll(Component).length).to.equal(1)
51+
})
52+
53+
it('stubs globally registered components when options.localVue is provided', () => {
54+
const localVue = Vue.extend()
55+
localVue.component('registered-component', ComponentWithLifecycleHooks)
56+
const Component = {
57+
render: h => h('registered-component')
58+
}
59+
shallowMount(Component, { localVue })
60+
mount(Component, { localVue })
61+
62+
expect(info.callCount).to.equal(4)
63+
})
64+
65+
it('stubs globally registered components', () => {
66+
Vue.component('registered-component', ComponentWithLifecycleHooks)
67+
const Component = {
68+
render: h => h('registered-component')
69+
}
70+
shallowMount(Component)
71+
mount(Component)
72+
73+
expect(info.callCount).to.equal(4)
74+
})
75+
76+
it('does not call stubbed children lifecycle hooks', () => {
77+
shallowMount(ComponentWithNestedChildren)
78+
expect(info.called).to.equal(false)
79+
})
80+
81+
it('stubs extended components', () => {
82+
const ComponentWithPTag = {
83+
template: `<p></p>`
84+
}
85+
const BaseComponent = {
86+
template: `
87+
<div>
88+
<component-with-p-tag />
89+
</div>
90+
`,
91+
components: {
92+
ComponentWithPTag
93+
}
94+
}
95+
96+
const TestComponent = {
97+
extends: BaseComponent
98+
}
99+
100+
const wrapper = shallowMount(TestComponent)
101+
expect(wrapper.find(ComponentWithPTag).exists()).to.equal(true)
102+
expect(wrapper.find('p').exists()).to.equal(false)
103+
})
104+
105+
it('stubs nested extended components', () => {
106+
const ComponentWithPTag = {
107+
template: `<p></p>`
108+
}
109+
const BaseComponent = {
110+
template: `
111+
<div>
112+
<component-with-p-tag />
113+
</div>
114+
`,
115+
components: {
116+
ComponentWithPTag
117+
}
118+
}
119+
120+
const ExtendedBaseComponent = {
121+
extends: BaseComponent
122+
}
123+
124+
const TestComponent = {
125+
extends: ExtendedBaseComponent
126+
}
127+
128+
const wrapper = shallowMount(TestComponent)
129+
expect(wrapper.find(ComponentWithPTag).exists()).to.equal(true)
130+
expect(wrapper.find('p').exists()).to.equal(false)
131+
})
132+
133+
it('stubs Vue class component children', () => {
134+
if (vueVersion < 2.3) {
135+
return
136+
}
137+
const wrapper = shallowMount(ComponentAsAClassWithChild)
138+
expect(wrapper.find(Component).exists()).to.equal(true)
139+
expect(wrapper.findAll('div').length).to.equal(1)
140+
})
141+
142+
it('works correctly with find, contains, findAll, and is on unnamed components', () => {
143+
const TestComponent = {
144+
template: `
145+
<div>
146+
<component-without-name />
147+
</div>
148+
`,
149+
components: {
150+
ComponentWithoutName
151+
}
152+
}
153+
const wrapper = shallowMount(TestComponent)
154+
expect(wrapper.contains(ComponentWithoutName)).to.equal(true)
155+
expect(wrapper.find(ComponentWithoutName).exists()).to.equal(true)
156+
expect(wrapper.findAll(ComponentWithoutName).length).to.equal(1)
157+
})
158+
159+
it('works correctly with find, contains, findAll, and is on named components', () => {
160+
const TestComponent = {
161+
template: `
162+
<div>
163+
<a-component />
164+
</div>
165+
`,
166+
components: {
167+
AComponent: Component
168+
}
169+
}
170+
const wrapper = shallowMount(TestComponent)
171+
expect(wrapper.contains(Component)).to.equal(true)
172+
expect(wrapper.find(Component).exists()).to.equal(true)
173+
expect(wrapper.findAll(Component).length).to.equal(1)
174+
})
175+
176+
it('works correctly with find on recursive components', () => {
177+
// this is for a bug that I've been unable to replicate.
178+
// Sometimes components mutate their components, in this line—
179+
RecursiveComponent.components = {
180+
RecursiveComponent: { render: h => h('div') }
181+
}
182+
183+
expect(shallowMount(RecursiveComponent, {
184+
propsData: {
185+
items: ['', '']
186+
}
187+
}).findAll(RecursiveComponent).length).to.equal(2)
188+
RecursiveComponent.components = {
189+
'recursive-component': { render: h => h('div') }
190+
}
191+
expect(shallowMount(RecursiveComponent, {
192+
propsData: {
193+
items: ['', '']
194+
}
195+
}).findAll(RecursiveComponent).length).to.equal(2)
196+
})
197+
198+
it('throws an error when the component fails to mount', () => {
199+
expect(() => shallowMount({
200+
template: '<div></div>',
201+
mounted: function () {
202+
throw (new Error('Error'))
203+
}
204+
})).to.throw()
205+
})
206+
})

0 commit comments

Comments
 (0)
Please sign in to comment.