Trying to create a test case for the following function. Not familiar with async and await, using Jest and Enzyme for React Js. Goal: test to pass properly And make sure the function is getting called correctly
async patchPolicy() {
const { user, services } = this.props;
const { data } = this.state;
const body = {
Policies: [
{
Choices: data.Choices || ''
}
]
};
PolicyModels.patchWork({
user,
services,
body,
id: this.state.Policy
});
const contextBody = data.WORK_CONTEXT[0];
const Context_UID = '';
const method = 'POST';
if (contextBody) {
//
if (contextBody.Context_UID) {
// If has UID, Patch
PolicyModels.patchWorkContext({
user,
services,
body: contextBody,
id: contextBody.Context_UID
});
} else {
contextBody.WorkID = this.state.data.WorkID;
PolicyModels.patchWorkContext({
user,
services,
body: contextBody,
id: contextBody.Context_UID
});
}
}
}
Created the following test for it but I keep getting this message:
expect(jest.fn()).toHaveBeenCalled() Expected mock function to have been called.
const patchWorkMock = jest.fn();
PolicyModels.patchWorkContext = patchWorkMock;
beforeEach(() => {
wrapper = mount(
<MemoryRouter keyLength={0}>
<EditPolicy {...baseProps} />
</MemoryRouter>
);
});
it('Test patchPolicy function', async () => {
wrapper.setProps({
services: {},
user: {}
});
wrapper.find('EditPolicy').setState({
data: {
Policy: {},
Description: {},
Help_UID: {},
Choices: {},
WorkID: [],
WORK_CONTEXT: []
}
});
wrapper.update();
wrapper
.find('EditPolicy')
.instance()
.patchPolicy();
expect(patchWorkMock).toHaveBeenCalled();
return wrapper
.find('EditPolicy')
.instance()
.patchPolicy()
.then(result => {
expect(result).toBeUndefined();
});
});
Expect the function to get called/ and works properly . I was able to get the test to pass but i believe it did not add anything into its coverage.