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

Commit d0ccead

Browse files
committed
feat: test declarative rendering
1 parent 228d4ef commit d0ccead

File tree

10 files changed

+186
-4
lines changed

10 files changed

+186
-4
lines changed

README.md

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,76 @@
1313
Requires [Node](https://nodejs.org/en/) version 6 or above.
1414

1515
```sh
16-
npm install --save cypress-vue-unit-test
16+
npm install --save-dev cypress-vue-unit-test
1717
```
1818

1919
## Use
2020

21+
Take a look at the first Vue v2 example:
22+
[Declarative Rendering](https://vuejs.org/v2/guide/#Declarative-Rendering).
23+
The code is pretty simple
24+
25+
```html
26+
<div id="app">
27+
{{ message }}
28+
</div>
29+
```
30+
```js
31+
var app = new Vue({
32+
el: '#app',
33+
data: {
34+
message: 'Hello Vue!'
35+
}
36+
})
37+
```
38+
It shows the message when running in the browser
39+
```
40+
Hello Vue!
41+
```
42+
43+
Let's test it in [Cypress.io][cypress.io] (for the current version see
44+
[cypress/integration/spec.js](cypress/integration/spec.js)).
45+
46+
```js
47+
const mountVue = require('cypress-vue-unit-test')
48+
49+
/* eslint-env mocha */
50+
describe('Declarative rendering', () => {
51+
// Vue code from https://vuejs.org/v2/guide/#Declarative-Rendering
52+
const template = `
53+
<div id="app">
54+
{{ message }}
55+
</div>
56+
`
57+
58+
const data = {
59+
message: 'Hello Vue!'
60+
}
61+
62+
// that's all you need to do
63+
beforeEach(mountVue({ template, data }))
64+
65+
it('shows hello', () => {
66+
cy.contains('Hello Vue!')
67+
})
68+
69+
it('changes message if data changes', () => {
70+
// mounted Vue instance is available under Cypress.vue
71+
Cypress.vue.$data.message = 'Vue rocks!'
72+
cy.contains('Vue rocks!')
73+
})
74+
})
75+
```
76+
77+
Fire up Cypress test runner and have real browser (Electron, Chrome) load
78+
Vue and mount your test code and be able to interact with the instance through
79+
the reference `Cypress.vue.$data` and via GUI. The full power of the
80+
[Cypress API](https://on.cypress.io/api) is available.
81+
82+
![Hello world tested](images/spec.png)
83+
84+
[cypress.io]: https://www.cypress.io/
85+
2186
### Small print
2287

2388
Author: Gleb Bahmutov &lt;[email protected]&gt; &copy; 2017

cypress.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"viewportWidth": 300,
3+
"viewportHeight": 100
4+
}

cypress/fixtures/example.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"name": "Using fixtures to represent data",
3+
"email": "[email protected]",
4+
"body": "Fixtures are a great way to mock data for responses to routes"
5+
}

cypress/integration/spec.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
const mountVue = require('../..')
2+
3+
/* eslint-env mocha */
4+
describe('Declarative rendering', () => {
5+
// Vue code from https://vuejs.org/v2/guide/#Declarative-Rendering
6+
const template = `
7+
<div id="app">
8+
{{ message }}
9+
</div>
10+
`
11+
12+
const data = {
13+
message: 'Hello Vue!'
14+
}
15+
16+
beforeEach(mountVue({ template, data }))
17+
18+
it('shows hello', () => {
19+
cy.contains('Hello Vue!')
20+
})
21+
22+
it('changes message if data changes', () => {
23+
// mounted Vue instance is available under Cypress.vue
24+
Cypress.vue.$data.message = 'Vue rocks!'
25+
cy.contains('Vue rocks!')
26+
})
27+
})

