2024-09-14 18:33:25 -04:00
|
|
|
// NumericalQuestion.test.tsx
|
2025-01-11 02:22:14 -05:00
|
|
|
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 NumericalQuestionDisplay from 'src/components/Questions/NumericalQuestionDisplay/NumericalQuestionDisplay';
|
|
|
|
|
import { NumericalQuestion, parse, ParsedGIFTQuestion } from 'gift-pegjs';
|
|
|
|
|
import { MemoryRouter } from 'react-router-dom';
|
|
|
|
|
|
|
|
|
|
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();
|
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-01-24 14:51:58 -05:00
|
|
|
expect(mockHandleOnSubmitAnswer).toHaveBeenCalledWith(7);
|
2024-03-29 20:08:34 -04:00
|
|
|
});
|
2024-09-14 18:33:25 -04:00
|
|
|
});
|