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 #116 from Arquisoft/groups-tests
Hechos test de Groups, UserGroups y GroupDetails
- Loading branch information
Showing
10 changed files
with
457 additions
and
59 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
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
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,110 @@ | ||
import React from 'react'; | ||
import { render, screen, waitFor, fireEvent } from "@testing-library/react"; | ||
import { MemoryRouter } from 'react-router-dom'; | ||
import GroupDetails from './GroupDetails'; | ||
import { I18nextProvider } from "react-i18next"; | ||
import i18n from "../../i18n.js"; | ||
|
||
const renderComponentWithRouter = async () => { | ||
render( | ||
<I18nextProvider i18n={i18n}> | ||
<MemoryRouter > | ||
<GroupDetails /> | ||
</MemoryRouter> | ||
</I18nextProvider> | ||
); | ||
localStorage.setItem("username", "user1"); | ||
}; | ||
|
||
let originalFetch; | ||
|
||
beforeEach(() => { | ||
originalFetch = global.fetch; | ||
global.fetch = jest.fn(); | ||
}); | ||
|
||
afterEach(() => { | ||
global.fetch = originalFetch; | ||
jest.restoreAllMocks(); | ||
}); | ||
|
||
const groupData = { | ||
name: 'exampleGroup', | ||
members: ['user1', 'user2'], | ||
createdAt: '2024-04-11T12:00:00Z', | ||
}; | ||
|
||
const mockNavigate = jest.fn(); | ||
jest.mock('react-router-dom', () => ({ | ||
...jest.requireActual('react-router-dom'), | ||
useNavigate: () => mockNavigate, | ||
})); | ||
|
||
describe('GroupDetails', () => { | ||
beforeEach(() => { | ||
localStorage.clear(); | ||
}); | ||
|
||
afterEach(() => { | ||
global.fetch.mockRestore(); | ||
}); | ||
|
||
|
||
it('renders loading text when group data is not yet fetched', () => { | ||
jest.spyOn(global, "fetch").mockResolvedValue({ | ||
json: jest.fn().mockResolvedValueOnce(groupData), | ||
}); | ||
renderComponentWithRouter(); | ||
expect(screen.getByText('Cargando...')).toBeInTheDocument(); | ||
}); | ||
|
||
it('renders group details when data is fetched', async () => { | ||
|
||
jest.spyOn(global, "fetch").mockResolvedValue({ | ||
ok: true, | ||
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(); | ||
}); | ||
}); | ||
|
||
it('redirects to user profile when view profile link is clicked', async () => { | ||
|
||
jest.spyOn(global, "fetch").mockResolvedValue({ | ||
ok: true, | ||
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'); | ||
|
||
fireEvent.click(viewProfileButtons); | ||
expect(mockNavigate).toHaveBeenCalledWith('/perfil?user=user1'); | ||
}); | ||
}); | ||
|
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
Oops, something went wrong.