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

Commit abb95e0

Browse files
Use Vue Test Utils to manage options like scoped slots and option normalizaiton (#365)
* Use Vue Test Utils to manage options like scoped slots and option normalization * using createLocalVue to ensure global Vue namespace doesn't bleed over * fix: spec was demonstrating using data as an object, which is a bug in components. It was being covered up by rendering the component as the Root Vue Component * chore: cleaning up spec * fix: data should be a function, i18n plugin was throwing error * fix: plugins not passing options through. i18n spec artifically slow * Update src/index.ts * fix: ...localVue => localVue * update mixins test * enable slot AppInput spec * add full slots and scopedSlots test * Update src/index.ts Co-authored-by: Gleb Bahmutov <gleb.bahmutov@gmail.com>
1 parent bf0ace3 commit abb95e0

File tree

19 files changed

+321
-173
lines changed

19 files changed

+321
-173
lines changed

README.md

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,8 @@ The code is pretty simple
114114
```js
115115
var app = new Vue({
116116
el: '#app',
117-
data: {
118-
message: 'Hello Vue!',
117+
data() {
118+
return { message: 'Hello Vue!' }
119119
},
120120
})
121121
```
@@ -210,12 +210,14 @@ describe('Declarative rendering', () => {
210210
</ol>
211211
`
212212

213-
const data = {
214-
todos: [
215-
{ text: 'Learn JavaScript' },
216-
{ text: 'Learn Vue' },
217-
{ text: 'Build something awesome' },
218-
],
213+
function data() {
214+
return {
215+
todos: [
216+
{ text: 'Learn JavaScript' },
217+
{ text: 'Learn Vue' },
218+
{ text: 'Build something awesome' },
219+
],
220+
}
219221
}
220222

221223
beforeEach(mountCallback({ template, data }))
@@ -274,8 +276,8 @@ describe('Handling User Input', () => {
274276
</div>
275277
`
276278

277-
const data = {
278-
message: 'Hello Vue.js!',
279+
function data() {
280+
return { message: 'Hello Vue.js!' }
279281
}
280282

281283
const methods = {
@@ -516,6 +518,7 @@ Spec | Description
516518
[Mixins](cypress/component/basic/mixins) | Registering Vue mixins
517519
[Plugins](cypress/component/basic/plugins) | Loading additional plugins
518520
[Props](cypress/component/basic/props) | Pass props to the component during mount
521+
[Slots](cypress/component/basic/slots) | Passing slots and scopedSlots to the component
519522
[Small examples](cypress/component/basic/small-examples) | A few small examples testing forms, buttons
520523
<!-- prettier-ignore-end -->
521524

@@ -552,13 +555,6 @@ Repo | Description
552555

553556
## Known problems
554557

555-
<details id="slots">
556-
<summary>Slots not supported</summary>
557-
558-
See issue [#364](https://github.com/bahmutov/cypress-vue-unit-test/issues/364)
559-
560-
</details>
561-
562558
<a name="bundling"/>
563559

564560
## Bundling

cypress/component/advanced/i18n/TranslatedMessage.vue

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
<template>
2121
<div id="app">
2222
<label for="locale">locale</label>
23-
<select v-model="locale">
23+
<select v-model="locale" id="locale">
2424
<option>en</option>
2525
<option>fa</option>
2626
<option>ja</option>
@@ -32,8 +32,10 @@
3232

3333
<script>
3434
export default {
35-
name: 'app',
36-
data () { return { locale: 'en' } },
35+
name: 'TranslatedMessage',
36+
data () {
37+
return { locale: 'en' }
38+
},
3739
watch: {
3840
locale (val) {
3941
this.$i18n.locale = val

cypress/component/advanced/i18n/spec.js

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,29 @@
11
/// <reference types="cypress" />
2-
2+
import Vue from 'vue'
33
import TranslatedMessage from './TranslatedMessage.vue'
44
import VueI18n from 'vue-i18n'
55
import { mountCallback } from 'cypress-vue-unit-test'
66

7-
describe('VueI18n', () => {
8-
// need to use VueI18n as a plugin
9-
const extensions = {
10-
plugins: [VueI18n],
11-
components: {
12-
TranslatedMessage,
13-
},
14-
}
15-
16-
const template = '<translated-message />'
7+
Vue.use(VueI18n)
8+
const i18n = new VueI18n({ locale: 'en' })
179

18-
beforeEach(mountCallback({ template }, { extensions }))
10+
describe('VueI18n', () => {
11+
beforeEach(mountCallback(TranslatedMessage, { i18n }))
1912

2013
it('shows English, Japanese and Russian greeting', () => {
2114
cy.viewport(400, 200)
2215

2316
cy.get('select').select('en').should('have.value', 'en')
24-
// wait for good demo movie
25-
cy.contains('message: hello').wait(1000)
17+
cy.contains('message: hello')
2618

2719
cy.get('select').select('fa').should('have.value', 'fa')
28-
cy.contains('message: سلام دنیا').wait(1000)
20+
cy.contains('message: سلام دنیا')
2921

3022
cy.get('select').select('ja').should('have.value', 'ja')
31-
cy.contains('message: こんにちは、世界').wait(1000)
23+
cy.contains('message: こんにちは、世界')
3224

3325
cy.get('select').select('ru').should('have.value', 'ru')
34-
cy.contains('message: Привет мир').wait(1000)
26+
cy.contains('message: Привет мир')
3527
})
3628

3729
// TODO how to load messages not from i18n block but from external JSON file?

cypress/component/basic/components/spec.js

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,6 @@ describe('Global components', () => {
2727
}
2828
beforeEach(mountCallback({ template, data }, { extensions }))
2929

30-
it('registers global component', () => {
31-
cy.window()
32-
.its('Vue')
33-
.invoke('component', 'message-list')
34-
// returns component constructor
35-
// so we can compare with our component's constructor (Ctor)
36-
.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])
44-
})
45-
4630
it('shows two items at the start in both lists', () => {
4731
getItems().should('have.length', 4)
4832
})

cypress/component/basic/hello/Hello.vue

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ export default {
1616
1717
computed: {
1818
error() {
19-
debugger;
2019
console.log(this.username);
2120
return this.username.trim().length < 7
2221
? "Please enter a longer username"

cypress/component/basic/list-spec.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@ describe('Declarative rendering', () => {
1111
</ol>
1212
`
1313

14-
const data = {
15-
todos: [
16-
{ text: 'Learn JavaScript' },
17-
{ text: 'Learn Vue' },
18-
{ text: 'Build something awesome' },
19-
],
14+
function data() {
15+
return {
16+
todos: [
17+
{ text: 'Learn JavaScript' },
18+
{ text: 'Learn Vue' },
19+
{ text: 'Build something awesome' },
20+
],
21+
}
2022
}
2123

2224
beforeEach(mountCallback({ template, data }))

cypress/component/basic/mixins/spec.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
import { mount, mountCallback } from 'cypress-vue-unit-test'
33

44
describe('Mixins', () => {
5+
const template = '<div>mixin test</div>'
6+
57
context('Global mixin', () => {
68
const MyMixin = {
79
// we have to use original Sinon to create a spy
@@ -14,10 +16,13 @@ describe('Mixins', () => {
1416
const extensions = {
1517
mixin,
1618
}
17-
beforeEach(mountCallback({}, { extensions }))
19+
beforeEach(mountCallback({ template }, { extensions }))
1820

1921
it('calls mixin "created" method', () => {
20-
expect(MyMixin.created).to.have.been.calledOnce
22+
// the "created" will be called twice
23+
// 1 - when the test wrapper element made by the Vue test utils is created
24+
// 2 - when the element above we are testing is created
25+
expect(MyMixin.created).to.have.been.calledTwice
2126
})
2227
})
2328

@@ -32,9 +37,12 @@ describe('Mixins', () => {
3237
const extensions = {
3338
mixin,
3439
}
35-
mount({}, { extensions })
40+
mount({ template }, { extensions })
3641
// use the alias to retrieve the stub to check
37-
cy.get('@created').should('have.been.calledOnce')
42+
// the "created" will be called twice
43+
// 1 - when the test wrapper element made by the Vue test utils is created
44+
// 2 - when the element above we are testing is created
45+
cy.get('@created').should('have.been.calledTwice')
3846
})
3947
})
4048
})

cypress/component/basic/options-spec.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,17 @@ const template = `
66
</div>
77
`
88

9-
const data = {
10-
message: 'Hello Vue!',
11-
}
12-
139
describe('Mount component', () => {
1410
// hmm, there are no more options to pass
1511

16-
const component = { template, data }
12+
const component = {
13+
template,
14+
data() {
15+
return {
16+
message: 'Hello Vue!',
17+
}
18+
},
19+
}
1720
beforeEach(mountCallback(component))
1821

1922
it('shows hello', () => {

cypress/component/basic/plugins/plugin-spec.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { MyPlugin } from './MyPlugin'
33
import { MyPluginWithOptions } from './MyPluginWithOptions'
44
import { mount, mountCallback } from 'cypress-vue-unit-test'
55

6+
const EmptyComponent = { template: '<div></div>' }
67
describe('Single component mount', () => {
78
it('has the plugin', () => {
89
const use = [MyPlugin]
@@ -12,7 +13,7 @@ describe('Single component mount', () => {
1213
use,
1314
}
1415

15-
mount({}, { extensions })
16+
mount(EmptyComponent, { extensions })
1617

1718
cy.window().its('Vue').invoke('aPluginMethod').should('equal', 'foo')
1819
})
@@ -26,7 +27,7 @@ describe('Custom plugin MyPlugin', () => {
2627
use,
2728
}
2829
// use "mountCallback" to register the plugins
29-
beforeEach(mountCallback({}, { extensions }))
30+
beforeEach(mountCallback(EmptyComponent, { extensions }))
3031

3132
it('registers global method on Vue instance', () => {
3233
cy.window().its('Vue').its('aPluginMethod').should('be.a', 'function')
@@ -49,7 +50,7 @@ describe('Plugins with options', () => {
4950
use,
5051
}
5152

52-
mount({}, { extensions })
53+
mount(EmptyComponent, { extensions })
5354

5455
// first plugin works
5556
cy.window().its('Vue').invoke('aPluginMethod').should('equal', 'foo')

cypress/component/basic/reverse-spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ describe('Handling User Input', () => {
1010
</div>
1111
`
1212

13-
const data = {
14-
message: 'Hello Vue.js!',
13+
function data() {
14+
return { message: 'Hello Vue.js!' }
1515
}
1616

1717
const methods = {

0 commit comments

Comments
 (0)