@ryanbrandt/react-testing-utils 0.1.0 → 0.4.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 CHANGED
@@ -1,49 +1,154 @@
1
1
  # react-testing-utils
2
2
 
3
- Common wrappers and utilities for unit testing and mocking in React applications with Jest and RTL
3
+ Wrappers and utilities for unit testing React components with Jest and React Testing Library.
4
4
 
5
5
  ## Usage
6
6
 
7
7
  ```tsx
8
- yarn add react-testing-utils
8
+ yarn add @ryanbrandt/react-testing-utils
9
9
 
10
10
  // or
11
11
 
12
- npm install react-testing-utils
12
+ npm install @ryanbrandt/react-testing-utils
13
13
  ```
14
14
 
15
- **Warning:** This project has several required peer dependencies which must be installed in your consuming project
15
+ **Required Peer Dependencies**
16
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+)
17
+ ```json
18
+ "peerDependencies": {
19
+ "@testing-library/dom": ">=8.13.0",
20
+ "@testing-library/jest-dom": ">=5.14.1",
21
+ "@testing-library/react": ">=12.1.2",
22
+ "@testing-library/user-event": ">=14.1.0",
23
+ "jest": ">=28.1.0",
24
+ "jest-environment-jsdom": ">=28.1.0",
25
+ "react": ">=17.0.2",
26
+ "react-dom": ">=17.0.2",
27
+ }
28
+ ```
29
+
30
+ ## API
22
31
 
23
- ### Example Usage of Key Utilities
32
+ ### Mocking Function Components
24
33
 
25
34
  ```tsx
26
- jest.mock("path/to/your/component");
27
- const mockedMyComponent = new MockFunctionComponentWrapper(MyComponent);
35
+ jest.mock("path/to/component");
36
+ const mockFunctionComponent = new MockFunctionComponent(MyFunctionComponent);
37
+ ```
28
38
 
29
- jest.mock("path/to/some/hook");
30
- // resolves to Jest.MockedFunction<typeof useMyHook>;
31
- const mockUseMyHook = JestUtilities.assertAsMockFunction(useMyHook);
39
+ ### Mocking Class Components
32
40
 
