Skip to content

Commit cc7f214

Browse files
authored
Major version update: React-18 support & removal of Enzyme support (#26)
* updated package.json, yarn.lock, and documentation to remove enzyme with react-18 * updated react-testing-library * updated rtl again
1 parent 4001f59 commit cc7f214

11 files changed

+2023
-2684
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ node_modules/
66
!*.config.js
77
!.eslintrc.js
88
!test/*
9+
.DS_Store

README.md

+11-29
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,24 @@
99
Standardized test setup methods for React components.
1010

1111
Tired of copy and pasting default prop templates for React component tests?
12-
Use this cute little package with React Testing Library or Enzyme to standardize your component setups.
12+
Use this cute little package with React Testing Library to standardize your component setups.
1313

14-
> 🧠 This library is a _very_ small wrapper on top of Enzyme or RTL, and exists only to help standardize test setup behavior.
14+
> 🧠 This library is a _very_ small wrapper on top of RTL, and exists only to help standardize test setup behavior.
1515
1616
## Usage
1717

1818
```shell
1919
npm install component-test-setup --save-dev
2020
```
2121

22-
For both RTL and Enzyme, this library provides a `setup*` function that takes in:
22+
For RTL, this library provides a `setup*` function that takes in:
2323

2424
1. Your React component class or function
2525
2. Any prop defaults for the component _(optional)_
2626

2727
That function returns a `render*` function that takes in any more props and returns:
2828

29-
- The library's rendered equivalent: `view` for RTL and `wrapper` for Enzyme.
29+
- The library's rendered equivalent: `view` for RTL
3030
- `props`: The computed props the component rendered with.
3131

3232
### React Testing Library
@@ -66,49 +66,31 @@ const { view } = renderView(MyComponent).options({
6666

6767
### Enzyme
6868

69-
Use `setupEnzyme` to create a `renderWrapper` function.
70-
It returns a `wrapper` result from RTL and a `props` object of the computed props used to render.
69+
Previous versions of this library supported Enzyme, but with the upgrade to React-18 and Enzyme nop longer being supported,
70+
this library has moved to a place of no longer supporting it either.
7171

72-
```js
73-
import { setupEnzyme } from "component-test-setup";
74-
75-
import { MyComponent } from "./MyComponent";
76-
77-
const renderWrapper = setupEnzyme(MyComponent, {
78-
someProp: "value",
79-
});
80-
81-
describe("MyComponent", () => {
82-
it("does a thing", () => {
83-
const { props, wrapper } = renderWrapper({
84-
some: "otherProp",
85-
});
86-
87-
wrapper.getByText(props.someProp);
88-
});
89-
});
90-
```
72+
For Enzyme support, please check older major versions of this library.
9173

9274
### Updating Props
9375

9476
Often you'd like to test lifecycle methods/hooks and `component-test-setup` is written to help accommodate that.
9577

96-
For both RTL and Enzyme the API is the same, the `update` method returned in the `render*` method:
78+
The `update` method returned in the `render*` method:
9779

9880
```js
99-
const renderWrapper = setupEnzyme(MyComponent, {
81+
const renderView = setupRtl(MyComponent, {
10082
someProp: "value",
10183
});
10284

10385
describe("MyComponent", () => {
10486
it("does a thing", () => {
105-
const { wrapper, update } = renderWrapper({
87+
const { view, update } = renderView({
10688
some: "otherProp",
10789
});
10890

10991
update({ someProp: "another-value" });
11092

111-
wrapper.getByText("another-value"); // It has been updated to render with the new someProp value
93+
view.getByText("another-value"); // It has been updated to render with the new someProp value
11294
});
11395
});
11496
```

jest.config.json

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
{
22
"moduleFileExtensions": ["tsx", "ts", "js"],
3-
"setupFiles": ["<rootDir>test/base-setup.js"],
43
"testRegex": ["\\.test\\.tsx?$"]
54
}

package.json

+7-11
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,22 @@
66
"@babel/preset-env": "^7.12.11",
77
"@babel/preset-react": "^7.12.10",
88
"@babel/preset-typescript": "^7.12.7",
9-
"@testing-library/react": "^11.2.3",
10-
"@types/enzyme": "^3.10.8",
9+
"@testing-library/react": "^15.0.0",
1110
"@types/jest": "^26.0.20",
12-
"@types/react": "^17.0.0",
13-
"@types/react-dom": "^17.0.0",
11+
"@types/react": "^18.0.0",
12+
"@types/react-dom": "^18.0.0",
1413
"@typescript-eslint/eslint-plugin": "4.13.0",
1514
"@typescript-eslint/parser": "^4.13.0",
16-
"@wojtekmaj/enzyme-adapter-react-17": "^0.4.1",
1715
"babel-jest": "^26.6.3",
18-
"enzyme": "^3.11.0",
1916
"eslint": "^7.17.0",
2017
"eslint-config-prettier": "^7.1.0",
2118
"husky": "^4.3.7",
2219
"jest": "^26.6.3",
2320
"lint-staged": "^10.5.3",
2421
"prettier": "^2.2.1",
25-
"react": "^17.0.1",
26-
"react-dom": "^17.0.1",
27-
"typescript": "^4.1.3"
22+
"react": "^18.0.1",
23+
"react-dom": "^18.0.1",
24+
"typescript": "^4.9.5"
2825
},
2926
"main": "src/index.js",
3027
"homepage": "https://github.com/Codecademy/component-test-setup#readme",
@@ -35,7 +32,6 @@
3532
},
3633
"keywords": [
3734
"component",
38-
"enzyme",
3935
"react",
4036
"rtl",
4137
"test"
@@ -60,5 +56,5 @@
6056
"compile": "tsc --module commonjs"
6157
},
6258
"types": "src/index.d.ts",
63-
"version": "0.3.3"
59+
"version": "1.0.0"
6460
}

src/index.ts

-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
1-
export * from "./setupEnzyme";
21
export * from "./setupRtl";
32
export * from "./types";

src/setupEnzyme.test.tsx

-71
This file was deleted.

src/setupEnzyme.tsx

-48
This file was deleted.

src/setupRtl.tsx

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { RenderOptions } from "@testing-library/react";
1+
import { render, type RenderOptions } from "@testing-library/react";
22
import React from "react";
33

44
import { RemainingPropsAndTestOverrides, FullProps, RenderRtl, SetupComponentType } from "./types";
@@ -25,7 +25,6 @@ export function setupRtl<
2525
/* eslint-disable-next-line @typescript-eslint/ban-types */
2626
BaseProps extends Partial<FullProps<ComponentType>> = {}
2727
>(Component: ComponentType, baseProps?: BaseProps): RenderRtl<ComponentType, BaseProps> {
28-
const { render } = require("@testing-library/react") as typeof import("@testing-library/react");
2928
let options: RenderOptions;
3029

3130
function renderView(testProps?: RemainingPropsAndTestOverrides<ComponentType, BaseProps>) {

src/types.ts

+11-23
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { RenderOptions, RenderResult } from "@testing-library/react";
2-
import { ReactWrapper } from "enzyme";
32
import { ReactElement } from "react";
43

54
// eslint-disable-next-line @typescript-eslint/ban-types
@@ -9,34 +8,27 @@ interface PureFunctionComponent<P = {}> {
98
export type SetupComponentType = React.ComponentType<any> | PureFunctionComponent;
109

1110
// Given a C component type, extracts the props of it:
12-
export type FullProps<C extends SetupComponentType> = React.ComponentProps<C>
11+
export type FullProps<C extends SetupComponentType> = React.ComponentProps<C>;
1312

14-
export type RenderEnzyme<
15-
Component extends SetupComponentType,
16-
Props extends Partial<FullProps<Component>>
17-
> = (
18-
// By using the spread operator in this type, we leverage the optionality of the entire argument itself.
19-
// eg: If the caller needs no more required props, we don't require they provide test props. But if
20-
// they DO still need props, we require a parameter in the function signature.
21-
// So the syntax `renderWrapper()` is valid if there are no required props,
22-
// otherwise we force `renderWrapper({ ...missingReqs })`
23-
...testProps: ConditionallyRequiredTestProps<Component, Props>
24-
) => RenderEnzymeReturn<Component>;
25-
26-
// Just like RenderEnzyme, but RenderRtl also allows callers to do `renderRtl.options({...})(props)`
13+
// RenderRtl allows callers to do `renderRtl.options({...})(props)`
2714
// In order to support both signature types, we extend the function's base type and add the options
2815
// attribute for those "in the know" ;)
2916
export type RenderRtl<
3017
Component extends SetupComponentType,
31-
Props extends Partial<FullProps<Component>>
18+
Props extends Partial<FullProps<Component>>,
3219
> = {
20+
// By using the spread operator in this type, we leverage the optionality of the entire argument itself.
21+
// eg: If the caller needs no more required props, we don't require they provide test props. But if
22+
// they DO still need props, we require a parameter in the function signature.
23+
// So the syntax `renderView()` is valid if there are no required props,
24+
// otherwise we force `renderView({ ...missingReqs })`
3325
(...testProps: ConditionallyRequiredTestProps<Component, Props>): RenderRtlReturn<Component>;
3426
options: (options: RenderOptions) => RenderRtl<Component, Props>;
3527
};
3628

3729
type ConditionallyRequiredTestProps<
3830
Component extends SetupComponentType,
39-
Props extends Partial<FullProps<Component>>
31+
Props extends Partial<FullProps<Component>>,
4032
> =
4133
// This is where the real magic happens. At type-interpretation time, the compiler can infer from the
4234
// component's prop type and from the passed props into the `setup*` function if there are any missing
@@ -63,10 +55,6 @@ type RequiredKeys<T> = {
6355
[K in keyof T]-?: Record<string, unknown> extends { [P in K]: T[K] } ? never : K;
6456
}[keyof T];
6557

66-
interface RenderEnzymeReturn<Component extends SetupComponentType>
67-
extends BaseRenderReturn<Component> {
68-
wrapper: ReactWrapper<FullProps<Component>, React.ComponentState>;
69-
}
7058
interface RenderRtlReturn<Component extends SetupComponentType>
7159
extends BaseRenderReturn<Component> {
7260
view: RenderResult;
@@ -85,10 +73,10 @@ interface BaseRenderReturn<Component extends SetupComponentType> {
8573
*/
8674
export type RemainingPropsAndTestOverrides<
8775
ComponentType extends SetupComponentType,
88-
BaseProps extends Partial<FullProps<ComponentType>>
76+
BaseProps extends Partial<FullProps<ComponentType>>,
8977
> = RemainingProps<ComponentType, BaseProps> & Partial<FullProps<ComponentType>>;
9078

9179
type RemainingProps<
9280
ComponentType extends SetupComponentType,
93-
BaseProps extends Partial<FullProps<ComponentType>>
81+
BaseProps extends Partial<FullProps<ComponentType>>,
9482
> = Omit<FullProps<ComponentType>, keyof BaseProps>;

test/base-setup.js

-5
This file was deleted.

0 commit comments

Comments
 (0)