export const useSharedStore = createStore({
id: 'shared',
- state: () => ({}),
getters: {
summary() {
const user = useUserStore()
})
})
+ it('can be reset', () => {
+ const store = useStore()
+ store.state.a = false
+ const spy = jest.fn()
+ store.subscribe(spy)
+ store.reset()
+ store.state.nested.foo = 'bar'
+ expect(spy).not.toHaveBeenCalled()
+ expect(store.state).toEqual({
+ a: true,
+ nested: {
+ foo: 'bar',
+ a: { b: 'string' },
+ },
+ })
+ })
+
+ it('can create an empty state if no state option is provided', () => {
+ const store = createStore({ id: 'some' })()
+
+ expect(store.state).toEqual({})
+ })
+
it('can hydrate the state', () => {
setActiveReq({})
const useStore = createStore({
store.state.nested.a.b = 'hey'
expect(store2.state.nested.a.b).toBe('string')
})
+
+ it('subscribe to changes', () => {
+ const store = useStore()
+ const spy = jest.fn()
+ store.subscribe(spy)
+
+ store.state.a = false
+
+ expect(spy).toHaveBeenCalledWith(
+ {
+ payload: {},
+ storeName: 'main',
+ type: expect.stringContaining('in place'),
+ },
+ store.state
+ )
+ })
+
+ it('subscribe to changes done via patch', () => {
+ const store = useStore()
+ const spy = jest.fn()
+ store.subscribe(spy)
+
+ const patch = { a: false }
+ store.patch(patch)
+
+ expect(spy).toHaveBeenCalledWith(
+ {
+ payload: patch,
+ storeName: 'main',
+ type: expect.stringContaining('patch'),
+ },
+ store.state
+ )
+ })
})
+++ /dev/null
-import { Store, StoreGetter, StateTree, StoreWithGetters } from './types'
-import { CombinedStore, buildStore } from './store'
-
-export type CombinedState<
- S extends Record<
- string,
- (
- ...args: any[]
- ) => CombinedStore<
- string,
- StateTree,
- Record<string, StoreGetter<StateTree>>
- >
- >
-> = {
- [k in keyof S]: S[k] extends (
- ...args: any[]
- ) => CombinedStore<
- string,
- infer State,
- Record<string, StoreGetter<infer State>>
- >
- ? State
- : never
-}
-
-export type CombinedGetters<
- S extends Record<
- string,
- (
- ...args: any[]
- ) => CombinedStore<
- string,
- StateTree,
- Record<string, StoreGetter<StateTree>>
- >
- >
-> = {
- [k in keyof S]: S[k] extends (
- ...args: any[]
- ) => CombinedStore<string, infer State, infer Getters>
- ? StoreWithGetters<State, Getters>
- : never
-}
-
-function buildCombinedStore<
- S extends Record<
- string,
- CombinedStore<string, StateTree, Record<string, StoreGetter<StateTree>>>
- >
->(stores: S): Store<'', CombinedState<S>> & CombinedGetters<S> {
- const state = {}
- for (const name in stores) {
- const store = stores[name]
- Object.defineProperty(state, name, {
- get: () => store.state,
- })
- }
-
- // @ts-ignore
- return {
- state,
- }
-}
-
-export function pinia<
- S extends Record<
- string,
- (
- ...args: any[]
- ) => CombinedStore<
- string,
- StateTree,
- Record<string, StoreGetter<StateTree>>
- >
- >
->(stores: S): Store<'', CombinedState<S>> & CombinedGetters<S> {
- // TODO: implement if makes sense
- const state = {}
- for (const name in stores) {
- const store = stores[name]()
- Object.defineProperty(state, name, {
- get: () => store.state,
- })
- }
-
- // @ts-ignore
- return {
- state,
- }
-}
StoreWithGetters,
StoreGetter,
NonNullObject,
- StoreReactiveGetters,
} from './types'
import { useStoreDevtools } from './devtools'
A extends Record<string, StoreAction>
> = StoreWithState<Id, S> & StoreWithGetters<S, G> & StoreWithActions<A>
-export type WrapStoreWithId<
- S extends Store<string, StateTree, any, any>
-> = S extends Store<infer Id, infer S, infer G, infer A>
- ? {
- [k in Id]: Store<Id, S, G, A>
- }
- : never
-
-export type ExtractGettersFromStore<S> = S extends Store<
- any,
- infer S,
- infer G,
- any
->
- ? {
- [k in keyof G]: ReturnType<G[k]>
- }
- : never
-
export type PiniaStore<
P extends Record<string, Store<any, any, any, any>>
> = P extends Record<infer name, any>
A extends Record<string, StoreAction>
>(
id: Id,
- buildState: () => S,
+ buildState = () => ({} as S),
getters: G = {} as G,
actions: A = {} as A,
initialState?: S | undefined
A extends Record<string, StoreAction>
>(options: {
id: Id
- state: () => S
+ state?: () => S
getters?: G
// allow actions use other actions
actions?: A & ThisType<A & StoreWithState<Id, S> & StoreWithGetters<S, G>>