33
- describe("SomeOtherComponent", () => {
34
- it("renders a MyComponent", () => {
35
- render(<SomeOtherComponent />);
41
+ ```tsx
42
+ jest.mock("path/to/component");
43
+ const mockClassComponent = new MockClassComponent(MyClassComponent);
44
+ ```
36
45
 
37
- mockedMyComponent.assertOnScreen();
38
- mockedMyComponent.assertCalledWith({ someProps: "expected value" });
39
- });
46
+ ### Methods
47
+
48
+ **Setup**
49
+
50
+ A setup is provided by default as `<div data-testid={MockComponent.DATA_TEST_ID} />` when instantiated.
51
+
52
+ However, if a specific setup is needed, you can define your own. Each mock provided mock setup
53
+ will be wrapped in a div as follows: `<div data-testid={MockComponent.DATA_TEST_ID}>{MockSetup}</div>`
54
+
55
+ ```tsx
56
+ // Function
57
+
58
+ mockFunctionComponent.mockReturnValue(<div />);
59
+ mockFunctionComponent.mockImplementation((props) => <div>{children}</div>);
60
+
61
+ // Class
62
+
63
+ mockClassComponent.mockRenderReturnValue(<div />);
64
+ mockClassComponent.mockRenderImplementation((props) => (
65
+ <div>{props.children}</div>
66
+ ));
67
+ ```
68
+
69
+ **Getters**
70
+
71
+ ```tsx
72
+ it("renders one instance", () => {
73
+ ...
74
+ expect(mockComponent.mockRoot).toBeInTheDocument();
75
+ });
76
+
77
+ it("renders many instances", () => {
78
+ expect(mockComponent.mockRoots.length).toBeGreaterThan(1);
79
+ });
80
+
81
+ ```
82
+
83
+ **Click Interactions**
84
+
85
+ ```tsx
86
+
87
+ it("does something when clicked", async () => {
88
+ ...
89
+ await mockComponent.click();
90
+ });
40
91
 
41
- describe("when useMyHook does something", () => {
42
- mockUseMyHook.mockReturnValue("something done");
92
+ it("does something when an instance is clicked", async () => {
93
+ ...
94
+ await mockComponent.clickInstance(1);
95
+ });
96
+
97
+ ```
98
+
99
+ **Typing Interactions**
100
+
101
+ ```tsx
102
+
103
+ it("does something when typed into", async () => {
104
+ ...
105
+ await mockComponent.type("Foo");
106
+ });
107
+
108
+ it("does something when an instance is typed into", async () => {
109
+ ...
110
+ await mockComponent.typeInstance("Foo", 1);
111
+ });
112
+
113
+ ```
114
+
115
+ **Screen Assertions**
116
+
117
+ ```tsx
118
+ it("is on the screen", () => {
119
+ ...
120
+ mockComponent.assertOnScreen();
121
+ });
122
+
123
+ it("is not on the screen", () => {
124
+ ...
125
+ mockComponent.assertNotOnScreen();
126
+ });
127
+
128
+ ```
129
+
130
+ **Invocation Assertions**
43
131
 
44
- render(<SomeOtherComponent />);
132
+ ```tsx
133
+ it("is called with what I expect", () => {
134
+ ...
135
+ mockComponent.assertCalledWith({
136
+ foo: "bar",
137
+ });
138
+ });
45
139
 
46
- ...
47
- })
140
+ it("is called with what I expect when I expect it", () => {
141
+ ...
142
+ mockComponent.assertNthCalledWith({
143
+ foo: "bar",
144
+ }, 1);
48
145
  });
146
+
147
+ it("is last called with what I expect", () => {
148
+ ...
149
+ mockComponent.assertLastCalledWith({
150
+ foo: "bar"
151
+ });
152
+ });
153
+
49
154
  ```
@@ -0,0 +1,106 @@
1
+ /// <reference types="jest" />
2
+ import "@testing-library/jest-dom";
3
+ /**
4
+ * Abstract base mock component class
5
+ */
6
+ declare abstract class AbstractMockComponent<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 this.DATA_TEST_ID applied
18
+ *
19
+ * If more than one instance of the component or no instances of the component
20
+ * are on the screen this method will throw
21
+ *
22
+ * @see mockRoots
23
+ */
24
+ get mockRoot(): HTMLElement;
25
+ /**
26
+ * Retrieves the root elements of all of the mocked component instances, which will
27
+ * always be a div with this.DATA_TEST_ID applied
28
+ *
29
+ * Can be used when one or many instances of the mock component exist on the screen.
30
+ * If no instances exist this method will throw
31
+ *
32
+ * @see mockRoot
33
+ */
34
+ get mockRoots(): HTMLElement[];
35
+ /**
36
+ * Asserts that the mock component was called with the provided subset of props
37
+ * @param props The subset of props to match against
38
+ */
39
+ assertCalledWith: (props: Partial<T>) => void;
40
+ /**
41
+ * Asserts that the mock component was last called with the provided subset of props
42
+ * @param props The subset of props to match against
43
+ */
44
+ assertLastCalledWith: (props: Partial<T>) => void;
45
+ /**
46
+ * Asserts that the mock component was nth called with the provided subset of props
47
+ * @param props The subset of props to match against
48
+ * @param n The render number to check against
49
+ */
50
+ assertNthCalledWith: (props: Partial<T>, n: number) => void;
51
+ /**
52
+ * Asserts that an instance of the component exists within the document
53
+ * @param allowMultiple Optional boolean to allow for multiple instances
54
+ * of the component to exist. When set false, if more than 1 instance
55
+ * of the component is on the screen this assertion will fail.
56
+ * @default true
57
+ */
58
+ assertOnScreen: (allowMultiple?: boolean) => void;
59
+ /**
60
+ * Asserts that the component is not currently rendered within the document
61
+ */
62
+ assertNotOnScreen: () => void;
63
+ /**
64
+ * Clicks the root element defined in the mock component's return value/implementation
65
+ * This method will throw if no or more than a single instance of the mock component
66
+ * exists on the screen
67
+ *
68
+ * @param root The root element to click
69
+ * @default this.mockRoot.firstChild
70
+ *
71
+ * @see clickInstance
72
+ *
73
+ */
74
+ click: (root?: HTMLElement) => Promise<void>;
75
+ /**
76
+ * Clicks the root element of a specific instance of the mock component as defined in the
77
+ * mock return value/implementation.This method will throw if the provided index is not found.
78
+ *
79
+ * @param instanceIndex The index of the instance which should be clicked
80
+ *
81
+ * @see click
82
+ *
83
+ */
84
+ clickInstance: (instanceIndex: number) => Promise<void>;
85
+ /**
86
+ * Types into the root element of your mock. This method will throw if the root element
87
+ * does not support typing or if there are none or mulitple instances of the mock on the
88
+ * screen.
89
+ *
90
+ * @param value The value to type
91
+ * @param root The root element to type into
92
+ * @default this.mockRoot.firstChild
93
+ *
94
+ * @see typeInstance
95
+ */
96
+ type: (value: string, root?: HTMLElement) => Promise<void>;
97
+ /**
98
+ * Types into the root element of a specific instance of the mock component. This method will throw
99
+ * if the root element does not support typing or if the provided instance index is not found.
100
+ *
101
+ * @param value The value to type
102
+ * @param instanceIndex The index of the instance to type into
103
+ */
104
+ typeInstance: (value: string, instanceIndex: number) => Promise<void>;
105
+ }
106
+ export default AbstractMockComponent;
@@ -1,16 +1,16 @@
1
1
  /// <reference types="jest" />
2
2
  import "@testing-library/jest-dom";
3
- import AbstractMockComponentWrapper from "./AbstractMockComponentWrapper";
3
+ import AbstractMockComponent from "./AbstractMockComponent";
4
4
  declare type ReactClassComponentWithProps<T> = React.ComponentClass<T>;
5
5
  /**
6
- * A standard mock class component wrapper
6
+ * A standard mock class component
7
7
  *
8
8
  * @example
9
9
  * jest.mock("path/to/my/class/component");
10
- * const mockMyClassComponent = new MockClassComponentWrapper(MyClassComponent);
10
+ * const mockMyClassComponent = new MockClassComponent(MyClassComponent);
11
11
  */
12
- declare class MockClassComponentWrapper<T> extends AbstractMockComponentWrapper<T> {
13
- protected _mock: jest.MockedClass<ReactClassComponentWithProps<T>>;
12
+ declare class MockClassComponent<T> extends AbstractMockComponent<T> {
13
+ protected _mock: jest.MockedObject<ReactClassComponentWithProps<T>>;
14
14
  constructor(actual: ReactClassComponentWithProps<T>);
15
15
  protected _configureMockImplementation: (valueGenerator?: (props: T) => JSX.Element | null) => void;
16
16
  /**
@@ -18,7 +18,7 @@ declare class MockClassComponentWrapper<T> extends AbstractMockComponentWrapper<
18
18
  *
19
19
  * Only should be used when truly necessary
20
20
  */
21
- get __OVERRIDE__mock(): jest.MockedClass<ReactClassComponentWithProps<T>>;
21
+ get __OVERRIDE__mock(): jest.MockedObject<ReactClassComponentWithProps<T>>;
22
22
  /**
23
23
  * Mocks the return value of the render method
24
24
  * @param value The value render should return
@@ -30,4 +30,4 @@ declare class MockClassComponentWrapper<T> extends AbstractMockComponentWrapper<
30
30
  */
31
31
  mockRenderImplementation: (implementation: (props: T) => JSX.Element | null) => void;
32
32
  }
33
- export default MockClassComponentWrapper;
33
+ export default MockClassComponent;
@@ -1,15 +1,15 @@
1
1
  /// <reference types="jest" />
2
2
  import "@testing-library/jest-dom";
3
- import AbstractMockComponentWrapper from "./AbstractMockComponentWrapper";
3
+ import AbstractMockComponent from "./AbstractMockComponent";
4
4
  declare type ReactFunctionComponentWithProps<T> = React.FunctionComponent<T>;
5
5
  /**
6
- * A standard mock function component wrapper
6
+ * A standard mock function component instance
7
7
  *
8
8
  * @example
9
9
  * jest.mock("path/to/my/function/component");
10
- * const mockMyFunctionComponent = new MockFunctionComponentWrapper(MyFunctionComponent);
10
+ * const mockMyFunctionComponent = new MockFunctionComponent(MyFunctionComponent);
11
11
  */
12
- declare class MockFunctionComponentWrapper<T> extends AbstractMockComponentWrapper<T> {
12
+ declare class MockFunctionComponent<T> extends AbstractMockComponent<T> {
13
13
  protected _mock: jest.MockedFunction<ReactFunctionComponentWithProps<T>>;
14
14
  constructor(actual: ReactFunctionComponentWithProps<T>);
15
15
  protected _configureMockImplementation: (valueGenerator?: (props: T) => JSX.Element | null) => void;
@@ -30,4 +30,4 @@ declare class MockFunctionComponentWrapper<T> extends AbstractMockComponentWrapp
30
30
  */
31
31
  mockImplementation: (implementation: (props: T) => JSX.Element | null) => void;
32
32
  }
33
- export default MockFunctionComponentWrapper;
33
+ export default MockFunctionComponent;
@@ -1,3 +1,2 @@
1
- export { default as JestUtilities } from "./jest";
2
- export { default as MockClassComponentWrapper } from "./react/MockClassComponentWrapper";
3
- export { default as MockFunctionComponentWrapper } from "./react/MockFunctionComponentWrapper";
1
+ export { default as MockClassComponentWrapper } from "./MockClassComponent";
2
+ export { default as MockFunctionComponentWrapper } from "./MockFunctionComponent";
package/dist/index.esm.js CHANGED
@@ -1,2 +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};
1
+ import{jsx as t}from"react/jsx-runtime";import"@testing-library/jest-dom";import{screen as e}from"@testing-library/react";import o from"@testing-library/user-event";class i{_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()};click=async(t=this.mockRoot.firstChild)=>{await o.click(t)};clickInstance=async t=>{await this.click(this.mockRoots[t].firstChild)};type=async(t,e=this.mockRoot.firstChild)=>{await o.type(e,t)};typeInstance=async(t,e)=>{await this.type(t,this.mockRoots[e].firstChild)}}class s extends i{constructor(t){super(),this._mock=jest.mocked(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 i{constructor(t){super(),this._mock=jest.mocked(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{s as MockClassComponentWrapper,c as MockFunctionComponentWrapper};
2
2
  //# sourceMappingURL=index.esm.js.map
@@ -1 +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"}
1
+ {"version":3,"file":"index.esm.js","sources":["../src/AbstractMockComponent.tsx","../src/MockClassComponent.tsx","../src/MockFunctionComponent.tsx"],"sourcesContent":[null,null,null],"names":["AbstractMockComponent","_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","click","async","root","firstChild","userEvent","clickInstance","instanceIndex","type","value","typeInstance","MockClassComponent","actual","super","jest","mocked","_configureMockImplementation","valueGenerator","mockImplementation","render","_jsx","children","__OVERRIDE__mock","mockRenderReturnValue","mockRenderImplementation","implementation","MockFunctionComponent","mockReturnValue"],"mappings":"qKAOA,MAAeA,EACHC,MAEVC,cAKAC,aAEAC,cACEC,KAAKF,aAAeG,KAAKC,SAASC,SAAS,IAAIC,MAAM,GAI/CC,uBAA0BC,GAAsB,CACtDC,OAAOC,iBAAiB,IACnBF,IAELC,OAAOE,YAYLC,eACF,OAAOC,EAAOC,YAAYZ,KAAKF,cAY7Be,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,WAc9BC,MAAQC,MACNC,EAAO/B,KAAKU,SAASsB,oBAEfC,EAAUJ,MAAME,IAYxBG,cAAgBJ,MAAOK,UACfnC,KAAK6B,MAAM7B,KAAKa,UAAUsB,GAAeH,aAcjDI,KAAON,MACLO,EACAN,EAAO/B,KAAKU,SAASsB,oBAEfC,EAAUG,KAAKL,EAAMM,IAU7BC,aAAeR,MACbO,EACAF,WAEMnC,KAAKoC,KACTC,EACArC,KAAKa,UAAUsB,GAAeH,aC7JpC,MAAMO,UAA8B5C,EAGlCI,YAAYyC,GACVC,QACAzC,KAAKJ,MAAQ8C,KAAKC,OAAOH,GAEzBxC,KAAK4C,+BAGGA,6BACRC,IAIA7C,KAAKJ,MAAMkD,oBAAoBxC,IAC7BN,KAAKH,cAAgBS,EAEd,CACLyC,OAAQ,IACNC,uBAAkBhD,KAAKF,aAAYmD,SAChCJ,GAAkBA,EAAevC,UAYxC4C,uBACF,OAAOlD,KAAKJ,MAOduD,sBAAyBd,IACvBrC,KAAK4C,8BAA6B,IAAMP,KAO1Ce,yBACEC,IAEArD,KAAK4C,8BAA8BtC,GAAU+C,EAAe/C,MCpDhE,MAAMgD,UAAiC3D,EAKrCI,YAAYyC,GACVC,QACAzC,KAAKJ,MAAQ8C,KAAKC,OAAOH,GAEzBxC,KAAK4C,+BAGGA,6BACRC,IAEA7C,KAAKJ,MAAMkD,oBAAoBxC,IAC7BN,KAAKH,cAAgBS,EAGnB0C,EAAA,MAAA,CAAA,cAAkBhD,KAAKF,aACpBmD,SAAAJ,GAAkBA,EAAevC,SAWtC4C,uBACF,OAAOlD,KAAKJ,MAOd2D,gBAAmBlB,IACjBrC,KAAK4C,8BAA6B,IAAMP,KAO1CS,mBACEO,IAEArD,KAAK4C,8BAA8BtC,GAAU+C,EAAe/C"}
package/dist/index.js CHANGED
@@ -1,2 +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)))}};
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t=require("react/jsx-runtime");require("@testing-library/jest-dom");var e=require("@testing-library/react");function o(t){return t&&"object"==typeof t&&"default"in t?t:{default:t}}var i=o(require("@testing-library/user-event"));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.screen.getByTestId(this.DATA_TEST_ID)}get mockRoots(){return e.screen.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()};click=async(t=this.mockRoot.firstChild)=>{await i.default.click(t)};clickInstance=async t=>{await this.click(this.mockRoots[t].firstChild)};type=async(t,e=this.mockRoot.firstChild)=>{await i.default.type(e,t)};typeInstance=async(t,e)=>{await this.type(t,this.mockRoots[e].firstChild)}}exports.MockClassComponentWrapper=class extends s{constructor(t){super(),this._mock=jest.mocked(t),this._configureMockImplementation()}_configureMockImplementation=e=>{this._mock.mockImplementation((o=>(this.receivedProps=o,{render:()=>t.jsx("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)))}},exports.MockFunctionComponentWrapper=class extends s{constructor(t){super(),this._mock=jest.mocked(t),this._configureMockImplementation()}_configureMockImplementation=e=>{this._mock.mockImplementation((o=>(this.receivedProps=o,t.jsx("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)))}};
2
2
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +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"}
1
+ {"version":3,"file":"index.js","sources":["../src/AbstractMockComponent.tsx","../src/MockClassComponent.tsx","../src/MockFunctionComponent.tsx"],"sourcesContent":[null,null,null],"names":["AbstractMockComponent","_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","click","async","root","firstChild","userEvent","clickInstance","instanceIndex","type","value","typeInstance","actual","super","jest","mocked","_configureMockImplementation","valueGenerator","mockImplementation","render","_jsx","children","__OVERRIDE__mock","mockRenderReturnValue","mockRenderImplementation","implementation","mockReturnValue"],"mappings":"4SAOA,MAAeA,EACHC,MAEVC,cAKAC,aAEAC,cACEC,KAAKF,aAAeG,KAAKC,SAASC,SAAS,IAAIC,MAAM,GAI/CC,uBAA0BC,GAAsB,CACtDC,OAAOC,iBAAiB,IACnBF,IAELC,OAAOE,YAYLC,eACF,OAAOC,SAAOC,YAAYZ,KAAKF,cAY7Be,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,WAc9BC,MAAQC,MACNC,EAAO/B,KAAKU,SAASsB,oBAEfC,EAAS,QAACJ,MAAME,IAYxBG,cAAgBJ,MAAOK,UACfnC,KAAK6B,MAAM7B,KAAKa,UAAUsB,GAAeH,aAcjDI,KAAON,MACLO,EACAN,EAAO/B,KAAKU,SAASsB,oBAEfC,UAAUG,KAAKL,EAAMM,IAU7BC,aAAeR,MACbO,EACAF,WAEMnC,KAAKoC,KACTC,EACArC,KAAKa,UAAUsB,GAAeH,+CC7JpC,cAAoCrC,EAGlCI,YAAYwC,GACVC,QACAxC,KAAKJ,MAAQ6C,KAAKC,OAAOH,GAEzBvC,KAAK2C,+BAGGA,6BACRC,IAIA5C,KAAKJ,MAAMiD,oBAAoBvC,IAC7BN,KAAKH,cAAgBS,EAEd,CACLwC,OAAQ,IACNC,2BAAkB/C,KAAKF,aAAYkD,SAChCJ,GAAkBA,EAAetC,UAYxC2C,uBACF,OAAOjD,KAAKJ,MAOdsD,sBAAyBb,IACvBrC,KAAK2C,8BAA6B,IAAMN,KAO1Cc,yBACEC,IAEApD,KAAK2C,8BAA8BrC,GAAU8C,EAAe9C,4CCpDhE,cAAuCX,EAKrCI,YAAYwC,GACVC,QACAxC,KAAKJ,MAAQ6C,KAAKC,OAAOH,GAEzBvC,KAAK2C,+BAGGA,6BACRC,IAEA5C,KAAKJ,MAAMiD,oBAAoBvC,IAC7BN,KAAKH,cAAgBS,EAGnByC,MAAA,MAAA,CAAA,cAAkB/C,KAAKF,aACpBkD,SAAAJ,GAAkBA,EAAetC,SAWtC2C,uBACF,OAAOjD,KAAKJ,MAOdyD,gBAAmBhB,IACjBrC,KAAK2C,8BAA6B,IAAMN,KAO1CQ,mBACEO,IAEApD,KAAK2C,8BAA8BrC,GAAU8C,EAAe9C"}
package/package.json CHANGED
@@ -1,8 +1,9 @@
1
1
  {
2
2
  "name": "@ryanbrandt/react-testing-utils",
3
- "version": "0.1.0",
3
+ "version": "0.4.0",
4
4
  "repository": "https://github.com/ryanbrandt/react-testing-utils",
5
- "description": "Common Jest/React/RTL testing utilities",
5
+ "license": "MIT",
6
+ "description": "Jest/React/RTL component unit testing utilities",
6
7
  "author": "Ryan Brandt",
7
8
  "main": "dist/index.js",
8
9
  "module": "dist/index.esm.js",
@@ -21,10 +22,11 @@
21
22
  "@rollup/plugin-commonjs": "^22.0.0",
22
23
  "@rollup/plugin-node-resolve": "^13.3.0",
23
24
  "@rollup/plugin-typescript": "^8.3.2",
25
+ "@testing-library/dom": "^8.13.0",
24
26
  "@testing-library/jest-dom": "^5.16.4",
25
27
  "@testing-library/react": "^13.0.1",
26
28
  "@testing-library/user-event": "^14.1.0",
27
- "@types/jest": "^27.4.1",
29
+ "@types/jest": "^27.5.1",
28
30
  "@types/node": "^17.0.23",
29
31
  "@types/react": "^18.0.1",
30
32
  "@typescript-eslint/eslint-plugin": "^5.19.0",
@@ -34,22 +36,28 @@
34
36
  "eslint-config-prettier": "^8.5.0",
35
37
  "eslint-import-resolver-typescript": "^2.7.1",
36
38
  "eslint-plugin-import": "^2.26.0",
37
- "jest": "^27.5.1",
39
+ "jest": "28.1.0",
40
+ "jest-environment-jsdom": "^28.1.0",
38
41
  "prettier": "^2.6.2",
39
42
  "react": "^18.0.0",
40
43
  "react-dom": "^18.0.0",
41
44
  "rollup": "^2.73.0",
42
45
  "rollup-plugin-peer-deps-external": "^2.2.4",
43
46
  "rollup-plugin-terser": "^7.0.2",
44
- "ts-jest": "^27.1.4",
47
+ "ts-jest": "^28.0.2",
48
+ "ts-node": "^10.8.0",
49
+ "tslib": "^2.4.0",
45
50
  "ttypescript": "^1.5.13",
46
51
  "typescript": "^4.6.3",
47
52
  "typescript-transform-paths": "^3.3.1"
48
53
  },
49
54
  "peerDependencies": {
55
+ "@testing-library/dom": ">=8.13.0",
50
56
  "@testing-library/jest-dom": ">=5.14.1",
51
57
  "@testing-library/react": ">=12.1.2",
52
- "jest": ">=27.3.1",
58
+ "@testing-library/user-event": ">=14.1.0",
59
+ "jest": ">=28.1.0",
60
+ "jest-environment-jsdom": ">=28.1.0",
53
61
  "react": ">=17.0.2",
54
62
  "react-dom": ">=17.0.2"
55
63
  },
@@ -1,7 +0,0 @@
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;
@@ -1,58 +0,0 @@
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;
@@ -1,33 +0,0 @@
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;
@@ -1,33 +0,0 @@
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;
@@ -1,3 +0,0 @@
1
- export { default as JestUtilities } from "./jest";
2
- export { default as MockClassComponentWrapper } from "./react/MockClassComponentWrapper";
3
- export { default as MockFunctionComponentWrapper } from "./react/MockFunctionComponentWrapper";
@@ -1,7 +0,0 @@
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;
@@ -1,58 +0,0 @@
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;