I am new in testing in my project i want to test call API i have been test get call API but i have no idea how to test POST call what i did for testing get call like this in my folder mocks in file firebase.js:
export default {
get: () => {
return Promise.resolve({
data: [{
"id": "qcCK3SzECmaZAGRrHjaC",
"status": "refused",
"pct": 20,
"amount": 200,
"email": "a@a",
"name": "test2",
"vat": "40",
"fileName": "preview-facture-free-201801-pdf-1.jpg",
"date": "2002-02-02",
"commentAdmin": "pas la bonne facture",
"commentary": "test2",
"type": "Restaurants et bars",
"fileUrl": "https://firebasestorage.googleapis.com/v0/b/billable-677b6.a…f-1.jpg?alt=media&token=4df6ed2c-12c8-42a2-b013-346c1346f732"
}]
})
},
}
in the folder tests :
import firebase from "../__mocks__/firebase"
import BillsUI from "../views/BillsUI.js"
describe("Given I am connected as Employee",()=>{
describe("When I navigate to bills page",()=>{
test("fetches bills from mock API GET", async () => {
const getSpy = jest.spyOn(firebase, "get")
const bills = await firebase.get()
expect(getSpy).toHaveBeenCalledTimes(1)
expect(bills.data.length).toBe(1)
})
test("fetches bills from an API and fails with 404 message error", async () => {
firebase.get.mockImplementationOnce(() =>
Promise.reject(new Error("Erreur 404"))
)
const html = BillsUI({ error: "Erreur 404" })
document.body.innerHTML = html
const message = await screen.getByText(/Erreur 404/)
expect(message).toBeTruthy()
})
test("fetches messages from an API and fails with 500 message error", async () => {
firebase.get.mockImplementationOnce(() =>
Promise.reject(new Error("Erreur 500"))
)
const html = BillsUI({ error: "Erreur 500" })
document.body.innerHTML = html
const message = await screen.getByText(/Erreur 500/)
expect(message).toBeTruthy()
})
})
})
this how Mocking API get and testing any idea how to mock call API POST ?