const useStore = () => {
// create a new store
setActivePinia(createPinia())
- return defineStore({
- id: 'main',
+ return defineStore('main', {
state: () => ({
a: true,
nested: {
})()
}
- const useB = defineStore({
- id: 'B',
- state: () => ({ b: 'b' }),
- })
+ const useB = defineStore('B', { state: () => ({ b: 'b' }) })
- const useA = defineStore({
- id: 'A',
+ const useA = defineStore('A', {
state: () => ({ a: 'a' }),
actions: {
swap() {
setActivePinia(createPinia())
})
- const useStore = defineStore({
- id: 'main',
+ const useStore = defineStore('main', {
state: () => ({
name: 'Eduardo',
}),
},
})
- const useB = defineStore({
- id: 'B',
- state: () => ({ b: 'b' }),
- })
+ const useB = defineStore('B', { state: () => ({ b: 'b' }) })
- const useA = defineStore({
- id: 'A',
+ const useA = defineStore('A', {
state: () => ({ a: 'a' }),
getters: {
fromB(): string {
describe('Store Lifespan', () => {
function defineMyStore() {
- return defineStore({
- id: 'main',
+ return defineStore('main', {
state: () => ({
a: true,
n: 0,
const globalWatch = vi.fn()
const destroy = watch(() => pinia.state.value.a?.n, globalWatch)
- const useStore = defineStore({
- id: 'a',
+ const useStore = defineStore('a', {
state: () => {
n = n || ref(0)
return { n }
import { mockWarn } from './vitest-mock-warn'
describe('Map Helpers', () => {
- const useStore = defineStore({
- id: 'main',
+ const useStore = defineStore('main', {
state: () => ({
a: true,
n: 0,
})
describe('mapActions', () => {
- const useStore = defineStore({
- id: 'main',
+ const useStore = defineStore('main', {
state: () => ({ n: 0 }),
actions: {
increment() {
const useStore = () => {
// create a new store
setActivePinia(createPinia())
- return defineStore({
- id: 'main',
+ return defineStore('main', {
state: () => ({
user: 'Eduardo',
}),
})
describe('multiple store instances', () => {
- const useStore = defineStore({
- id: 'main',
+ const useStore = defineStore('main', {
state: () => ({
name: 'Eduardo',
}),
const pinia = createPinia()
setActivePinia(pinia)
- const useStore = defineStore({
- id: 'main',
+ const useStore = defineStore('main', {
state: () => ({
name,
counter,
const useStore = () => {
// create a new store
setActivePinia(createPinia())
- return defineStore({
- id: 'main',
+ return defineStore('main', {
state: () => ({
a: true,
nested: {
const useArrayStore = () => {
// create a new store
setActivePinia(createPinia())
- return defineStore({
- id: 'main',
+ return defineStore('main', {
state: () => ({
items: [{ id: 0 }],
currentItem: { id: 1 },
const useStore = (pinia?: Pinia) => {
// create a new store
setActivePinia(pinia || createPinia())
- return defineStore({
- id: 'main',
+ return defineStore('main', {
state: () => ({
arr: [] as any[],
name: 'Eduardo',
setActivePinia(createPinia())
})
- const useStore = defineStore({
- id: 'main',
+ const useStore = defineStore('main', {
state: () => ({
a: true,
nested: {
})
it('reuses a store', () => {
- const useStore = defineStore({ id: 'main' })
+ const useStore = defineStore('main', {})
expect(useStore()).toBe(useStore())
})
it('works without setting the active pinia', async () => {
setActivePinia(undefined)
const pinia = createPinia()
- const useStore = defineStore({
- id: 'main',
+ const useStore = defineStore('main', {
state: () => ({ n: 0 }),
})
const TestComponent = defineComponent({
})
it('can create an empty state if no state option is provided', () => {
- const store = defineStore({ id: 'some' })()
+ const store = defineStore('some', {})()
expect(store.$state).toEqual({})
})
it('can hydrate the state', () => {
const pinia = createPinia()
setActivePinia(pinia)
- const useStore = defineStore({
- id: 'main',
+ const useStore = defineStore('main', {
state: () => ({
a: true,
nested: {
it('should outlive components', async () => {
const pinia = createPinia()
- const useStore = defineStore({
- id: 'main',
+ const useStore = defineStore('main', {
state: () => ({ n: 0 }),
})
it('reuses stores from parent components', () => {
let s1, s2
- const useStore = defineStore({ id: 'one' })
+ const useStore = defineStore('one', {})
const pinia = createPinia()
const Child = defineComponent({
})
it('can share the same pinia in two completely different instances', async () => {
- const useStore = defineStore({ id: 'one', state: () => ({ n: 0 }) })
+ const useStore = defineStore('one', { state: () => ({ n: 0 }) })
const pinia = createPinia()
const Comp = defineComponent({
it('can be disposed', () => {
const pinia = createPinia()
- const useStore = defineStore({
- id: 'main',
- state: () => ({ n: 0 }),
- })
+ const useStore = defineStore('main', { state: () => ({ n: 0 }) })
const store = useStore(pinia)
const spy = vi.fn()
it('warns when state is created with a class constructor', () => {
class MyState {}
- const useMyStore = defineStore({
- id: 'store',
- state: () => new MyState(),
- })
+ const useMyStore = defineStore('store', { state: () => new MyState() })
useMyStore()
expect(warnTextCheckPlainObject('store')).toHaveBeenWarned()
})
it('only warns about constructors when store is initially created', () => {
class MyState {}
- const useMyStore = defineStore({
- id: 'arrowInit',
- state: () => new MyState(),
- })
+ const useMyStore = defineStore('arrowInit', { state: () => new MyState() })
useMyStore()
expect(warnTextCheckPlainObject('arrowInit')).toHaveBeenWarnedTimes(1)
})
it('does not warn when state is created with a plain object', () => {
- const useMyStore = defineStore({
- id: 'poInit',
+ const useMyStore = defineStore('poInit', {
state: () => ({ someValue: undefined }),
})
useMyStore()
`[🍍]: A getter cannot have the same name as another state property. Rename one of them. Found with "anyName" in store "main".`
).toHaveBeenWarnedTimes(1)
})
-
- it('throws an error if no store id is provided', () => {
- expect(() => defineStore({} as any)).toThrowError(
- /must be passed a store id/
- )
- })
})
it('passes the options of the options store', async () => {
const options = {
- id: 'main',
state: () => ({ n: 0 }),
actions: {
increment() {
},
},
}
- const useStore = defineStore(options)
+ const useStore = defineStore('main', options)
const pinia = createPinia()
mount({ template: 'none' }, { global: { plugins: [pinia] } })
const pinia = createPinia()
setActivePinia(pinia)
- const useStore = defineStore({
- id: 'main',
+ const useStore = defineStore('main', {
state: () => ({
name,
counter,
it('empty state', () => {
expect(storeToRefs(defineStore('a', {})())).toEqual({})
expect(storeToRefs(defineStore('a', () => {})())).toEqual({})
- expect(storeToRefs(defineStore({ id: 'a' })())).toEqual({})
})
it('plain values', () => {
options: Omit<DefineStoreOptions<Id, S, G, A>, 'id'>
): StoreDefinition<Id, S, G, A>
-/**
- * Creates a `useStore` function that retrieves the store instance
- *
- * @param options - options to define the store
- *
- * @deprecated use `defineStore(id, options)` instead
- */
-export function defineStore<
- Id extends string,
- S extends StateTree = {},
- G extends _GettersTree<S> = {},
- // cannot extends ActionsTree because we loose the typings
- A /* extends ActionsTree */ = {},
->(options: DefineStoreOptions<Id, S, G, A>): StoreDefinition<Id, S, G, A>
-
/**
* Creates a `useStore` function that retrieves the store instance
*
/*! #__NO_SIDE_EFFECTS__ */
export function defineStore(
// TODO: add proper types from above
- idOrOptions: any,
+ id: any,
setup?: any,
setupOptions?: any
): StoreDefinition {
- let id: string
let options:
| DefineStoreOptions<
string,
>
const isSetupStore = typeof setup === 'function'
- if (typeof idOrOptions === 'string') {
- id = idOrOptions
- // the option store setup will contain the actual options in this case
- options = isSetupStore ? setupOptions : setup
- } else {
- options = idOrOptions
- id = idOrOptions.id
-
- if (__DEV__ && typeof id !== 'string') {
- throw new Error(
- `[🍍]: "defineStore()" must be passed a store id as its first argument.`
- )
- }
- }
+ // the option store setup will contain the actual options in this case
+ options = isSetupStore ? setupOptions : setup
function useStore(pinia?: Pinia | null, hot?: StoreGeneric): StoreGeneric {
const hasContext = hasInjectionContext()
import { defineStore, expectType } from './'
-const useStore = defineStore({
- id: 'name',
+const useStore = defineStore('name', {
state: () => ({ count: 0 }),
actions: {
useOtherAction() {
}
})
-const useStore = defineStore({
- id: 'main',
+const useStore = defineStore('main', {
actions: {
one() {},
two() {
import { describe, it, expectTypeOf } from 'vitest'
describe('mapHelpers', () => {
- const useOptionsStore = defineStore({
- id: 'name',
+ const useOptionsStore = defineStore('name', {
state: () => ({ a: 'on' as 'on' | 'off', nested: { counter: 0 } }),
getters: {
upper: (state) => state.a.toUpperCase(),
return { a, upper, writableUpper, toggleA, setToggle }
})
- const useCounter = defineStore({
- id: 'counter',
- state: () => ({ n: 0 }),
- })
+ const useCounter = defineStore('counter', { state: () => ({ n: 0 }) })
- const useStoreDos = defineStore({
- id: 'dos',
- state: () => ({}),
- })
+ const useStoreDos = defineStore('dos', { state: () => ({}) })
type MainStore = ReturnType<typeof useOptionsStore>
type DosStore = ReturnType<typeof useStoreDos>
import { defineStore, expectType } from './'
-const useStore = defineStore({
- id: 'main',
+const useStore = defineStore('main', {
state: () => ({
user: 'Eduardo',
}),
} from './'
import { computed, Ref, ref, UnwrapRef, watch, WritableComputedRef } from 'vue'
-const useStore = defineStore({
- id: 'name',
+const useStore = defineStore('name', {
state: () => ({ a: 'on' as 'on' | 'off', nested: { counter: 0 } }),
getters: {
upper: (state) => {
},
})
-defineStore('name', {
- // @ts-expect-error: id is passed as the first argument
- id: 'name',
-})
defineStore('name', {})
// @ts-expect-error
defineStore('name')
})
// actions on not existing properties
-defineStore({
- id: '',
+defineStore('', {
actions: {
a() {
// @ts-expect-error
},
})
-defineStore({
- id: '',
+defineStore('', {
state: () => ({}),
actions: {
a() {
},
})
-defineStore({
- id: '',
+defineStore('', {
getters: {},
actions: {
a() {
s.set({ id: 1 })
// getters on not existing properties
-defineStore({
- id: '',
+defineStore('', {
getters: {
a(): number {
// @ts-expect-error
},
})
-defineStore({
- id: '',
+defineStore('', {
state: () => ({}),
getters: {
a(): number {
return
})
-const useNoSAG = defineStore({
- id: 'noSAG',
-})
-const useNoAG = defineStore({
- id: 'noAG',
- state: () => ({}),
-})
-const useNoSG = defineStore({
- id: 'noAG',
- actions: {},
-})
-const useNoSA = defineStore({
- id: 'noAG',
- getters: {},
-})
-const useNoS = defineStore({
- id: 'noAG',
- actions: {},
- getters: {},
-})
-const useNoA = defineStore({
- id: 'noAG',
- state: () => ({}),
- getters: {},
-})
-const useNoG = defineStore({
- id: 'noAG',
- state: () => ({}),
- actions: {},
-})
+const useNoSAG = defineStore('noSAG', {})
+const useNoAG = defineStore('noAG', { state: () => ({}) })
+const useNoSG = defineStore('noAG', { actions: {} })
+const useNoSA = defineStore('noAG', { getters: {} })
+const useNoS = defineStore('noAG', { actions: {}, getters: {} })
+const useNoA = defineStore('noAG', { state: () => ({}), getters: {} })
+const useNoG = defineStore('noAG', { state: () => ({}), actions: {} })
const noSAG = useNoSAG()
const noSA = useNoSA()