Skip to content

Instantly share code, notes, and snippets.

@Jnforja
Last active April 23, 2020 20:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Jnforja/fc8af531e23791ee676ab244d11b5a29 to your computer and use it in GitHub Desktop.
Save Jnforja/fc8af531e23791ee676ab244d11b5a29 to your computer and use it in GitHub Desktop.
Tests to add function and Add component from article https://joaoforja.com/blog/how-to-get-started-testing-react-applications/
import { add, Add } from "./Add";
import React from "react";
import { render, fireEvent, waitFor } from "@testing-library/react";
test("6 + 4 = 10", function () {
expect.assertions(1);
return expect(add(6, 4)).resolves.toBe(10);
});
test("Add component has expected fields", function () {
const { getByLabelText, getByText } = render(<Add />);
expect(getByLabelText("Term1")).toBeInTheDocument();
expect(getByLabelText("Term2")).toBeInTheDocument();
expect(getByText("ADD")).toBeInTheDocument();
});
test("User can add 3 to 4", function () {
const { getByLabelText, getByText } = render(<Add />);
fireEvent.change(getByLabelText("Term1"), { target: { value: 3 } });
fireEvent.change(getByLabelText("Term2"), { target: { value: 4 } });
fireEvent.click(getByText("ADD"));
return waitFor(() => expect(getByText("7")).toBeInTheDocument());
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment