diff --git a/client/src/__tests__/components/GiftTemplate/GIFTTemplatePreview.test.tsx b/client/src/__tests__/components/GiftTemplate/GIFTTemplatePreview.test.tsx index 967d33c..2b0e36d 100644 --- a/client/src/__tests__/components/GiftTemplate/GIFTTemplatePreview.test.tsx +++ b/client/src/__tests__/components/GiftTemplate/GIFTTemplatePreview.test.tsx @@ -3,66 +3,87 @@ import { render, screen } from '@testing-library/react'; import '@testing-library/jest-dom'; import GIFTTemplatePreview from 'src/components/GiftTemplate/GIFTTemplatePreview'; +const validQuestions = [ + '::TFTitle::[markdown]Troo statement {TRUE}', + '::SATitle::[markdown]What is the answer? {=ShortAnswerOne =ShortAnswerTwo}', + '::MCQTitle::[markdown]MultiChoice question? {=MQAnswerOne ~MQAnswerTwo#feedback####Gen feedback}', +]; + +const unsupportedQuestions = [ + '::Title::[markdown]Essay {}', + '::Title::[markdown]Matching {}', + '::Title::[markdown]Description', + '$CATEGORY a/b/c' +]; + describe('GIFTTemplatePreview Component', () => { - test('renders error message when questions contain invalid syntax', () => { - render(); - const errorMessage = screen.getByText(/Title ::, Category, Description, or Question formatted stem but ":" found./i); + it('renders error message when questions contain invalid syntax', () => { + render(); + const previewContainer = screen.getByTestId('preview-container'); + expect(previewContainer).toBeInTheDocument(); + const errorMessage = previewContainer.querySelector('div[label="error-message"]'); expect(errorMessage).toBeInTheDocument(); }); - test('renders preview when valid questions are provided', () => { - const questions = [ - 'Stem1 {=ans1 ~ans2 ~ans3}', - ]; - render(); + it('renders preview when valid questions are provided, including answers, has no errors', () => { + render(); const previewContainer = screen.getByTestId('preview-container'); expect(previewContainer).toBeInTheDocument(); - // const question1 = screen.getByText('Stem1'); - const mcQuestion1 = screen.getByText('Stem1'); - const ans1 = screen.getByText('ans1'); - const ans2 = screen.getByText('ans2'); - const ans3 = screen.getByText('ans3'); - expect(mcQuestion1).toBeInTheDocument(); - expect(ans1).toBeInTheDocument(); - expect(ans2).toBeInTheDocument(); - expect(ans3).toBeInTheDocument(); + // Check that all question titles are rendered inside the previewContainer + validQuestions.forEach((question) => { + const title = question.split('::')[1].split('::')[0]; + expect(previewContainer).toHaveTextContent(title); + }); + // There should be no errors + const errorMessage = previewContainer.querySelector('div[label="error-message"]'); + expect(errorMessage).not.toBeInTheDocument(); + // Check that some stems and answers are rendered inside the previewContainer + expect(previewContainer).toHaveTextContent('Troo statement'); + expect(previewContainer).toHaveTextContent('What is the answer?'); + expect(previewContainer).toHaveTextContent('MultiChoice question?'); + expect(previewContainer).toHaveTextContent('Vrai'); + // short answers are stored in a textbox + const answerInputElements = screen.getAllByRole('textbox'); + const giftInputElements = answerInputElements.filter(element => element.classList.contains('gift-input')); - // each answer should have a radio button before it - const radioButtons = screen.getAllByRole('radio'); - expect(radioButtons).toHaveLength(3); - // ans1 should be the