Skip to content

Commit

Permalink
Menos código duplicado
Browse files Browse the repository at this point in the history
  • Loading branch information
CANCI0 committed Apr 7, 2024
1 parent b3108f6 commit d9aa210
Showing 1 changed file with 19 additions and 27 deletions.
46 changes: 19 additions & 27 deletions webapp/src/pages/Social/FriendsList.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,15 @@ const renderComponent = () => {
</I18nextProvider>
);
});
}
};

const checkFriends = async (friends) => {
await waitFor(() => {
expect(screen.getByText("Friend 1")).toBeInTheDocument();
expect(screen.getByText("Friend 2")).toBeInTheDocument();
expect(screen.getByText("Friend 3")).toBeInTheDocument();
});
};

describe("FriendList Component", () => {
beforeEach(() => {
Expand All @@ -42,11 +50,7 @@ describe("FriendList Component", () => {

renderComponent();

await waitFor(() => {
expect(screen.getByText("Lista de amigos")).toBeInTheDocument();
expect(screen.getByText("Friend 1")).toBeInTheDocument();
expect(screen.getByText("Friend 2")).toBeInTheDocument();
});
await checkFriends();
});

test("renders no friends message", async () => {
Expand Down Expand Up @@ -94,11 +98,7 @@ describe("FriendList Component", () => {
json: jest.fn().mockResolvedValueOnce(data),
});

await waitFor(() => {
expect(screen.getByText("Friend 1")).toBeInTheDocument();
expect(screen.getByText("Friend 2")).toBeInTheDocument();
expect(screen.getByText("Friend 3")).toBeInTheDocument();
});
await checkFriends();

fireEvent.click(screen.getAllByRole("button", { name: /Ver perfil/i })[0]);

Expand All @@ -118,19 +118,13 @@ describe("FriendList Component", () => {

renderComponent();

await waitFor(() => {
expect(screen.getByText("Friend 1")).toBeInTheDocument();
expect(screen.getByText("Friend 2")).toBeInTheDocument();
expect(screen.getByText("Friend 3")).toBeInTheDocument();
});
await checkFriends();

fireEvent.click(
screen.getAllByRole("button", { name: /eliminar amigo/i })[0]
);

expect(screen.queryByText("Friend 1")).toBeInTheDocument();
expect(screen.getByText("Friend 2")).toBeInTheDocument();
expect(screen.getByText("Friend 3")).toBeInTheDocument();
await checkFriends();
});

test("fetch returns error", async () => {
Expand All @@ -151,16 +145,14 @@ describe("FriendList Component", () => {
jest.spyOn(global, "fetch").mockResolvedValueOnce({
json: jest.fn().mockResolvedValueOnce({ friends: mockFriends }),
});

renderComponent();

await waitFor(() => {
expect(screen.getByText("Friend 1")).toBeInTheDocument();
expect(screen.getByText("Friend 2")).toBeInTheDocument();
expect(screen.getByText("Friend 3")).toBeInTheDocument();
});

jest.spyOn(global, "fetch").mockRejectedValueOnce(new Error("Failed to fetch"));
await checkFriends();

jest
.spyOn(global, "fetch")
.mockRejectedValueOnce(new Error("Failed to fetch"));

fireEvent.click(
screen.getAllByRole("button", { name: /eliminar amigo/i })[0]
Expand Down

0 comments on commit d9aa210

Please sign in to comment.