Return

All tests / components/QuestionBlock index.tsx

100% Statements 9/9
60% Branches 3/5
100% Functions 3/3
100% Lines 7/7

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 691x 1x                                                   1x             3x                   4x     1x                                       3x  
import React from 'react';
import styles from '@/styles/survey.module.css';
 
export type AnswerOption = {
    answerText: string;
    isCorrect: boolean;
};
 
export type Question = {
    questionText?: string;
    questionHtml?: string;
    answerOptions?: AnswerOption[];
};
 
type Props = {
    question: Question;
    index: number;
    currentQuestionNum: number;
    answers: Array<{ answer: string }>;
    handleAnswerOptionClick: (payload: {
        question: string;
        answer: string;
        isCorrect: boolean;
        questionNum: number;
    }) => void;
};
 
const QuestionBlock = ({
    question,
    index,
    currentQuestionNum,
    answers,
    handleAnswerOptionClick,
}: Props) => {
    if (currentQuestionNum !== index) return null;
 
    return (
        <div data-testid="question-block" className={styles.question}>
            <div
                // skipcq: JS-0440 - dangerouslySetInnerHTML is safe here as questionHtml is sanitized content
                dangerouslySetInnerHTML={{ __html: question.questionHtml || '' }}
                className={styles.questionHtml}
            />
            {question.answerOptions?.map((answerOption) => (
                <label htmlFor={answerOption.answerText} key={answerOption.answerText}>
                    <input
                        onChange={(e) =>
                            handleAnswerOptionClick({
                                question: question.questionText || '',
                                answer: e.target.value,
                                isCorrect: answerOption.isCorrect,
                                questionNum: index,
                            })
                        }
                        type="radio"
                        id={answerOption.answerText}
                        name={String(index)}
                        value={answerOption.answerText}
                        checked={answers[index]?.answer === answerOption.answerText}
                    />
                    {answerOption.answerText}
                </label>
            ))}
        </div>
    );
};
 
export default QuestionBlock;