Skip to content

Commit

Permalink
feat(testing): allow stubing $patch
Browse files Browse the repository at this point in the history
  • Loading branch information
posva committed Jun 28, 2021
1 parent 5359fe4 commit 10bef8a
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 5 deletions.
35 changes: 34 additions & 1 deletion __tests__/testing.spec.ts
Expand Up @@ -62,7 +62,7 @@ describe('Testing', () => {
})

it('can execute actions', () => {
const { counter, wrapper } = factory({ bypassActions: false })
const { counter, wrapper } = factory({ stubActions: false })

counter.increment()
expect(counter.n).toBe(1)
Expand All @@ -84,4 +84,37 @@ describe('Testing', () => {
expect(counter.increment).toHaveBeenCalledTimes(4)
expect(counter.increment).toHaveBeenLastCalledWith(10)
})

it('spies $patch calls', () => {
const { counter } = factory()

expect(counter.n).toBe(0)
expect(counter.$patch).toHaveBeenCalledTimes(0)
counter.$patch({ n: 1 })
expect(counter.$patch).toHaveBeenCalledTimes(1)
expect(counter.$patch).toHaveBeenLastCalledWith({ n: 1 })
expect(counter.n).toBe(1)
})

it('can stub $patch calls', () => {
const { counter } = factory({ stubPatch: true })

expect(counter.n).toBe(0)
expect(counter.$patch).toHaveBeenCalledTimes(0)
counter.$patch({ n: 1 })
expect(counter.$patch).toHaveBeenCalledTimes(1)
expect(counter.$patch).toHaveBeenLastCalledWith({ n: 1 })
expect(counter.n).toBe(0)
})

it('can stub $patch calls', () => {
const { counter } = factory({ stubPatch: true })

expect(counter.n).toBe(0)
expect(counter.$patch).toHaveBeenCalledTimes(0)
counter.$patch({ n: 1 })
expect(counter.$patch).toHaveBeenCalledTimes(1)
expect(counter.$patch).toHaveBeenLastCalledWith({ n: 1 })
expect(counter.n).toBe(0)
})
})
17 changes: 13 additions & 4 deletions src/testing.ts
Expand Up @@ -12,7 +12,13 @@ export interface TestingOptions {
* set to true, actions will be replaced with spies, resulting in their code
* not being executed. Defaults to true.
*/
bypassActions?: boolean
stubActions?: boolean

/**
* When set to true, calls to `$patch()` won't change the state. Defaults to
* false.
*/
stubPatch?: boolean

createSpy?: (fn?: (...args: any[]) => any) => (...args: any[]) => any
}
Expand All @@ -28,7 +34,7 @@ export interface TestingPinia extends Pinia {
* Creates a pinia instance designed for unit tests that **requires mocking**
* the stores. By default, **all actions are mocked** and therefore not
* executed. This allows you to unit test your store and components separately.
* You can change this with the `bypassActions` option. If you are using jest,
* You can change this with the `stubActions` option. If you are using jest,
* they are replaced with `jest.fn()`, otherwise, you must provide your own
* `createSpy` option.
*
Expand All @@ -37,7 +43,8 @@ export interface TestingPinia extends Pinia {
*/
export function createTestingPinia({
plugins = [],
bypassActions = true,
stubActions = true,
stubPatch = false,
createSpy,
}: TestingOptions = {}): TestingPinia {
const pinia = createPinia()
Expand All @@ -62,13 +69,15 @@ export function createTestingPinia({
Object.keys(options.actions || {}).forEach((action) => {
actionsCache[action] =
actionsCache[action] ||
(bypassActions
(stubActions
? createSpy!()
: // @ts-expect-error:
createSpy!(store[action]))
// @ts-expect-error:
store[action] = actionsCache[action]
})

store.$patch = stubPatch ? createSpy!() : createSpy!(store.$patch)
})

setActivePinia(pinia)
Expand Down

0 comments on commit 10bef8a

Please sign in to comment.