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

Commit 568020e

Browse files
committed
feat: extend Vue with plugins for #5
1 parent faa895d commit 568020e

File tree

4 files changed

+57
-0
lines changed

4 files changed

+57
-0
lines changed

README.md

+17
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,23 @@ const extensions = {
109109
beforeEach(mountVue({ template, data }, { extensions }))
110110
```
111111

112+
See [Vue component docs](https://vuejs.org/v2/api/#Vue-component),
113+
[message-list-spec.js](cypress/integration/message-list-spec.js)
114+
115+
* `use` - list of plugins
116+
117+
```js
118+
const use = [MyPlugin]
119+
// extend Vue with plugins
120+
const extensions = {
121+
use
122+
}
123+
beforeEach(mountVue({}, { extensions }))
124+
```
125+
126+
See [Vue plugin docs](https://vuejs.org/v2/guide/plugins.html)
127+
and [plugin-spec.js](cypress/integrstion/plugin-spec.js)
128+
112129
### The intro example
113130

114131
Take a look at the first Vue v2 example:

components/MyPlugin.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// https://vuejs.org/v2/guide/plugins.html
2+
export const MyPlugin = {
3+
install (Vue, options) {
4+
// 1. add global method or property
5+
Vue.aPluginMethod = function () {
6+
return 'foo'
7+
}
8+
}
9+
}

cypress/integration/plugin-spec.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { MyPlugin } from '../../components/MyPlugin'
2+
const mountVue = require('../..')
3+
4+
/* eslint-env mocha */
5+
describe('Custom plugin MyPlugin', () => {
6+
const use = [MyPlugin]
7+
8+
// extend Vue with plugins
9+
const extensions = {
10+
use
11+
}
12+
beforeEach(mountVue({}, { extensions }))
13+
14+
it('registers global method on Vue instance', () => {
15+
cy.window().its('Vue').its('aPluginMethod').should('be.a', 'function')
16+
})
17+
18+
it('can call this global function', () => {
19+
cy.window().its('Vue').invoke('aPluginMethod').should('equal', 'foo')
20+
})
21+
})

src/index.js

+10
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,15 @@ const registerGlobalComponents = (Vue, options) => {
9999
}
100100
}
101101

102+
const installPlugins = (Vue, options) => {
103+
const plugins = Cypress._.get(options, 'extensions.use')
104+
if (Cypress._.isArray(plugins)) {
105+
plugins.forEach(plugin => {
106+
Vue.use(plugin)
107+
})
108+
}
109+
}
110+
102111
const mountVue = (component, options = {}) => () => {
103112
const vueHtml = getPageHTML(options)
104113
const document = cy.state('document')
@@ -111,6 +120,7 @@ const mountVue = (component, options = {}) => () => {
111120
.window({ log: false })
112121
.its('Vue')
113122
.then(Vue => {
123+
installPlugins(Vue, options)
114124
registerGlobalComponents(Vue, options)
115125
deleteCachedConstructors(component)
116126
Cypress.vue = new Vue(component).$mount('#app')

0 commit comments

Comments
 (0)