Skip to content

Commit

Permalink
For danielroe#266: add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
drodichkin committed Feb 24, 2022
1 parent cf8be2e commit 9b7d20a
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 19 deletions.
50 changes: 34 additions & 16 deletions packages/typed-vuex/src/accessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ import { BlankStore, MergedStoreType, NuxtStoreInput } from './types/store'
import { State, StateType } from './types/state'
import { NuxtModules } from './types/modules'

export const getAccessorType = <T extends State,
export const getAccessorType = <
T extends State,
G extends GetterTree<StateType<T>, any>,
M extends MutationTree<StateType<T>>,
A extends ActionTree<StateType<T>, any>,
S extends NuxtModules>(
S extends NuxtModules
>(
store: Partial<NuxtStoreInput<T, G, M, A, S>>
) => {
return (undefined as any) as MergedStoreType<typeof store & BlankStore>
Expand Down Expand Up @@ -96,6 +98,30 @@ export const getAccessorFromStore = (pattern: any) => {
useAccessor(store, pattern._modules.root._rawModule)
}

const processModuleByPath = (
accessor: MergedStoreType<Partial<NuxtStoreInput<any, any, any, any, any>> & BlankStore, string>,
path: string | string[]
): {target: Record<string, any>, key: string} => {
const paths = typeof path === 'string' ? [path] : path

let target = accessor;
let key: string | undefined;
paths.forEach((part, index) => {
if (index === paths.length - 1) {
if (!target) throw new Error(`Could not find parent module for ${paths[index - 1] || paths[index]}`);
key = part;
} else {
target = target[part];
}
})

return {
target,
//Key can not be undefined
key: key as string,
}
}

export const registerModule = (
path: string | [string, ...string[]],
store: Store<any>,
Expand Down Expand Up @@ -124,27 +150,19 @@ export const registerModule = (
}

const paths = typeof path === 'string' ? [path] : path

let target = accessor;
paths.forEach((part, index) => {
if (index === paths.length - 1) {
if (!target) throw new Error(`Could not find parent module for ${paths[index - 1] || paths[index]}`);

store.registerModule(path as string, module as Module<any, any>, {
preserveState,
})
target[part] = useAccessor(store, module, paths.join('/'));
} else {
target = target[part];
}
const processedModule = processModuleByPath(accessor, paths)
store.registerModule(path as string, module as Module<any, any>, {
preserveState,
})
processedModule.target[processedModule.key] = useAccessor(store, module, paths.join('/'))
}

export const unregisterModule = (
path: string | [string, ...string[]],
store: Store<any>,
accessor: MergedStoreType<Partial<NuxtStoreInput<any, any, any, any, any>> & BlankStore, string>
) => {
const processedModule = processModuleByPath(accessor, path)
store.unregisterModule(path as string)
delete accessor[path[path.length - 1]]
delete processedModule.target[processedModule.key]
}
35 changes: 32 additions & 3 deletions packages/typed-vuex/test/accessor.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useAccessor, getAccessorType, getAccessorFromStore } from 'typed-vuex'
import { useAccessor, getAccessorType, getAccessorFromStore, registerModule, unregisterModule } from 'typed-vuex'
import Vuex, { Store } from 'vuex'
import Vue from 'vue'

Expand Down Expand Up @@ -118,9 +118,9 @@ describe('accessor', () => {
submoduleBehaviour(accessor.submodule.nestedSubmodule)
})
test('namespaced dynamic modules work', async () => {
store.registerModule('dynamicSubmodule', { ...submodule, namespaced: true })
store.registerModule('dynamicSubmodule', {...submodule, namespaced: true})
const dynamicAccessor = useAccessor(store, {
modules: { dynamicSubmodule: { ...submodule, namespaced: true } },
modules: {dynamicSubmodule: {...submodule, namespaced: true}},
})

submoduleBehaviour(dynamicAccessor.dynamicSubmodule)
Expand All @@ -134,4 +134,33 @@ describe('accessor', () => {
submoduleBehaviour(dynamicAccessor)
store.unregisterModule('dynamicSubmodule')
})
test('dynamic module global registration works', async () => {
registerModule('dynamicSubmodule', store, accessor, submodule)
expect(store.state.dynamicSubmodule.firstName).toBeDefined()
expect(store.state.dynamicSubmodule.nestedSubmodule).toBeUndefined()
submoduleBehaviour(accessor.dynamicSubmodule)
expect(accessor.dynamicSubmodule.nestedSubmodule).toBeUndefined()

registerModule(['dynamicSubmodule', 'nestedSubmodule'], store, accessor, submodule)
expect(store.state.dynamicSubmodule.nestedSubmodule.firstName).toBeDefined()
submoduleBehaviour(accessor.dynamicSubmodule.nestedSubmodule)

unregisterModule('dynamicSubmodule', store, accessor)
expect(store.state.dynamicSubmodule).toBeUndefined()
expect(accessor.dynamicSubmodule).toBeUndefined()

registerModule('dynamicSubmodule', store, accessor, submodule)
expect(store.state.dynamicSubmodule.nestedSubmodule).toBeUndefined()
expect(accessor.dynamicSubmodule.nestedSubmodule).toBeUndefined()

registerModule(['dynamicSubmodule', 'nestedSubmodule'], store, accessor, submodule)
expect(store.state.dynamicSubmodule.nestedSubmodule.firstName).toBeDefined()
expect(accessor.dynamicSubmodule.nestedSubmodule.firstName).toBeDefined()

unregisterModule(['dynamicSubmodule', 'nestedSubmodule'], store, accessor)
expect(store.state.dynamicSubmodule).toBeDefined()
expect(accessor.dynamicSubmodule).toBeDefined()
expect(store.state.dynamicSubmodule.nestedSubmodule).toBeUndefined()
expect(accessor.dynamicSubmodule.nestedSubmodule).toBeUndefined()
})
})

0 comments on commit 9b7d20a

Please sign in to comment.