]> git.ipfire.org Git - thirdparty/vuejs/pinia.git/commitdiff
chore: add test of whole store
authorEduardo San Martin Morote <posva13@gmail.com>
Fri, 8 Jul 2022 07:45:53 +0000 (09:45 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Fri, 8 Jul 2022 07:45:53 +0000 (09:45 +0200)
packages/playground/src/stores/wholeStore.ts [new file with mode: 0644]

diff --git a/packages/playground/src/stores/wholeStore.ts b/packages/playground/src/stores/wholeStore.ts
new file mode 100644 (file)
index 0000000..3f59971
--- /dev/null
@@ -0,0 +1,46 @@
+interface User {
+  id: string
+}
+type State<T extends string> = { 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))
+}