Skip to content
This repository was archived by the owner on Dec 12, 2020. It is now read-only.

Commit d088c40

Browse files
committed
feat: add global mixins, close #5
1 parent 568020e commit d088c40

File tree

5 files changed

+105
-40
lines changed

5 files changed

+105
-40
lines changed

README.md

+26-2
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,9 @@ beforeEach(mountVue({ template, data }, { extensions }))
110110
```
111111

112112
See [Vue component docs](https://vuejs.org/v2/api/#Vue-component),
113-
[message-list-spec.js](cypress/integration/message-list-spec.js)
113+
[global-components-spec.js](cypress/integration/global-components-spec.js)
114114

115-
* `use` - list of plugins
115+
* `use` (alias `plugins`) - list of plugins
116116

117117
```js
118118
const use = [MyPlugin]
@@ -126,6 +126,30 @@ beforeEach(mountVue({}, { extensions }))
126126
See [Vue plugin docs](https://vuejs.org/v2/guide/plugins.html)
127127
and [plugin-spec.js](cypress/integrstion/plugin-spec.js)
128128

129+
* `mixin` (alias `mixins`) - list of global mixins
130+
131+
```js
132+
const MyMixin = {
133+
// we have to use original Sinon to create a spy
134+
// because we are outside a test function
135+
// and cannot use "cy.spy"
136+
created: Cypress.sinon.spy()
137+
}
138+
const mixin = [MyMixin]
139+
// extend Vue with mixins
140+
const extensions = {
141+
mixin
142+
}
143+
beforeEach(mountVue({}, { extensions }))
144+
145+
it('calls mixin "created" method', () => {
146+
expect(MyMixin.created).to.have.been.calledOnce
147+
})
148+
```
149+
150+
See [Vue global mixin docs](https://vuejs.org/v2/guide/mixins.html#Global-Mixin)
151+
and [mixin-spec.js](cypress/integrstion/mixin-spec.js)
152+
129153
### The intro example
130154

131155
Take a look at the first Vue v2 example:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import MessageList from '../../components/MessageList.vue'
2+
const mountVue = require('../..')
3+
4+
// common utils for MessageList
5+
const getItems = () => cy.get('ul li')
6+
7+
/* eslint-env mocha */
8+
describe('Global components', () => {
9+
// two different components, each gets "numbers" list
10+
// into its property "messages"
11+
const template = `
12+
<div>
13+
<message-list :messages="numbers"/>
14+
<a-list :messages="numbers"/>
15+
</div>
16+
`
17+
// our top level data
18+
const data = () => ({ numbers: ['uno', 'dos'] })
19+
// register same component globally under different names
20+
const components = {
21+
'message-list': MessageList,
22+
'a-list': MessageList
23+
}
24+
// extend Vue with global components
25+
const extensions = {
26+
components
27+
}
28+
beforeEach(mountVue({ template, data }, { extensions }))
29+
30+
it('registers global component', () => {
31+
cy
32+
.window()
33+
.its('Vue')
34+
.invoke('component', 'message-list')
35+
// returns component constructor
36+
// so we can compare with our component's constructor (Ctor)
37+
.should('equal', MessageList._Ctor[0])
38+
})
39+
40+
it('shows two items at the start in both lists', () => {
41+
getItems().should('have.length', 4)
42+
})
43+
})

cypress/integration/message-list-spec.js

-37
Original file line numberDiff line numberDiff line change
@@ -22,43 +22,6 @@ describe('MessageList', () => {
2222
})
2323
})
2424

25-
describe('MessageList as global component', () => {
26-
// two different components, each gets "numbers" list
27-
// into its property "messages"
28-
const template = `
29-
<div>
30-
<message-list :messages="numbers"/>
31-
<a-list :messages="numbers"/>
32-
</div>
33-
`
34-
// our top level data
35-
const data = () => ({ numbers: ['uno', 'dos'] })
36-
// register same component globally under different names
37-
const components = {
38-
'message-list': MessageList,
39-
'a-list': MessageList
40-
}
41-
// extend Vue with global components
42-
const extensions = {
43-
components
44-
}
45-
beforeEach(mountVue({ template, data }, { extensions }))
46-
47-
it('registers global component', () => {
48-
cy
49-
.window()
50-
.its('Vue')
51-
.invoke('component', 'message-list')
52-
// returns component constructor
53-
// so we can compare with our component's constructor (Ctor)
54-
.should('equal', MessageList._Ctor[0])
55-
})
56-
57-
it('shows two items at the start in both lists', () => {
58-
getItems().should('have.length', 4)
59-
})
60-
})
61-
6225
describe('MessageList with props', () => {
6326
const template = `
6427
<div>

cypress/integration/mixin-spec.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const mountVue = require('../..')
2+
3+
/* eslint-env mocha */
4+
describe('Global mixin', () => {
5+
const MyMixin = {
6+
// we have to use original Sinon to create a spy
7+
// because we are outside a test function
8+
// and cannot use "cy.spy"
9+
created: Cypress.sinon.spy()
10+
}
11+
const mixin = [MyMixin]
12+
// extend Vue with mixins
13+
const extensions = {
14+
mixin
15+
}
16+
beforeEach(mountVue({}, { extensions }))
17+
18+
it('calls mixin "created" method', () => {
19+
expect(MyMixin.created).to.have.been.calledOnce
20+
})
21+
})

src/index.js

+15-1
Original file line numberDiff line numberDiff line change
@@ -100,14 +100,27 @@ const registerGlobalComponents = (Vue, options) => {
100100
}
101101

102102
const installPlugins = (Vue, options) => {
103-
const plugins = Cypress._.get(options, 'extensions.use')
103+
const plugins =
104+
Cypress._.get(options, 'extensions.use') ||
105+
Cypress._.get(options, 'extensions.plugins')
104106
if (Cypress._.isArray(plugins)) {
105107
plugins.forEach(plugin => {
106108
Vue.use(plugin)
107109
})
108110
}
109111
}
110112

113+
const installMixins = (Vue, options) => {
114+
const mixins =
115+
Cypress._.get(options, 'extensions.mixin') ||
116+
Cypress._.get(options, 'extensions.mixins')
117+
if (Cypress._.isArray(mixins)) {
118+
mixins.forEach(mixin => {
119+
Vue.mixin(mixin)
120+
})
121+
}
122+
}
123+
111124
const mountVue = (component, options = {}) => () => {
112125
const vueHtml = getPageHTML(options)
113126
const document = cy.state('document')
@@ -120,6 +133,7 @@ const mountVue = (component, options = {}) => () => {
120133
.window({ log: false })
121134
.its('Vue')
122135
.then(Vue => {
136+
installMixins(Vue, options)
123137
installPlugins(Vue, options)
124138
registerGlobalComponents(Vue, options)
125139
deleteCachedConstructors(component)

0 commit comments

Comments
 (0)