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

Commit 7e0d5a7

Browse files
committed
working on tests from Vue jest tutorial
1 parent d088c40 commit 7e0d5a7

File tree

3 files changed

+111
-4
lines changed

3 files changed

+111
-4
lines changed

components/Message.vue

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<template>
2+
<li
3+
style="margin-top: 10px"
4+
class="message"
5+
@click="handleClick">
6+
{{message}}
7+
</li>
8+
</template>
9+
10+
<script>
11+
export default {
12+
name: 'Message',
13+
props: {
14+
message: {
15+
type: String,
16+
required: true,
17+
validator: value => value.length > 1
18+
},
19+
message2: String,
20+
author: {
21+
type: String,
22+
default: 'Paco'
23+
}
24+
},
25+
methods: {
26+
handleClick() {
27+
console.log('lalala')
28+
this.$emit('message-clicked', this.message)
29+
}
30+
}
31+
}
32+
</script>

cypress/integration/message-spec.js

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import Message from '../../components/Message.vue'
2+
const mountVue = require('../..')
3+
4+
// test example from
5+
// https://github.com/alexjoverm/vue-testing-series/blob/lesson-1/test/Message.test.js
6+
7+
const createCmp = propsData => mountVue(Message, { propsData })()
8+
9+
/* eslint-env mocha */
10+
describe('Message', () => {
11+
describe('properties', () => {
12+
it('has a message property', () => {
13+
createCmp({ message: 'hey' })
14+
cy.wrap(Cypress).its('vue.message').should('equal', 'hey')
15+
})
16+
17+
it('has no cat property', () => {
18+
createCmp({ cat: 'hey', message: 'hey' })
19+
cy.wrap(Cypress).its('vue').should('not.have.property', 'cat')
20+
})
21+
22+
it('Paco is the default author', () => {
23+
createCmp({ message: 'hey' })
24+
cy.wrap(Cypress).its('vue.author').should('equal', 'Paco')
25+
})
26+
27+
describe('Validation', () => {
28+
let message
29+
30+
beforeEach(() => {
31+
createCmp().then(() => {
32+
message = Cypress.vue.$options.props.message
33+
})
34+
})
35+
36+
it('message is of type string', () => {
37+
expect(message.type).to.equal(String)
38+
})
39+
40+
it('message is required', () => {
41+
expect(message.required).to.be.true
42+
})
43+
44+
it('message has at least length 2', () => {
45+
expect(message.validator && message.validator('a')).to.be.falsy
46+
expect(message.validator && message.validator('aa')).to.be.truthy
47+
})
48+
})
49+
})
50+
})

src/index.js

+29-4
Original file line numberDiff line numberDiff line change
@@ -121,24 +121,49 @@ const installMixins = (Vue, options) => {
121121
}
122122
}
123123

124-
const mountVue = (component, options = {}) => () => {
124+
const isOptionName = name =>
125+
['vue', 'html', 'vuePath', 'base', 'extensions'].includes(name)
126+
127+
const isOptions = object => Object.keys(object).every(isOptionName)
128+
129+
const isConstructor = object => object && object._compiled
130+
131+
// the double function allows mounting a component quickly
132+
// beforeEach(mountVue(component, options))
133+
const mountVue = (component, optionsOrProps = {}) => () => {
134+
let options = {}
135+
let props = {}
136+
137+
if (isOptions(optionsOrProps)) {
138+
options = optionsOrProps
139+
} else {
140+
props = optionsOrProps
141+
}
142+
125143
const vueHtml = getPageHTML(options)
126144
const document = cy.state('document')
127145
document.write(vueHtml)
128146
document.close()
129147

130148
// TODO: do not log out "its(Vue)" command
131149
// but it currently does not support it
132-
cy
150+
return cy
133151
.window({ log: false })
134152
.its('Vue')
135153
.then(Vue => {
136154
installMixins(Vue, options)
137155
installPlugins(Vue, options)
138156
registerGlobalComponents(Vue, options)
139157
deleteCachedConstructors(component)
140-
Cypress.vue = new Vue(component).$mount('#app')
141-
copyStyles(component)
158+
159+
if (isConstructor(component)) {
160+
const Cmp = Vue.extend(component)
161+
Cypress.vue = new Cmp(props).$mount('#app')
162+
copyStyles(Cmp)
163+
} else {
164+
Cypress.vue = new Vue(component).$mount('#app')
165+
copyStyles(component)
166+
}
142167
})
143168
}
144169

0 commit comments

Comments
 (0)