File tree Expand file tree Collapse file tree 3 files changed +83
-3
lines changed Expand file tree Collapse file tree 3 files changed +83
-3
lines changed Original file line number Diff line number Diff line change @@ -19,9 +19,44 @@ npm i --save-dev react-assert
19
19
20
20
### Usage
21
21
22
- Imports:
23
-
24
22
``` javascript
23
+ import React from " react" ;
25
24
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
+ });
27
62
```
Original file line number Diff line number Diff line change
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
+ } ) ;
Original file line number Diff line number Diff line change 34
34
"forceConsistentCasingInFileNames" : true ,
35
35
"noEmit" : true ,
36
36
// "emitDeclarationOnly": true,
37
+ "jsx" : " react"
37
38
}
38
39
}
You can’t perform that action at this time.
0 commit comments