This repository was archived by the owner on Dec 12, 2020. It is now read-only.
File tree 3 files changed +78
-0
lines changed
3 files changed +78
-0
lines changed Original file line number Diff line number Diff line change @@ -78,6 +78,37 @@ const options = {
78
78
beforeEach (mountVue (/* my Vue code */ , options))
79
79
```
80
80
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
+
81
112
### The intro example
82
113
83
114
Take a look at the first Vue v2 example:
Original file line number Diff line number Diff line change @@ -22,6 +22,43 @@ describe('MessageList', () => {
22
22
} )
23
23
} )
24
24
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
+
25
62
describe ( 'MessageList with props' , ( ) => {
26
63
const template = `
27
64
<div>
Original file line number Diff line number Diff line change @@ -90,6 +90,15 @@ const getPageHTML = options => {
90
90
return vueHtml
91
91
}
92
92
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
+
93
102
const mountVue = ( component , options = { } ) => ( ) => {
94
103
const vueHtml = getPageHTML ( options )
95
104
const document = cy . state ( 'document' )
@@ -102,6 +111,7 @@ const mountVue = (component, options = {}) => () => {
102
111
. window ( { log : false } )
103
112
. its ( 'Vue' )
104
113
. then ( Vue => {
114
+ registerGlobalComponents ( Vue , options )
105
115
deleteCachedConstructors ( component )
106
116
Cypress . vue = new Vue ( component ) . $mount ( '#app' )
107
117
copyStyles ( component )
You can’t perform that action at this time.
0 commit comments