]> git.ipfire.org Git - thirdparty/vuejs/pinia.git/commitdiff
test: test actions
authorEduardo San Martin Morote <posva13@gmail.com>
Thu, 16 Jan 2020 17:15:04 +0000 (18:15 +0100)
committerEduardo San Martin Morote <posva13@gmail.com>
Mon, 20 Jan 2020 18:21:18 +0000 (19:21 +0100)
__tests__/actions.spec.ts [new file with mode: 0644]
src/store.ts

diff --git a/__tests__/actions.spec.ts b/__tests__/actions.spec.ts
new file mode 100644 (file)
index 0000000..90ea845
--- /dev/null
@@ -0,0 +1,48 @@
+import { createStore, setActiveReq } from '../src'
+
+describe('Store', () => {
+  const useStore = () => {
+    // create a new store
+    setActiveReq({})
+    return createStore({
+      id: 'main',
+      state: () => ({
+        a: true,
+        nested: {
+          foo: 'foo',
+          a: { b: 'string' },
+        },
+      }),
+      actions: {
+        toggle() {
+          this.state.a = !this.state.a
+        },
+
+        setFoo(foo: string) {
+          this.patch({ nested: { foo } })
+        },
+
+        combined() {
+          this.toggle()
+          this.setFoo('bar')
+        },
+      },
+    })()
+  }
+
+  it('can use the store as this', () => {
+    const store = useStore()
+    expect(store.state.a).toBe(true)
+    store.toggle()
+    expect(store.state.a).toBe(false)
+  })
+
+  it('can call other actions', () => {
+    const store = useStore()
+    expect(store.state.a).toBe(true)
+    expect(store.state.nested.foo).toBe('foo')
+    store.combined()
+    expect(store.state.a).toBe(false)
+    expect(store.state.nested.foo).toBe('bar')
+  })
+})
index 548fa3afbc583fed69990e6cf288b8c096df0e28..76be5843d319db6ba46e24eb474fd14adbd9bc8c 100644 (file)
@@ -169,9 +169,10 @@ export function buildStore<
     )
   }
 
-  const store = {
+  const store: Store<Id, S, G, A> = {
     ...storeWithState,
     ...computedGetters,
+    ...((actions as unknown) as StoreWithActions<A>),
   }
 
   // make state access invisible
@@ -184,7 +185,6 @@ export function buildStore<
     },
   })
 
-  // @ts-ignore TODO: actions
   return store
 }