From 0791ec65b0419bbbd5db8887141f4d2d183d237e Mon Sep 17 00:00:00 2001 From: SimaoNery Date: Wed, 7 Feb 2024 15:34:36 +0000 Subject: [PATCH] Added tests to offerService to test editOffer --- src/services/offerService.spec.js | 32 ++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/src/services/offerService.spec.js b/src/services/offerService.spec.js index 29dc4f15..3d15be6b 100644 --- a/src/services/offerService.spec.js +++ b/src/services/offerService.spec.js @@ -1,6 +1,6 @@ import config from "../config"; -import { hideOffer, disableOffer, enableOffer } from "./offerService"; +import {hideOffer, disableOffer, enableOffer, editOffer} from "./offerService"; import Constants from "../utils/Constants"; const { API_HOSTNAME } = config; @@ -8,6 +8,7 @@ describe("Offer Service", () => { const id = "60f16140fb2b9800321e2ca1"; const adminReason = "This offer is offensive."; + const data = { }; beforeEach(() => { fetch.resetMocks(); @@ -56,6 +57,23 @@ describe("Offer Service", () => { }); }); + it("Should send a POST request to edit a specific offer", async () => { + + //Simulate request success + fetch.mockResponse(JSON.stringify({mockData: true })); + + await editOffer({offerId:id}, data); + + expect(fetch).toHaveBeenCalledWith(`${API_HOSTNAME}/offers/${id}/edit`, { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + credentials: "include", + body: JSON.stringify(data), + }); + }); + it("Should handle non successful requests", async () => { const errors = [{ msg: "error1" }, { msg: "error2" }]; @@ -79,6 +97,12 @@ describe("Offer Service", () => { } catch (e) { expect(e).toStrictEqual(errors); } + + try { + await editOffer(id); + } catch (e) { + expect(e).toStrictEqual(errors); + } }); it("Should handle network error", async () => { @@ -103,5 +127,11 @@ describe("Offer Service", () => { } catch (e) { expect(e).toStrictEqual([{ msg: Constants.UNEXPECTED_ERROR_MESSAGE }]); } + + try { + await editOffer(id); + } catch (e) { + expect(e).toStrictEqual([{ msg: Constants.UNEXPECTED_ERROR_MESSAGE }]); + } }); });