This commit is contained in:
Juba.M 2025-03-19 16:26:21 -04:00 committed by GitHub
commit 3e4a83465f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 149 additions and 57 deletions

View file

@ -293,4 +293,65 @@ describe('ManageRoom', () => {
expect(screen.queryByText('Student 1')).not.toBeInTheDocument(); expect(screen.queryByText('Student 1')).not.toBeInTheDocument();
}); });
}); });
test('initializes isQuestionShown based on quizMode', async() => {
render(<MemoryRouter>
<ManageRoom />
</MemoryRouter>);
await act(async () => {
const createSuccessCallback = (mockSocket.on as jest.Mock).mock.calls.find(call => call[0] === 'create-success')[1];
createSuccessCallback('Test Room');
});
await waitFor(() => {
expect(ApiService.getQuiz).toHaveBeenCalledWith('test-quiz-id');
});
const launchButton = screen.getByText('Lancer');
fireEvent.click(launchButton);
const rythmeButton = screen.getByText(`Rythme de l'étudiant`);
fireEvent.click(rythmeButton);
const secondLaunchButton = screen.getAllByText('Lancer');
fireEvent.click(secondLaunchButton[1]);
expect(screen.queryByText('Question')).not.toBeInTheDocument();
});
test('renders the close button when quizMode is student and isQuestionShown is true', async() => {
render(<MemoryRouter>
<ManageRoom />
</MemoryRouter>);
await act(async () => {
const createSuccessCallback = (mockSocket.on as jest.Mock).mock.calls.find(call => call[0] === 'create-success')[1];
createSuccessCallback('Test Room');
});
await waitFor(() => {
expect(ApiService.getQuiz).toHaveBeenCalledWith('test-quiz-id');
});
const launchButton = screen.getByText('Lancer');
fireEvent.click(launchButton);
const rythmeButton = screen.getByText(`Rythme de l'étudiant`);
fireEvent.click(rythmeButton);
const secondLaunchButton = screen.getAllByText('Lancer');
fireEvent.click(secondLaunchButton[1]);
const tableHeader = screen.getByText('Q1');
fireEvent.click(tableHeader);
const questionVisibilitySwitch = screen.getByTestId('question-visibility-switch'); // Get the specific switch
expect(screen.getByText(/Question 1\//i)).toBeInTheDocument();
fireEvent.click(questionVisibilitySwitch);
expect(screen.queryByRole('button', { name: /✖/i })).not.toBeInTheDocument();
expect(screen.queryByText(/Question 1\//i)).not.toBeInTheDocument();
});
}); });

View file

