Here is the Component Code where i call API with getAllData method and set data to the state:
class MyComponent extends Component {
state = {
allStatus: []
}
componentDidMount() {
this.getAllData();
}
getAllData = async() => {
let res = await apiCalls(`${Config.masterUrl}/ContentState`, 'GET', {}, `/user-data`, false);
if (res) {
this.setState({allStatus: res});
}
}
}
Here are the Test cases first I call componentDidMount and call method getAllData then mock API with moxios but it doesn't mock the request.
describe('Render MyComponent Component', () => {
let wrapper;
beforeEach(() => {
wrapper = setup(initialState);
moxios.install();
});
afterEach(() => {
moxios.uninstall();
})
it("should call getAllData API Success", async(done) => {
const responseData = {
status: 200,
error: null,
data: [Array] // for example
}
await wrapper.instance().componentDidMount();
await wrapper.instance().getAllData()
moxios.wait(function() {
const request = moxios.requests.mostRecent();
request.respondWith(responseData)
expect(wrapper.instance().state.allStatus.length).not.toBe(0)
done()
})
})
})