Skip to content

Commit

Permalink
tests: adiciona teste do membershipService
Browse files Browse the repository at this point in the history
  • Loading branch information
clara-ribeiro committed Dec 17, 2024
1 parent 1148449 commit 535dbe5
Showing 1 changed file with 102 additions and 0 deletions.
102 changes: 102 additions & 0 deletions src/Services/memberShipService.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import { describe, it, expect, vi, beforeEach } from "vitest";
import {
createMemberShip,
getMemberShip,
getMemberShipById,
updateMemberStatus,
updateMembership,
deleteMember,
} from "./memberShipService";
import { APIUsers } from "./BaseService";

vi.mock("../services/BaseService", () => ({
APIUsers: {
post: vi.fn(),
get: vi.fn(),
patch: vi.fn(),
delete: vi.fn(),
},
}));

describe("MembershipService", () => {
beforeEach(() => {
vi.clearAllMocks();
});

it("deve retornar o status correto ao criar uma filiação", async () => {
APIUsers.post.mockResolvedValueOnce({ status: 201 });

Check failure on line 27 in src/Services/memberShipService.test.js

View workflow job for this annotation

GitHub Actions / test

src/Services/memberShipService.test.js > MembershipService > deve retornar o status correto ao criar uma filiação

TypeError: APIUsers.post.mockResolvedValueOnce is not a function ❯ src/Services/memberShipService.test.js:27:19

const result = await createMemberShip({ name: "John Doe" });

expect(result).toBe(201);
expect(APIUsers.post).toHaveBeenCalledWith("membership/create", {
formData: { name: "John Doe" },
});
});

it("deve retornar dados da filiação com base no status", async () => {
const mockData = [{ id: 1, status: "active" }];
APIUsers.get.mockResolvedValueOnce({ data: mockData });

Check failure on line 39 in src/Services/memberShipService.test.js

View workflow job for this annotation

GitHub Actions / test

src/Services/memberShipService.test.js > MembershipService > deve retornar dados da filiação com base no status

TypeError: APIUsers.get.mockResolvedValueOnce is not a function ❯ src/Services/memberShipService.test.js:39:18

const result = await getMemberShip("active");

expect(result).toEqual(mockData);
expect(APIUsers.get).toHaveBeenCalledWith("membership", {
params: { status: "active" },
});
});

it("deve retornar dados da filiação pelo ID", async () => {
const mockData = { id: 1, name: "John Doe" };
APIUsers.get.mockResolvedValueOnce({ data: mockData });

Check failure on line 51 in src/Services/memberShipService.test.js

View workflow job for this annotation

GitHub Actions / test

src/Services/memberShipService.test.js > MembershipService > deve retornar dados da filiação pelo ID

TypeError: APIUsers.get.mockResolvedValueOnce is not a function ❯ src/Services/memberShipService.test.js:51:18

const result = await getMemberShipById(1);

expect(result).toEqual(mockData);
expect(APIUsers.get).toHaveBeenCalledWith("membership/1");
});

it("deve atualizar o status da filiação e retornar dados", async () => {
const mockResponse = { success: true };
APIUsers.patch.mockResolvedValueOnce({ data: mockResponse });

Check failure on line 61 in src/Services/memberShipService.test.js

View workflow job for this annotation

GitHub Actions / test

src/Services/memberShipService.test.js > MembershipService > deve atualizar o status da filiação e retornar dados

TypeError: APIUsers.patch.mockResolvedValueOnce is not a function ❯ src/Services/memberShipService.test.js:61:20

const result = await updateMemberStatus(1, { status: "inactive" });

expect(result).toEqual(mockResponse);
expect(APIUsers.patch).toHaveBeenCalledWith("membership/updateStatus/1", {
formData: { status: "inactive" },
});
});

it("deve retornar false quando atualizar a filiação com sucesso", async () => {
APIUsers.patch.mockResolvedValueOnce({});

Check failure on line 72 in src/Services/memberShipService.test.js

View workflow job for this annotation

GitHub Actions / test

src/Services/memberShipService.test.js > MembershipService > deve retornar false quando atualizar a filiação com sucesso

TypeError: APIUsers.patch.mockResolvedValueOnce is not a function ❯ src/Services/memberShipService.test.js:72:20

const result = await updateMembership(1, { name: "John Updated" });

expect(result).toBe(false);
expect(APIUsers.patch).toHaveBeenCalledWith("membership/update/1", {
formData: { name: "John Updated" },
});
});

it("deve retornar false ao deletar a filiação com sucesso", async () => {
APIUsers.delete.mockResolvedValueOnce({});

Check failure on line 83 in src/Services/memberShipService.test.js

View workflow job for this annotation

GitHub Actions / test

src/Services/memberShipService.test.js > MembershipService > deve retornar false ao deletar a filiação com sucesso

TypeError: APIUsers.delete.mockResolvedValueOnce is not a function ❯ src/Services/memberShipService.test.js:83:21

const result = await deleteMember(1);

expect(result).toBe(false);
expect(APIUsers.delete).toHaveBeenCalledWith("membership/delete/1");
});

it("deve retornar erro ao falhar no createMemberShip", async () => {
const mockError = { response: { data: { erro: "Erro na requisição" } } };
APIUsers.post.mockRejectedValueOnce(mockError);

Check failure on line 93 in src/Services/memberShipService.test.js

View workflow job for this annotation

GitHub Actions / test

src/Services/memberShipService.test.js > MembershipService > deve retornar erro ao falhar no createMemberShip

TypeError: APIUsers.post.mockRejectedValueOnce is not a function ❯ src/Services/memberShipService.test.js:93:19

const result = await createMemberShip({ name: "John Doe" });

expect(result).toBe("Erro na requisição");
expect(APIUsers.post).toHaveBeenCalledWith("membership/create", {
formData: { name: "John Doe" },
});
});
});

0 comments on commit 535dbe5

Please sign in to comment.