Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to test Button click function in jest.
It seems like its not able to hit API from test file. I am using MSW (Mock Server Work) to mock API calls. Its look like I am loading main component and trying to access another component click. I am not able to hit API defined in handler.js.

What I have tried:

Below is the code I had tried to test.

mainComponent.js

export default function MainComponent(){
const saveApiSettings = () => {
apiCall('rest/test_api/').then(res => {
console.log(res)
});
}

return(

<commonmodule
saveapidata="{" saveapisettings}
="">
)
}


commonModule.js

export default function CommonModule() {
const saveData = () => {
props.saveApiData();
}
return (
<button
onclick="{(e)" ==""> saveData(e)}
id = "save-btn"
>
click

)
}


test.js

it('save data to DB', async () => {
act(() => {
render(<maincomponent>);
});

act(() => {
const checkSaveBtnenable = document.querySelector(`button[id = 'save-btn']`);
checkSaveBtnenable.dispatchEvent(new MouseEvent("click", { bubbles: true }));
});


handler.js

import { rest } from 'msw';

export const handlers = [
rest.post(`rest/test_api/`, async (req, res, ctx) => {
console.log(req, "Not printing");
})
]
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900