diff --git a/__mocks__/marked/marked.js b/__mocks__/marked/marked.js new file mode 100644 index 0000000..76d7dd0 --- /dev/null +++ b/__mocks__/marked/marked.js @@ -0,0 +1,3 @@ +import marked from "marked"; +jest.genMockFromModule(marked); +module.exports = marked; diff --git a/src/components/FileUpload.tsx b/src/components/FileUpload.tsx index b1c1863..ed4efe6 100644 --- a/src/components/FileUpload.tsx +++ b/src/components/FileUpload.tsx @@ -104,7 +104,7 @@ export const FileUpload: React.FC = ({ ); })} -
+

{stateText}

diff --git a/src/components/Text.tsx b/src/components/Text.tsx index 7e3f49c..11cd68d 100644 --- a/src/components/Text.tsx +++ b/src/components/Text.tsx @@ -66,12 +66,13 @@ export const Text: React.FC = ({ {title} - {label && {label}} + {label && {label}} { if (maxWords) { @@ -91,7 +92,12 @@ export const Text: React.FC = ({ pt={1} textAlign="right" > - {diff >= 0 ? `${diff}` : 0} words Remaining + + {" "} + {diff >= 0 + ? `${diff} words Remaining` + : `0 words Remaining`}{" "} + )} {unit && ( diff --git a/src/components/__tests__/HVCenterContainer.tsx b/src/components/__tests__/HVCenterContainer.tsx new file mode 100644 index 0000000..4a8d2dc --- /dev/null +++ b/src/components/__tests__/HVCenterContainer.tsx @@ -0,0 +1,18 @@ +import { cleanup, fireEvent, render, wait } from "@testing-library/react"; +import * as React from "react"; + +import HVCenterContainer from "../HVCenterContainer"; + +afterEach(cleanup); + +describe("HVCenterContainer Component", () => { + it("should render snapshot", () => { + const children = "This should be rendered inside"; + const { asFragment } = render( + + {children} + + ); + expect(asFragment()).toMatchSnapshot(); + }); +}); diff --git a/src/components/__tests__/InlineSelect.tsx b/src/components/__tests__/InlineSelect.tsx new file mode 100644 index 0000000..78d4f12 --- /dev/null +++ b/src/components/__tests__/InlineSelect.tsx @@ -0,0 +1,13 @@ +import { cleanup, fireEvent, render, wait } from "@testing-library/react"; +import * as React from "react"; + +import SelectQuestion from "../InlineSelect"; + +afterEach(cleanup); + +describe("Select Question Component", () => { + it("should render snapshot", () => { + const { asFragment } = render(); + expect(asFragment()).toMatchSnapshot(); + }); +}); diff --git a/src/components/__tests__/Loading.tsx b/src/components/__tests__/Loading.tsx new file mode 100644 index 0000000..6aefaaa --- /dev/null +++ b/src/components/__tests__/Loading.tsx @@ -0,0 +1,17 @@ +import { cleanup, fireEvent, render, wait } from "@testing-library/react"; +import * as React from "react"; + +import Loading from "../Loading"; + +afterEach(cleanup); + +describe("Loading Component", () => { + it("should have loading text in document", async () => { + const { findByText } = render(); + expect(await findByText("Loading...")).toBeInTheDocument(); + }); + it("should render snapshot", () => { + const { asFragment } = render(); + expect(asFragment()).toMatchSnapshot(); + }); +}); diff --git a/src/components/__tests__/MoreInfo.tsx b/src/components/__tests__/MoreInfo.tsx new file mode 100644 index 0000000..4487f1f --- /dev/null +++ b/src/components/__tests__/MoreInfo.tsx @@ -0,0 +1,18 @@ +import { cleanup, fireEvent, render, wait } from "@testing-library/react"; +import marked from "marked"; +import * as React from "react"; + +import MoreInfo from "../MoreInfo"; + +afterEach(cleanup); +jest.mock("marked"); + +describe("MoreInfo Component", () => { + it("should render snapshot", () => { + // console.log(jest.mock("marked")); + // const { findByText } = render( + // + // ); + // expect(findByText("content title")).toBeInTheDocument(); + }); +}); diff --git a/src/components/__tests__/Question.tsx b/src/components/__tests__/Question.tsx new file mode 100644 index 0000000..ba4d795 --- /dev/null +++ b/src/components/__tests__/Question.tsx @@ -0,0 +1,14 @@ +import { cleanup, fireEvent, render, wait } from "@testing-library/react"; +import * as React from "react"; + +import Question from "../Question"; + +afterEach(cleanup); + +describe("Question Component", () => { + it("render snapshot", () => { + const children = "This is a question"; + const { asFragment } = render({children}); + expect(asFragment()).toMatchSnapshot(); + }); +}); diff --git a/src/components/__tests__/QuestionImage.tsx b/src/components/__tests__/QuestionImage.tsx new file mode 100644 index 0000000..6042c28 --- /dev/null +++ b/src/components/__tests__/QuestionImage.tsx @@ -0,0 +1,17 @@ +import { cleanup, fireEvent, render, wait } from "@testing-library/react"; +import * as React from "react"; + +import StatementImage from "../QuestionImage"; + +afterEach(cleanup); + +describe("Statement Image Component", () => { + it("should render snapshot when image is available", () => { + const { asFragment } = render(); + expect(asFragment()).toMatchSnapshot(); + }); + it("should render snapshot when image is not available", () => { + const { asFragment } = render(); + expect(asFragment()).toMatchSnapshot(); + }); +}); diff --git a/src/components/__tests__/Response.tsx b/src/components/__tests__/Response.tsx new file mode 100644 index 0000000..b123285 --- /dev/null +++ b/src/components/__tests__/Response.tsx @@ -0,0 +1,20 @@ +import { cleanup, fireEvent, render, wait } from "@testing-library/react"; +import * as React from "react"; + +import Response from "../Response"; + +afterEach(cleanup); + +describe("Response Component", () => { + it("should render snapshot when image is available", () => { + const response = { id: "1", text: "example text", img: "img.png" }; + const { asFragment } = render(); + expect(asFragment()).toMatchSnapshot(); + }); + it("should render snapshot when image is not available", () => { + const response = { id: "2", text: "example text" }; + + const { asFragment } = render(); + expect(asFragment()).toMatchSnapshot(); + }); +}); diff --git a/src/components/__tests__/ResponseButton.tsx b/src/components/__tests__/ResponseButton.tsx new file mode 100644 index 0000000..78e9650 --- /dev/null +++ b/src/components/__tests__/ResponseButton.tsx @@ -0,0 +1,60 @@ +import { cleanup, fireEvent, render, waitFor } from "@testing-library/react"; +import * as React from "react"; + +import ResponseButton from "../ResponseButton"; + +afterEach(cleanup); + +describe("Response Button Component", () => { + let handleClick; + let response; + let responseKey; + beforeEach(() => { + handleClick = jest.fn(); + responseKey = "A"; + response = { + id: "1", + text: "inner text" + }; + }); + it("should render snapshot when selected is true", () => { + const { asFragment } = render( + + {response.text} + + ); + expect(asFragment()).toMatchSnapshot(); + }); + it("should render snapshot when selected is false", () => { + const { asFragment } = render( + + {response.text} + + ); + expect(asFragment()).toMatchSnapshot(); + }); + it("should trigger handleClick when clicked", () => { + const { getByText } = render( + + {response.text} + + ); + fireEvent.click(getByText("inner text")); + expect(handleClick).toHaveBeenCalled(); + }); +}); diff --git a/src/components/__tests__/ResponseImageButton.tsx b/src/components/__tests__/ResponseImageButton.tsx new file mode 100644 index 0000000..e668c17 --- /dev/null +++ b/src/components/__tests__/ResponseImageButton.tsx @@ -0,0 +1,60 @@ +import { cleanup, fireEvent, render } from "@testing-library/react"; +import * as React from "react"; + +import ResponseImageButton from "../ResponseImageButton"; + +afterEach(cleanup); + +describe("Response Button Component", () => { + let handleClick: jest.Mock; + let children; + let responseKey; + let image; + + beforeEach(() => { + responseKey = "A"; + handleClick = jest.fn(); + image = "img.png"; + children = "Inner text"; + }); + it("should render snapshot when selected is true", () => { + const { asFragment } = render( + + {children} + + ); + expect(asFragment()).toMatchSnapshot(); + }); + it("should render snapshot when selected is false", () => { + const { asFragment } = render( + + {children} + + ); + expect(asFragment()).toMatchSnapshot(); + }); + it("should trigger handleClick when clicked", async () => { + const { getByText } = render( + + {children} + + ); + fireEvent.click(getByText("Inner text")); + expect(handleClick).toHaveBeenCalled(); + }); +}); diff --git a/src/components/__tests__/Text.tsx b/src/components/__tests__/Text.tsx index 38ffb79..b66349b 100644 --- a/src/components/__tests__/Text.tsx +++ b/src/components/__tests__/Text.tsx @@ -6,13 +6,266 @@ import Text from "../Text"; afterEach(cleanup); describe("Text Component", () => { - it("should render snapshot", () => { - const title = " test title"; - const name = "test name"; - const type = "text"; - const { asFragment } = render( - - ); - expect(asFragment()).toMatchSnapshot(); + let title: string; + let name: string; + let type: string; + let placeholder: string; + let multiline: boolean; + let maxWords: number; + let label: string; + let unit: string; + + beforeEach(() => { + title = "test title"; + name = "test name"; + }); + describe("of type short text", () => { + beforeEach(() => { + placeholder = "short text input placeholder"; + type = "text"; + label = "short text input label"; + }); + it("should render snapshot", () => { + const { asFragment } = render( + + ); + expect(asFragment()).toMatchSnapshot(); + }); + it("should have the right label and placeholder text", async () => { + const { findByPlaceholderText, findByLabelText } = render( + + ); + expect(await findByPlaceholderText(placeholder)).toBeInTheDocument(); + expect(await findByLabelText(label)).toBeInTheDocument(); + }); + it("should have a value if an text is entered", async () => { + const value = "Lorem ipsum dolor sit amet, consectetur adipiscing"; + const { findByPlaceholderText, getByPlaceholderText } = render( + + ); + fireEvent.change(getByPlaceholderText(placeholder), { + target: { value } + }); + expect(await findByPlaceholderText(placeholder)).toHaveValue(value); + }); + }); + describe("Of type long text", () => { + beforeEach(() => { + type = "text"; + maxWords = 10; + placeholder = "long text input placeholder"; + multiline = true; + label = "long text input label"; + }); + it("should render snapshot", () => { + const { asFragment } = render( + + ); + expect(asFragment()).toMatchSnapshot(); + }); + it("should have the right label and placeholder text", async () => { + const { findByPlaceholderText, findByLabelText } = render( + + ); + expect(await findByPlaceholderText(placeholder)).toBeInTheDocument(); + expect(await findByLabelText(label)).toBeInTheDocument(); + }); + it("should have a value if an text is entered", async () => { + const value = + "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."; + const { findByPlaceholderText, getByPlaceholderText } = render( + + ); + fireEvent.change(getByPlaceholderText(placeholder), { + target: { value } + }); + expect(await findByPlaceholderText(placeholder)).toHaveValue(value); + }); + it("should have a value if an text is entered", async () => { + const value = + "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."; + const { findByText, getByPlaceholderText } = render( + + ); + fireEvent.change(getByPlaceholderText(placeholder), { + target: { value } + }); + expect(await findByText("0 words Remaining")).toBeInTheDocument(); + }); + }); + describe("of type Email", () => { + beforeEach(() => { + type = "email"; + label = "Email text input label"; + placeholder = "Email text input placeholder"; + }); + it("should render snapshot", () => { + const { asFragment } = render( + + ); + expect(asFragment()).toMatchSnapshot(); + }); + it("should have the right label and placeholder text", async () => { + const { findByPlaceholderText, findByLabelText } = render( + + ); + expect(await findByPlaceholderText(placeholder)).toBeInTheDocument(); + expect(await findByLabelText(label)).toBeInTheDocument(); + }); + it("should have a value if an email is entered", async () => { + const { findByPlaceholderText, getByPlaceholderText } = render( + + ); + fireEvent.change(getByPlaceholderText(placeholder), { + target: { value: "test@test.com" } + }); + expect(await findByPlaceholderText(placeholder)).toHaveValue( + "test@test.com" + ); + }); + }); + describe("of type Number", () => { + beforeEach(() => { + type = "number"; + label = "number input label"; + placeholder = "number input placeholder"; + }); + it("should render snapshot", () => { + const { asFragment } = render( + + ); + expect(asFragment()).toMatchSnapshot(); + }); + it("should have the right label and placeholder text", async () => { + const { findByPlaceholderText, findByLabelText } = render( + + ); + expect(await findByPlaceholderText(placeholder)).toBeInTheDocument(); + expect(await findByLabelText(label)).toBeInTheDocument(); + }); + it("should render the unit when specified", async () => { + const unit = "meters"; + const { findByText } = render( + + ); + expect(await findByText("meters")).toBeInTheDocument(); + }); + it("should not have any value if text is entered", async () => { + const { findByPlaceholderText, getByPlaceholderText } = render( + + ); + fireEvent.change(getByPlaceholderText(placeholder), { + target: { value: "Test text" } + }); + expect(await findByPlaceholderText(placeholder)).toHaveValue(null); + }); + it("should have a value if a number is entered", async () => { + const { findByPlaceholderText, debug, getByPlaceholderText } = render( + + ); + fireEvent.change(getByPlaceholderText(placeholder), { + target: { value: 123 } + }); + expect(await findByPlaceholderText(placeholder)).toHaveValue(123); + }); }); }); diff --git a/src/components/__tests__/__snapshots__/ExpandableCheckboxes.tsx.snap b/src/components/__tests__/__snapshots__/ExpandableCheckboxes.tsx.snap index b08cf41..14b4163 100644 --- a/src/components/__tests__/__snapshots__/ExpandableCheckboxes.tsx.snap +++ b/src/components/__tests__/__snapshots__/ExpandableCheckboxes.tsx.snap @@ -43,7 +43,6 @@ exports[`Expandable Checkboxes Component render snapshot 1`] = ` aria-hidden="true" class="MuiSvgIcon-root" focusable="false" - role="presentation" viewBox="0 0 24 24" >