Skip to content

Commit

Permalink
Added tests to offerService to test editOffer
Browse files Browse the repository at this point in the history
  • Loading branch information
SimaoNery committed Feb 7, 2024
1 parent 97f482f commit 0791ec6
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion src/services/offerService.spec.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import config from "../config";

import { hideOffer, disableOffer, enableOffer } from "./offerService";
import {hideOffer, disableOffer, enableOffer, editOffer} from "./offerService";

Check failure on line 3 in src/services/offerService.spec.js

View workflow job for this annotation

GitHub Actions / build

A space is required after '{'

Check failure on line 3 in src/services/offerService.spec.js

View workflow job for this annotation

GitHub Actions / build

A space is required before '}'
import Constants from "../utils/Constants";
const { API_HOSTNAME } = config;

describe("Offer Service", () => {

const id = "60f16140fb2b9800321e2ca1";
const adminReason = "This offer is offensive.";
const data = { };

beforeEach(() => {
fetch.resetMocks();
Expand Down Expand Up @@ -56,6 +57,23 @@ describe("Offer Service", () => {
});
});

it("Should send a POST request to edit a specific offer", async () => {

//Simulate request success

Check failure on line 62 in src/services/offerService.spec.js

View workflow job for this annotation

GitHub Actions / build

Expected exception block, space or tab after '//' in comment
fetch.mockResponse(JSON.stringify({mockData: true }));

Check failure on line 63 in src/services/offerService.spec.js

View workflow job for this annotation

GitHub Actions / build

A space is required after '{'

await editOffer({offerId:id}, data);

Check failure on line 65 in src/services/offerService.spec.js

View workflow job for this annotation

GitHub Actions / build

A space is required after '{'

Check failure on line 65 in src/services/offerService.spec.js

View workflow job for this annotation

GitHub Actions / build

Missing space before value for key 'offerId'

Check failure on line 65 in src/services/offerService.spec.js

View workflow job for this annotation

GitHub Actions / build

A space is required before '}'

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" }];

Expand All @@ -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 () => {
Expand All @@ -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 }]);
}
});
});

0 comments on commit 0791ec6

Please sign in to comment.