Jest.js Vue 3 Pina尝试测试依赖于另一个存储的Pina存储

icomxhvb  于 2022-12-08  发布在  Jest
关注(0)|答案(1)|浏览(258)

我基本上有一个存储,依赖于另一个存储,我没有看到一种方法,只模仿依赖存储.示例伪代码vue 3 ish:

// the store I want to mock
export const useStore1 = defineStore({
    id: 'store1',
    state: (): State => ({
  
        someName:'blarg', // I know this is static but lets pretend it can change.
    }),
    // getter I want to mock
    getters: {
        name: (state) => state.someName,
    }
}

// store I want to test

export const useStoreTwo = defineStore({
    id: 'store2',
    state: (): State => ({
  
       someValue:'bar'
    }),
    getters: {
        value: (state) => {
        const store1 = useStore1() // dependency here
        return `${store1.name} state.someValue`
        },
    }
}

test
it('should return something' () => {
      //**** 
         someplace I would mock useStateOne and have it return
         a stub store with the getter name that returns 'foo'
      ***//
      const store2 = useStoreTwo();
      expect(store2.value).toBe('foo bar');
})
f1tvaqid

f1tvaqid1#

这对你很有帮助。
https://pinia.vuejs.org/cookbook/testing.html#mocking-getters
默认情况下,将像常规用法一样计算任何getter**,但您可以通过将getter设置为任何所需的值来手动强制指定值**

import { createTestingPinia } from '@pinia/testing';

const pinia = createTestingPinia()
    
it('should return something' () => {
   const store1 = useStore1(pinia);
   store1.name = "foobar";
    
   const store2 = useStoreTwo();
   expect(store2.value).toBe('foobar');
})

相关问题