]> git.ipfire.org Git - thirdparty/vuejs/pinia.git/commitdiff
test: use createPinia()
authorEduardo San Martin Morote <posva13@gmail.com>
Wed, 3 Mar 2021 14:45:26 +0000 (15:45 +0100)
committerEduardo San Martin Morote <posva@users.noreply.github.com>
Thu, 4 Mar 2021 15:43:47 +0000 (16:43 +0100)
__tests__/actions.spec.ts
__tests__/getters.spec.ts
__tests__/rootState.spec.ts
__tests__/subscriptions.spec.ts

index f1fdd32478714237ac3360dca62aba888a4c529d..75abfa221a9dfb563b06c27c71f5566e32c5805b 100644 (file)
@@ -1,9 +1,13 @@
-import { defineStore, setActiveReq } from '../src'
+import Vue from 'vue'
+import { defineStore, createPinia, setActivePinia, Pinia } from '../src'
 
 describe('Actions', () => {
+  let pinia: Pinia
   const useStore = () => {
     // create a new store
-    setActiveReq({})
+    pinia = createPinia()
+    pinia.Vue = Vue
+    setActivePinia(pinia)
     return defineStore({
       id: 'main',
       state: () => ({
@@ -83,13 +87,15 @@ describe('Actions', () => {
   })
 
   it('supports being called between requests', () => {
-    const req1 = {}
-    const req2 = {}
-    setActiveReq(req1)
+    const pinia1 = createPinia()
+    pinia1.Vue = Vue
+    const pinia2 = createPinia()
+    pinia2.Vue = Vue
+    setActivePinia(pinia1)
     const aStore = useA()
 
     // simulate a different request
-    setActiveReq(req2)
+    setActivePinia(pinia2)
     const bStore = useB()
     bStore.$state.b = 'c'
 
@@ -100,18 +106,20 @@ describe('Actions', () => {
   })
 
   it('can force the req', () => {
-    const req1 = {}
-    const req2 = {}
-    const aStore = useA(req1)
+    const pinia1 = createPinia()
+    pinia1.Vue = Vue
+    const pinia2 = createPinia()
+    pinia2.Vue = Vue
+    const aStore = useA(pinia1)
 
-    let bStore = useB(req2)
+    let bStore = useB(pinia2)
     bStore.$state.b = 'c'
 
     aStore.swap()
     expect(aStore.$state.a).toBe('b')
     // a different instance of b store was used
     expect(bStore.$state.b).toBe('c')
-    bStore = useB(req1)
+    bStore = useB(pinia1)
     expect(bStore.$state.b).toBe('a')
   })
 })
index 47e3a3a488572ba7e59b0f0cb72a0626d36fdc0f..6181f9be320c7a46868f218ca6430cf1d96a12fb 100644 (file)
@@ -1,11 +1,15 @@
-import { defineStore, setActiveReq } from '../src'
+import Vue from 'vue'
+import { defineStore, createPinia, setActivePinia, Pinia } from '../src'
 
 describe('Getters', () => {
   jest.useFakeTimers()
 
+  let pinia: Pinia
   const useStore = () => {
     // create a new store
-    setActiveReq({})
+    pinia = createPinia()
+    pinia.Vue = Vue
+    setActivePinia(pinia)
     return defineStore({
       id: 'main',
       state: () => ({
@@ -70,13 +74,15 @@ describe('Getters', () => {
   })
 
   it('supports changing between requests', () => {
-    const req1 = {}
-    const req2 = {}
-    setActiveReq(req1)
+    const pinia1 = createPinia()
+    pinia1.Vue = Vue
+    const pinia2 = createPinia()
+    pinia2.Vue = Vue
+    setActivePinia(pinia1)
     const aStore = useA()
 
     // simulate a different request
-    setActiveReq(req2)
+    setActivePinia(pinia2)
     const bStore = useB()
     bStore.b = 'c'
 
index 5c9519ae67bd314ee00d95b4c99c5f89c2c2368c..ff0448d70fcb85bca96e6d49a9726b7731c64c75 100644 (file)
@@ -1,4 +1,5 @@
-import { defineStore, getRootState } from '../src'
+import { defineStore, createPinia } from '../src'
+import Vue from 'vue'
 
 describe('Root State', () => {
   const useA = defineStore({
@@ -12,35 +13,39 @@ describe('Root State', () => {
   })
 
   it('works with no stores', () => {
-    expect(getRootState({})).toEqual({})
+    expect(createPinia().state.value).toEqual({})
   })
 
   it('retrieves the root state of one store', () => {
-    const req = {}
-    useA(req)
-    expect(getRootState(req)).toEqual({
+    const pinia = createPinia()
+    pinia.Vue = Vue
+    useA(pinia)
+    expect(pinia.state.value).toEqual({
       a: { a: 'a' },
     })
   })
 
-  it('does not mix up different requests', () => {
-    const req1 = {}
-    const req2 = {}
-    useA(req1)
-    useB(req2)
-    expect(getRootState(req1)).toEqual({
+  it('does not mix up different applications', () => {
+    const pinia1 = createPinia()
+    pinia1.Vue = Vue
+    const pinia2 = createPinia()
+    pinia2.Vue = Vue
+    useA(pinia1)
+    useB(pinia2)
+    expect(pinia1.state.value).toEqual({
       a: { a: 'a' },
     })
-    expect(getRootState(req2)).toEqual({
+    expect(pinia2.state.value).toEqual({
       b: { b: 'b' },
     })
   })
 
   it('can hold multiple stores', () => {
-    const req1 = {}
-    useA(req1)
-    useB(req1)
-    expect(getRootState(req1)).toEqual({
+    const pinia1 = createPinia()
+    pinia1.Vue = Vue
+    useA(pinia1)
+    useB(pinia1)
+    expect(pinia1.state.value).toEqual({
       a: { a: 'a' },
       b: { b: 'b' },
     })
index 2884b8cb6b1f05d7718eaadfc50f8e1d1d62324e..6c17e12651756f2a05e53a121cc008b6fa87148c 100644 (file)
@@ -4,9 +4,9 @@ import { defineStore, setActivePinia, createPinia, Pinia } from '../src'
 describe('Subscriptions', () => {
   let pinia: Pinia
   const useStore = () => {
+    // create a new store
     pinia = createPinia()
     pinia.Vue = Vue
-    // create a new store
     setActivePinia(pinia)
     return defineStore({
       id: 'main',