EvalueTonSavoir/client/src/__tests__/components/QuestionsDisplay/NumericalQuestionDisplay/NumericalQuestionDisplay.test.tsx

118 lines
3.5 KiB
TypeScript
Raw Normal View History

import React from 'react';
2024-03-29 20:08:34 -04:00
import { render, screen, fireEvent } from '@testing-library/react';
import '@testing-library/jest-dom';
2025-01-24 14:51:58 -05:00
import { NumericalQuestion, parse, ParsedGIFTQuestion } from 'gift-pegjs';
import { MemoryRouter } from 'react-router-dom';
2025-01-24 16:50:01 -05:00
import NumericalQuestionDisplay from 'src/components/QuestionsDisplay/NumericalQuestionDisplay/NumericalQuestionDisplay';
2025-01-24 14:51:58 -05:00
const questions = parse(
`
::Sample Question 1:: Question stem
{
#5..10
}`
) as ParsedGIFTQuestion[];
const question = questions[0] as NumericalQuestion;
describe('NumericalQuestion parse', () => {
const q = questions[0];
it('The question is Numerical', () => {
expect(q.type).toBe('Numerical');
});
});
2024-03-29 20:08:34 -04:00
describe('NumericalQuestion Component', () => {
2025-01-24 14:51:58 -05:00
const mockHandleOnSubmitAnswer = jest.fn();
2024-03-29 20:08:34 -04:00
const sampleProps = {
2025-01-24 14:51:58 -05:00
question: question,
handleOnSubmitAnswer: mockHandleOnSubmitAnswer,
2024-03-29 20:08:34 -04:00
showAnswer: false
};
beforeEach(() => {
2025-01-24 14:51:58 -05:00
render(
<MemoryRouter>
<NumericalQuestionDisplay
{...sampleProps}
/>
</MemoryRouter>);
2024-03-29 20:08:34 -04:00
});
it('renders correctly', () => {
2025-01-24 14:51:58 -05:00
expect(screen.getByText(question.formattedStem.text)).toBeInTheDocument();
2024-03-29 20:08:34 -04:00
expect(screen.getByTestId('number-input')).toBeInTheDocument();
expect(screen.getByText('Répondre')).toBeInTheDocument();
});
it('handles input change correctly', () => {
const inputElement = screen.getByTestId('number-input') as HTMLInputElement;
fireEvent.change(inputElement, { target: { value: '7' } });
expect(inputElement.value).toBe('7');
});
it('Submit button should be disable if nothing is entered', () => {
const submitButton = screen.getByText('Répondre');
expect(submitButton).toBeDisabled();
});
it('not submited answer if nothing is entered', () => {
const submitButton = screen.getByText('Répondre');
fireEvent.click(submitButton);
2025-01-24 14:51:58 -05:00
expect(mockHandleOnSubmitAnswer).not.toHaveBeenCalled();
2025-03-21 09:31:36 -04:00
mockHandleOnSubmitAnswer.mockClear();
2024-03-29 20:08:34 -04:00
});
it('submits answer correctly', () => {
const inputElement = screen.getByTestId('number-input');
const submitButton = screen.getByText('Répondre');
fireEvent.change(inputElement, { target: { value: '7' } });
fireEvent.click(submitButton);
2025-03-21 09:31:36 -04:00
expect(mockHandleOnSubmitAnswer).toHaveBeenCalledWith([7]);
mockHandleOnSubmitAnswer.mockClear();
2024-03-29 20:08:34 -04:00
});
2025-04-24 18:01:54 -04:00
it('calculates and displays correct answer rate when showResults is true', () => {
const mockStudents = [
{
id: '1',
name: 'Alice',
answers: [{ idQuestion: 1, answer: [7], isCorrect: true }]
},
{
id: '2',
name: 'Bob',
answers: [{ idQuestion: 1, answer: [3], isCorrect: false }]
},
{
id: '3',
name: 'Charlie',
answers: [{ idQuestion: 1, answer: [6], isCorrect: true }]
}
];
render(
<MemoryRouter>
<NumericalQuestionDisplay
question={{ ...question, id: '1' }}
showResults={true}
students={mockStudents}
/>
</MemoryRouter>
);
expect(screen.getByText('Taux de réponse correcte: 2/3')).toBeInTheDocument();
expect(screen.getByText('66.7%')).toBeInTheDocument();
});
2024-09-14 18:33:25 -04:00
});