Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace Enzyme with React Testing Library #2195

Closed
19 of 20 tasks
becca-bailey opened this issue Apr 14, 2022 · 8 comments
Closed
19 of 20 tasks

Replace Enzyme with React Testing Library #2195

becca-bailey opened this issue Apr 14, 2022 · 8 comments
Labels
Note: Testing 🟢 Issues related to testing Type: Infrastructure 🏠 Requested changes that won't affect core functionality

Comments

@becca-bailey
Copy link
Contributor

becca-bailey commented Apr 14, 2022

In addition to some of the general issues with our tests being a little slow and out of date, we will need to replace Enzyme as our method of rendering components in tests if we want to upgrade to React 18+.

We should re-write our existing tests using Jest + React Testing Library.

Related issue: #2187

See also: https://dev.to/wojtekmaj/enzyme-is-dead-now-what-ekl

Completed packages

  • victory
  • victory-area
  • victory-bar
  • victory-box-plot
  • victory-brush-container
  • victory-candlestick
  • victory-chart
  • victory-core
  • victory-errorbars
  • victory-group
  • victory-histogram
  • victory-legend
  • victory-line
  • victory-pie
  • victory-scatter
  • victory-selection-container
  • victory-shared-events (this might be going away soon, so it's a lower priority test suite to convert)
  • victory-stack
  • victory-tooltip
  • victory-voronoi

General tips and pointers

  • We can use querySelector in a test to test attributes on SVG elements.
  • We can also use getByRole or getByTestId to isolate specific nodes. Add safe user props to all top-level components #2191 should allow us to add data-testid to our test components, but there may be some primitive components that need to be set up to work with these user-provided props.
  • We can replace some of the test assertions with .toMatchInlineSnapshot, provided that the snapshot is focused (< 10 lines) and easy to read.
  • If we want to test the data that is being passed to the SVG component, there are some workarounds we can use, such as giving our test component a property that outputs the rendered data as JSON. Here is an example:
it("renders data ordered by value of sortKey, if given", () => {
      const data = [{ t: 0 /*x: 10, y: 1*/ }, { t: 1 /*x:  9, y: 1*/ }];
      const { container } = render(
        <VictoryLine
          data={data}
          sortKey="t"
          x={({ t }) => 10 - t}
          y={() => 1}
          dataComponent={
            <Curve data-json={(props) => JSON.stringify(props.data)} />
          }
        />
      );

      const line = container.querySelector("path");
      const renderedData = JSON.parse(line.getAttribute("data-json")).map(
        ({ t }) => t
      );

      expect(renderedData).toEqual([0, 1]);
    });

Long-term, we might want to figure out how to test data transformations in a different place.

@becca-bailey becca-bailey added this to the Overhaul Testing milestone Apr 29, 2022
@becca-bailey becca-bailey added Note: Testing 🟢 Issues related to testing Type: Infrastructure 🏠 Requested changes that won't affect core functionality labels Apr 29, 2022
@heythisispaul
Copy link
Contributor

@becca-bailey I can start work on victory-bar.

@heythisispaul
Copy link
Contributor

#2216 for victory-bar tests should be good to go, I'm moving on to victory-box-plot.

@becca-bailey
Copy link
Contributor Author

I'm going to start work on victory-core.

@becca-bailey
Copy link
Contributor Author

I am working on victory-errorbar next.

@heythisispaul
Copy link
Contributor

I'm picking up victory-histogram now

@heythisispaul
Copy link
Contributor

#2255 is up for the histogram tests, moving on to victory-legend

@heythisispaul
Copy link
Contributor

Moving on to victory-pie

@becca-bailey
Copy link
Contributor Author

The modules that were excluded from this conversion are add-events and victory-shared-events, due to their heavy reliance on mocking and class inheritance. We are going to re-write those tests once we refactor Victory events and state management.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Note: Testing 🟢 Issues related to testing Type: Infrastructure 🏠 Requested changes that won't affect core functionality
Projects
None yet
Development

No branches or pull requests

2 participants