|
| 1 | +import { describe, expect, it, vi, type Mock } from 'vitest' |
| 2 | +import { computed, defineComponent, ref, type UnwrapRef } from 'vue' |
| 3 | +import { defineStore, type Store, type StoreDefinition } from 'pinia' |
| 4 | +import { TestingOptions, createTestingPinia } from './testing' |
| 5 | +import { mount } from '@vue/test-utils' |
| 6 | + |
| 7 | +function mockedStore<TStoreDef extends () => unknown>( |
| 8 | + useStore: TStoreDef |
| 9 | +): TStoreDef extends StoreDefinition< |
| 10 | + infer Id, |
| 11 | + infer State, |
| 12 | + infer Getters, |
| 13 | + infer Actions |
| 14 | +> |
| 15 | + ? Store< |
| 16 | + Id, |
| 17 | + State, |
| 18 | + Record<string, never>, |
| 19 | + { |
| 20 | + [K in keyof Actions]: Actions[K] extends (...args: any[]) => any |
| 21 | + ? // 👇 depends on your testing framework |
| 22 | + Mock<Actions[K]> |
| 23 | + : Actions[K] |
| 24 | + } |
| 25 | + > & { |
| 26 | + [K in keyof Getters]: UnwrapRef<Getters[K]> |
| 27 | + } |
| 28 | + : ReturnType<TStoreDef> { |
| 29 | + return useStore() as any |
| 30 | +} |
| 31 | + |
| 32 | +describe('mockedStore', () => { |
| 33 | + const useCounter = defineStore('counter-setup', () => { |
| 34 | + const n = ref(0) |
| 35 | + const doubleComputedCallCount = ref(0) |
| 36 | + const double = computed(() => { |
| 37 | + doubleComputedCallCount.value++ |
| 38 | + return n.value * 2 |
| 39 | + }) |
| 40 | + const doublePlusOne = computed(() => double.value + 1) |
| 41 | + function increment(amount = 1) { |
| 42 | + n.value += amount |
| 43 | + } |
| 44 | + function decrement() { |
| 45 | + n.value-- |
| 46 | + } |
| 47 | + function setValue(newValue: number) { |
| 48 | + n.value = newValue |
| 49 | + } |
| 50 | + function $reset() { |
| 51 | + n.value = 0 |
| 52 | + } |
| 53 | + |
| 54 | + return { |
| 55 | + n, |
| 56 | + doubleComputedCallCount, |
| 57 | + double, |
| 58 | + doublePlusOne, |
| 59 | + increment, |
| 60 | + decrement, |
| 61 | + setValue, |
| 62 | + $reset, |
| 63 | + } |
| 64 | + }) |
| 65 | + |
| 66 | + const Counter = defineComponent({ |
| 67 | + setup() { |
| 68 | + const counter = useCounter() |
| 69 | + return { counter } |
| 70 | + }, |
| 71 | + template: ` |
| 72 | + <button @click="counter.increment()">+1</button> |
| 73 | + <span>{{ counter.n }}</span> |
| 74 | + <button @click="counter.increment(10)">+10</button> |
| 75 | + `, |
| 76 | + }) |
| 77 | + |
| 78 | + function factory(options?: TestingOptions) { |
| 79 | + const wrapper = mount(Counter, { |
| 80 | + global: { |
| 81 | + plugins: [createTestingPinia(options)], |
| 82 | + }, |
| 83 | + }) |
| 84 | + |
| 85 | + const counter = mockedStore(useCounter) |
| 86 | + |
| 87 | + return { wrapper, counter } |
| 88 | + } |
| 89 | + |
| 90 | + it('can unstub actions', () => { |
| 91 | + const { counter } = factory({ |
| 92 | + stubActions: false, |
| 93 | + createSpy: (fn) => vi.fn(fn).mockImplementation(() => {}), |
| 94 | + }) |
| 95 | + counter.increment() |
| 96 | + expect(counter.increment).toHaveBeenCalledTimes(1) |
| 97 | + expect(counter.n).toBe(0) |
| 98 | + |
| 99 | + counter.increment.mockRestore() |
| 100 | + counter.increment() |
| 101 | + expect(counter.increment).toHaveBeenCalledTimes(1) |
| 102 | + expect(counter.n).toBe(1) |
| 103 | + }) |
| 104 | +}) |
0 commit comments