Skip to content

Commit 1ae4c45

Browse files
committed
fix(testing): properly clear effect mocks when clearing all mocks
1 parent b2b0299 commit 1ae4c45

2 files changed

Lines changed: 29 additions & 1 deletion

File tree

packages/testing/src/effects.spec.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { createEffect } from '@simplux/core'
2+
import { clearAllSimpluxMocks } from './cleanup'
23
import { mockEffect } from './effects'
34

45
describe(mockEffect.name, () => {
@@ -35,6 +36,25 @@ describe(mockEffect.name, () => {
3536
expect(mockSpy).toHaveBeenCalledTimes(1)
3637
})
3738

39+
it('can be removed all at once', () => {
40+
const spy = jest.fn()
41+
const mockSpy1 = jest.fn()
42+
const mockSpy2 = jest.fn()
43+
const effect1 = createEffect(spy)
44+
const effect2 = createEffect(spy)
45+
mockEffect(effect1, mockSpy1)
46+
mockEffect(effect2, mockSpy2)
47+
effect1()
48+
effect2()
49+
expect(mockSpy1).toHaveBeenCalledTimes(1)
50+
expect(mockSpy2).toHaveBeenCalledTimes(1)
51+
clearAllSimpluxMocks()
52+
effect1()
53+
effect2()
54+
expect(mockSpy1).toHaveBeenCalledTimes(1)
55+
expect(mockSpy2).toHaveBeenCalledTimes(1)
56+
})
57+
3858
it('can safely be removed twice', () => {
3959
const spy = jest.fn()
4060
const mockSpy1 = jest.fn()

packages/testing/src/effects.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { EffectMockDefinition, getMockDefinitionsInternal } from '@simplux/core'
2+
import { registerMockCleanupFunction } from './cleanup'
23

34
/**
45
* Specify a mock function that should be called instead of the
@@ -23,7 +24,14 @@ export function mockEffect<TEffect extends Function>(
2324
mockFn,
2425
})
2526

26-
return () => removeMock(mockDefinitions, effectToMock)
27+
const cleanup = () => {
28+
removeMock(mockDefinitions, effectToMock)
29+
clearCleanup()
30+
}
31+
32+
const clearCleanup = registerMockCleanupFunction(cleanup)
33+
34+
return cleanup
2735
}
2836

2937
function removeMock<TEffect extends Function>(

0 commit comments

Comments
 (0)