Skip to content

Commit

Permalink
chore: add test of whole store
Browse files Browse the repository at this point in the history
  • Loading branch information
posva committed Jul 8, 2022
1 parent 8626aac commit 8ed5800
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions packages/playground/src/stores/wholeStore.ts
Original file line number Diff line number Diff line change
@@ -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))
}

0 comments on commit 8ed5800

Please sign in to comment.