2
I’m starting studies with TDD and came across a function like this:
const fetchexample = callback => {
fetch('/token', {
method: 'POST',
body: 'user=teste'
}).then(res => res.json()).then(json => {
localStorage.setItem('token', json.access_token)
}).then(() => callback())
}
But my question is, how would we test that function? Since she does an asynchronous requisition and I’ve never worked with this kind of test for this kind of requisition? Would anyone have any example on the subject or article?
Thank you @Sergio basically did what you told me. I also added a lib to help me by simulating fetch so I could test the actual function.
– Edmo
@Great! Jest is a very good library, I’m glad to have helped.
– Sergio
@Edmo if the answer solved your problem can mark as accepted
– Sergio