cypress/plugins/index.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// ***********************************************************
2+
// This example plugins/index.js can be used to load plugins
3+
//
4+
// You can change the location of this file or turn off loading
5+
// the plugins file with the 'pluginsFile' configuration option.
6+
//
7+
// You can read more here:
8+
// https://on.cypress.io/plugins-guide
9+
// ***********************************************************
10+
11+
// This function is called when a project is opened or re-opened (e.g. due to
12+
// the project's config changing)
13+
14+
module.exports = (on, config) => {
15+
// `on` is used to hook into various events Cypress emits
16+
// `config` is the resolved Cypress config
17+
}

cypress/support/commands.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// ***********************************************
2+
// This example commands.js shows you how to
3+
// create various custom commands and overwrite
4+
// existing commands.
5+
//
6+
// For more comprehensive examples of custom
7+
// commands please read more here:
8+
// https://on.cypress.io/custom-commands
9+
// ***********************************************
10+
//
11+
//
12+
// -- This is a parent command --
13+
// Cypress.Commands.add("login", (email, password) => { ... })
14+
//
15+
//
16+
// -- This is a child command --
17+
// Cypress.Commands.add("drag", { prevSubject: 'element'}, (subject, options) => { ... })
18+
//
19+
//
20+
// -- This is a dual command --
21+
// Cypress.Commands.add("dismiss", { prevSubject: 'optional'}, (subject, options) => { ... })
22+
//
23+
//
24+
// -- This is will overwrite an existing command --
25+
// Cypress.Commands.overwrite("visit", (originalFn, url, options) => { ... })

cypress/support/index.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// ***********************************************************
2+
// This example support/index.js is processed and
3+
// loaded automatically before your test files.
4+
//
5+
// This is a great place to put global configuration and
6+
// behavior that modifies Cypress.
7+
//
8+
// You can change the location of this file or turn off
9+
// automatically serving support files with the
10+
// 'supportFile' configuration option.
11+
//
12+
// You can read more here:
13+
// https://on.cypress.io/configuration
14+
// ***********************************************************
15+
16+
// Import commands.js using ES2015 syntax:
17+
import './commands'
18+
19+
// Alternatively you can use CommonJS syntax:
20+
// require('./commands')

images/spec.png

194 KB
Loading

package.json

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,12 @@
5858
"pretty": "prettier-standard 'src/*.js'",
5959
"secure": "nsp check",
6060
"size": "t=\"$(npm pack .)\"; wc -c \"${t}\"; tar tvf \"${t}\"; rm \"${t}\";",
61-
"test": "npm run unit",
61+
"test": "npm run cy:run",
6262
"unit": "mocha src/*-spec.js",
6363
"unused-deps": "dependency-check --unused --no-dev .",
64-
"semantic-release": "semantic-action pre && npm publish && semantic-action post"
64+
"semantic-release": "semantic-action pre && npm publish && semantic-action post",
65+
"cy:open": "cypress open",
66+
"cy:run": "cypress run"
6567
},
6668
"release": {
6769
"analyzeCommits": "simple-commit-message"
@@ -78,7 +80,11 @@
7880
"pre-git": "3.16.0",
7981
"prettier-standard": "8.0.0",
8082
"semantic-action": "1.1.0",
83+
"simple-commit-message": "3.3.2",
8184
"standard": "10.0.3",
8285
"vue": "2.5.13"
86+
},
87+
"standard": {
88+
"globals": [ "Cypress", "cy" ]
8389
}
8490
}

src/index.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,21 @@ function deleteCachedConstructors (component) {
4242
Cypress._.values(component.components).forEach(deleteConstructor)
4343
}
4444

45+
const vueHtml = `
46+
<html>
47+
<head></head>
48+
<body>
49+
<script src="../node_modules/vue/dist/vue.js"></script>
50+
<div id="app"></div>
51+
</body>
52+
</html>
53+
`
54+
4555
const mountVue = component => () => {
46-
cy.visit('index.html')
56+
cy.document().then(document => {
57+
document.write(vueHtml)
58+
document.close()
59+
})
4760
cy
4861
.window()
4962
.its('Vue')

0 commit comments

Comments
 (0)