From 68def403ddb4a3a5db5918d3926bee22a36b3835 Mon Sep 17 00:00:00 2001 From: iyanfdezz Date: Tue, 16 Apr 2024 19:04:18 +0200 Subject: [PATCH] Arreglado un test de GroupDetails --- webapp/src/pages/Social/GroupDetails.js | 8 ++-- webapp/src/pages/Social/GroupDetails.test.js | 42 +++++++++----------- 2 files changed, 22 insertions(+), 28 deletions(-) diff --git a/webapp/src/pages/Social/GroupDetails.js b/webapp/src/pages/Social/GroupDetails.js index f63c2d80..5fb0c60d 100644 --- a/webapp/src/pages/Social/GroupDetails.js +++ b/webapp/src/pages/Social/GroupDetails.js @@ -39,11 +39,11 @@ const GroupDetails = () => { const response = await fetch( `${apiEndpoint}/group/${encodeURIComponent(groupName)}` ); - // if (!response.status === 200) { - // throw new Error("Network response was not ok"); - // } + if (!response.status === 200) { + throw new Error("Network response was not ok"); + } const data = await response.json(); - setGroup(data.group); + setGroup(data); setError(null); } catch (error) { setError(error); diff --git a/webapp/src/pages/Social/GroupDetails.test.js b/webapp/src/pages/Social/GroupDetails.test.js index bf6b5bb8..e56c0efd 100644 --- a/webapp/src/pages/Social/GroupDetails.test.js +++ b/webapp/src/pages/Social/GroupDetails.test.js @@ -40,6 +40,20 @@ const mockNavigate = jest.fn(); useNavigate: () => mockNavigate, })); +const checks = (async () => { + await waitFor(() => { + expect(screen.getByText('Detalles del grupo exampleGroup')).toBeInTheDocument(); + expect(screen.getByText('Avatar')).toBeInTheDocument(); + expect(screen.getByText('Nombre')).toBeInTheDocument(); + const viewProfile = screen.getAllByText('Ver perfil'); + expect(viewProfile).toHaveLength(3); + expect(screen.getByTestId('user-avatar-user1')).toBeInTheDocument(); + expect(screen.getByTestId('user-avatar-user2')).toBeInTheDocument(); + expect(screen.getByText('user1')).toBeInTheDocument(); + expect(screen.getByText('user2')).toBeInTheDocument(); + }); +}); + describe('GroupDetails', () => { beforeEach(() => { localStorage.clear(); @@ -61,45 +75,25 @@ describe('GroupDetails', () => { it('renders group details when data is fetched', async () => { jest.spyOn(global, "fetch").mockResolvedValue({ - ok: true, + status: 200, json: jest.fn().mockResolvedValueOnce(groupData), }); renderComponentWithRouter(); - await waitFor(() => { - expect(screen.getByText('Detalles del grupo exampleGroup')).toBeInTheDocument(); - expect(screen.getByText('Avatar')).toBeInTheDocument(); - expect(screen.getByText('Nombre')).toBeInTheDocument(); - const viewProfile = screen.getAllByText('Ver perfil'); - expect(viewProfile).toHaveLength(3); - expect(screen.getByTestId('user-avatar-user1')).toBeInTheDocument(); - expect(screen.getByTestId('user-avatar-user2')).toBeInTheDocument(); - expect(screen.getByText('user1')).toBeInTheDocument(); - expect(screen.getByText('user2')).toBeInTheDocument(); - }); + checks(); }); it('redirects to user profile when view profile link is clicked', async () => { jest.spyOn(global, "fetch").mockResolvedValue({ - ok: true, + status: 200, json: jest.fn().mockResolvedValueOnce(groupData), }); renderComponentWithRouter(); - await waitFor(() => { - expect(screen.getByText('Detalles del grupo exampleGroup')).toBeInTheDocument(); - expect(screen.getByText('Avatar')).toBeInTheDocument(); - expect(screen.getByText('Nombre')).toBeInTheDocument(); - const viewProfile = screen.getAllByText('Ver perfil'); - expect(viewProfile).toHaveLength(3); - expect(screen.getByTestId('user-avatar-user1')).toBeInTheDocument(); - expect(screen.getByTestId('user-avatar-user2')).toBeInTheDocument(); - expect(screen.getByText('user1')).toBeInTheDocument(); - expect(screen.getByText('user2')).toBeInTheDocument(); - }); + const viewProfileButtons = screen.getByTestId('view-profile-button-user1');