Skip to content

Commit 9438bab

Browse files
committed
test: also run unit tests under SSR environment
1 parent 852ac43 commit 9438bab

File tree

3 files changed

+85
-75
lines changed

3 files changed

+85
-75
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@
1919
"build:main": "node build/build.main.js",
2020
"build:logger": "rollup -c build/rollup.logger.config.js",
2121
"lint": "eslint src test",
22-
"test": "npm run lint && npm run test:types && npm run test:unit && npm run test:e2e",
22+
"test": "npm run lint && npm run test:types && npm run test:unit && npm run test:ssr && npm run test:e2e",
2323
"test:unit": "rollup -c build/rollup.dev.config.js && jasmine JASMINE_CONFIG_PATH=test/unit/jasmine.json",
2424
"test:e2e": "node test/e2e/runner.js",
25+
"test:ssr": "rollup -c build/rollup.dev.config.js && VUE_ENV=server jasmine JASMINE_CONFIG_PATH=test/unit/jasmine.json",
2526
"test:types": "tsc -p types/test",
2627
"release": "bash build/release.sh",
2728
"docs": "cd docs && gitbook serve",

test/unit/hot-reload.spec.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import Vue from 'vue/dist/vue.common.js'
22
import Vuex from '../../dist/vuex.common.js'
33

44
const TEST = 'TEST'
5+
const isSSR = process.env.VUE_ENV === 'server'
56

67
describe('Hot Reload', () => {
78
it('mutations', function () {
@@ -278,10 +279,14 @@ describe('Hot Reload', () => {
278279
expect(vm.a).toBe(10)
279280
store.dispatch('check', 10)
280281

281-
Vue.nextTick(() => {
282-
expect(spy).toHaveBeenCalled()
282+
if (isSSR) {
283283
done()
284-
})
284+
} else {
285+
Vue.nextTick(() => {
286+
expect(spy).toHaveBeenCalled()
287+
done()
288+
})
289+
}
285290
})
286291

287292
it('provide warning if a new module is given', () => {

test/unit/store.spec.js

Lines changed: 75 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import Vue from 'vue/dist/vue.common.js'
22
import Vuex from '../../dist/vuex.common.js'
33

44
const TEST = 'TEST'
5+
const isSSR = process.env.VUE_ENV === 'server'
56

67
describe('Store', () => {
78
it('committing mutations', () => {
@@ -263,77 +264,6 @@ describe('Store', () => {
263264
)
264265
})
265266

266-
it('strict mode: warn mutations outside of handlers', () => {
267-
const store = new Vuex.Store({
268-
state: {
269-
a: 1
270-
},
271-
strict: true
272-
})
273-
Vue.config.silent = true
274-
expect(() => { store.state.a++ }).toThrow()
275-
Vue.config.silent = false
276-
})
277-
278-
it('watch: with resetting vm', done => {
279-
const store = new Vuex.Store({
280-
state: {
281-
count: 0
282-
},
283-
mutations: {
284-
[TEST]: state => state.count++
285-
}
286-
})
287-
288-
const spy = jasmine.createSpy()
289-
store.watch(state => state.count, spy)
290-
291-
// reset store vm
292-
store.registerModule('test', {})
293-
294-
Vue.nextTick(() => {
295-
store.commit(TEST)
296-
expect(store.state.count).toBe(1)
297-
298-
Vue.nextTick(() => {
299-
expect(spy).toHaveBeenCalled()
300-
done()
301-
})
302-
})
303-
})
304-
305-
it('watch: getter function has access to store\'s getters object', done => {
306-
const store = new Vuex.Store({
307-
state: {
308-
count: 0
309-
},
310-
mutations: {
311-
[TEST]: state => state.count++
312-
},
313-
getters: {
314-
getCount: state => state.count
315-
}
316-
})
317-
318-
const getter = function getter (state, getters) {
319-
return state.count
320-
}
321-
const spy = spyOn({ getter }, 'getter').and.callThrough()
322-
const spyCb = jasmine.createSpy()
323-
324-
store.watch(spy, spyCb)
325-
326-
Vue.nextTick(() => {
327-
store.commit(TEST)
328-
expect(store.state.count).toBe(1)
329-
330-
Vue.nextTick(() => {
331-
expect(spy).toHaveBeenCalledWith(store.state, store.getters)
332-
done()
333-
})
334-
})
335-
})
336-
337267
it('asserts the call with the new operator', () => {
338268
expect(() => {
339269
Vuex.Store({})
@@ -355,4 +285,78 @@ describe('Store', () => {
355285
store.commit(TEST, 2)
356286
expect(store.state.a).toBe(3)
357287
})
288+
289+
// store.watch should only be asserted in non-SSR environment
290+
if (!isSSR) {
291+
it('strict mode: warn mutations outside of handlers', () => {
292+
const store = new Vuex.Store({
293+
state: {
294+
a: 1
295+
},
296+
strict: true
297+
})
298+
Vue.config.silent = true
299+
expect(() => { store.state.a++ }).toThrow()
300+
Vue.config.silent = false
301+
})
302+
303+
it('watch: with resetting vm', done => {
304+
const store = new Vuex.Store({
305+
state: {
306+
count: 0
307+
},
308+
mutations: {
309+
[TEST]: state => state.count++
310+
}
311+
})
312+
313+
const spy = jasmine.createSpy()
314+
store.watch(state => state.count, spy)
315+
316+
// reset store vm
317+
store.registerModule('test', {})
318+
319+
Vue.nextTick(() => {
320+
store.commit(TEST)
321+
expect(store.state.count).toBe(1)
322+
323+
Vue.nextTick(() => {
324+
expect(spy).toHaveBeenCalled()
325+
done()
326+
})
327+
})
328+
})
329+
330+
it('watch: getter function has access to store\'s getters object', done => {
331+
const store = new Vuex.Store({
332+
state: {
333+
count: 0
334+
},
335+
mutations: {
336+
[TEST]: state => state.count++
337+
},
338+
getters: {
339+
getCount: state => state.count
340+
}
341+
})
342+
343+
const getter = function getter (state, getters) {
344+
return state.count
345+
}
346+
const spy = spyOn({ getter }, 'getter').and.callThrough()
347+
const spyCb = jasmine.createSpy()
348+
349+
store.watch(spy, spyCb)
350+
351+
Vue.nextTick(() => {
352+
store.commit(TEST)
353+
expect(store.state.count).toBe(1)
354+
355+
Vue.nextTick(() => {
356+
expect(spy).toHaveBeenCalledWith(store.state, store.getters)
357+
done()
358+
})
359+
})
360+
})
361+
}
358362
})

0 commit comments

Comments
 (0)