generated from Arquisoft/wiq_0
-
Notifications
You must be signed in to change notification settings - Fork 1
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 #238 from Arquisoft/front_end_rita
more test for webapp
- Loading branch information
Showing
5 changed files
with
155 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,15 @@ jest.mock('../../stores/user-store'); | |
jest.mock('../../services/auth-service'); | ||
|
||
describe('Statistics component', () => { | ||
let originalConsoleError; | ||
|
||
beforeAll(() => { | ||
// Guardar la función original de console.error | ||
originalConsoleError = console.error; | ||
// Sustituir console.error con una función mock | ||
console.error = jest.fn(); | ||
}); | ||
|
||
it('renders user information correctly', async () => { | ||
const mockUser = { username: 'testuser', email: '[email protected]' }; | ||
(useUserStore.getState as jest.Mock).mockReturnValue({ user: mockUser }); | ||
|
@@ -23,4 +32,22 @@ describe('Statistics component', () => { | |
expect(screen.getByText(mockUser.username)).toBeInTheDocument(); | ||
expect(screen.getByText(mockUser.email)).toBeInTheDocument(); | ||
}); | ||
}); | ||
|
||
|
||
|
||
it('handles error during user retrieval', async () => { | ||
const errorMessage = 'Failed to retrieve user'; | ||
const mockError = new Error(errorMessage); | ||
(useUserStore.getState as jest.Mock).mockReturnValue({ user: { username: 'testuser' } }); | ||
(getUser as jest.Mock).mockRejectedValue(mockError); | ||
|
||
await act(async () => { | ||
render(<Router> | ||
<Statistics /> | ||
</Router>); | ||
}); | ||
|
||
// Verifica que console.error haya sido llamado con el mensaje de error correcto | ||
expect(console.error).toHaveBeenCalledWith('Error during retrieving the user', mockError); | ||
}); | ||
}); |
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,49 @@ | ||
import axios from 'axios'; | ||
import { getQuestionsFromApi, getEasyString, getHardString } from './question-service'; | ||
|
||
// Mockear axios | ||
jest.mock('axios'); | ||
|
||
describe('getQuestionsFromApi function', () => { | ||
it('fetches questions from API and returns data', async () => { | ||
// Datos de ejemplo que quieres que devuelva el mock de axios | ||
const mockedData = [ | ||
{ | ||
text: 'Example question', | ||
answers: ['Answer 1', 'Answer 2', 'Answer 3'], | ||
correctAnswer: 0, | ||
wikiLink: 'https://example.com' | ||
} | ||
// Puedes agregar más datos de ejemplo si lo deseas | ||
]; | ||
|
||
// Mockear la función axios.get para que devuelva los datos de ejemplo | ||
(axios.get as jest.MockedFunction<typeof axios.get>).mockResolvedValueOnce({ data: mockedData }); | ||
|
||
// Llamar a la función que quieres probar | ||
const questions = await getQuestionsFromApi(); | ||
|
||
// Verificar que axios.get haya sido llamado con la URL correcta | ||
expect(axios.get).toHaveBeenCalledWith('http://localhost:8000/GetQuestions'); | ||
|
||
// Verificar que la función retorna los datos esperados | ||
expect(questions).toEqual(mockedData); | ||
}); | ||
|
||
|
||
}); | ||
|
||
describe('getEasyString function', () => { | ||
it('returns the string "easy"', () => { | ||
const result = getEasyString(); | ||
expect(result).toBe('easy'); | ||
}); | ||
}); | ||
|
||
describe('getHardString function', () => { | ||
it('returns the string "hard"', () => { | ||
const result = getHardString(); | ||
expect(result).toBe('hard'); | ||
}); | ||
|
||
}); |