VA.gov Onboarding

Write a Unit Test for the React Component

Make a directory for unit tests:

mkdir src/applications/static-pages/hello-world/tests

Create a test file for the React component:

touch src/applications/static-pages/hello-world/tests/HelloWorldWidget.unit.spec.jsx

Add the following contents to the file:

import React from "react";
import { render } from "@testing-library/react";
import { HelloWorldWidget } from "../components/HelloWorldWidget";

describe("HelloWorldWidget", () => {
  it('renders "Hello World!"', () => {
    const { getByText } = render(<HelloWorldWidget />);

    getByText("Hello World!");
  });
});

The test above:

  1. Renders the HelloWorldWidget React component
  2. Verifies that the widget’s content is visible

Run the test:

yarn test:unit src/applications/static-pages/hello-world/tests/HelloWorldWidget.unit.spec.jsx

It should pass. Add more tests as you develop your widget.