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

Commit faa895d

Browse files
committed
feat: extend Vue with global components for #5
1 parent f8a807c commit faa895d

File tree

3 files changed

+78
-0
lines changed

3 files changed

+78
-0
lines changed

README.md

+31
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,37 @@ const options = {
7878
beforeEach(mountVue(/* my Vue code */, options))
7979
```
8080

81+
### Global Vue extensions
82+
83+
You can pass extensions (global components, mixins, modules to use)
84+
when mounting Vue component. Use `{ extensions: { ... }}` object inside
85+
the `options`.
86+
87+
* `components` - object of 'id' and components to register globally.
88+
89+
```js
90+
// two different components, each gets "numbers" list
91+
// into its property "messages"
92+
const template = `
93+
<div>
94+
<message-list :messages="numbers"/>
95+
<a-list :messages="numbers"/>
96+
</div>
97+
`
98+
// our top level data
99+
const data = () => ({ numbers: ['uno', 'dos'] })
100+
// register same component globally under different names
101+
const components = {
102+
'message-list': MessageList,
103+
'a-list': MessageList
104+
}
105+
// extend Vue with global components
106+
const extensions = {
107+
components
108+
}
109+
beforeEach(mountVue({ template, data }, { extensions }))
110+
```
111+
81112
### The intro example
82113

83114
Take a look at the first Vue v2 example:

cypress/integration/message-list-spec.js

+37
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,43 @@ 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+
2562
describe('MessageList with props', () => {
2663
const template = `
2764
<div>

src/index.js

+10
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,15 @@ const getPageHTML = options => {
9090
return vueHtml
9191
}
9292

93+
const registerGlobalComponents = (Vue, options) => {
94+
const globalComponents = Cypress._.get(options, 'extensions.components')
95+
if (Cypress._.isPlainObject(globalComponents)) {
96+
Cypress._.forEach(globalComponents, (component, id) => {
97+
Vue.component(id, component)
98+
})
99+
}
100+
}
101+
93102
const mountVue = (component, options = {}) => () => {
94103
const vueHtml = getPageHTML(options)
95104
const document = cy.state('document')
@@ -102,6 +111,7 @@ const mountVue = (component, options = {}) => () => {
102111
.window({ log: false })
103112
.its('Vue')
104113
.then(Vue => {
114+
registerGlobalComponents(Vue, options)
105115
deleteCachedConstructors(component)
106116
Cypress.vue = new Vue(component).$mount('#app')
107117
copyStyles(component)

0 commit comments

Comments
 (0)