From: Eduardo San Martin Morote Date: Sat, 18 Dec 2021 11:06:53 +0000 (+0100) Subject: test: use user instead of name X-Git-Tag: @pinia/nuxt@0.1.7~3 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=145192a7ec74ba754060a46b23f09f78cdf66235;p=thirdparty%2Fvuejs%2Fpinia.git test: use user instead of name for some reason using a property called name in the state fails --- diff --git a/packages/pinia/__tests__/subscriptions.spec.ts b/packages/pinia/__tests__/subscriptions.spec.ts index 25db3a57..4fb53935 100644 --- a/packages/pinia/__tests__/subscriptions.spec.ts +++ b/packages/pinia/__tests__/subscriptions.spec.ts @@ -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 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) diff --git a/packages/pinia/test-dts/state.test-d.ts b/packages/pinia/test-dts/state.test-d.ts index b69abd4f..b53749b2 100644 --- a/packages/pinia/test-dts/state.test-d.ts +++ b/packages/pinia/test-dts/state.test-d.ts @@ -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(state.counter) + expectType(state.some) + expectType(state.name) })