forked from bencodezen/vue-enterprise-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.js
185 lines (163 loc) · 4.63 KB
/
setup.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import Vue from 'vue'
import Vuex from 'vuex'
import fs from 'fs'
import path from 'path'
import axios from 'axios'
// ===
// Utility functions
// ===
// https://vue-test-utils.vuejs.org/en/
import vueTestUtils from '@vue/test-utils'
// https://lodash.com/
import _ from 'lodash'
_.mixin({ pascalCase: _.flow(_.camelCase, _.upperFirst) })
// ===
// Configure Vue
// ===
// Don't warn about not using the production build of Vue, as
// we care more about the quality of errors than performance
// for tests.
Vue.config.productionTip = false
// ===
// Register global components
// ===
const globalComponentFiles = fs
.readdirSync(path.join(__dirname, '../../src/components'))
.filter(fileName => /^_base-.+\.vue$/.test(fileName))
for (const fileName of globalComponentFiles) {
const componentName = _.pascalCase(fileName.match(/^_(base-.+)\.vue$/)[1])
const componentConfig = require('../../src/components/' + fileName)
Vue.component(componentName, componentConfig.default || componentConfig)
}
// ===
// Patch all components with a global mixin
// ===
Vue.mixin({
created() {
// HACK: Set a fallback for the `$style` until vue-jest
// includes better support for CSS modules.
this.$style = this.$style || {}
},
})
// ===
// Mock window properties not handled by jsdom
// ===
Object.defineProperty(window, 'localStorage', {
value: (function() {
let store = {}
return {
getItem: function(key) {
return store[key] || null
},
setItem: function(key, value) {
store[key] = value.toString()
},
clear: function() {
store = {}
},
}
})(),
})
// ===
// Global helpers
// ===
// https://vue-test-utils.vuejs.org/en/api/mount.html
global.mount = vueTestUtils.mount
// Aliasing `shallow` to a more descriptive name
// https://vue-test-utils.vuejs.org/en/api/shallow.html
global.mountShallow = vueTestUtils.shallow
// A special version of `mountShallow` for view components
global.mountShallowView = (Component, options = {}) => {
return global.mountShallow(Component, {
...options,
stubs: {
Layout: {
functional: true,
render(h, { slots }) {
return <div>{slots().default}</div>
},
},
...(options.stubs || {}),
},
})
}
// A helper for creating Vue component mocks
global.createComponentMocks = ({ store, router, style, mocks, stubs }) => {
// Use a local version of Vue, to avoid polluting the global
// Vue and thereby affecting other tests.
// https://vue-test-utils.vuejs.org/en/api/createLocalVue.html
const localVue = vueTestUtils.createLocalVue()
const returnOptions = { localVue }
// https://vue-test-utils.vuejs.org/en/api/options.html#stubs
returnOptions.stubs = stubs || {}
// https://vue-test-utils.vuejs.org/en/api/options.html#mocks
returnOptions.mocks = mocks || {}
// Converts a `store` option shaped like:
//
// store: {
// someModuleName: {
// state: { ... },
// getters: { ... },
// actions: { ... },
// },
// anotherModuleName: {
// getters: { ... },
// },
// },
//
// to a store instance, with each module namespaced by
// default, just like in our app.
if (store) {
localVue.use(Vuex)
returnOptions.store = new Vuex.Store({
modules: Object.keys(store)
.map(moduleName => {
const storeModule = store[moduleName]
return {
[moduleName]: {
state: storeModule.state || {},
getters: storeModule.getters || {},
actions: storeModule.actions || {},
namespaced:
typeof storeModule.namespaced === 'undefined'
? true
: storeModule.namespaced,
},
}
})
.reduce((moduleA, moduleB) => Object.assign({}, moduleA, moduleB), {}),
})
}
// If using `router: true`, we'll automatically stub out
// components from Vue Router.
if (router) {
returnOptions.stubs['router-link'] = true
returnOptions.stubs['router-view'] = true
}
// If a `style` object is provided, mock some styles.
if (style) {
returnOptions.mocks.$style = style
}
return returnOptions
}
global.createModuleStore = (vuexModule, options = {}) => {
vueTestUtils.createLocalVue().use(Vuex)
const store = new Vuex.Store({
..._.cloneDeep(vuexModule),
modules: {
auth: {
namespaced: true,
state: {
currentUser: options.currentUser,
},
},
},
})
axios.defaults.headers.common.Authorization = options.currentUser
? options.currentUser.token
: ''
if (vuexModule.actions.init) {
store.dispatch('init')
}
return store
}