2024-09-15 00:34:41 -04:00
|
|
|
// TrueFalseQuestion.test.tsx
|
2025-01-31 15:55:37 -05:00
|
|
|
import React, { useState } from 'react';
|
2024-09-15 21:41:24 -04:00
|
|
|
import { render, fireEvent, screen, act } from '@testing-library/react';
|
2024-03-29 20:08:34 -04:00
|
|
|
import '@testing-library/jest-dom';
|
2024-09-15 21:41:24 -04:00
|
|
|
import { MemoryRouter } from 'react-router-dom';
|
2025-01-25 02:02:18 -05:00
|
|
|
import TrueFalseQuestionDisplay from 'src/components/QuestionsDisplay/TrueFalseQuestionDisplay/TrueFalseQuestionDisplay';
|
|
|
|
|
import { parse, TrueFalseQuestion } from 'gift-pegjs';
|
2025-03-08 11:05:25 -05:00
|
|
|
import { AnswerType } from 'src/pages/Student/JoinRoom/JoinRoom';
|
2024-03-29 20:08:34 -04:00
|
|
|
|
|
|
|
|
describe('TrueFalseQuestion Component', () => {
|
|
|
|
|
const mockHandleSubmitAnswer = jest.fn();
|
2025-01-25 02:02:18 -05:00
|
|
|
const sampleStem = 'Sample True False Question';
|
2025-01-31 15:55:37 -05:00
|
|
|
const trueFalseQuestion =
|
2025-01-25 02:02:18 -05:00
|
|
|
parse(`${sampleStem}{T}`)[0] as TrueFalseQuestion;
|
2024-03-29 20:08:34 -04:00
|
|
|
|
2025-01-31 15:55:37 -05:00
|
|
|
|
|
|
|
|
const TestWrapper = ({ showAnswer }: { showAnswer: boolean }) => {
|
|
|
|
|
const [showAnswerState, setShowAnswerState] = useState(showAnswer);
|
|
|
|
|
|
2025-03-08 11:05:25 -05:00
|
|
|
const handleOnSubmitAnswer = (answer: AnswerType) => {
|
2025-01-31 15:55:37 -05:00
|
|
|
mockHandleSubmitAnswer(answer);
|
|
|
|
|
setShowAnswerState(true);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<MemoryRouter>
|
|
|
|
|
<TrueFalseQuestionDisplay
|
|
|
|
|
question={trueFalseQuestion}
|
|
|
|
|
handleOnSubmitAnswer={handleOnSubmitAnswer}
|
|
|
|
|
showAnswer={showAnswerState}
|
|
|
|
|
/>
|
|
|
|
|
</MemoryRouter>
|
|
|
|
|
);
|
2024-03-29 20:08:34 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
beforeEach(() => {
|
2025-01-31 15:55:37 -05:00
|
|
|
render(<TestWrapper showAnswer={false} />);
|
2024-03-29 20:08:34 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('renders correctly', () => {
|
2024-09-15 00:34:41 -04:00
|
|
|
expect(screen.getByText(sampleStem)).toBeInTheDocument();
|
2024-03-29 20:08:34 -04:00
|
|
|
expect(screen.getByText('Vrai')).toBeInTheDocument();
|
|
|
|
|
expect(screen.getByText('Faux')).toBeInTheDocument();
|
|
|
|
|
expect(screen.getByText('Répondre')).toBeInTheDocument();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('Submit button should be disabled if no option is selected', () => {
|
|
|
|
|
const submitButton = screen.getByText('Répondre');
|
|
|
|
|
expect(submitButton).toBeDisabled();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('not submit answer if no option is selected', () => {
|
|
|
|
|
const submitButton = screen.getByText('Répondre');
|
2024-09-15 21:41:24 -04:00
|
|
|
act(() => {
|
|
|
|
|
fireEvent.click(submitButton);
|
|
|
|
|
});
|
2024-03-29 20:08:34 -04:00
|
|
|
|
|
|
|
|
expect(mockHandleSubmitAnswer).not.toHaveBeenCalled();
|
2025-03-21 09:31:36 -04:00
|
|
|
mockHandleSubmitAnswer.mockClear();
|
2024-03-29 20:08:34 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('submits answer correctly for True', () => {
|
|
|
|
|
const trueButton = screen.getByText('Vrai');
|
|
|
|
|
const submitButton = screen.getByText('Répondre');
|
|
|
|
|
|
2024-09-15 21:41:24 -04:00
|
|
|
act(() => {
|
|
|
|
|
fireEvent.click(trueButton);
|
|
|
|
|
});
|
2024-03-29 20:08:34 -04:00
|
|
|
|
2024-09-15 21:41:24 -04:00
|
|
|
act(() => {
|
|
|
|
|
fireEvent.click(submitButton);
|
|
|
|
|
});
|
2024-03-29 20:08:34 -04:00
|
|
|
|
2025-03-21 09:31:36 -04:00
|
|
|
expect(mockHandleSubmitAnswer).toHaveBeenCalledWith([true]);
|
|
|
|
|
mockHandleSubmitAnswer.mockClear();
|
2024-03-29 20:08:34 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('submits answer correctly for False', () => {
|
|
|
|
|
const falseButton = screen.getByText('Faux');
|
|
|
|
|
const submitButton = screen.getByText('Répondre');
|
2024-09-15 21:41:24 -04:00
|
|
|
act(() => {
|
|
|
|
|
fireEvent.click(falseButton);
|
|
|
|
|
});
|
|
|
|
|
act(() => {
|
|
|
|
|
fireEvent.click(submitButton);
|
|
|
|
|
});
|
2024-03-29 20:08:34 -04:00
|
|
|
|
2025-03-21 09:31:36 -04:00
|
|
|
expect(mockHandleSubmitAnswer).toHaveBeenCalledWith([false]);
|
|
|
|
|
mockHandleSubmitAnswer.mockClear();
|
2024-03-29 20:08:34 -04:00
|
|
|
});
|
2025-01-31 15:55:37 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
it('should show ✅ next to the correct answer and ❌ next to the wrong answers when showAnswer is true', async () => {
|
|
|
|
|
const choiceButton = screen.getByText('Vrai').closest('button');
|
|
|
|
|
if (!choiceButton) throw new Error('T button not found');
|
|
|
|
|
|
|
|
|
|
// Click on choiceButton
|
|
|
|
|
act(() => {
|
|
|
|
|
fireEvent.click(choiceButton);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const button = screen.getByText("Répondre");
|
|
|
|
|
|
|
|
|
|
act(() => {
|
|
|
|
|
fireEvent.click(button);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Wait for the DOM to update
|
|
|
|
|
const correctAnswer = screen.getByText("Vrai").closest('button');
|
|
|
|
|
expect(correctAnswer).toBeInTheDocument();
|
|
|
|
|
expect(correctAnswer?.textContent).toContain('✅');
|
|
|
|
|
|
|
|
|
|
const wrongAnswer1 = screen.getByText("Faux").closest('button');
|
|
|
|
|
expect(wrongAnswer1).toBeInTheDocument();
|
|
|
|
|
expect(wrongAnswer1?.textContent).toContain('❌');
|
|
|
|
|
});
|
|
|
|
|
|
2025-03-21 09:31:36 -04:00
|
|
|
it('should not show ✅ or ❌ when Répondre button is not clicked', async () => {
|
2025-01-31 15:55:37 -05:00
|
|
|
const choiceButton = screen.getByText('Vrai').closest('button');
|
|
|
|
|
if (!choiceButton) throw new Error('Choice button not found');
|
|
|
|
|
|
|
|
|
|
// Click on choiceButton
|
|
|
|
|
act(() => {
|
|
|
|
|
fireEvent.click(choiceButton);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Check for correct answer
|
|
|
|
|
const correctAnswer = screen.getByText("Vrai").closest('button');
|
|
|
|
|
expect(correctAnswer).toBeInTheDocument();
|
|
|
|
|
expect(correctAnswer?.textContent).not.toContain('✅');
|
|
|
|
|
|
|
|
|
|
// Check for wrong answers
|
|
|
|
|
const wrongAnswer1 = screen.getByText("Faux");
|
|
|
|
|
expect(wrongAnswer1).toBeInTheDocument();
|
|
|
|
|
expect(wrongAnswer1?.textContent).not.toContain('❌');
|
|
|
|
|
});
|
2024-09-15 00:34:41 -04:00
|
|
|
});
|