Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions components/application/FormQuestion.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { QuestionDefinition, QuestionType } from '../../common/types';
import ShortTextQuestion from '../questions/short-test-question/ShortTextQuestion';
import LongTextQuestion from '../questions/long-text-question/LongTextQuestion';
import DropdownQuestion from '../questions/DropdownQuestion';
import CheckboxesQuestion from '../questions/CheckboxesQuestion';
import FileUploadQuestion from '../questions/file-upload-question/FileUploadQuestion';
import { assertUnreachable } from '../../common/utils/utils';
import React from 'react';
import { FormInstance } from 'antd';
import DropdownQuestion from '../questions/dropdown-question/DropdownQuestion';
import CheckboxesQuestion from '../questions/checkboxes-question/CheckboxesQuestion';

// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
export const getQuestionComponentFromType = (type: QuestionType) => {
switch (type) {
case QuestionType.ShortText:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { describe, expect, it } from '@jest/globals';
import { render, screen } from '@testing-library/react';
import CheckboxesQuestion from './CheckboxesQuestion';
import { Checkboxes, QuestionResponse, QuestionType } from '../../../common/types';
import { Form } from 'antd';

Object.defineProperty(window, 'matchMedia', {
writable: true,
value: jest.fn().mockImplementation(query => ({
matches: false,
media: query,
onchange: null,
addListener: jest.fn(), // Deprecated
removeListener: jest.fn(), // Deprecated
addEventListener: jest.fn(),
removeEventListener: jest.fn(),
dispatchEvent: jest.fn(),
})),
});

describe('CheckboxesQuestion component', () => {
const n = (name: string) => ({ name });
const exampleContent = 'Your top two most proficient languages. Select at least 1, but no more than 2.';
const exampleQuestion: Checkboxes = {
field: 'education',
type: QuestionType.Checkboxes,
id: '1',
content: exampleContent,
minNumber: 1,
maxNumber: 2,
options: ['English', 'Java', 'Typescript', 'Japanese'].map(n),
required: true,
};

const CheckboxesQuestionWrapper = () => {
const form = Form.useForm<Record<string, QuestionResponse>>()[0];

return <CheckboxesQuestion question={exampleQuestion} form={form} disabled={false} />;
};

it('renders', () => {
render(<CheckboxesQuestionWrapper />);
const confirmedDialogText = screen.getByTestId('checkboxes-question');

expect(confirmedDialogText.textContent).toContain(exampleContent);
expect(confirmedDialogText.textContent).toContain("English");
expect(confirmedDialogText.textContent).toContain("Java");
expect(confirmedDialogText.textContent).toContain("Typescript");
expect(confirmedDialogText.textContent).toContain("Japanese");
});
});
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { FC } from 'react';
import { Checkboxes, QuestionResponse } from '../../common/types';
import { Checkboxes, QuestionResponse } from '../../../common/types';
import { Checkbox, Form, FormInstance, Row } from 'antd';

type CheckboxesProps = {
Expand All @@ -15,7 +15,7 @@ const CheckboxesQuestion: FC<CheckboxesProps> = ({
const options: string[] = [];
question.options.map((o) => options.push(o.name));
return (
<Form.Item
<Form.Item data-testid="checkboxes-question"
className="question"
name={question.id}
label={question.content}
Expand Down
77 changes: 77 additions & 0 deletions components/questions/dropdown-question/DropdownQuestion.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { describe, expect, it } from '@jest/globals';
import { fireEvent, render, screen, waitFor } from '@testing-library/react';
import DropdownQuestion from './DropdownQuestion';
import { Dropdown, QuestionResponse, QuestionType } from '../../../common/types';
import { Form } from 'antd';

Object.defineProperty(window, 'matchMedia', {
writable: true,
value: jest.fn().mockImplementation(query => ({
matches: false,
media: query,
onchange: null,
addListener: jest.fn(), // Deprecated
removeListener: jest.fn(), // Deprecated
addEventListener: jest.fn(),
removeEventListener: jest.fn(),
dispatchEvent: jest.fn(),
})),
});

describe('CheckboxesQuestion component', () => {
const n = (name: string) => ({ name });
const exampleContent = 'How old are you?';
const exampleQuestion: Dropdown = {
field: 'adult',
type: QuestionType.Dropdown,
id: '1',
content: exampleContent,
options: ['1', '2', '3', '4+'].map(n),
required: true,
};

const DropdownQuestionWrapper = () => {
const form = Form.useForm<Record<string, QuestionResponse>>()[0];

return <DropdownQuestion question={exampleQuestion} form={form} disabled={false} />;
};

const DisabledDropdownQuestionWrapper = () => {
const form = Form.useForm<Record<string, QuestionResponse>>()[0];

return <DropdownQuestion question={exampleQuestion} form={form} disabled={true} />;
};

it('renders', () => {
render(<DropdownQuestionWrapper />);
const confirmedDialogText = screen.getByTestId('dropdown-question');

expect(confirmedDialogText.textContent).toContain(exampleContent);
expect(confirmedDialogText.textContent).toContain("Select");
Copy link
Contributor

@deanframe deanframe Nov 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we also be checking that the correct dropdown options render when it is clicked?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

^^ this is a rlly good point!

});

// it('renders the correct dropdown values', async () => {
// render(<DropdownQuestionWrapper />);
// const confirmedDialogText = screen.getByTestId('dropdown-question');

// if (confirmedDialogText instanceof Element) {
// fireEvent.mouseDown(confirmedDialogText);
// }
// await waitFor(() => expect(screen.getAllByText('1')).toHaveLength(2));

// if (confirmedDialogText instanceof Element) {
// fireEvent.mouseDown(confirmedDialogText);
// }
// await waitFor(() => expect(screen.getAllByText('2')).toHaveLength(2));
// });

it('does not render options when clicked, if it is disabled', async () => {
render(<DisabledDropdownQuestionWrapper />);
const confirmedDialogText = screen.getByTestId('dropdown-question').firstElementChild;

if (confirmedDialogText instanceof Element) {
fireEvent.mouseDown(confirmedDialogText);
}
await waitFor(() => expect(screen.getAllByText('1')).toHaveLength(0));
});
});
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { FC } from 'react';
import { Dropdown as DropdownType, QuestionResponse } from '../../common/types';
import { Dropdown as DropdownType, QuestionResponse } from '../../../common/types';
import { FormInstance, Select, Form } from 'antd';

type DropdownProps = {
Expand All @@ -9,7 +9,7 @@ type DropdownProps = {
};
const DropdownQuestion: FC<DropdownProps> = ({ question, form, disabled }) => {
return (
<Form.Item
<Form.Item data-testid="dropdown-question"
className="question"
name={question.id}
wrapperCol={{ span: 8 }}
Expand Down
2 changes: 1 addition & 1 deletion jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ module.exports = {
'^.+\\.(js|jsx|ts|tsx)$': ['babel-jest', { presets: ['next/babel'] }],
},
transformIgnorePatterns: ['/node_modules/', '^.+\\.module\\.(css|sass|scss)$'],
setupFilesAfterEnv: ['<rootDir>/jest.setup.ts'],
setupFilesAfterEnv: ['<rootDir>/jest.setup.ts', "@testing-library/jest-dom/extend-expect"],
};

export {};