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

Commit aa023eb

Browse files
authored
add more basic examples (#362)
1 parent f67e37a commit aa023eb

File tree

15 files changed

+218
-174
lines changed

15 files changed

+218
-174
lines changed

README.md

Lines changed: 8 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -92,87 +92,10 @@ You can pass extensions (global components, mixins, modules to use)
9292
when mounting Vue component. Use `{ extensions: { ... }}` object inside
9393
the `options`.
9494

95-
- `components` - object of 'id' and components to register globally.
96-
97-
```js
98-
// two different components, each gets "numbers" list
99-
// into its property "messages"
100-
const template = `
101-
<div>
102-
<message-list :messages="numbers"/>
103-
<a-list :messages="numbers"/>
104-
</div>
105-
`
106-
// our top level data
107-
const data = () => ({ numbers: ['uno', 'dos'] })
108-
// register same component globally under different names
109-
const components = {
110-
'message-list': MessageList,
111-
'a-list': MessageList,
112-
}
113-
// extend Vue with global components
114-
const extensions = {
115-
components,
116-
}
117-
beforeEach(mountCallback({ template, data }, { extensions }))
118-
```
119-
120-
See [Vue component docs](https://vuejs.org/v2/api/#Vue-component),
121-
[global-components-spec.js](cypress/integration/global-components-spec.js)
122-
123-
- `use` (alias `plugins`) - list of plugins
124-
125-
```js
126-
const use = [MyPlugin]
127-
// extend Vue with plugins
128-
const extensions = {
129-
use,
130-
}
131-
beforeEach(mountCallback({}, { extensions }))
132-
```
133-
134-
See [Vue plugin docs](https://vuejs.org/v2/guide/plugins.html)
135-
and [plugin-spec.js](cypress/integration/plugin-spec.js)
136-
137-
- `mixin` (alias `mixins`) - list of global mixins
138-
139-
```js
140-
const MyMixin = {
141-
// we have to use original Sinon to create a spy
142-
// because we are outside a test function
143-
// and cannot use "cy.spy"
144-
created: Cypress.sinon.spy(),
145-
}
146-
const mixin = [MyMixin]
147-
// extend Vue with mixins
148-
const extensions = {
149-
mixin,
150-
}
151-
beforeEach(mountCallback({}, { extensions }))
152-
153-
it('calls mixin "created" method', () => {
154-
expect(MyMixin.created).to.have.been.calledOnce
155-
})
156-
```
157-
158-
See [Vue global mixin docs](https://vuejs.org/v2/guide/mixins.html#Global-Mixin)
159-
and [mixin-spec.js](cypress/integration/mixin-spec.js)
160-
161-
- `filters` - hash of global filters
162-
163-
```js
164-
const filters = {
165-
reverse: (s) => s.split().reverse().join(),
166-
}
167-
// extend Vue with global filters
168-
const extensions = {
169-
filters,
170-
}
171-
beforeEach(mountCallback({}, { extensions }))
172-
```
173-
174-
See [Vue global filters docs](https://vuejs.org/v2/api/#Vue-filter)
175-
and [filters-spec.js](cypress/integration/filters-spec.js)
95+
- `components` - object of 'id' and components to register globally, see [Components](cypress/component/basic/components) example
96+
- `use` (alias `plugins`) - list of plugins, see [Plugins](cypress/component/basic/plugins)
97+
- `mixin` (alias `mixins`) - list of global mixins, see [Mixins](cypress/component/basic/mixins) example
98+
- `filters` - hash of global filters, see [Filters](cypress/component/basic/filters) example
17699

177100
<a name="intro-example"/>
178101

@@ -587,8 +510,12 @@ describe('HelloWorld component', () => {
587510
<!-- prettier-ignore-start -->
588511
Spec | Description
589512
--- | ---
513+
[Components](cypress/component/basic/components) | Registers global components to use
514+
[Filters](cypress/component/basic/filters) | Registering global filters
590515
[Hello](cypress/component/basic/hello) | Testing examples from Vue2 cookbook
516+
[Mixins](cypress/component/basic/mixins) | Registering Vue mixins
591517
[Plugins](cypress/component/basic/plugins) | Loading additional plugins
518+
[Props](cypress/component/basic/props) | Pass props to the component during mount
592519
<!-- prettier-ignore-end -->
593520

594521
### Advanced examples

cypress/component/basic/MessageList.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<template>
22
<ul>
3-
<li v-for="message in messages">
3+
<li v-for="(message, index) in messages" :key="index" >
44
{{ message }}
55
</li>
66
</ul>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# global components
2+
3+
During mounting, you can register other components, even fake ones. See [spec.js](spec.js)
4+
5+
```js
6+
import MessageList from '../MessageList.vue'
7+
// two different components, each gets "numbers" list
8+
// into its property "messages"
9+
const template = `
10+
<div>
11+
<message-list :messages="numbers"/>
12+
<a-list :messages="numbers"/>
13+
</div>
14+
`
15+
// our top level data
16+
const data = () => ({ numbers: ['uno', 'dos'] })
17+
// register same component globally under different names
18+
const components = {
19+
'message-list': MessageList,
20+
'a-list': MessageList,
21+
}
22+
// extend Vue with global components
23+
const extensions = {
24+
components,
25+
}
26+
beforeEach(mountCallback({ template, data }, { extensions }))
27+
```
28+
29+
![Components spec](./images/components.png)
30+
31+
See [Vue component docs](https://vuejs.org/v2/api/#Vue-component)
230 KB
Loading

cypress/component/basic/global-components-spec.js renamed to cypress/component/basic/components/spec.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import MessageList from './MessageList.vue'
1+
/// <reference types="cypress" />
2+
import MessageList from '../MessageList.vue'
23
import { mountCallback } from 'cypress-vue-unit-test'
34

45
// common utils for MessageList
56
const getItems = () => cy.get('ul li')
67

7-
/* eslint-env mocha */
88
describe('Global components', () => {
99
// two different components, each gets "numbers" list
1010
// into its property "messages"
@@ -34,6 +34,13 @@ describe('Global components', () => {
3434
// returns component constructor
3535
// so we can compare with our component's constructor (Ctor)
3636
.should('equal', MessageList._Ctor[0])
37+
38+
// second registered component "a-list" also points
39+
// at the same component
40+
cy.window()
41+
.its('Vue')
42+
.invoke('component', 'a-list')
43+
.should('equal', MessageList._Ctor[0])
3744
})
3845

3946
it('shows two items at the start in both lists', () => {
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# filters
2+
3+
You can register global Vue filters when mounting the component. See [spec.js](spec.js)
4+
5+
```js
6+
mount(component, {
7+
extensions: {
8+
filters: { ... }
9+
}
10+
})
11+
```
12+
13+
Read [Vue global filters docs](https://vuejs.org/v2/api/#Vue-filter)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/**
2+
* Reverses a string
3+
* @param {string} s String to reverse
4+
* @returns {string} Reversed string
5+
*/
6+
export const reverse = (s) => s.split('').reverse().join('')

cypress/component/basic/filters-spec.js renamed to cypress/component/basic/filters/spec.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
/// <reference types="cypress" />
12
import { mountCallback } from 'cypress-vue-unit-test'
3+
import { reverse } from './reverse'
24

35
describe('Global filters', () => {
46
const filters = {
5-
reverse: (s) => s.split('').reverse().join(''),
7+
reverse,
68
}
79

810
// use reverse filter in template
@@ -18,11 +20,14 @@ describe('Global filters', () => {
1820

1921
it('registers global filter', () => {
2022
cy.wrap(window.Vue)
21-
// cy.window().its('Vue')
2223
.invoke('filter', 'reverse')
2324
.should('equal', filters.reverse)
2425
})
2526

27+
it('unit tests the reverse function', () => {
28+
expect(reverse('Hello')).to.equal('olleH')
29+
})
30+
2631
it('reverses the string', () => {
2732
cy.contains('rab-oof')
2833
})

cypress/component/basic/message-list-spec.js

Lines changed: 0 additions & 67 deletions
This file was deleted.

cypress/component/basic/mixin-spec.js

Lines changed: 0 additions & 21 deletions
This file was deleted.

0 commit comments

Comments
 (0)