store.$state.a = false
const spy = jest.fn()
store.$subscribe(spy)
+ expect(spy).not.toHaveBeenCalled()
store.$reset()
store.$state.nested.foo = 'bar'
- expect(spy).not.toHaveBeenCalled()
+ expect(spy).toHaveBeenCalledTimes(2)
expect(store.$state).toEqual({
a: true,
nested: {
expect(func1).not.toHaveBeenCalled()
expect(func2).toHaveBeenCalledTimes(1)
})
+
+ describe('multiple', () => {
+ const useStore = defineStore({
+ id: 'main',
+ state: () => ({
+ name: 'Eduardo',
+ }),
+ })
+
+ it('triggers subscribe only once', async () => {
+ const s1 = useStore()
+ const s2 = useStore()
+
+ const spy1 = jest.fn()
+ const spy2 = jest.fn()
+
+ s1.$subscribe(spy1)
+ s2.$subscribe(spy2)
+
+ expect(spy1).toHaveBeenCalledTimes(0)
+ expect(spy2).toHaveBeenCalledTimes(0)
+
+ s1.name = 'Edu'
+
+ expect(spy1).toHaveBeenCalledTimes(1)
+ expect(spy2).toHaveBeenCalledTimes(1)
+ })
+ })
})
() => pinia.state.value[$id],
(state) => {
if (isListening) {
- subscriptions.forEach((callback) => {
- callback(
- { storeName: $id, type: '🧩 in place', payload: {} },
- state
- )
- })
+ callback({ storeName: $id, type: '🧩 in place', payload: {} }, state)
}
},
{
}
function $reset() {
- subscriptions = []
pinia.state.value[$id] = buildState()
}
$patch(stateMutator: (state: S) => void): void
/**
- * Resets the store to its initial state by removing all subscriptions and
- * building a new state object
+ * Resets the store to its initial state by building a new state object.
*/
$reset(): void