})
it('can execute actions', () => {
- const { counter, wrapper } = factory({ bypassActions: false })
+ const { counter, wrapper } = factory({ stubActions: false })
counter.increment()
expect(counter.n).toBe(1)
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)
+ })
})
* 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
}
* 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.
*
*/
export function createTestingPinia({
plugins = [],
- bypassActions = true,
+ stubActions = true,
+ stubPatch = false,
createSpy,
}: TestingOptions = {}): TestingPinia {
const pinia = createPinia()
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)