forked from Arquisoft/wiq_0
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #98 from Arquisoft/96-realizar-pruebas-de-la-funci…
…onalidad-de-amistades Prueba
- Loading branch information
Showing
5 changed files
with
263 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -167,7 +167,7 @@ app.post('/users/remove-friend', async (req, res) => { | |
// Route to get friends of the authenticated user | ||
app.get('/friends', async (req, res) => { | ||
try { | ||
const username = req.query.user; | ||
const username = req.query.user; | ||
|
||
// Buscar al usuario por su nombre de usuario | ||
const user = await User.findOne({ username }); | ||
Check failure Code scanning / SonarCloud NoSQL operations should not be vulnerable to injection attacks High
Change this code to not construct database queries directly from user-controlled data. See more on SonarCloud
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
import React from "react"; | ||
import { | ||
render, | ||
screen, | ||
waitFor, | ||
fireEvent, | ||
act, | ||
} from "@testing-library/react"; | ||
import { MemoryRouter } from "react-router-dom"; | ||
import FriendList from "./FriendsList.js"; | ||
import { I18nextProvider } from "react-i18next"; | ||
import i18n from "../../i18n.js"; | ||
|
||
const renderComponent = () => { | ||
act(() => { | ||
render( | ||
<I18nextProvider i18n={i18n}> | ||
<MemoryRouter> | ||
<FriendList /> | ||
</MemoryRouter> | ||
</I18nextProvider> | ||
); | ||
}); | ||
}; | ||
|
||
const checkFriends = async () => { | ||
await waitFor(() => { | ||
expect(screen.getByText("Friend 1")).toBeInTheDocument(); | ||
expect(screen.getByText("Friend 2")).toBeInTheDocument(); | ||
expect(screen.getByText("Friend 3")).toBeInTheDocument(); | ||
}); | ||
}; | ||
|
||
describe("FriendList Component", () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
test("renders loading state", () => { | ||
renderComponent(); | ||
|
||
expect(screen.getByText("Cargando ...")).toBeInTheDocument(); | ||
}); | ||
|
||
test("renders friend list", async () => { | ||
const mockFriends = ["Friend 1", "Friend 2", "Friend 3"]; | ||
jest.spyOn(global, "fetch").mockResolvedValueOnce({ | ||
json: jest.fn().mockResolvedValueOnce({ friends: mockFriends }), | ||
}); | ||
|
||
renderComponent(); | ||
|
||
await waitFor(() => { | ||
expect(screen.getByText("Lista de amigos")).toBeInTheDocument(); | ||
expect(screen.getByText("Friend 1")).toBeInTheDocument(); | ||
expect(screen.getByText("Friend 2")).toBeInTheDocument(); | ||
}); | ||
}); | ||
|
||
test("renders no friends message", async () => { | ||
jest.spyOn(global, "fetch").mockResolvedValueOnce({ | ||
json: jest.fn().mockResolvedValueOnce({ friends: [] }), | ||
}); | ||
|
||
renderComponent(); | ||
|
||
await waitFor(() => { | ||
expect( | ||
screen.getByText("No tienes amigos actualmente.") | ||
).toBeInTheDocument(); | ||
}); | ||
}); | ||
|
||
test("navigates to friend profile", async () => { | ||
const mockFriends = ["Friend 1", "Friend 2", "Friend 3"]; | ||
jest.spyOn(global, "fetch").mockResolvedValueOnce({ | ||
json: jest.fn().mockResolvedValueOnce({ friends: mockFriends }), | ||
}); | ||
|
||
renderComponent(); | ||
|
||
const data = { | ||
username: "admin", | ||
createdAt: "2024-01-30T14:59:11.576Z", | ||
games: [ | ||
{ | ||
correctAnswers: 5, | ||
incorrectAnswers: 4, | ||
points: 5, | ||
avgTime: 0, | ||
}, | ||
{ | ||
correctAnswers: 0, | ||
incorrectAnswers: 0, | ||
points: 0, | ||
avgTime: 0, | ||
}, | ||
], | ||
}; | ||
|
||
jest.spyOn(global, "fetch").mockResolvedValueOnce({ | ||
json: jest.fn().mockResolvedValueOnce(data), | ||
}); | ||
|
||
checkFriends(); | ||
|
||
await waitFor(() => { | ||
fireEvent.click( | ||
screen.getAllByRole("button", { name: /Ver perfil/i })[0] | ||
); | ||
}); | ||
|
||
await waitFor(() => { | ||
expect(screen.getByText("Perfil de usuario")).toBeInTheDocument(); | ||
}); | ||
|
||
fireEvent.click(screen.getByRole("button", { name: /Volver/i })); | ||
}); | ||
|
||
test("fetch returns error", async () => { | ||
global.fetch.mockRejectedValue(new Error("Failed to fetch")); | ||
jest.spyOn(global, "fetch").mockResolvedValueOnce({ ok: false }); | ||
|
||
renderComponent(); | ||
|
||
await waitFor(() => { | ||
expect( | ||
screen.getByText("No tienes amigos actualmente.") | ||
).toBeInTheDocument(); | ||
}); | ||
}); | ||
|
||
test("remove friend returns error", async () => { | ||
const mockFriends = ["Friend 1", "Friend 2", "Friend 3"]; | ||
jest.spyOn(global, "fetch").mockResolvedValueOnce({ | ||
json: jest.fn().mockResolvedValueOnce({ friends: mockFriends }), | ||
}); | ||
|
||
renderComponent(); | ||
|
||
checkFriends(); | ||
|
||
jest | ||
.spyOn(global, "fetch") | ||
.mockRejectedValueOnce(new Error("Failed to fetch")); | ||
|
||
await waitFor(() => { | ||
fireEvent.click( | ||
screen.getAllByRole("button", { name: /eliminar amigo/i })[0] | ||
); | ||
}); | ||
|
||
expect(screen.getByText("Friend 3")).toBeInTheDocument(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import React from "react"; | ||
import { render, screen, waitFor, fireEvent } from "@testing-library/react"; | ||
import UsersPage from "./UsersPage"; | ||
import { MemoryRouter } from "react-router-dom"; | ||
import { I18nextProvider } from "react-i18next"; | ||
import i18n from "../../i18n.js"; | ||
|
||
// Mock de la función fetch | ||
const mockData = [ | ||
{ _id: "1", username: "user1", isFriend: false }, | ||
{ _id: "2", username: "user2", isFriend: true }, | ||
]; | ||
|
||
// Antes de cada prueba, limpiar los mocks | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
describe("UsersPage", () => { | ||
beforeEach(() => { | ||
jest.spyOn(global, "fetch").mockResolvedValueOnce({ | ||
json: jest.fn().mockResolvedValueOnce(mockData), | ||
}); | ||
|
||
render( | ||
<I18nextProvider i18n={i18n}> | ||
<MemoryRouter> | ||
<UsersPage /> | ||
</MemoryRouter> | ||
</I18nextProvider> | ||
); | ||
}); | ||
|
||
test("renders loading message", () => { | ||
expect(screen.getByText("Cargando usuarios ...")).toBeInTheDocument(); | ||
}); | ||
|
||
test("renders error message", async () => { | ||
global.fetch.mockImplementationOnce(() => | ||
Promise.reject(new Error("Fetch error")) | ||
); | ||
|
||
await waitFor(() => { | ||
expect(screen.getByText("Amigo")).toBeInTheDocument(); | ||
}); | ||
}); | ||
|
||
test("renders user list", async () => { | ||
await waitFor(() => { | ||
expect(screen.getByText("user1")).toBeInTheDocument(); | ||
expect(screen.getByText("user2")).toBeInTheDocument(); | ||
}); | ||
}); | ||
|
||
test("adds friend successfully", async () => { | ||
await waitFor(() => { | ||
expect(screen.getByText("user1")).toBeInTheDocument(); | ||
}); | ||
|
||
fireEvent.click( | ||
screen.getAllByRole("button", { name: /Añadir amigo/i })[0] | ||
); | ||
|
||
await waitFor(() => { | ||
expect(screen.getByText("Amigo")).toBeInTheDocument(); | ||
}); | ||
}); | ||
|
||
test("handles add friend error", async () => { | ||
global.fetch.mockImplementationOnce(() => | ||
Promise.resolve({ | ||
ok: false, | ||
json: () => Promise.resolve({ error: "Add friend error" }), | ||
}) | ||
); | ||
|
||
await waitFor(() => { | ||
expect(screen.getByText("user1")).toBeInTheDocument(); | ||
}); | ||
|
||
fireEvent.click( | ||
screen.getAllByRole("button", { name: /Añadir amigo/i })[0] | ||
); | ||
|
||
await waitFor(() => { | ||
expect(screen.getByText("Amigo")).toBeInTheDocument(); | ||
}); | ||
}); | ||
|
||
}); | ||
|