generated 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 #156 from Arquisoft/150-increase-code-coverage
Removed console logs
- Loading branch information
Showing
6 changed files
with
69 additions
and
9 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
69 changes: 69 additions & 0 deletions
69
webapp/src/components/questionView/QuestionGenerator.test.js
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,69 @@ | ||
import QuestionGenerator from './QuestionGenerator'; | ||
import axios from 'axios'; | ||
import Question from './Question'; | ||
|
||
jest.mock('axios'); // Mockear axios para pruebas | ||
|
||
describe('QuestionGenerator', () => { | ||
let questionGenerator; | ||
|
||
beforeEach(() => { | ||
questionGenerator = new QuestionGenerator(); // Crear una nueva instancia antes de cada prueba | ||
}); | ||
|
||
it('should generate questions with type "ALL"', async () => { | ||
const token = 'mockedToken'; | ||
const lang = 'en'; | ||
const amount = 10; | ||
|
||
// Simular respuesta exitosa de la API | ||
axios.get.mockResolvedValue({ | ||
data: [{question: "What is the population of Oviedo?", | ||
answers: ["225089","272357","267855","231841"]}], | ||
}); | ||
|
||
const questions = await questionGenerator.generateQuestions(lang, 'ALL', amount, token); | ||
|
||
// Verificar que axios.get fue llamado con la URL correcta | ||
expect(axios.get).toHaveBeenCalledWith( | ||
expect.stringMatching(/\/questions\/en\/10$/), | ||
{ headers: { 'token': 'mockedToken' } } | ||
); | ||
|
||
// Verificar que se generaron las preguntas correctas | ||
expect(questions.length).toBe(1); | ||
expect(questions[0]).toBeInstanceOf(Question); | ||
expect(questions[0].question).toBe("What is the population of Oviedo?"); | ||
}); | ||
|
||
it('should handle errors gracefully', async () => { | ||
const token = 'mockedToken'; | ||
const lang = 'en'; | ||
const amount = 10; | ||
|
||
// Simular una respuesta con error | ||
axios.get.mockRejectedValue(new Error('API request failed')); | ||
|
||
await expect(questionGenerator.generateQuestions(lang, 'ALL', amount, token)).rejects.toThrow('API request failed'); // Verificar que se lanza un error | ||
}); | ||
|
||
it('should generate competitive questions', async () => { | ||
const token = 'mockedToken'; | ||
const lang = 'en'; | ||
|
||
axios.get.mockResolvedValue({ | ||
data: [{question: "What is the population of Oviedo?", | ||
answers: ["225089","272357","267855","231841"]}], | ||
}); | ||
|
||
const questions = await questionGenerator.generateQuestions(lang, 'COMPETITIVE', 0, token); | ||
|
||
expect(axios.get).toHaveBeenCalledWith( | ||
expect.stringMatching(/\/questions\/en$/), | ||
{ headers: { 'token': 'mockedToken' } } | ||
); | ||
|
||
expect(questions.length).toBe(1); | ||
expect(questions[0].question).toBe("What is the population of Oviedo?"); | ||
}); | ||
}); |
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