From: Eduardo San Martin Morote Date: Fri, 27 Aug 2021 13:17:16 +0000 (+0200) Subject: test: add tests and increase coverage X-Git-Tag: @pinia/nuxt@0.0.2~25 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=6360b2cf0e1b48b28b8c0c5bbc5ad4408190e712;p=thirdparty%2Fvuejs%2Fpinia.git test: add tests and increase coverage --- diff --git a/packages/pinia/__tests__/state.spec.ts b/packages/pinia/__tests__/state.spec.ts index 0a3c3fcd..5513a4b8 100644 --- a/packages/pinia/__tests__/state.spec.ts +++ b/packages/pinia/__tests__/state.spec.ts @@ -207,4 +207,11 @@ describe('State', () => { }, }) }) + + it('can reset the state of an empty store', () => { + const store = defineStore('a', {})(createPinia()) + expect(store.$state).toEqual({}) + store.$reset() + expect(store.$state).toEqual({}) + }) }) diff --git a/packages/pinia/__tests__/subscriptions.spec.ts b/packages/pinia/__tests__/subscriptions.spec.ts index 1352d84f..2fef5252 100644 --- a/packages/pinia/__tests__/subscriptions.spec.ts +++ b/packages/pinia/__tests__/subscriptions.spec.ts @@ -1,5 +1,6 @@ import { createPinia, defineStore, MutationType, setActivePinia } from '../src' import { mount } from '@vue/test-utils' +import { nextTick } from 'vue' describe('Subscriptions', () => { const useStore = () => { @@ -138,4 +139,20 @@ describe('Subscriptions', () => { expect(spy2).toHaveBeenCalledTimes(3) }) }) + + it('subscribe is post by default', async () => { + const spy = jest.fn() + store.$subscribe(spy) + store.$state.name = 'Cleiton' + expect(spy).toHaveBeenCalledTimes(0) + await nextTick() + expect(spy).toHaveBeenCalledTimes(1) + expect(spy).toHaveBeenCalledWith( + expect.objectContaining({ + storeId: 'main', + type: MutationType.direct, + }), + store.$state + ) + }) }) diff --git a/packages/pinia/src/store.ts b/packages/pinia/src/store.ts index d77f4daf..4d9df150 100644 --- a/packages/pinia/src/store.ts +++ b/packages/pinia/src/store.ts @@ -170,6 +170,11 @@ function createSetupStore< ...options, } + /* istanbul ignore if */ + if (__DEV__ && !pinia._e.active) { + throw new Error('Pinia destroyed') + } + // watcher options for $subscribe const $subscribeOptions: WatchOptions = { deep: true,