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

Commit 87ea052

Browse files
committed
fix: data should be a function, i18n plugin was throwing error
1 parent 8cb069d commit 87ea052

File tree

8 files changed

+69
-84
lines changed

8 files changed

+69
-84
lines changed

README.md

Lines changed: 12 additions & 10 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 = {

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: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,15 @@
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+
// const i18n = new VueI18n({ locale: 'en' })
12+
beforeEach(mountCallback(TranslatedMessage, { i18n }))
1913

2014
it('shows English, Japanese and Russian greeting', () => {
2115
cy.viewport(400, 200)

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/options-spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ describe('Mount component', () => {
1313
template,
1414
data() {
1515
return {
16-
message: 'Hello Vue!'
16+
message: 'Hello Vue!',
1717
}
1818
},
1919
}

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 = {

cypress/component/basic/spec.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,14 @@ describe('Declarative rendering', () => {
99
</div>
1010
`
1111

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

1821
it('shows hello', () => {
1922
cy.contains('Hello Vue!')

src/index.ts

Lines changed: 27 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
VueTestUtilsConfigOptions,
66
Wrapper
77
} from '@vue/test-utils'
8+
import VueI18n from "vue-i18n";
89
const { stripIndent } = require('common-tags')
910

1011
// mountVue options
@@ -45,21 +46,14 @@ const installFilters = (Vue, options) => {
4546
}
4647
}
4748

48-
const installPlugins = (Vue, options) => {
49+
const installPlugins = (Vue, options, props) => {
4950
const plugins: VuePlugins =
50-
Cypress._.get(options, 'extensions.use') ||
51-
Cypress._.get(options, 'extensions.plugins')
52-
53-
if (Cypress._.isArray(plugins)) {
54-
plugins.forEach((plugin) => {
55-
if (Array.isArray(plugin)) {
56-
const [aPlugin, options] = plugin
57-
Vue.use(aPlugin, options)
58-
} else {
59-
Vue.use(plugin)
60-
}
61-
})
62-
}
51+
Cypress._.get(props, 'plugins') ||
52+
Cypress._.get(options, 'extensions.use') ||
53+
Cypress._.get(options, 'extensions.plugins') ||
54+
[]
55+
56+
plugins.forEach(Vue.use)
6357
}
6458

6559
const installMixins = (Vue, options) => {
@@ -132,15 +126,12 @@ type VueFilters = {
132126
type VueMixin = unknown
133127
type VueMixins = VueMixin | VueMixin[]
134128

135-
/**
136-
* A Vue plugin to register,
137-
* or a plugin + its options pair inside an array
138-
*/
139-
type VuePlugin = unknown | [unknown, unknown]
129+
type VuePluginOptions = unknown
130+
type VuePlugin = unknown | [unknown, VuePluginOptions]
140131
/**
141132
* A single Vue plugin or a list of plugins to register
142133
*/
143-
type VuePlugins = VuePlugin | VuePlugin[]
134+
type VuePlugins = VuePlugin[]
144135

145136
/**
146137
* Additional Vue services to register while mounting the component, like
@@ -352,29 +343,6 @@ export const mount = (
352343
Please remove it from your 'mountVue' options.`)
353344
}
354345

355-
// set global Vue instance:
356-
// 1. convenience for debugging in DevTools
357-
// 2. some libraries might check for this global
358-
// appIframe.contentWindow.Vue = Vue
359-
360-
// refresh inner Vue instance of Vuex store
361-
// @ts-ignore
362-
if (hasStore(component)) {
363-
// @ts-ignore
364-
component.store = resetStoreVM(Vue, component)
365-
}
366-
367-
// render function components should be market to be properly initialized
368-
// https://github.com/bahmutov/cypress-vue-unit-test/issues/313
369-
if (
370-
Cypress._.isPlainObject(component) &&
371-
// @ts-ignore
372-
Cypress._.isFunction(component.render)
373-
) {
374-
// @ts-ignore
375-
component._compiled = true
376-
}
377-
378346
return cy
379347
.window({
380348
log: false,
@@ -385,6 +353,19 @@ export const mount = (
385353
win.Vue = localVue
386354
localVue.config.errorHandler = failTestOnVueError
387355

356+
357+
// set global Vue instance:
358+
// 1. convenience for debugging in DevTools
359+
// 2. some libraries might check for this global
360+
// appIframe.contentWindow.Vue = localVue
361+
362+
// refresh inner Vue instance of Vuex store
363+
// @ts-ignore
364+
if (hasStore(component)) {
365+
// @ts-ignore
366+
component.store = resetStoreVM(localVue, component)
367+
}
368+
388369
// @ts-ignore
389370
const document: Document = cy.state('document')
390371
document.body.innerHTML = ''
@@ -423,11 +404,12 @@ export const mount = (
423404
// setup Vue instance
424405
installFilters(localVue, options)
425406
installMixins(localVue, options)
426-
installPlugins(localVue, options)
407+
// @ts-ignore
408+
installPlugins(localVue, options, props)
427409
registerGlobalComponents(localVue, options)
428410

429411
// @ts-ignore
430-
props.attachTo = el
412+
props.attachTo = componentNode
431413

432414
const wrapper = localVue.extend(component as any)
433415

0 commit comments

Comments
 (0)