@ryanbrandt/react-testing-utils 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +49 -0
- package/dist/@types/index.d.ts +3 -0
- package/dist/@types/jest/index.d.ts +7 -0
- package/dist/@types/react/AbstractMockComponentWrapper.d.ts +58 -0
- package/dist/@types/react/MockClassComponentWrapper.d.ts +33 -0
- package/dist/@types/react/MockFunctionComponentWrapper.d.ts +33 -0
- package/dist/@types/src/index.d.ts +3 -0
- package/dist/@types/src/jest/index.d.ts +7 -0
- package/dist/@types/src/react/AbstractMockComponentWrapper.d.ts +58 -0
- package/dist/@types/src/react/MockClassComponentWrapper.d.ts +33 -0
- package/dist/@types/src/react/MockFunctionComponentWrapper.d.ts +33 -0
- package/dist/index.esm.js +2 -0
- package/dist/index.esm.js.map +1 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -0
- package/package.json +57 -0
package/README.md
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
# react-testing-utils
|
|
2
|
+
|
|
3
|
+
Common wrappers and utilities for unit testing and mocking in React applications with Jest and RTL
|
|
4
|
+
|
|
5
|
+
## Usage
|
|
6
|
+
|
|
7
|
+
```tsx
|
|
8
|
+
yarn add react-testing-utils
|
|
9
|
+
|
|
10
|
+
// or
|
|
11
|
+
|
|
12
|
+
npm install react-testing-utils
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
**Warning:** This project has several required peer dependencies which must be installed in your consuming project
|
|
16
|
+
|
|
17
|
+
1. `@tesing-library/jest-dom`
|
|
18
|
+
2. `@testing-library/react`
|
|
19
|
+
3. `jest` (27+)
|
|
20
|
+
4. `react` (17+)
|
|
21
|
+
5. `react-dom` (17+)
|
|
22
|
+
|
|
23
|
+
### Example Usage of Key Utilities
|
|
24
|
+
|
|
25
|
+
```tsx
|
|
26
|
+
jest.mock("path/to/your/component");
|
|
27
|
+
const mockedMyComponent = new MockFunctionComponentWrapper(MyComponent);
|
|
28
|
+
|
|
29
|
+
jest.mock("path/to/some/hook");
|
|
30
|
+
// resolves to Jest.MockedFunction<typeof useMyHook>;
|
|
31
|
+
const mockUseMyHook = JestUtilities.assertAsMockFunction(useMyHook);
|
|
32
|
+
|
|
33
|
+
describe("SomeOtherComponent", () => {
|
|
34
|
+
it("renders a MyComponent", () => {
|
|
35
|
+
render(<SomeOtherComponent />);
|
|
36
|
+
|
|
37
|
+
mockedMyComponent.assertOnScreen();
|
|
38
|
+
mockedMyComponent.assertCalledWith({ someProps: "expected value" });
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
describe("when useMyHook does something", () => {
|
|
42
|
+
mockUseMyHook.mockReturnValue("something done");
|
|
43
|
+
|
|
44
|
+
render(<SomeOtherComponent />);
|
|
45
|
+
|
|
46
|
+
...
|
|
47
|
+
})
|
|
48
|
+
});
|
|
49
|
+
```
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/// <reference types="jest" />
|
|
2
|
+
declare type AnyFunction = (...args: any[]) => any;
|
|
3
|
+
declare class JestUtilities {
|
|
4
|
+
static assertAsMockFunction: <T extends AnyFunction>(actual: T) => jest.MockedFunction<T>;
|
|
5
|
+
static assertAsMockClass: <T extends jest.Constructable>(actual: T) => jest.MockedClass<T>;
|
|
6
|
+
}
|
|
7
|
+
export default JestUtilities;
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/// <reference types="jest" />
|
|
2
|
+
import "@testing-library/jest-dom";
|
|
3
|
+
/**
|
|
4
|
+
* Abstract base mock component wrapper class
|
|
5
|
+
*/
|
|
6
|
+
declare abstract class AbstractMockComponentWrapper<T> {
|
|
7
|
+
protected _mock: jest.Mocked<unknown>;
|
|
8
|
+
receivedProps: T;
|
|
9
|
+
/**
|
|
10
|
+
* The data-testid applied to the component mocks
|
|
11
|
+
*/
|
|
12
|
+
DATA_TEST_ID: string;
|
|
13
|
+
constructor();
|
|
14
|
+
private _buildPropsExpectation;
|
|
15
|
+
/**
|
|
16
|
+
* Retrieves the root element of the mocked component, which will
|
|
17
|
+
* always be a div with the component data-testid applied
|
|
18
|
+
*
|
|
19
|
+
* Should only be used when one instance of the mock component is on the screen
|
|
20
|
+
*/
|
|
21
|
+
get mockRoot(): HTMLElement;
|
|
22
|
+
/**
|
|
23
|
+
* Retrieves the root elements of all of the mocked component instances, which will
|
|
24
|
+
* always be a div with the component data-testid applied
|
|
25
|
+
*
|
|
26
|
+
* Can be used when multiple instances of the mock component are on the screen
|
|
27
|
+
*/
|
|
28
|
+
get mockRoots(): HTMLElement[];
|
|
29
|
+
/**
|
|
30
|
+
* Asserts that the mock component was called with the provided subset of props
|
|
31
|
+
* @param props The subset of props to match against
|
|
32
|
+
*/
|
|
33
|
+
assertCalledWith: (props: Partial<T>) => void;
|
|
34
|
+
/**
|
|
35
|
+
* Asserts that the mock component was last called with the provided subset of props
|
|
36
|
+
* @param props The subset of props to match against
|
|
37
|
+
*/
|
|
38
|
+
assertLastCalledWith: (props: Partial<T>) => void;
|
|
39
|
+
/**
|
|
40
|
+
* Asserts that the mock component was nth called with the provided subset of props
|
|
41
|
+
* @param props The subset of props to match against
|
|
42
|
+
* @param n The render number to check against
|
|
43
|
+
*/
|
|
44
|
+
assertNthCalledWith: (props: Partial<T>, n: number) => void;
|
|
45
|
+
/**
|
|
46
|
+
* Asserts that an instance of the component exists within the document
|
|
47
|
+
* @param allowMultiple Optional boolean to allow for multiple instances
|
|
48
|
+
* of the component to exist. When set false, if more than 1 instance
|
|
49
|
+
* of the component is on the screen this assertion will fail.
|
|
50
|
+
* @default true
|
|
51
|
+
*/
|
|
52
|
+
assertOnScreen: (allowMultiple?: boolean) => void;
|
|
53
|
+
/**
|
|
54
|
+
* Asserts that the component is not currently rendered within the document
|
|
55
|
+
*/
|
|
56
|
+
assertNotOnScreen: () => void;
|
|
57
|
+
}
|
|
58
|
+
export default AbstractMockComponentWrapper;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/// <reference types="jest" />
|
|
2
|
+
import "@testing-library/jest-dom";
|
|
3
|
+
import AbstractMockComponentWrapper from "./AbstractMockComponentWrapper";
|
|
4
|
+
declare type ReactClassComponentWithProps<T> = React.ComponentClass<T>;
|
|
5
|
+
/**
|
|
6
|
+
* A standard mock class component wrapper
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* jest.mock("path/to/my/class/component");
|
|
10
|
+
* const mockMyClassComponent = new MockClassComponentWrapper(MyClassComponent);
|
|
11
|
+
*/
|
|
12
|
+
declare class MockClassComponentWrapper<T> extends AbstractMockComponentWrapper<T> {
|
|
13
|
+
protected _mock: jest.MockedClass<ReactClassComponentWithProps<T>>;
|
|
14
|
+
constructor(actual: ReactClassComponentWithProps<T>);
|
|
15
|
+
protected _configureMockImplementation: (valueGenerator?: (props: T) => JSX.Element | null) => void;
|
|
16
|
+
/**
|
|
17
|
+
* Not recommended, retrieves the actual mocked component instance
|
|
18
|
+
*
|
|
19
|
+
* Only should be used when truly necessary
|
|
20
|
+
*/
|
|
21
|
+
get __OVERRIDE__mock(): jest.MockedClass<ReactClassComponentWithProps<T>>;
|
|
22
|
+
/**
|
|
23
|
+
* Mocks the return value of the render method
|
|
24
|
+
* @param value The value render should return
|
|
25
|
+
*/
|
|
26
|
+
mockRenderReturnValue: (value: JSX.Element | null) => void;
|
|
27
|
+
/**
|
|
28
|
+
* Mocks the implementation of the render method
|
|
29
|
+
* @param implementation The implementation to apply to render
|
|
30
|
+
*/
|
|
31
|
+
mockRenderImplementation: (implementation: (props: T) => JSX.Element | null) => void;
|
|
32
|
+
}
|
|
33
|
+
export default MockClassComponentWrapper;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/// <reference types="jest" />
|
|
2
|
+
import "@testing-library/jest-dom";
|
|
3
|
+
import AbstractMockComponentWrapper from "./AbstractMockComponentWrapper";
|
|
4
|
+
declare type ReactFunctionComponentWithProps<T> = React.FunctionComponent<T>;
|
|
5
|
+
/**
|
|
6
|
+
* A standard mock function component wrapper
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* jest.mock("path/to/my/function/component");
|
|
10
|
+
* const mockMyFunctionComponent = new MockFunctionComponentWrapper(MyFunctionComponent);
|
|
11
|
+
*/
|
|
12
|
+
declare class MockFunctionComponentWrapper<T> extends AbstractMockComponentWrapper<T> {
|
|
13
|
+
protected _mock: jest.MockedFunction<ReactFunctionComponentWithProps<T>>;
|
|
14
|
+
constructor(actual: ReactFunctionComponentWithProps<T>);
|
|
15
|
+
protected _configureMockImplementation: (valueGenerator?: (props: T) => JSX.Element | null) => void;
|
|
16
|
+
/**
|
|
17
|
+
* Not recommended, retrieves the actual mocked component instance
|
|
18
|
+
*
|
|
19
|
+
* Only should be used when truly necessary
|
|
20
|
+
*/
|
|
21
|
+
get __OVERRIDE__mock(): jest.MockedFunction<ReactFunctionComponentWithProps<T>>;
|
|
22
|
+
/**
|
|
23
|
+
* Mocks the return value of the function component
|
|
24
|
+
* @param value The value the mocked function component should return
|
|
25
|
+
*/
|
|
26
|
+
mockReturnValue: (value: JSX.Element | null) => void;
|
|
27
|
+
/**
|
|
28
|
+
* Mocks the implementation of the function component
|
|
29
|
+
* @param implementation The implementation to apply to the mock function component
|
|
30
|
+
*/
|
|
31
|
+
mockImplementation: (implementation: (props: T) => JSX.Element | null) => void;
|
|
32
|
+
}
|
|
33
|
+
export default MockFunctionComponentWrapper;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/// <reference types="jest" />
|
|
2
|
+
declare type AnyFunction = (...args: any[]) => any;
|
|
3
|
+
declare class JestUtilities {
|
|
4
|
+
static assertAsMockFunction: <T extends AnyFunction>(actual: T) => jest.MockedFunction<T>;
|
|
5
|
+
static assertAsMockClass: <T extends jest.Constructable>(actual: T) => jest.MockedClass<T>;
|
|
6
|
+
}
|
|
7
|
+
export default JestUtilities;
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/// <reference types="jest" />
|
|
2
|
+
import "@testing-library/jest-dom";
|
|
3
|
+
/**
|
|
4
|
+
* Abstract base mock component wrapper class
|
|
5
|
+
*/
|
|
6
|
+
declare abstract class AbstractMockComponentWrapper<T> {
|
|
7
|
+
protected _mock: jest.Mocked<unknown>;
|
|
8
|
+
receivedProps: T;
|
|
9
|
+
/**
|
|
10
|
+
* The data-testid applied to the component mocks
|
|
11
|
+
*/
|
|
12
|
+
DATA_TEST_ID: string;
|
|
13
|
+
constructor();
|
|
14
|
+
private _buildPropsExpectation;
|
|
15
|
+
/**
|
|
16
|
+
* Retrieves the root element of the mocked component, which will
|
|
17
|
+
* always be a div with the component data-testid applied
|
|
18
|
+
*
|
|
19
|
+
* Should only be used when one instance of the mock component is on the screen
|
|
20
|
+
*/
|
|
21
|
+
get mockRoot(): HTMLElement;
|
|
22
|
+
/**
|
|
23
|
+
* Retrieves the root elements of all of the mocked component instances, which will
|
|
24
|
+
* always be a div with the component data-testid applied
|
|
25
|
+
*
|
|
26
|
+
* Can be used when multiple instances of the mock component are on the screen
|
|
27
|
+
*/
|
|
28
|
+
get mockRoots(): HTMLElement[];
|
|
29
|
+
/**
|
|
30
|
+
* Asserts that the mock component was called with the provided subset of props
|
|
31
|
+
* @param props The subset of props to match against
|
|
32
|
+
*/
|
|
33
|
+
assertCalledWith: (props: Partial<T>) => void;
|
|
34
|
+
/**
|
|
35
|
+
* Asserts that the mock component was last called with the provided subset of props
|
|
36
|
+
* @param props The subset of props to match against
|
|
37
|
+
*/
|
|
38
|
+
assertLastCalledWith: (props: Partial<T>) => void;
|
|
39
|
+
/**
|
|
40
|
+
* Asserts that the mock component was nth called with the provided subset of props
|
|
41
|
+
* @param props The subset of props to match against
|
|
42
|
+
* @param n The render number to check against
|
|
43
|
+
*/
|
|
44
|
+
assertNthCalledWith: (props: Partial<T>, n: number) => void;
|
|
45
|
+
/**
|
|
46
|
+
* Asserts that an instance of the component exists within the document
|
|
47
|
+
* @param allowMultiple Optional boolean to allow for multiple instances
|
|
48
|
+
* of the component to exist. When set false, if more than 1 instance
|
|
49
|
+
* of the component is on the screen this assertion will fail.
|
|
50
|
+
* @default true
|
|
51
|
+
*/
|
|
52
|
+
assertOnScreen: (allowMultiple?: boolean) => void;
|
|
53
|
+
/**
|
|
54
|
+
* Asserts that the component is not currently rendered within the document
|
|
55
|
+
*/
|
|
56
|
+
assertNotOnScreen: () => void;
|
|
57
|
+
}
|
|
58
|
+
export default AbstractMockComponentWrapper;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/// <reference types="jest" />
|
|
2
|
+
import "@testing-library/jest-dom";
|
|
3
|
+
import AbstractMockComponentWrapper from "./AbstractMockComponentWrapper";
|
|
4
|
+
declare type ReactClassComponentWithProps<T> = React.ComponentClass<T>;
|
|
5
|
+
/**
|
|
6
|
+
* A standard mock class component wrapper
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* jest.mock("path/to/my/class/component");
|
|
10
|
+
* const mockMyClassComponent = new MockClassComponentWrapper(MyClassComponent);
|
|
11
|
+
*/
|
|
12
|
+
declare class MockClassComponentWrapper<T> extends AbstractMockComponentWrapper<T> {
|
|
13
|
+
protected _mock: jest.MockedClass<ReactClassComponentWithProps<T>>;
|
|
14
|
+
constructor(actual: ReactClassComponentWithProps<T>);
|
|
15
|
+
protected _configureMockImplementation: (valueGenerator?: (props: T) => JSX.Element | null) => void;
|
|
16
|
+
/**
|
|
17
|
+
* Not recommended, retrieves the actual mocked component instance
|
|
18
|
+
*
|
|
19
|
+
* Only should be used when truly necessary
|
|
20
|
+
*/
|
|
21
|
+
get __OVERRIDE__mock(): jest.MockedClass<ReactClassComponentWithProps<T>>;
|
|
22
|
+
/**
|
|
23
|
+
* Mocks the return value of the render method
|
|
24
|
+
* @param value The value render should return
|
|
25
|
+
*/
|
|
26
|
+
mockRenderReturnValue: (value: JSX.Element | null) => void;
|
|
27
|
+
/**
|
|
28
|
+
* Mocks the implementation of the render method
|
|
29
|
+
* @param implementation The implementation to apply to render
|
|
30
|
+
*/
|
|
31
|
+
mockRenderImplementation: (implementation: (props: T) => JSX.Element | null) => void;
|
|
32
|
+
}
|
|
33
|
+
export default MockClassComponentWrapper;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/// <reference types="jest" />
|
|
2
|
+
import "@testing-library/jest-dom";
|
|
3
|
+
import AbstractMockComponentWrapper from "./AbstractMockComponentWrapper";
|
|
4
|
+
declare type ReactFunctionComponentWithProps<T> = React.FunctionComponent<T>;
|
|
5
|
+
/**
|
|
6
|
+
* A standard mock function component wrapper
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* jest.mock("path/to/my/function/component");
|
|
10
|
+
* const mockMyFunctionComponent = new MockFunctionComponentWrapper(MyFunctionComponent);
|
|
11
|
+
*/
|
|
12
|
+
declare class MockFunctionComponentWrapper<T> extends AbstractMockComponentWrapper<T> {
|
|
13
|
+
protected _mock: jest.MockedFunction<ReactFunctionComponentWithProps<T>>;
|
|
14
|
+
constructor(actual: ReactFunctionComponentWithProps<T>);
|
|
15
|
+
protected _configureMockImplementation: (valueGenerator?: (props: T) => JSX.Element | null) => void;
|
|
16
|
+
/**
|
|
17
|
+
* Not recommended, retrieves the actual mocked component instance
|
|
18
|
+
*
|
|
19
|
+
* Only should be used when truly necessary
|
|
20
|
+
*/
|
|
21
|
+
get __OVERRIDE__mock(): jest.MockedFunction<ReactFunctionComponentWithProps<T>>;
|
|
22
|
+
/**
|
|
23
|
+
* Mocks the return value of the function component
|
|
24
|
+
* @param value The value the mocked function component should return
|
|
25
|
+
*/
|
|
26
|
+
mockReturnValue: (value: JSX.Element | null) => void;
|
|
27
|
+
/**
|
|
28
|
+
* Mocks the implementation of the function component
|
|
29
|
+
* @param implementation The implementation to apply to the mock function component
|
|
30
|
+
*/
|
|
31
|
+
mockImplementation: (implementation: (props: T) => JSX.Element | null) => void;
|
|
32
|
+
}
|
|
33
|
+
export default MockFunctionComponentWrapper;
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import{jsx as t}from"react/jsx-runtime";import"@testing-library/jest-dom";import{screen as e}from"@testing-library/react";class o{static assertAsMockFunction=t=>t;static assertAsMockClass=t=>t}class s{_mock;receivedProps;DATA_TEST_ID;constructor(){this.DATA_TEST_ID=Math.random().toString(36).slice(2)}_buildPropsExpectation=t=>[expect.objectContaining({...t}),expect.anything()];get mockRoot(){return e.getByTestId(this.DATA_TEST_ID)}get mockRoots(){return e.getAllByTestId(this.DATA_TEST_ID)}assertCalledWith=t=>{expect(this._mock).toHaveBeenCalledWith(...this._buildPropsExpectation(t))};assertLastCalledWith=t=>{expect(this._mock).toHaveBeenLastCalledWith(...this._buildPropsExpectation(t))};assertNthCalledWith=(t,e)=>{expect(this._mock).toHaveBeenNthCalledWith(e,...this._buildPropsExpectation(t))};assertOnScreen=(t=!0)=>{t?expect(this.mockRoots.length).toBeGreaterThanOrEqual(1):expect(this.mockRoot).toBeInTheDocument()};assertNotOnScreen=()=>{expect((()=>this.mockRoot)).toThrow()}}class i extends s{constructor(t){super(),this._mock=o.assertAsMockClass(t),this._configureMockImplementation()}_configureMockImplementation=e=>{this._mock.mockImplementation((o=>(this.receivedProps=o,{render:()=>t("div",{"data-testid":this.DATA_TEST_ID,children:e&&e(o)})})))};get __OVERRIDE__mock(){return this._mock}mockRenderReturnValue=t=>{this._configureMockImplementation((()=>t))};mockRenderImplementation=t=>{this._configureMockImplementation((e=>t(e)))}}class c extends s{constructor(t){super(),this._mock=o.assertAsMockFunction(t),this._configureMockImplementation()}_configureMockImplementation=e=>{this._mock.mockImplementation((o=>(this.receivedProps=o,t("div",{"data-testid":this.DATA_TEST_ID,children:e&&e(o)}))))};get __OVERRIDE__mock(){return this._mock}mockReturnValue=t=>{this._configureMockImplementation((()=>t))};mockImplementation=t=>{this._configureMockImplementation((e=>t(e)))}}export{o as JestUtilities,i as MockClassComponentWrapper,c as MockFunctionComponentWrapper};
|
|
2
|
+
//# sourceMappingURL=index.esm.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.esm.js","sources":["../src/jest/index.ts","../src/react/AbstractMockComponentWrapper.tsx","../src/react/MockClassComponentWrapper.tsx","../src/react/MockFunctionComponentWrapper.tsx"],"sourcesContent":[null,null,null,null],"names":["JestUtilities","static","actual","AbstractMockComponentWrapper","_mock","receivedProps","DATA_TEST_ID","constructor","this","Math","random","toString","slice","_buildPropsExpectation","props","expect","objectContaining","anything","mockRoot","screen","getByTestId","mockRoots","getAllByTestId","assertCalledWith","toHaveBeenCalledWith","assertLastCalledWith","toHaveBeenLastCalledWith","assertNthCalledWith","n","toHaveBeenNthCalledWith","assertOnScreen","allowMultiple","length","toBeGreaterThanOrEqual","toBeInTheDocument","assertNotOnScreen","toThrow","MockClassComponentWrapper","super","assertAsMockClass","_configureMockImplementation","valueGenerator","mockImplementation","render","_jsx","children","__OVERRIDE__mock","mockRenderReturnValue","value","mockRenderImplementation","implementation","MockFunctionComponentWrapper","assertAsMockFunction","mockReturnValue"],"mappings":"0HAEA,MAAMA,EACJC,4BACEC,GAEuBA,EAKzBD,yBACEC,GAEoBA,ECRxB,MAAeC,EACHC,MAEVC,cAKAC,aAEAC,cACEC,KAAKF,aAAeG,KAAKC,SAASC,SAAS,IAAIC,MAAM,GAI/CC,uBAA0BC,GAAsB,CACtDC,OAAOC,iBAAiB,IACnBF,IAELC,OAAOE,YASLC,eACF,OAAOC,EAAOC,YAAYZ,KAAKF,cAS7Be,gBACF,OAAOF,EAAOG,eAAed,KAAKF,cAOpCiB,iBAAoBT,IAClBC,OAAOP,KAAKJ,OAAOoB,wBACdhB,KAAKK,uBAAuBC,KAQnCW,qBAAwBX,IACtBC,OAAOP,KAAKJ,OAAOsB,4BACdlB,KAAKK,uBAAuBC,KASnCa,oBAAsB,CAACb,EAAmBc,KACxCb,OAAOP,KAAKJ,OAAOyB,wBACjBD,KACGpB,KAAKK,uBAAuBC,KAWnCgB,eAAiB,CAACC,GAAgB,KAC5BA,EACFhB,OAAOP,KAAKa,UAAUW,QAAQC,uBAAuB,GAErDlB,OAAOP,KAAKU,UAAUgB,qBAO1BC,kBAAoB,KAClBpB,QAAO,IAAMP,KAAKU,WAAUkB,WCrFhC,MAAMC,UAAqClC,EAGzCI,YAAYL,GACVoC,QACA9B,KAAKJ,MAAQJ,EAAcuC,kBAAkBrC,GAE7CM,KAAKgC,+BAGGA,6BACRC,IAIAjC,KAAKJ,MAAMsC,oBAAoB5B,IAC7BN,KAAKH,cAAgBS,EAEd,CACL6B,OAAQ,IACNC,uBAAkBpC,KAAKF,aAAYuC,SAChCJ,GAAkBA,EAAe3B,UAYxCgC,uBACF,OAAOtC,KAAKJ,MAOd2C,sBAAyBC,IACvBxC,KAAKgC,8BAA6B,IAAMQ,KAO1CC,yBACEC,IAEA1C,KAAKgC,8BAA8B1B,GAAUoC,EAAepC,MCpDhE,MAAMqC,UAAwChD,EAK5CI,YAAYL,GACVoC,QACA9B,KAAKJ,MAAQJ,EAAcoD,qBAAqBlD,GAEhDM,KAAKgC,+BAGGA,6BACRC,IAEAjC,KAAKJ,MAAMsC,oBAAoB5B,IAC7BN,KAAKH,cAAgBS,EAGnB8B,EAAA,MAAA,CAAA,cAAkBpC,KAAKF,aACpBuC,SAAAJ,GAAkBA,EAAe3B,SAWtCgC,uBACF,OAAOtC,KAAKJ,MAOdiD,gBAAmBL,IACjBxC,KAAKgC,8BAA6B,IAAMQ,KAO1CN,mBACEQ,IAEA1C,KAAKgC,8BAA8B1B,GAAUoC,EAAepC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("react/jsx-runtime");require("@testing-library/jest-dom");var t=require("@testing-library/react");class o{static assertAsMockFunction=e=>e;static assertAsMockClass=e=>e}class s{_mock;receivedProps;DATA_TEST_ID;constructor(){this.DATA_TEST_ID=Math.random().toString(36).slice(2)}_buildPropsExpectation=e=>[expect.objectContaining({...e}),expect.anything()];get mockRoot(){return t.screen.getByTestId(this.DATA_TEST_ID)}get mockRoots(){return t.screen.getAllByTestId(this.DATA_TEST_ID)}assertCalledWith=e=>{expect(this._mock).toHaveBeenCalledWith(...this._buildPropsExpectation(e))};assertLastCalledWith=e=>{expect(this._mock).toHaveBeenLastCalledWith(...this._buildPropsExpectation(e))};assertNthCalledWith=(e,t)=>{expect(this._mock).toHaveBeenNthCalledWith(t,...this._buildPropsExpectation(e))};assertOnScreen=(e=!0)=>{e?expect(this.mockRoots.length).toBeGreaterThanOrEqual(1):expect(this.mockRoot).toBeInTheDocument()};assertNotOnScreen=()=>{expect((()=>this.mockRoot)).toThrow()}}exports.JestUtilities=o,exports.MockClassComponentWrapper=class extends s{constructor(e){super(),this._mock=o.assertAsMockClass(e),this._configureMockImplementation()}_configureMockImplementation=t=>{this._mock.mockImplementation((o=>(this.receivedProps=o,{render:()=>e.jsx("div",{"data-testid":this.DATA_TEST_ID,children:t&&t(o)})})))};get __OVERRIDE__mock(){return this._mock}mockRenderReturnValue=e=>{this._configureMockImplementation((()=>e))};mockRenderImplementation=e=>{this._configureMockImplementation((t=>e(t)))}},exports.MockFunctionComponentWrapper=class extends s{constructor(e){super(),this._mock=o.assertAsMockFunction(e),this._configureMockImplementation()}_configureMockImplementation=t=>{this._mock.mockImplementation((o=>(this.receivedProps=o,e.jsx("div",{"data-testid":this.DATA_TEST_ID,children:t&&t(o)}))))};get __OVERRIDE__mock(){return this._mock}mockReturnValue=e=>{this._configureMockImplementation((()=>e))};mockImplementation=e=>{this._configureMockImplementation((t=>e(t)))}};
|
|
2
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../src/jest/index.ts","../src/react/AbstractMockComponentWrapper.tsx","../src/react/MockClassComponentWrapper.tsx","../src/react/MockFunctionComponentWrapper.tsx"],"sourcesContent":[null,null,null,null],"names":["JestUtilities","static","actual","AbstractMockComponentWrapper","_mock","receivedProps","DATA_TEST_ID","constructor","this","Math","random","toString","slice","_buildPropsExpectation","props","expect","objectContaining","anything","mockRoot","screen","getByTestId","mockRoots","getAllByTestId","assertCalledWith","toHaveBeenCalledWith","assertLastCalledWith","toHaveBeenLastCalledWith","assertNthCalledWith","n","toHaveBeenNthCalledWith","assertOnScreen","allowMultiple","length","toBeGreaterThanOrEqual","toBeInTheDocument","assertNotOnScreen","toThrow","super","assertAsMockClass","_configureMockImplementation","valueGenerator","mockImplementation","render","_jsx","children","__OVERRIDE__mock","mockRenderReturnValue","value","mockRenderImplementation","implementation","assertAsMockFunction","mockReturnValue"],"mappings":"oLAEA,MAAMA,EACJC,4BACEC,GAEuBA,EAKzBD,yBACEC,GAEoBA,ECRxB,MAAeC,EACHC,MAEVC,cAKAC,aAEAC,cACEC,KAAKF,aAAeG,KAAKC,SAASC,SAAS,IAAIC,MAAM,GAI/CC,uBAA0BC,GAAsB,CACtDC,OAAOC,iBAAiB,IACnBF,IAELC,OAAOE,YASLC,eACF,OAAOC,SAAOC,YAAYZ,KAAKF,cAS7Be,gBACF,OAAOF,SAAOG,eAAed,KAAKF,cAOpCiB,iBAAoBT,IAClBC,OAAOP,KAAKJ,OAAOoB,wBACdhB,KAAKK,uBAAuBC,KAQnCW,qBAAwBX,IACtBC,OAAOP,KAAKJ,OAAOsB,4BACdlB,KAAKK,uBAAuBC,KASnCa,oBAAsB,CAACb,EAAmBc,KACxCb,OAAOP,KAAKJ,OAAOyB,wBACjBD,KACGpB,KAAKK,uBAAuBC,KAWnCgB,eAAiB,CAACC,GAAgB,KAC5BA,EACFhB,OAAOP,KAAKa,UAAUW,QAAQC,uBAAuB,GAErDlB,OAAOP,KAAKU,UAAUgB,qBAO1BC,kBAAoB,KAClBpB,QAAO,IAAMP,KAAKU,WAAUkB,qECrFhC,cAA2CjC,EAGzCI,YAAYL,GACVmC,QACA7B,KAAKJ,MAAQJ,EAAcsC,kBAAkBpC,GAE7CM,KAAK+B,+BAGGA,6BACRC,IAIAhC,KAAKJ,MAAMqC,oBAAoB3B,IAC7BN,KAAKH,cAAgBS,EAEd,CACL4B,OAAQ,IACNC,2BAAkBnC,KAAKF,aAAYsC,SAChCJ,GAAkBA,EAAe1B,UAYxC+B,uBACF,OAAOrC,KAAKJ,MAOd0C,sBAAyBC,IACvBvC,KAAK+B,8BAA6B,IAAMQ,KAO1CC,yBACEC,IAEAzC,KAAK+B,8BAA8BzB,GAAUmC,EAAenC,4CCpDhE,cAA8CX,EAK5CI,YAAYL,GACVmC,QACA7B,KAAKJ,MAAQJ,EAAckD,qBAAqBhD,GAEhDM,KAAK+B,+BAGGA,6BACRC,IAEAhC,KAAKJ,MAAMqC,oBAAoB3B,IAC7BN,KAAKH,cAAgBS,EAGnB6B,MAAA,MAAA,CAAA,cAAkBnC,KAAKF,aACpBsC,SAAAJ,GAAkBA,EAAe1B,SAWtC+B,uBACF,OAAOrC,KAAKJ,MAOd+C,gBAAmBJ,IACjBvC,KAAK+B,8BAA6B,IAAMQ,KAO1CN,mBACEQ,IAEAzC,KAAK+B,8BAA8BzB,GAAUmC,EAAenC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@ryanbrandt/react-testing-utils",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"repository": "https://github.com/ryanbrandt/react-testing-utils",
|
|
5
|
+
"description": "Common Jest/React/RTL testing utilities",
|
|
6
|
+
"author": "Ryan Brandt",
|
|
7
|
+
"main": "dist/index.js",
|
|
8
|
+
"module": "dist/index.esm.js",
|
|
9
|
+
"typings": "dist/@types/index",
|
|
10
|
+
"files": [
|
|
11
|
+
"dist"
|
|
12
|
+
],
|
|
13
|
+
"publishConfig": {
|
|
14
|
+
"access": "public"
|
|
15
|
+
},
|
|
16
|
+
"scripts": {
|
|
17
|
+
"test": "jest --coverage",
|
|
18
|
+
"build": "rollup -c"
|
|
19
|
+
},
|
|
20
|
+
"devDependencies": {
|
|
21
|
+
"@rollup/plugin-commonjs": "^22.0.0",
|
|
22
|
+
"@rollup/plugin-node-resolve": "^13.3.0",
|
|
23
|
+
"@rollup/plugin-typescript": "^8.3.2",
|
|
24
|
+
"@testing-library/jest-dom": "^5.16.4",
|
|
25
|
+
"@testing-library/react": "^13.0.1",
|
|
26
|
+
"@testing-library/user-event": "^14.1.0",
|
|
27
|
+
"@types/jest": "^27.4.1",
|
|
28
|
+
"@types/node": "^17.0.23",
|
|
29
|
+
"@types/react": "^18.0.1",
|
|
30
|
+
"@typescript-eslint/eslint-plugin": "^5.19.0",
|
|
31
|
+
"@typescript-eslint/parser": "^5.19.0",
|
|
32
|
+
"eslint": "^8.13.0",
|
|
33
|
+
"eslint-config-airbnb-base": "^15.0.0",
|
|
34
|
+
"eslint-config-prettier": "^8.5.0",
|
|
35
|
+
"eslint-import-resolver-typescript": "^2.7.1",
|
|
36
|
+
"eslint-plugin-import": "^2.26.0",
|
|
37
|
+
"jest": "^27.5.1",
|
|
38
|
+
"prettier": "^2.6.2",
|
|
39
|
+
"react": "^18.0.0",
|
|
40
|
+
"react-dom": "^18.0.0",
|
|
41
|
+
"rollup": "^2.73.0",
|
|
42
|
+
"rollup-plugin-peer-deps-external": "^2.2.4",
|
|
43
|
+
"rollup-plugin-terser": "^7.0.2",
|
|
44
|
+
"ts-jest": "^27.1.4",
|
|
45
|
+
"ttypescript": "^1.5.13",
|
|
46
|
+
"typescript": "^4.6.3",
|
|
47
|
+
"typescript-transform-paths": "^3.3.1"
|
|
48
|
+
},
|
|
49
|
+
"peerDependencies": {
|
|
50
|
+
"@testing-library/jest-dom": ">=5.14.1",
|
|
51
|
+
"@testing-library/react": ">=12.1.2",
|
|
52
|
+
"jest": ">=27.3.1",
|
|
53
|
+
"react": ">=17.0.2",
|
|
54
|
+
"react-dom": ">=17.0.2"
|
|
55
|
+
},
|
|
56
|
+
"packageManager": "yarn@3.2.0"
|
|
57
|
+
}
|