1
- const assert = require ( 'assert' ) ;
2
1
const React = require ( 'react' ) ;
3
2
const htmlToDOM = require ( 'html-dom-parser' ) ;
4
3
const domToReact = require ( '../lib/dom-to-react' ) ;
@@ -10,17 +9,15 @@ describe('DOM to React', () => {
10
9
const html = data . html . single ;
11
10
const reactElement = domToReact ( htmlToDOM ( html ) ) ;
12
11
13
- assert ( React . isValidElement ( reactElement ) ) ;
14
12
expect ( reactElement ) . toMatchSnapshot ( ) ;
15
13
} ) ;
16
14
17
15
it ( 'converts multiple DOM nodes to React' , ( ) => {
18
16
const html = data . html . multiple ;
19
17
const reactElements = domToReact ( htmlToDOM ( html ) ) ;
20
18
21
- reactElements . forEach ( reactElement => {
22
- assert ( React . isValidElement ( reactElement ) ) ;
23
- assert ( reactElement . key ) ;
19
+ reactElements . forEach ( ( reactElement , index ) => {
20
+ expect ( reactElement . key ) . toBe ( String ( index ) ) ;
24
21
} ) ;
25
22
26
23
expect ( reactElements ) . toMatchSnapshot ( ) ;
@@ -31,38 +28,36 @@ describe('DOM to React', () => {
31
28
const html = data . html . textarea ;
32
29
const reactElement = domToReact ( htmlToDOM ( html ) ) ;
33
30
34
- assert ( React . isValidElement ( reactElement ) ) ;
35
31
expect ( reactElement ) . toMatchSnapshot ( ) ;
36
32
} ) ;
37
33
38
34
it ( 'does not escape <script> content' , ( ) => {
39
35
const html = data . html . script ;
40
36
const reactElement = domToReact ( htmlToDOM ( html ) ) ;
41
37
42
- assert ( React . isValidElement ( reactElement ) ) ;
43
38
expect ( reactElement ) . toMatchSnapshot ( ) ;
44
39
} ) ;
45
40
46
41
it ( 'does not escape <style> content' , ( ) => {
47
42
const html = data . html . style ;
48
43
const reactElement = domToReact ( htmlToDOM ( html ) ) ;
49
44
50
- assert ( React . isValidElement ( reactElement ) ) ;
51
45
expect ( reactElement ) . toMatchSnapshot ( ) ;
52
46
} ) ;
53
47
54
48
it ( 'does not have `children` for void elements' , ( ) => {
55
49
const html = data . html . img ;
56
50
const reactElement = domToReact ( htmlToDOM ( html ) ) ;
57
- assert ( ! reactElement . props . children ) ;
51
+
52
+ expect ( reactElement . props . children ) . toBe ( null ) ;
58
53
} ) ;
59
54
60
55
it ( 'does not throw an error for void elements' , ( ) => {
61
56
const html = data . html . void ;
62
57
const reactElements = domToReact ( htmlToDOM ( html ) ) ;
63
- assert . doesNotThrow ( ( ) => {
58
+ expect ( ( ) => {
64
59
render ( React . createElement ( 'div' , { } , reactElements ) ) ;
65
- } ) ;
60
+ } ) . not . toThrow ( ) ;
66
61
} ) ;
67
62
68
63
it ( 'skips doctype and comments' , ( ) => {
@@ -72,13 +67,11 @@ describe('DOM to React', () => {
72
67
data . html . comment ,
73
68
data . html . single
74
69
] . join ( '' ) ;
75
-
76
70
const reactElements = domToReact ( htmlToDOM ( html ) ) ;
77
- reactElements . forEach ( ( reactElement , index ) => {
78
- assert . strictEqual ( React . isValidElement ( reactElement ) , true ) ;
79
- assert ( reactElement . key , String ( index ) ) ;
80
- } ) ;
81
71
72
+ expect ( reactElements ) . toHaveLength ( 2 ) ;
73
+ expect ( reactElements [ 0 ] . key ) . toBe ( '1' ) ;
74
+ expect ( reactElements [ 1 ] . key ) . toBe ( '3' ) ;
82
75
expect ( reactElements ) . toMatchSnapshot ( ) ;
83
76
} ) ;
84
77
@@ -105,14 +98,16 @@ describe('DOM to React', () => {
105
98
const html = data . html . single ;
106
99
const reactElement = domToReact ( htmlToDOM ( html ) ) ;
107
100
108
- assert . deepEqual ( reactElement , React . createElement ( 'p' , { } , 'foo' ) ) ;
101
+ expect ( React . isValidElement ( reactElement ) ) . toBe ( true ) ;
102
+ expect ( reactElement ) . toEqual ( React . createElement ( 'p' , { } , 'foo' ) ) ;
109
103
} ) ;
110
104
111
105
it ( 'converts with Preact instead of React' , ( ) => {
112
106
const html = data . html . single ;
113
107
const preactElement = domToReact ( htmlToDOM ( html ) , { library : Preact } ) ;
114
108
115
- assert . deepEqual ( preactElement , Preact . createElement ( 'p' , { } , 'foo' ) ) ;
109
+ expect ( Preact . isValidElement ( preactElement ) ) . toBe ( true ) ;
110
+ expect ( preactElement ) . toEqual ( Preact . createElement ( 'p' , { } , 'foo' ) ) ;
116
111
} ) ;
117
112
} ) ;
118
113
@@ -122,7 +117,7 @@ describe('DOM to React', () => {
122
117
const reactElement = domToReact ( htmlToDOM ( data . html . single ) , {
123
118
replace : ( ) => replaceElement
124
119
} ) ;
125
- assert . strictEqual ( reactElement . key , null ) ;
120
+ expect ( reactElement . key ) . toBe ( null ) ;
126
121
} ) ;
127
122
128
123
it ( "does not modify keys if it's already set" , ( ) => {
@@ -147,8 +142,8 @@ describe('DOM to React', () => {
147
142
}
148
143
} ) ;
149
144
150
- assert . strictEqual ( reactElements [ 0 ] . key , '0' ) ;
151
- assert . strictEqual ( reactElements [ 1 ] . key , 'myKey' ) ;
145
+ expect ( reactElements [ 0 ] . key ) . toBe ( '0' ) ;
146
+ expect ( reactElements [ 1 ] . key ) . toBe ( 'myKey' ) ;
152
147
} ) ;
153
148
} ) ;
154
149
0 commit comments