-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjest-spyOn-read-only-functions.js
38 lines (30 loc) · 1.23 KB
/
jest-spyOn-read-only-functions.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
//ES6 import from other modules are read-only
//when we try to to mock a function that has been imported ( like props or regular import )
//jest will complain that the function is read-only and can not be mocked.
//Solution is to mock the function before mount the component.
//This way the function will be already mocked when the component will import it
//Example in the unit test file
//This example mocks action creators
import { saveModalItems, saveModalSelectedItems, updateModalTreeData, updateModalIsLoading } from '../../../src/common/actions';
jest.mock('../../../src/common/actions');
saveModalItems.mockImplementationOnce(() => { return {
type:null,
selectedTreeData: 1
} })
saveModalSelectedItems.mockImplementationOnce(() => { return {
type:null,
selectedTreeData: 1
} })
updateModalTreeData.mockImplementationOnce(() => { return {
type:null,
selectedTreeData: 1
} })
updateModalIsLoading.mockImplementationOnce(() => { return {
type:null,
selectedTreeData: 1
} })
//Then use it :
//call the function that will be calling he next functions here
expect(updateModalTreeData).toHaveBeenCalled()
expect(saveModalSelectedItems).toHaveBeenCalled()
expect(saveModalItems).toHaveBeenCalled()