@ -23,7 +23,7 @@ import DisconnectButton from 'src/components/DisconnectButton/DisconnectButton';
import QuestionDisplay from 'src/components/QuestionsDisplay/QuestionDisplay'; import QuestionDisplay from 'src/components/QuestionsDisplay/QuestionDisplay';
import ApiService from '../../../services/ApiService'; import ApiService from '../../../services/ApiService';
import { QuestionType } from 'src/Types/QuestionType'; import { QuestionType } from 'src/Types/QuestionType';
import { Button } from '@mui/material'; import { Button, FormControlLabel, Switch } from '@mui/material';
import { AnswerType } from 'src/pages/Student/JoinRoom/JoinRoom'; import { AnswerType } from 'src/pages/Student/JoinRoom/JoinRoom';
const ManageRoom: React.FC = () => { const ManageRoom: React.FC = () => {
@ -39,6 +39,7 @@ const ManageRoom: React.FC = () => {
const [quizStarted, setQuizStarted] = useState<boolean>(false); const [quizStarted, setQuizStarted] = useState<boolean>(false);
const [formattedRoomName, setFormattedRoomName] = useState(""); const [formattedRoomName, setFormattedRoomName] = useState("");
const [newlyConnectedUser, setNewlyConnectedUser] = useState<StudentType | null>(null); const [newlyConnectedUser, setNewlyConnectedUser] = useState<StudentType | null>(null);
const [isQuestionShown, setIsQuestionShown] = useState<boolean>(quizMode === 'student' ? false : true);
// Handle the newly connected user in useEffect, because it needs state info // Handle the newly connected user in useEffect, because it needs state info
// not available in the socket.on() callback // not available in the socket.on() callback
@ -71,6 +72,10 @@ const ManageRoom: React.FC = () => {
} }
}, [newlyConnectedUser]); }, [newlyConnectedUser]);
useEffect(() => {
setIsQuestionShown(quizMode === 'student' ? false : true);
}, [quizMode]);
useEffect(() => { useEffect(() => {
const verifyLogin = async () => { const verifyLogin = async () => {
if (!ApiService.isLoggedIn()) { if (!ApiService.isLoggedIn()) {
@ -258,10 +263,12 @@ const ManageRoom: React.FC = () => {
if (nextQuestionIndex === undefined || nextQuestionIndex > quizQuestions.length - 1) return; if (nextQuestionIndex === undefined || nextQuestionIndex > quizQuestions.length - 1) return;
setCurrentQuestion(quizQuestions[nextQuestionIndex]); setCurrentQuestion(quizQuestions[nextQuestionIndex]);
webSocketService.nextQuestion({roomName: formattedRoomName, webSocketService.nextQuestion({
roomName: formattedRoomName,
questions: quizQuestions, questions: quizQuestions,
questionIndex: nextQuestionIndex, questionIndex: nextQuestionIndex,
isLaunch: false}); isLaunch: false
});
}; };
const previousQuestion = () => { const previousQuestion = () => {
@ -311,6 +318,7 @@ const ManageRoom: React.FC = () => {
return; return;
} }
setQuizQuestions(quizQuestions); setQuizQuestions(quizQuestions);
setCurrentQuestion(quizQuestions[0]);
webSocketService.launchStudentModeQuiz(formattedRoomName, quizQuestions); webSocketService.launchStudentModeQuiz(formattedRoomName, quizQuestions);
}; };
@ -338,7 +346,9 @@ const ManageRoom: React.FC = () => {
if (quizMode === 'teacher') { if (quizMode === 'teacher') {
webSocketService.nextQuestion({ roomName: formattedRoomName, questions: quizQuestions, questionIndex, isLaunch: false }); webSocketService.nextQuestion({ roomName: formattedRoomName, questions: quizQuestions, questionIndex, isLaunch: false });
} }
setIsQuestionShown(true);
} }
}; };
const handleReturn = () => { const handleReturn = () => {
@ -460,15 +470,35 @@ const ManageRoom: React.FC = () => {
{/* the following breaks the css (if 'room' classes are nested) */} {/* the following breaks the css (if 'room' classes are nested) */}
<div className=""> <div className="">
{quizQuestions ? ( {quizQuestions ? (
<div style={{ display: 'flex', flexDirection: 'column' }}> <div style={{ display: 'flex', flexDirection: 'column' }}>
<div className="title center-h-align mb-2">{quiz?.title}</div> <div className="title center-h-align mb-2">{quiz?.title}</div>
{!isNaN(Number(currentQuestion?.question.id)) && (
<div className='close-button-wrapper'>
<FormControlLabel
label={<div className="text-sm">Afficher les questions</div>}
control={
<Switch
data-testid="question-visibility-switch" // Add a unique test ID
checked={isQuestionShown}
onChange={(e: React.ChangeEvent<HTMLInputElement>) =>
setIsQuestionShown(e.target.checked)
}
/>
}
/>
</div>
{!isNaN(Number(currentQuestion?.question.id))
&& isQuestionShown && (
<strong className="number of questions"> <strong className="number of questions">
Question {Number(currentQuestion?.question.id)}/ Question {Number(currentQuestion?.question.id)}/
{quizQuestions?.length} {quizQuestions?.length}
</strong> </strong>
)} )
}
{quizMode === 'teacher' && ( {quizMode === 'teacher' && (
<div className="mb-1"> <div className="mb-1">
@ -483,7 +513,7 @@ const ManageRoom: React.FC = () => {
<div className="mb-2 flex-column-wrapper"> <div className="mb-2 flex-column-wrapper">
<div className="preview-and-result-container"> <div className="preview-and-result-container">
{currentQuestion && ( {currentQuestion && isQuestionShown && (
<QuestionDisplay <QuestionDisplay
showAnswer={false} showAnswer={false}
question={currentQuestion?.question as Question} question={currentQuestion?.question as Question}

View file

@ -37,10 +37,11 @@
/* align-items: center; */ /* align-items: center; */
} }
.close-button-wrapper{
display: flex;
justify-content: flex-start;
margin-right: 1rem;
}
/* .create-room-container { /* .create-room-container {
display: flex; display: flex;