Skip to content

Commit 1658fda

Browse files
committed
support-functional-components
1 parent eeae7f8 commit 1658fda

23 files changed

+2208
-101
lines changed

.github/workflows/publish.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,5 @@ jobs:
4343
run: npm config set //registry.npmjs.org/:_authToken $NPM_TOKEN
4444

4545
- name: Publish
46-
run: npx --no release-it -- --ci
46+
run: npx --no release-it -- --ci --dry-run
4747
continue-on-error: true

.vscode/launch.json

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,8 @@
1212
"program": "${workspaceRoot}/node_modules/jest/bin/jest",
1313
"request": "launch",
1414
"sourceMaps": true,
15-
"type": "node"
16-
},
17-
{
18-
"args": [],
19-
"cwd": "${workspaceRoot}",
20-
"name": "Debug app",
21-
"outFiles": [],
22-
"preLaunchTask": "npm: build",
23-
"program": "${workspaceRoot}/bin/index",
24-
"request": "launch",
25-
"runtimeArgs": ["--nolazy"],
26-
"sourceMaps": true,
27-
"type": "node"
15+
"type": "node",
16+
"runtimeArgs": ["--experimental-vm-modules"]
2817
}
2918
],
3019
"version": "0.2.0"

CHANGELOG.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,4 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10-
## [0.0.0] - 2022-12-25
11-
1210
- Initial release

README.md

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,59 @@ Simple utiltiies to help with mocking React scenarios for testing.
44

55
## Usage
66

7-
### Mock dependent components
8-
9-
You can check our comprehesive unit tests for more examples of testing scenarios
7+
The best option is to check out our [unit tests](./src/functional-component/index.test.tsx) to see different uses however here's a quick code snippet.
108

119
```typescript
10+
// Step 1: if using typescript, import the Props for the child component
11+
import type { ChildProps } from "./test-asset.child.js";
12+
13+
// Step 2: Now mock the child component
14+
jest.unstable_mockModule("./test-asset.child.js", () => ({
15+
Child: createMockFunctionComponent<ChildProps>("button"),
16+
}));
17+
18+
// Step 3: Import the parent and child, mocking the child
19+
const { Parent, parentTestIdMap } = await import("./parent.js");
20+
const { Child } = jest.mocked(await import("./test-asset.child.js"));
21+
22+
afterEach(() => {
23+
Child.mockClear();
24+
});
25+
26+
// Step 4: Write your test
27+
it("Mock child callback causes click count to increase", async () => {
28+
// Act
29+
const result = render(<Parent />);
30+
await userEvent.click(result.getByRole("button"));
1231

32+
// Assert
33+
const countElement = result.getByTestId("click-count");
34+
expect(countElement.innerHTML).toBe("1");
35+
});
1336
```
1437

38+
### I get an error when using await import
39+
40+
There's two halves to this problem:
41+
42+
1. Dynamic imports (e.g. `import("...")`) were implemented in ES2020:
43+
- Does your runtime environment support this? Node started [support](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/import#browser_compatibility) in 13.2.0
44+
- If using typescript, is your `module` set to [ES2020](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-8.html#es2020-for-target-and-module) or later?
45+
1. Top level await statements were implemented in ES2022:
46+
- Does your runtime environment support this? Node started [support](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await#browser_compatibility) in 14.8.0.
47+
- If using typescript, is your `module` set to [ES2022](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-4-5.html#module-es2022) or later?
48+
1549
## Goals
1650

1751
### Why mock?
1852

19-
Unfortunately there exist scenarios where you may not what to render a child component; for example when that child component is delay loaded, complex, unstable, server driven, or not owned by you directly and is already covered by integration or end to end testing scenarios.
53+
Unfortunately there exist scenarios where you may not want to render a child component; for example when that child component is delay loaded, complex, unstable, server driven, or not owned by you directly and is already covered by integration or end to end testing scenarios.
54+
55+
A good example scenario is [Stripe's React Elements Component](https://www.npmjs.com/package/@stripe/react-stripe-js) and [Adyen's web components](https://www.npmjs.com/package/@adyen/adyen-web) both of which have the following implementation details which make it difficult to test cleanly:
2056

21-
A good example scenario is [Stripe's React Elements Component](https://www.npmjs.com/package/@stripe/react-stripe-js) and [Adyen's web components](https://www.npmjs.com/package/@adyen/adyen-web) which has a significant amount of internal logic, server side driven logic, along with use of iframes for security purposes making it much more difficult to test cleanly.
57+
- a significant amount of internal logic
58+
- server side driven logic
59+
- the use of iframes limiting access to textfields (for credit card security purposes)
2260

2361
To create a full integration test for this scenario would be extremely complex, costly, and constantly unpredictable as Stripe and Adyen can change the rendering of the components from their server side causing random instability of your tests.
2462

jest.config.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
import { jest } from "@snowcoders/renovate-config";
22

3-
export default jest;
3+
export default {
4+
...jest,
5+
testEnvironment: "jsdom",
6+
};

0 commit comments

Comments
 (0)