mirror of
https://github.com/ets-cfuhrman-pfe/EvalueTonSavoir.git
synced 2025-08-11 21:23:54 -04:00
[FEATURE] Ajout des rétroactions pour les questions à choix multiples et réponses courtes
Fixes #291
This commit is contained in:
parent
2de8bb0bac
commit
5d2777b485
4 changed files with 121 additions and 52 deletions
|
|
@ -1,44 +1,21 @@
|
||||||
import React from 'react';
|
|
||||||
import { render, screen, fireEvent } from '@testing-library/react';
|
|
||||||
import '@testing-library/jest-dom';
|
import '@testing-library/jest-dom';
|
||||||
import { NumericalQuestion, parse, ParsedGIFTQuestion } from 'gift-pegjs';
|
import { render, screen, fireEvent } from '@testing-library/react';
|
||||||
import { MemoryRouter } from 'react-router-dom';
|
import { parse, NumericalQuestion } from 'gift-pegjs';
|
||||||
|
import React from 'react';
|
||||||
import NumericalQuestionDisplay from 'src/components/QuestionsDisplay/NumericalQuestionDisplay/NumericalQuestionDisplay';
|
import NumericalQuestionDisplay from 'src/components/QuestionsDisplay/NumericalQuestionDisplay/NumericalQuestionDisplay';
|
||||||
|
|
||||||
const questions = parse(
|
describe('NumericalQuestionDisplay Component', () => {
|
||||||
`
|
|
||||||
::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');
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('NumericalQuestion Component', () => {
|
|
||||||
const mockHandleOnSubmitAnswer = jest.fn();
|
const mockHandleOnSubmitAnswer = jest.fn();
|
||||||
|
const question =
|
||||||
|
parse('::Sample Numerical Question:: What is 2+2? {#4}')[0] as NumericalQuestion;
|
||||||
|
|
||||||
const sampleProps = {
|
const sampleProps = {
|
||||||
question: question,
|
|
||||||
handleOnSubmitAnswer: mockHandleOnSubmitAnswer,
|
handleOnSubmitAnswer: mockHandleOnSubmitAnswer,
|
||||||
showAnswer: false
|
showAnswer: false
|
||||||
};
|
};
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
render(
|
render(<NumericalQuestionDisplay question={question} {...sampleProps} />);
|
||||||
<MemoryRouter>
|
|
||||||
<NumericalQuestionDisplay
|
|
||||||
{...sampleProps}
|
|
||||||
/>
|
|
||||||
</MemoryRouter>);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('renders correctly', () => {
|
it('renders correctly', () => {
|
||||||
|
|
@ -55,13 +32,13 @@ describe('NumericalQuestion Component', () => {
|
||||||
expect(inputElement.value).toBe('7');
|
expect(inputElement.value).toBe('7');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Submit button should be disable if nothing is entered', () => {
|
it('Submit button should be disabled if nothing is entered', () => {
|
||||||
const submitButton = screen.getByText('Répondre');
|
const submitButton = screen.getByText('Répondre');
|
||||||
|
|
||||||
expect(submitButton).toBeDisabled();
|
expect(submitButton).toBeDisabled();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('not submited answer if nothing is entered', () => {
|
it('does not submit answer if nothing is entered', () => {
|
||||||
const submitButton = screen.getByText('Répondre');
|
const submitButton = screen.getByText('Répondre');
|
||||||
|
|
||||||
fireEvent.click(submitButton);
|
fireEvent.click(submitButton);
|
||||||
|
|
@ -79,4 +56,52 @@ describe('NumericalQuestion Component', () => {
|
||||||
|
|
||||||
expect(mockHandleOnSubmitAnswer).toHaveBeenCalledWith(7);
|
expect(mockHandleOnSubmitAnswer).toHaveBeenCalledWith(7);
|
||||||
});
|
});
|
||||||
});
|
|
||||||
|
it('renders correctly with the correct answer shown', () => {
|
||||||
|
render(<NumericalQuestionDisplay question={question} {...sampleProps} showAnswer={true} passedAnswer={4} />);
|
||||||
|
expect(screen.getByText('Réponse(s) accepté(es):')).toBeInTheDocument();
|
||||||
|
expect(screen.getAllByText('4')).toHaveLength(2);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('handles input change and checks if the answer is correct', () => {
|
||||||
|
const inputElement = screen.getByTestId('number-input') as HTMLInputElement;
|
||||||
|
|
||||||
|
fireEvent.change(inputElement, { target: { value: '4' } });
|
||||||
|
|
||||||
|
expect(inputElement.value).toBe('4');
|
||||||
|
|
||||||
|
const submitButton = screen.getByText('Répondre');
|
||||||
|
fireEvent.click(submitButton);
|
||||||
|
|
||||||
|
expect(mockHandleOnSubmitAnswer).toHaveBeenCalledWith(4);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('submits the correct answer', () => {
|
||||||
|
const inputElement = screen.getByTestId('number-input') as HTMLInputElement;
|
||||||
|
|
||||||
|
fireEvent.change(inputElement, { target: { value: '4' } });
|
||||||
|
|
||||||
|
const submitButton = screen.getByText('Répondre');
|
||||||
|
fireEvent.click(submitButton);
|
||||||
|
|
||||||
|
expect(mockHandleOnSubmitAnswer).toHaveBeenCalledWith(4);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('submits an incorrect answer', () => {
|
||||||
|
const inputElement = screen.getByTestId('number-input') as HTMLInputElement;
|
||||||
|
|
||||||
|
fireEvent.change(inputElement, { target: { value: '5' } });
|
||||||
|
|
||||||
|
const submitButton = screen.getByText('Répondre');
|
||||||
|
fireEvent.click(submitButton);
|
||||||
|
|
||||||
|
expect(mockHandleOnSubmitAnswer).toHaveBeenCalledWith(5);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('displays feedback when the answer is shown', () => {
|
||||||
|
render(<NumericalQuestionDisplay question={question} {...sampleProps} showAnswer={true} passedAnswer={5} />);
|
||||||
|
expect(screen.getByText('❌ Incorrect!')).toBeInTheDocument();
|
||||||
|
expect(screen.getByText('Réponse(s) accepté(es):')).toBeInTheDocument();
|
||||||
|
expect(screen.getByText('5')).toBeInTheDocument();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
import React from 'react';
|
|
||||||
import { render, screen, fireEvent, within } from '@testing-library/react';
|
|
||||||
import '@testing-library/jest-dom';
|
import '@testing-library/jest-dom';
|
||||||
|
import { render, screen, fireEvent } from '@testing-library/react';
|
||||||
import { parse, ShortAnswerQuestion } from 'gift-pegjs';
|
import { parse, ShortAnswerQuestion } from 'gift-pegjs';
|
||||||
|
import React from 'react';
|
||||||
import ShortAnswerQuestionDisplay from 'src/components/QuestionsDisplay/ShortAnswerQuestionDisplay/ShortAnswerQuestionDisplay';
|
import ShortAnswerQuestionDisplay from 'src/components/QuestionsDisplay/ShortAnswerQuestionDisplay/ShortAnswerQuestionDisplay';
|
||||||
|
|
||||||
describe('ShortAnswerQuestion Component', () => {
|
describe('ShortAnswerQuestion Component', () => {
|
||||||
|
|
@ -20,28 +20,26 @@ describe('ShortAnswerQuestion Component', () => {
|
||||||
|
|
||||||
it('renders correctly', () => {
|
it('renders correctly', () => {
|
||||||
expect(screen.getByText(question.formattedStem.text)).toBeInTheDocument();
|
expect(screen.getByText(question.formattedStem.text)).toBeInTheDocument();
|
||||||
const container = screen.getByLabelText('short-answer-input');
|
const inputElement = screen.getByRole('textbox') as HTMLInputElement;
|
||||||
const inputElement = within(container).getByRole('textbox') as HTMLInputElement;
|
|
||||||
expect(inputElement).toBeInTheDocument();
|
expect(inputElement).toBeInTheDocument();
|
||||||
expect(screen.getByText('Répondre')).toBeInTheDocument();
|
expect(screen.getByText('Répondre')).toBeInTheDocument();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('handles input change correctly', () => {
|
it('handles input change correctly', () => {
|
||||||
const container = screen.getByLabelText('short-answer-input');
|
const inputElement = screen.getByRole('textbox') as HTMLInputElement;
|
||||||
const inputElement = within(container).getByRole('textbox') as HTMLInputElement;
|
|
||||||
|
|
||||||
fireEvent.change(inputElement, { target: { value: 'User Input' } });
|
fireEvent.change(inputElement, { target: { value: 'User Input' } });
|
||||||
|
|
||||||
expect(inputElement.value).toBe('User Input');
|
expect(inputElement.value).toBe('User Input');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Submit button should be disable if nothing is entered', () => {
|
it('Submit button should be disabled if nothing is entered', () => {
|
||||||
const submitButton = screen.getByText('Répondre');
|
const submitButton = screen.getByText('Répondre');
|
||||||
|
|
||||||
expect(submitButton).toBeDisabled();
|
expect(submitButton).toBeDisabled();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('not submitted answer if nothing is entered', () => {
|
it('does not submit answer if nothing is entered', () => {
|
||||||
const submitButton = screen.getByText('Répondre');
|
const submitButton = screen.getByText('Répondre');
|
||||||
|
|
||||||
fireEvent.click(submitButton);
|
fireEvent.click(submitButton);
|
||||||
|
|
@ -49,17 +47,51 @@ describe('ShortAnswerQuestion Component', () => {
|
||||||
expect(mockHandleSubmitAnswer).not.toHaveBeenCalled();
|
expect(mockHandleSubmitAnswer).not.toHaveBeenCalled();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('submits answer correctly', () => {
|
it('renders correctly with the correct answer shown', () => {
|
||||||
const container = screen.getByLabelText('short-answer-input');
|
render(<ShortAnswerQuestionDisplay question={question} {...sampleProps} showAnswer={true} passedAnswer="Correct Answer" />);
|
||||||
const inputElement = within(container).getByRole('textbox') as HTMLInputElement;
|
expect(screen.getByText('Réponse(s) accepté(es):')).toBeInTheDocument();
|
||||||
|
expect(screen.getByText('Correct Answer')).toBeInTheDocument();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('handles input change and checks if the answer is correct', () => {
|
||||||
|
const inputElement = screen.getByRole('textbox') as HTMLInputElement;
|
||||||
|
|
||||||
|
fireEvent.change(inputElement, { target: { value: 'Correct Answer' } });
|
||||||
|
|
||||||
|
expect(inputElement.value).toBe('Correct Answer');
|
||||||
|
|
||||||
// const inputElement = screen.getByRole('textbox', { name: 'short-answer-input'}) as HTMLInputElement;
|
|
||||||
const submitButton = screen.getByText('Répondre');
|
const submitButton = screen.getByText('Répondre');
|
||||||
|
|
||||||
fireEvent.change(inputElement, { target: { value: 'User Input' } });
|
|
||||||
|
|
||||||
fireEvent.click(submitButton);
|
fireEvent.click(submitButton);
|
||||||
|
|
||||||
expect(mockHandleSubmitAnswer).toHaveBeenCalledWith('User Input');
|
expect(mockHandleSubmitAnswer).toHaveBeenCalledWith('Correct Answer');
|
||||||
});
|
});
|
||||||
});
|
|
||||||
|
it('submits the correct answer', () => {
|
||||||
|
const inputElement = screen.getByRole('textbox') as HTMLInputElement;
|
||||||
|
|
||||||
|
fireEvent.change(inputElement, { target: { value: 'Correct Answer' } });
|
||||||
|
|
||||||
|
const submitButton = screen.getByText('Répondre');
|
||||||
|
fireEvent.click(submitButton);
|
||||||
|
|
||||||
|
expect(mockHandleSubmitAnswer).toHaveBeenCalledWith('Correct Answer');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('submits an incorrect answer', () => {
|
||||||
|
const inputElement = screen.getByRole('textbox') as HTMLInputElement;
|
||||||
|
|
||||||
|
fireEvent.change(inputElement, { target: { value: 'Incorrect Answer' } });
|
||||||
|
|
||||||
|
const submitButton = screen.getByText('Répondre');
|
||||||
|
fireEvent.click(submitButton);
|
||||||
|
|
||||||
|
expect(mockHandleSubmitAnswer).toHaveBeenCalledWith('Incorrect Answer');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('displays feedback when the answer is shown', () => {
|
||||||
|
render(<ShortAnswerQuestionDisplay question={question} {...sampleProps} showAnswer={true} passedAnswer="Incorrect Answer" />);
|
||||||
|
expect(screen.getByText('❌ Incorrect!')).toBeInTheDocument();
|
||||||
|
expect(screen.getByText('Réponse(s) accepté(es):')).toBeInTheDocument();
|
||||||
|
expect(screen.getByText('Incorrect Answer')).toBeInTheDocument();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
@ -66,6 +66,18 @@ describe('TeacherModeQuiz', () => {
|
||||||
expect(mockSubmitAnswer).toHaveBeenCalledWith('Option A', 1);
|
expect(mockSubmitAnswer).toHaveBeenCalledWith('Option A', 1);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
test('handles shows feedback for answered question', () => {
|
||||||
|
act(() => {
|
||||||
|
fireEvent.click(screen.getByText('Option B'));
|
||||||
|
});
|
||||||
|
act(() => {
|
||||||
|
fireEvent.click(screen.getByText('Répondre'));
|
||||||
|
});
|
||||||
|
expect(mockSubmitAnswer).toHaveBeenCalledWith('Option B', 1)
|
||||||
|
expect(screen.getByText('❌ Incorrect!')).toBeInTheDocument();
|
||||||
|
});
|
||||||
|
|
||||||
test('handles shows feedback for an already answered question', () => {
|
test('handles shows feedback for an already answered question', () => {
|
||||||
// Answer the first question
|
// Answer the first question
|
||||||
act(() => {
|
act(() => {
|
||||||
|
|
@ -106,7 +118,7 @@ describe('TeacherModeQuiz', () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
// Check if the feedback dialog is shown again
|
// Check if the feedback dialog is shown again
|
||||||
expect(screen.getByText('Rétroaction')).toBeInTheDocument();
|
expect(screen.getByText('❌ Incorrect!')).toBeInTheDocument();
|
||||||
});
|
});
|
||||||
|
|
||||||
test('handles disconnect button click', () => {
|
test('handles disconnect button click', () => {
|
||||||
|
|
|
||||||
|
|
@ -31,7 +31,7 @@ const ShortAnswerQuestionDisplay: React.FC<Props> = (props) => {
|
||||||
}, [answer]);
|
}, [answer]);
|
||||||
|
|
||||||
const checkAnswer = () => {
|
const checkAnswer = () => {
|
||||||
const isCorrect = question.choices.some((choice) => choice.text.toLowerCase() === (answer as String).toLowerCase());
|
const isCorrect = question.choices.some((choice) => String(choice.text).toLowerCase() === String(answer).toLowerCase());
|
||||||
setisGoodAnswer(isCorrect);
|
setisGoodAnswer(isCorrect);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue