]> git.ipfire.org Git - thirdparty/vuejs/pinia.git/commitdiff
test: use user instead of name
authorEduardo San Martin Morote <posva13@gmail.com>
Sat, 18 Dec 2021 11:06:53 +0000 (12:06 +0100)
committerEduardo San Martin Morote <posva13@gmail.com>
Sat, 18 Dec 2021 11:06:53 +0000 (12:06 +0100)
for some reason using a property called name in the state fails

packages/pinia/__tests__/subscriptions.spec.ts
packages/pinia/test-dts/state.test-d.ts

index 25db3a57ade44ba9cb1fe7b1bcfbbbc364157428..4fb53935c0375c2c0561614838d9d5446e685046 100644 (file)
@@ -3,26 +3,22 @@ import { mount } from '@vue/test-utils'
 import { nextTick } from 'vue'
 
 describe('Subscriptions', () => {
-  const useStore = () => {
-    // create a new store
-    setActivePinia(createPinia())
-    return defineStore({
-      id: 'main',
-      state: () => ({
-        name: 'Eduardo',
-      }),
-    })()
-  }
+  const useStore = defineStore({
+    id: 'main',
+    state: () => ({
+      user: 'Eduardo',
+    }),
+  })
 
-  let store: ReturnType<typeof useStore>
   beforeEach(() => {
-    store = useStore()
+    setActivePinia(createPinia())
   })
 
   it('fires callback when patch is applied', () => {
+    const store = useStore()
     const spy = jest.fn()
     store.$subscribe(spy, { flush: 'sync' })
-    store.$state.name = 'Cleiton'
+    store.$state.user = 'Cleiton'
     expect(spy).toHaveBeenCalledTimes(1)
     expect(spy).toHaveBeenCalledWith(
       expect.objectContaining({
@@ -38,7 +34,7 @@ describe('Subscriptions', () => {
     const spy = jest.fn()
     store.$subscribe(spy, { flush: 'sync' })
 
-    const patch = { name: 'Cleiton' }
+    const patch = { user: 'Cleiton' }
     store.$patch(patch)
 
     expect(spy).toHaveBeenCalledWith(
@@ -53,34 +49,28 @@ describe('Subscriptions', () => {
 
   it('unsubscribes callback when unsubscribe is called', () => {
     const spy = jest.fn()
+    const store = useStore()
     const unsubscribe = store.$subscribe(spy, { flush: 'sync' })
     unsubscribe()
-    store.$state.name = 'Cleiton'
+    store.$state.user = 'Cleiton'
     expect(spy).not.toHaveBeenCalled()
   })
 
   it('listeners are not affected when unsubscribe is called multiple times', () => {
     const func1 = jest.fn()
     const func2 = jest.fn()
+    const store = useStore()
     const unsubscribe1 = store.$subscribe(func1, { flush: 'sync' })
     store.$subscribe(func2, { flush: 'sync' })
     unsubscribe1()
     unsubscribe1()
-    store.$state.name = 'Cleiton'
+    store.$state.user = 'Cleiton'
     expect(func1).not.toHaveBeenCalled()
     expect(func2).toHaveBeenCalledTimes(1)
   })
 
   describe('multiple', () => {
-    const useStore = defineStore({
-      id: 'main',
-      state: () => ({
-        name: 'Eduardo',
-      }),
-    })
-
     it('triggers subscribe only once', async () => {
-      setActivePinia(createPinia())
       const s1 = useStore()
       const s2 = useStore()
 
@@ -93,7 +83,7 @@ describe('Subscriptions', () => {
       expect(spy1).toHaveBeenCalledTimes(0)
       expect(spy2).toHaveBeenCalledTimes(0)
 
-      s1.name = 'Edu'
+      s1.user = 'Edu'
 
       expect(spy1).toHaveBeenCalledTimes(1)
       expect(spy2).toHaveBeenCalledTimes(1)
@@ -101,6 +91,7 @@ describe('Subscriptions', () => {
 
     it('removes on unmount', async () => {
       const pinia = createPinia()
+      setActivePinia(pinia)
       const spy1 = jest.fn()
       const spy2 = jest.fn()
 
@@ -123,16 +114,16 @@ describe('Subscriptions', () => {
       expect(spy1).toHaveBeenCalledTimes(0)
       expect(spy2).toHaveBeenCalledTimes(0)
 
-      s1.name = 'Edu'
+      s1.user = 'Edu'
       expect(spy1).toHaveBeenCalledTimes(1)
       expect(spy2).toHaveBeenCalledTimes(1)
 
-      s1.$patch({ name: 'a' })
+      s1.$patch({ user: 'a' })
       expect(spy1).toHaveBeenCalledTimes(2)
       expect(spy2).toHaveBeenCalledTimes(2)
 
       s1.$patch((state) => {
-        state.name = 'other'
+        state.user = 'other'
       })
       expect(spy1).toHaveBeenCalledTimes(3)
       expect(spy2).toHaveBeenCalledTimes(3)
@@ -140,15 +131,15 @@ describe('Subscriptions', () => {
       wrapper.unmount()
       await nextTick()
 
-      s1.$patch({ name: 'b' })
+      s1.$patch({ user: 'b' })
       expect(spy1).toHaveBeenCalledTimes(3)
       expect(spy2).toHaveBeenCalledTimes(4)
       s1.$patch((state) => {
-        state.name = 'c'
+        state.user = 'c'
       })
       expect(spy1).toHaveBeenCalledTimes(3)
       expect(spy2).toHaveBeenCalledTimes(5)
-      s1.name = 'd'
+      s1.user = 'd'
       expect(spy1).toHaveBeenCalledTimes(3)
       expect(spy2).toHaveBeenCalledTimes(6)
     })
@@ -156,8 +147,9 @@ describe('Subscriptions', () => {
 
   it('subscribe is post by default', async () => {
     const spy = jest.fn()
+    const store = useStore()
     store.$subscribe(spy)
-    store.$state.name = 'Cleiton'
+    store.$state.user = 'Cleiton'
     expect(spy).toHaveBeenCalledTimes(0)
     await nextTick()
     expect(spy).toHaveBeenCalledTimes(1)
index b69abd4fd662454db62d3ea63d70640201e69446..b53749b2dc1329b1c20a1bd95a3b2ef3dcebc70f 100644 (file)
@@ -71,11 +71,15 @@ expectType<{ msg: string }>(store.$state.aShallowRef)
 const onlyState = defineStore({
   id: 'main',
   state: () => ({
-    counter: 0,
+    // counter: 0,
+    // TODO: having only name fails...
+    name: 'hey',
+    some: 'hello',
   }),
 })()
 
-onlyState.$patch({ counter: 2 })
+onlyState.$patch({ some: 'other' })
 onlyState.$patch((state) => {
-  expectType<number>(state.counter)
+  expectType<string>(state.some)
+  expectType<string>(state.name)
 })