Skip to content

Commit

Permalink
test: also run unit tests under SSR environment
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Sep 1, 2017
1 parent 852ac43 commit 9438bab
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 75 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@
"build:main": "node build/build.main.js",
"build:logger": "rollup -c build/rollup.logger.config.js",
"lint": "eslint src test",
"test": "npm run lint && npm run test:types && npm run test:unit && npm run test:e2e",
"test": "npm run lint && npm run test:types && npm run test:unit && npm run test:ssr && npm run test:e2e",
"test:unit": "rollup -c build/rollup.dev.config.js && jasmine JASMINE_CONFIG_PATH=test/unit/jasmine.json",
"test:e2e": "node test/e2e/runner.js",
"test:ssr": "rollup -c build/rollup.dev.config.js && VUE_ENV=server jasmine JASMINE_CONFIG_PATH=test/unit/jasmine.json",
"test:types": "tsc -p types/test",
"release": "bash build/release.sh",
"docs": "cd docs && gitbook serve",
Expand Down
11 changes: 8 additions & 3 deletions test/unit/hot-reload.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Vue from 'vue/dist/vue.common.js'
import Vuex from '../../dist/vuex.common.js'

const TEST = 'TEST'
const isSSR = process.env.VUE_ENV === 'server'

describe('Hot Reload', () => {
it('mutations', function () {
Expand Down Expand Up @@ -278,10 +279,14 @@ describe('Hot Reload', () => {
expect(vm.a).toBe(10)
store.dispatch('check', 10)

Vue.nextTick(() => {
expect(spy).toHaveBeenCalled()
if (isSSR) {
done()
})
} else {
Vue.nextTick(() => {
expect(spy).toHaveBeenCalled()
done()
})
}
})

it('provide warning if a new module is given', () => {
Expand Down
146 changes: 75 additions & 71 deletions test/unit/store.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Vue from 'vue/dist/vue.common.js'
import Vuex from '../../dist/vuex.common.js'

const TEST = 'TEST'
const isSSR = process.env.VUE_ENV === 'server'

describe('Store', () => {
it('committing mutations', () => {
Expand Down Expand Up @@ -263,77 +264,6 @@ describe('Store', () => {
)
})

it('strict mode: warn mutations outside of handlers', () => {
const store = new Vuex.Store({
state: {
a: 1
},
strict: true
})
Vue.config.silent = true
expect(() => { store.state.a++ }).toThrow()
Vue.config.silent = false
})

it('watch: with resetting vm', done => {
const store = new Vuex.Store({
state: {
count: 0
},
mutations: {
[TEST]: state => state.count++
}
})

const spy = jasmine.createSpy()
store.watch(state => state.count, spy)

// reset store vm
store.registerModule('test', {})

Vue.nextTick(() => {
store.commit(TEST)
expect(store.state.count).toBe(1)

Vue.nextTick(() => {
expect(spy).toHaveBeenCalled()
done()
})
})
})

it('watch: getter function has access to store\'s getters object', done => {
const store = new Vuex.Store({
state: {
count: 0
},
mutations: {
[TEST]: state => state.count++
},
getters: {
getCount: state => state.count
}
})

const getter = function getter (state, getters) {
return state.count
}
const spy = spyOn({ getter }, 'getter').and.callThrough()
const spyCb = jasmine.createSpy()

store.watch(spy, spyCb)

Vue.nextTick(() => {
store.commit(TEST)
expect(store.state.count).toBe(1)

Vue.nextTick(() => {
expect(spy).toHaveBeenCalledWith(store.state, store.getters)
done()
})
})
})

it('asserts the call with the new operator', () => {
expect(() => {
Vuex.Store({})
Expand All @@ -355,4 +285,78 @@ describe('Store', () => {
store.commit(TEST, 2)
expect(store.state.a).toBe(3)
})

// store.watch should only be asserted in non-SSR environment
if (!isSSR) {
it('strict mode: warn mutations outside of handlers', () => {
const store = new Vuex.Store({
state: {
a: 1
},
strict: true
})
Vue.config.silent = true
expect(() => { store.state.a++ }).toThrow()
Vue.config.silent = false
})

it('watch: with resetting vm', done => {
const store = new Vuex.Store({
state: {
count: 0
},
mutations: {
[TEST]: state => state.count++
}
})

const spy = jasmine.createSpy()
store.watch(state => state.count, spy)

// reset store vm
store.registerModule('test', {})

Vue.nextTick(() => {
store.commit(TEST)
expect(store.state.count).toBe(1)

Vue.nextTick(() => {
expect(spy).toHaveBeenCalled()
done()
})
})
})

it('watch: getter function has access to store\'s getters object', done => {
const store = new Vuex.Store({
state: {
count: 0
},
mutations: {
[TEST]: state => state.count++
},
getters: {
getCount: state => state.count
}
})

const getter = function getter (state, getters) {
return state.count
}
const spy = spyOn({ getter }, 'getter').and.callThrough()
const spyCb = jasmine.createSpy()

store.watch(spy, spyCb)

Vue.nextTick(() => {
store.commit(TEST)
expect(store.state.count).toBe(1)

Vue.nextTick(() => {
expect(spy).toHaveBeenCalledWith(store.state, store.getters)
done()
})
})
})
}
})

0 comments on commit 9438bab

Please sign in to comment.