-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
172 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { getCurrentInstance } from '@vue/composition-api' | ||
import { Module } from './module' | ||
import { assert } from './utils' | ||
|
||
export function createComposable<Mod extends Module<any, any, any, any, any>>( | ||
module: Mod | ||
) { | ||
return function useContext() { | ||
const vm = getCurrentInstance()?.proxy | ||
assert(vm, 'Failed to get the current component instance') | ||
assert(vm.$store, 'Vuex store is not installed') | ||
|
||
return module.context(vm.$store) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import { getCurrentInstance } from '@vue/composition-api' | ||
import { mount } from 'vue-composable-tester' | ||
import { Store } from 'vuex' | ||
import { | ||
Actions, | ||
createComposable, | ||
createStore, | ||
Getters, | ||
Module, | ||
Mutations, | ||
} from '../src' | ||
|
||
describe('composables', () => { | ||
class FooState { | ||
value = 1 | ||
} | ||
|
||
class FooGetters extends Getters<FooState> { | ||
get double(): number { | ||
return this.state.value * 2 | ||
} | ||
} | ||
|
||
class FooMutations extends Mutations<FooState> { | ||
inc() { | ||
this.state.value++ | ||
} | ||
} | ||
|
||
class FooActions extends Actions< | ||
FooState, | ||
FooGetters, | ||
FooMutations, | ||
FooActions | ||
> { | ||
inc() { | ||
return new Promise((resolve) => { | ||
setTimeout(() => { | ||
this.commit('inc', undefined) | ||
resolve() | ||
}, 0) | ||
}) | ||
} | ||
} | ||
|
||
const foo = new Module({ | ||
state: FooState, | ||
getters: FooGetters, | ||
mutations: FooMutations, | ||
actions: FooActions, | ||
}) | ||
|
||
let store: Store<unknown> | ||
beforeEach(() => { | ||
store = createStore(foo) | ||
}) | ||
|
||
describe('createComposable', () => { | ||
const useFooContext = createComposable(foo) | ||
|
||
it('state', () => { | ||
const { result } = mount(useFooContext, { | ||
provider: () => { | ||
getCurrentInstance()!.proxy.$store = store | ||
}, | ||
}) | ||
expect(result.state.value).toBe(1) | ||
}) | ||
|
||
it('getters', () => { | ||
const { result } = mount(useFooContext, { | ||
provider: () => { | ||
getCurrentInstance()!.proxy.$store = store | ||
}, | ||
}) | ||
expect(result.getters.double).toBe(2) | ||
}) | ||
|
||
it('mutations', () => { | ||
const { result } = mount(useFooContext, { | ||
provider: () => { | ||
getCurrentInstance()!.proxy.$store = store | ||
}, | ||
}) | ||
expect(result.state.value).toBe(1) | ||
result.commit('inc') | ||
expect(result.state.value).toBe(2) | ||
}) | ||
|
||
it('actions', async () => { | ||
const { result } = mount(useFooContext, { | ||
provider: () => { | ||
getCurrentInstance()!.proxy.$store = store | ||
}, | ||
}) | ||
expect(result.getters.double).toBe(2) | ||
await result.dispatch('inc') | ||
expect(result.getters.double).toBe(4) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters