I'm new to JEST and I'm currently testing a Javascript component that makes an API call in its onComponentDidMount. Depending on the return data of the ajax call (api call) my component either display a table or a simple text.
My JEST test are fairly simple, for now I'm only testing to match the current snapshots. So since my api call can return different data, my snapshot can be of two different aspects : 1) one with a table 2) one with a simple text.
I successfully mocked the service like that
jest.mock("/myService", () => ({
index: (data, callback) => {
const return = [
{
{...}
},
]
callback(return)
},
}))
My component does the myService.index()
call correctly, all I wish to pass to it different values which its gonna use to make the callback.
Here's how the it looks like
it("has proper snapshot", () => {
const props = {...}
const component = shallow(<MyComponent {...props} />)
expect(component).toMatchSnapshot()
})
This works great for the first exemple but I cannot seem to find a correct answer that suits me. Can you help me ? :)
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…