]> git.ipfire.org Git - thirdparty/vuejs/pinia.git/commitdiff
test: add tests and increase coverage
authorEduardo San Martin Morote <posva13@gmail.com>
Fri, 27 Aug 2021 13:17:16 +0000 (15:17 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Fri, 27 Aug 2021 13:17:16 +0000 (15:17 +0200)
packages/pinia/__tests__/state.spec.ts
packages/pinia/__tests__/subscriptions.spec.ts
packages/pinia/src/store.ts

index 0a3c3fcd91fd8ee720f5b6b2a6dc194e34557133..5513a4b8e1626489e1994a6d69c272aa118efcf9 100644 (file)
@@ -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({})
+  })
 })
index 1352d84f0a3d6622c844189bcb5204c888be7b87..2fef525206efeb58668da69ac4539a52c7cf28a6 100644 (file)
@@ -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
+    )
+  })
 })
index d77f4dafbb307f2dc216834cdeadc1c187625f79..4d9df150ebf510cfad56090d8ec77db5ea9dc3e4 100644 (file)
@@ -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,