From c3eb901501c2f128d60622a4366230e12dc4b7f7 Mon Sep 17 00:00:00 2001 From: amirasalah Date: Wed, 1 Apr 2020 21:46:47 +0200 Subject: [PATCH] unit testing checkboxes --- src/components/Checkboxes.tsx | 2 +- src/components/__tests__/Breadcrumbs.tsx | 34 ++-- src/components/__tests__/Checkboxes.tsx | 38 ++++ .../__snapshots__/Breadcrumbs.tsx.snap | 2 +- .../__snapshots__/Checkboxes.tsx.snap | 168 ++++++++++++++++++ 5 files changed, 225 insertions(+), 19 deletions(-) create mode 100644 src/components/__tests__/Checkboxes.tsx create mode 100644 src/components/__tests__/__snapshots__/Checkboxes.tsx.snap diff --git a/src/components/Checkboxes.tsx b/src/components/Checkboxes.tsx index 49baac8..c9f8945 100644 --- a/src/components/Checkboxes.tsx +++ b/src/components/Checkboxes.tsx @@ -39,7 +39,7 @@ const Checkboxes: React.FC = ({ const formik = useFormik({ initialValues: { [name]: "", - selectedOptions: ["redwood"] + selectedOptions: [] }, onSubmit: values => { if (required && values.selectedOptions.length === 0) { diff --git a/src/components/__tests__/Breadcrumbs.tsx b/src/components/__tests__/Breadcrumbs.tsx index b9c30b7..1fd8fbd 100644 --- a/src/components/__tests__/Breadcrumbs.tsx +++ b/src/components/__tests__/Breadcrumbs.tsx @@ -5,22 +5,22 @@ import Breadcrumbs from "../Breadcrumbs"; afterEach(cleanup); -it("renders items correctly", async () => { - const list = [ - "Property location", - "About the works", - "Application", - "Declaration", - "Pay fee" - ]; - const active = 2; - const { getByText } = await render( - - ); - expect(getByText("Property location")).toBeInTheDocument(); -}); +describe("Breadcrumbs Component", () => { + it("should render items correctly", () => { + const list = [ + "Property location", + "About the works", + "Application", + "Declaration", + "Pay fee" + ]; + const active = 2; + const { getByText } = render(); + expect(getByText(/Property location/i)).toBeInTheDocument(); + }); -it("renders snapshot", () => { - const { asFragment } = render(); - expect(asFragment()).toMatchSnapshot(); + it("render snapshot", () => { + const { asFragment } = render(); + expect(asFragment()).toMatchSnapshot(); + }); }); diff --git a/src/components/__tests__/Checkboxes.tsx b/src/components/__tests__/Checkboxes.tsx new file mode 100644 index 0000000..6a71369 --- /dev/null +++ b/src/components/__tests__/Checkboxes.tsx @@ -0,0 +1,38 @@ +import { cleanup, fireEvent, render, wait } from "@testing-library/react"; +import * as React from "react"; + +import Checkboxes from "../Checkboxes"; + +afterEach(cleanup); + +/* TO DO + 1 - Add test for the success scenario when form submits and there is a success message + 2- Add test for the fail scenario when the form doesn't submit and thee is an error message +*/ + +describe("Checkboxes Component", () => { + it("should render items correctly", async () => { + const title = "test title"; + const name = "test name"; + const options = {}; + const { findByText } = render( + + ); + expect(await findByText(/test title/i)).toBeInTheDocument(); + }); + + it("should render snapshot", () => { + const title = "test title"; + const name = "test name"; + const options = { + oak: "option 1", + plane: "option 2", + redwood: "option 3", + other: "option 4" + }; + const { asFragment } = render( + + ); + expect(asFragment()).toMatchSnapshot(); + }); +}); diff --git a/src/components/__tests__/__snapshots__/Breadcrumbs.tsx.snap b/src/components/__tests__/__snapshots__/Breadcrumbs.tsx.snap index d5e0d2a..253e7b5 100644 --- a/src/components/__tests__/__snapshots__/Breadcrumbs.tsx.snap +++ b/src/components/__tests__/__snapshots__/Breadcrumbs.tsx.snap @@ -1,6 +1,6 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`renders snapshot 1`] = ` +exports[`Breadcrumbs Component render snapshot 1`] = `
+
+
+
+ test title +
+
+ +
+ Select all that apply +
+
+
+
+ + + + +
+
+ +
+
+
+ +`;