Skip to content

Commit

Permalink
feat: 完善单元测试 测试类型 分离
Browse files Browse the repository at this point in the history
  • Loading branch information
imsunhao committed Aug 21, 2019
1 parent 40e85a6 commit 76eb2f9
Showing 1 changed file with 107 additions and 60 deletions.
167 changes: 107 additions & 60 deletions doc/tests/unit/store/base-utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,55 +6,18 @@ import { merge } from 'lodash'
/**
* Test Store
*/
namespace Tstore {
namespace TestStore {
/**
* vuex state
*/
export interface state {
/**
* 是否 使用 移动设备 访问
* * 来自服务器端
* * 这是一个例子
*/
isMobile: boolean

/**
* 是否 使用 移动设备 访问
* * 来自 中间件-服务器端
* * 这是一个例子
*/
hello: string

/**
* 测试 热加载 vuex
* * 来自 客户端
* * 这是一个例子
*/
testHotLoadingVuex: number

/**
* 初始化 跳转访问 URL
* * 来自服务器端
* * 这是一个例子
*/
initialReplaceStateUrl: string

/**
* 编辑器
*/
editor?: {
test: string
deepTest: {
test1: string
test2: number
}
}

/**
* 单元测试使用
*/
test?: {
test: string
testNumber: number
deepTest: {
test1: string
test2: number
Expand All @@ -72,20 +35,26 @@ namespace Tstore {
*/
editor?: {
test: string
deepTest: {
test1: string
test2: number
}
}
}
}
const { makeWrapper } = new VuexStoreHelper<Tstore.state, Tstore.getters>()
const { makeWrapper } = new VuexStoreHelper<TestStore.state, TestStore.getters>()
const DEFAULT_TEST_STRING = 'DEFAULT_TEST_STRING'
const DEFAULT_TEST_NUMBER = 1

describe('Vux base-utils.spec', () => {
const defalutTestString = 'defalutTestString'
const localVue = createLocalVue()

function getTestState(): Tstore.state['test'] {
function getTestState(): TestStore.state['test'] {
return {
test: defalutTestString,
test: DEFAULT_TEST_STRING,
testNumber: 0,
deepTest: {
test1: defalutTestString,
test1: DEFAULT_TEST_STRING,
test2: 2,
},
}
Expand All @@ -107,21 +76,32 @@ describe('Vux base-utils.spec', () => {
}

describe('全局测试', () => {
const globalHelper = makeWrapper<Tstore.state>()
const globalHelper = makeWrapper()
const mutations = globalHelper.makeMutations({
SET_set: (state, test: string) => {
state.test.test = test
},
})
const getters = globalHelper.makeGetters({
globleValue(state, getters, rootState, rootGetters) {
return DEFAULT_TEST_NUMBER
}
})
const getState = globalHelper.createGetState()
const getGetter = globalHelper.createGetGetter()
const commit = globalHelper.createCommit<typeof mutations>()

it('getGetter', () => {
const getGetter = globalHelper.createGetGetter()
const store = getStore<TestStore.state>({ mutations, getters })
const value = getGetter(store, 'globleValue')
expect(value).toEqual(DEFAULT_TEST_NUMBER)
})

it('commit getState', () => {
const store = getStore<Tstore.state>({ mutations })
const store = getStore<TestStore.state>({ mutations })
const testString = 'hi'

expect(store.state.test.test).toEqual(defalutTestString)
expect(store.state.test.test).toEqual(DEFAULT_TEST_STRING)

commit(store, 'SET_set', testString)

Expand All @@ -138,19 +118,22 @@ describe('Vux base-utils.spec', () => {
const dispatch = globalHelper.createDispatch<typeof actions>()

it('dispatch', () => {
const store = getStore<Tstore.state>({ mutations, actions })
const store = getStore<TestStore.state>({ mutations, actions })
const testString = 'hi'

expect(store.state.test.test).toEqual(defalutTestString)
expect(store.state.test.test).toEqual(DEFAULT_TEST_STRING)

dispatch(store, 'action_set', { test: testString })

expect(getState(store, 'test', 'test')).toEqual(testString)
})
})

type VUEX_NS_1 = typeof VUEX_NS_1
const VUEX_NS_1 = 'test'

describe('单模块测试', () => {
const testHelper = makeWrapper<Tstore.state['test']>('test')
const testHelper = makeWrapper<TestStore.state[VUEX_NS_1]>(VUEX_NS_1)
const mutations = testHelper.makeMutations({
SET_set: (state, test: string) => {
state.test = test
Expand All @@ -160,10 +143,14 @@ describe('Vux base-utils.spec', () => {
const commit = testHelper.createCommit<typeof mutations>()

it('commit getState', () => {
const store = getStore<Tstore.state>({ state: {}, modules: { test: { namespaced: true, state: getTestState(), mutations }}})
const store = getStore<TestStore.state>({
state: {},
modules: { test: { namespaced: true, state: getTestState(), mutations } },
})
const testString = 'hi'

expect(store.state.test.test).toEqual(defalutTestString)
expect(store.state.test.test).toEqual(DEFAULT_TEST_STRING)
expect(store.state.test.test).toEqual(DEFAULT_TEST_STRING)

commit(store, 'SET_set', testString)

Expand All @@ -172,6 +159,22 @@ describe('Vux base-utils.spec', () => {
expect(getState(store, 'test')).toEqual(testString)
})

it('getGetter', () => {
const getters = testHelper.makeGetters({
globleValue(state, getters, rootState, rootGetters) {
return DEFAULT_TEST_NUMBER
}
})
const store = getStore<TestStore.state>({
state: {},
modules: { test: { namespaced: true, state: getTestState(), mutations, getters } },
})
const getGetter = testHelper.createGetGetter()
const value = getGetter(store, 'globleValue')
expect(value).toEqual(DEFAULT_TEST_NUMBER)
})


const actions = testHelper.makeActions({
action_set(ctx, { test }: { test: string }) {
commit(ctx, 'SET_set', test)
Expand All @@ -180,19 +183,24 @@ describe('Vux base-utils.spec', () => {
const dispatch = testHelper.createDispatch<typeof actions>()

it('dispatch', () => {
const store = getStore<Tstore.state>({ state: {}, modules: { test: { namespaced: true, state: getTestState(), mutations, actions }}})
const store = getStore<TestStore.state>({
state: {},
modules: { test: { namespaced: true, state: getTestState(), mutations, actions } },
})
const testString = 'hi'

expect(store.state.test.test).toEqual(defalutTestString)
expect(store.state.test.test).toEqual(DEFAULT_TEST_STRING)

dispatch(store, 'action_set', { test: testString })

expect(getState(store, 'test')).toEqual(testString)
})
})

type VUEX_NS_1_1 = typeof VUEX_NS_1_1
const VUEX_NS_1_1 = 'deepTest'
describe('深模块(2层)测试', () => {
const testHelper = makeWrapper<Tstore.state['test']['deepTest']>(['test', 'deepTest'])
const testHelper = makeWrapper<TestStore.state[VUEX_NS_1][VUEX_NS_1_1]>([VUEX_NS_1, VUEX_NS_1_1])
const mutations = testHelper.makeMutations({
SET_set: (state, test: string) => {
state.test1 = test
Expand All @@ -202,10 +210,19 @@ describe('Vux base-utils.spec', () => {
const commit = testHelper.createCommit<typeof mutations>()

it('commit getState', () => {
const store = getStore<Tstore.state>({ state: {}, modules: { test: { namespaced: true, state: {}, modules: { deepTest: { namespaced: true, state: getTestState().deepTest, mutations }}}}})
const store = getStore<TestStore.state>({
state: {},
modules: {
test: {
namespaced: true,
state: {},
modules: { deepTest: { namespaced: true, state: getTestState().deepTest, mutations } },
},
},
})
const testString = 'hi'

expect(store.state.test.deepTest.test1).toEqual(defalutTestString)
expect(store.state.test.deepTest.test1).toEqual(DEFAULT_TEST_STRING)

commit(store, 'SET_set', testString)

Expand All @@ -214,6 +231,27 @@ describe('Vux base-utils.spec', () => {
expect(getState(store, 'test1')).toEqual(testString)
})

it('getGetter', () => {
const getters = testHelper.makeGetters({
globleValue(state, getters, rootState, rootGetters) {
return DEFAULT_TEST_NUMBER
}
})
const store = getStore<TestStore.state>({
state: {},
modules: {
test: {
namespaced: true,
state: {},
modules: { deepTest: { namespaced: true, state: getTestState().deepTest, mutations, getters } },
},
},
})
const getGetter = testHelper.createGetGetter()
const value = getGetter(store, 'globleValue')
expect(value).toEqual(DEFAULT_TEST_NUMBER)
})

const actions = testHelper.makeActions({
action_set(ctx, { test }: { test: string }) {
commit(ctx, 'SET_set', test)
Expand All @@ -222,10 +260,19 @@ describe('Vux base-utils.spec', () => {
const dispatch = testHelper.createDispatch<typeof actions>()

it('dispatch', () => {
const store = getStore<Tstore.state>({ state: {}, modules: { test: { namespaced: true, state: {}, modules: { deepTest: { namespaced: true, state: getTestState().deepTest, mutations, actions }}}}})
const store = getStore<TestStore.state>({
state: {},
modules: {
test: {
namespaced: true,
state: {},
modules: { deepTest: { namespaced: true, state: getTestState().deepTest, mutations, actions } },
},
},
})
const testString = 'hi'

expect(store.state.test.deepTest.test1).toEqual(defalutTestString)
expect(store.state.test.deepTest.test1).toEqual(DEFAULT_TEST_STRING)

dispatch(store, 'action_set', { test: testString })

Expand Down

0 comments on commit 76eb2f9

Please sign in to comment.