From 8ed5800b3ca28dfd3bbcdcc5e0556b42364228fa Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Fri, 8 Jul 2022 09:45:53 +0200 Subject: [PATCH] chore: add test of whole store --- packages/playground/src/stores/wholeStore.ts | 46 ++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 packages/playground/src/stores/wholeStore.ts diff --git a/packages/playground/src/stores/wholeStore.ts b/packages/playground/src/stores/wholeStore.ts new file mode 100644 index 0000000000..3f59971e07 --- /dev/null +++ b/packages/playground/src/stores/wholeStore.ts @@ -0,0 +1,46 @@ +interface User { + id: string +} +type State = { type: T } +type AuthStateLoggingIn = State<'loggingIn'> +type AuthStateLoggedIn = State<'loggedIn'> & { user: User } +type AuthStateError = State<'error'> & { errorMsg: string } +type AuhtStateLoggedOut = State<'loggedOut'> + +export type AuthState = + | AuthStateLoggingIn + | AuthStateLoggedIn + | AuthStateError + | AuhtStateLoggedOut + +import { acceptHMRUpdate, defineStore } from 'pinia' + +const delay = (t: number) => new Promise((r) => setTimeout(r, t)) + +export const useWholeStore = defineStore('whole', { + state: (): AuthState => ({ type: 'loggedIn', user: { id: '1' } }), + + getters: { + errorMsg: (state) => (state.type === 'error' ? state.errorMsg : ''), + }, + + actions: { + async login() { + try { + await delay(1000) + this.$patch({ type: 'loggedIn', user: { id: '1' } }) + } catch (e) { + const error = e as Error + this.$patch({ type: 'error', errorMsg: error.message }) + } + }, + + logout() { + this.$patch({ type: 'loggedOut' }) + }, + }, +}) + +if (import.meta.hot) { + import.meta.hot.accept(acceptHMRUpdate(useWholeStore, import.meta.hot)) +}