Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.