Skip to content

Commit 2161c8b

Browse files
Added example usage
1 parent 98f74dd commit 2161c8b

File tree

3 files changed

+83
-3
lines changed

3 files changed

+83
-3
lines changed

README.md

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,44 @@ npm i --save-dev react-assert
1919

2020
### Usage
2121

22-
Imports:
23-
2422
```javascript
23+
import React from "react";
2524
import TestRenderer from "react-test-renderer";
26-
import { assertComponent } from "react-assert";
25+
26+
// 1. import
27+
import { assertComponents } from "react-assert";
28+
29+
function MyComponent(props) {
30+
return (
31+
<div>
32+
<SubComponent />
33+
<p className="my">{props.text}</p>
34+
</div>
35+
);
36+
}
37+
MyComponent.displayName = "MyComponent";
38+
39+
function SubComponent() {
40+
return <p className="sub">Sub</p>;
41+
}
42+
43+
describe("MyComponent", () => {
44+
it("should render component", () => {
45+
//given
46+
const text = "Hello";
47+
48+
//when
49+
const result = TestRenderer.create(<MyComponent text={text} />).root;
50+
51+
//then
52+
// 2. call it with result.children and expected components tree
53+
assertComponents(
54+
result.children,
55+
<div>
56+
<p className="sub">Sub</p>
57+
<p className="my">{text}</p>
58+
</div>
59+
);
60+
});
61+
});
2762
```

test/example.jsx

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import React from "react";
2+
import TestRenderer from "react-test-renderer";
3+
import { assertComponents } from "../index.mjs";
4+
5+
const { describe, it } = await (async () => {
6+
// @ts-ignore
7+
return process.isBun // @ts-ignore
8+
? Promise.resolve({ describe: (_, fn) => fn(), it: test })
9+
: import("node:test");
10+
})();
11+
12+
// @ts-ignore
13+
function MyComponent(props) {
14+
return (
15+
<div>
16+
<SubComponent />
17+
<p className="my">{props.text}</p>
18+
</div>
19+
);
20+
}
21+
MyComponent.displayName = "MyComponent";
22+
23+
function SubComponent() {
24+
return <p className="sub">Sub</p>;
25+
}
26+
27+
describe("MyComponent", () => {
28+
it("should render component", () => {
29+
//given
30+
const text = "Hello";
31+
32+
//when
33+
const result = TestRenderer.create(<MyComponent text={text} />).root;
34+
35+
//then
36+
assertComponents(
37+
result.children,
38+
<div>
39+
<p className="sub">Sub</p>
40+
<p className="my">{text}</p>
41+
</div>
42+
);
43+
});
44+
});

tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,6 @@
3434
"forceConsistentCasingInFileNames": true,
3535
"noEmit": true,
3636
// "emitDeclarationOnly": true,
37+
"jsx": "react"
3738
}
3839
}

0 commit comments

Comments
 (0)