From 972bda4bbafe9cca762575230924c833fe26cba5 Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Thu, 16 Jan 2020 18:15:04 +0100 Subject: [PATCH] test: test actions --- __tests__/actions.spec.ts | 48 +++++++++++++++++++++++++++++++++++++++ src/store.ts | 4 ++-- 2 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 __tests__/actions.spec.ts diff --git a/__tests__/actions.spec.ts b/__tests__/actions.spec.ts new file mode 100644 index 00000000..90ea8454 --- /dev/null +++ b/__tests__/actions.spec.ts @@ -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') + }) +}) diff --git a/src/store.ts b/src/store.ts index 548fa3af..76be5843 100644 --- a/src/store.ts +++ b/src/store.ts @@ -169,9 +169,10 @@ export function buildStore< ) } - const store = { + const store: Store = { ...storeWithState, ...computedGetters, + ...((actions as unknown) as StoreWithActions), } // make state access invisible @@ -184,7 +185,6 @@ export function buildStore< }, }) - // @ts-ignore TODO: actions return store } -- 2.47.2