1
1
import jsdom from 'jsdom' ;
2
2
import { assert } from 'chai' ;
3
3
import sinon from 'sinon' ;
4
- import ReactDOM from 'react-dom' ;
5
- import ifReact from 'enzyme-adapter-react-helper/build/ifReact' ;
6
4
7
5
import ExampleReactComponent from './components/ExampleReactComponent' ;
8
6
import { renderReact } from '..' ;
9
- import { renderReact as renderReactClient } from '../lib/client' ;
10
- import { renderReact as renderReactServer } from '../lib/server' ;
7
+ import * as renderReactClientModule from '../lib/client' ;
8
+ import * as renderReactServerModule from '../lib/server' ;
11
9
12
10
describe ( 'renderReact' , ( ) => {
13
- let result ;
14
- beforeEach ( ( ) => {
15
- result = renderReact ( 'ExampleReactComponent' , ExampleReactComponent ) ( { name : 'Desmond' } ) ;
16
- } ) ;
17
-
18
11
it ( 'exists' , ( ) => {
19
12
assert . isFunction ( renderReact ) ;
20
13
assert . equal ( renderReact . length , 2 ) ;
21
14
} ) ;
22
15
23
- it ( 'has correct markup on server' , ( ) => {
24
- assert . isString ( result ) ;
25
- assert . match ( result , / H e l l o D e s m o n d / ) ;
26
- } ) ;
27
-
28
- ifReact ( '>= 16' , it , it . skip ) ( 'calls hypernova.client (hydrate method)' , ( done ) => {
29
- jsdom . env ( result , ( err , window ) => {
30
- if ( err ) {
31
- done ( err ) ;
32
- return ;
33
- }
34
-
35
- global . window = window ;
36
- global . document = window . document ;
37
-
38
- const hydrateMethod = sinon . spy ( ReactDOM , 'hydrate' ) ;
39
-
40
- // Calling it again for the client.
41
- renderReact ( 'ExampleReactComponent' , ExampleReactComponent ) ;
42
-
43
- assert ( hydrateMethod . calledOnce ) ;
44
-
45
- hydrateMethod . restore ( ) ;
46
-
47
- delete global . window ;
48
- delete global . document ;
49
-
50
- done ( ) ;
51
- } ) ;
52
- } ) ;
53
-
54
- it ( 'calls hypernova.client (render method)' , ( done ) => {
55
- jsdom . env ( result , ( err , window ) => {
56
- if ( err ) {
57
- done ( err ) ;
58
- return ;
59
- }
60
-
61
- const sandbox = sinon . createSandbox ( ) ;
62
- if ( ReactDOM . hydrate ) {
63
- sandbox . stub ( ReactDOM , 'hydrate' ) . value ( undefined ) ;
64
- }
65
-
66
- const renderMethod = sinon . spy ( ReactDOM , 'render' ) ;
67
-
68
- global . window = window ;
69
- global . document = window . document ;
70
-
71
- // Calling it again for the client.
72
- renderReact ( 'ExampleReactComponent' , ExampleReactComponent ) ;
73
-
74
- assert ( renderMethod . calledOnce ) ;
16
+ it ( 'calls renderReactServer' , ( ) => {
17
+ const renderReactServerSpy = sinon . spy ( renderReactServerModule , 'renderReactServer' ) ;
75
18
76
- sandbox . restore ( ) ;
77
- renderMethod . restore ( ) ;
19
+ renderReact ( 'ExampleReactComponent' , ExampleReactComponent ) ( { name : 'Desmond' } ) ;
78
20
79
- delete global . window ;
80
- delete global . document ;
21
+ assert ( renderReactServerSpy . calledOnce , `renderReactServer was not called once but ${ renderReactServerSpy . callCount } times` ) ;
81
22
82
- done ( ) ;
83
- } ) ;
23
+ renderReactServerSpy . restore ( ) ;
84
24
} ) ;
85
- } ) ;
86
25
87
- describe ( 'renderReact client side endpoint' , ( ) => {
88
- let result ;
89
- beforeEach ( ( ) => {
90
- result = renderReact ( 'ExampleReactComponent' , ExampleReactComponent ) ( { name : 'Desmond' } ) ;
91
- } ) ;
26
+ it ( 'calls renderReactClient' , ( done ) => {
27
+ const result = renderReact ( 'ExampleReactComponent' , ExampleReactComponent ) ( { name : 'Desmond' } ) ;
92
28
93
- it ( 'exists' , ( ) => {
94
- assert . isFunction ( renderReactClient ) ;
95
- assert . equal ( renderReactClient . length , 2 ) ;
96
- } ) ;
97
-
98
- ifReact ( '>= 16' , it , it . skip ) ( 'calls hypernova.client (hydrate method)' , ( done ) => {
99
29
jsdom . env ( result , ( err , window ) => {
100
30
if ( err ) {
101
31
done ( err ) ;
102
32
return ;
103
33
}
104
34
105
- global . window = window ;
106
- global . document = window . document ;
107
-
108
- const hydrateMethod = sinon . spy ( ReactDOM , 'hydrate' ) ;
109
-
110
- // Calling it again for the client.
111
- renderReactClient ( 'ExampleReactComponent' , ExampleReactComponent ) ;
112
-
113
- assert ( hydrateMethod . calledOnce ) ;
114
-
115
- hydrateMethod . restore ( ) ;
116
-
117
- delete global . window ;
118
- delete global . document ;
119
-
120
- done ( ) ;
121
- } ) ;
122
- } ) ;
123
-
124
- it ( 'calls hypernova.client (render method)' , ( done ) => {
125
- jsdom . env ( result , ( err , window ) => {
126
- if ( err ) {
127
- done ( err ) ;
128
- return ;
129
- }
130
-
131
- const sandbox = sinon . createSandbox ( ) ;
132
- if ( ReactDOM . hydrate ) {
133
- sandbox . stub ( ReactDOM , 'hydrate' ) . value ( undefined ) ;
134
- }
135
-
136
- const renderMethod = sinon . spy ( ReactDOM , 'render' ) ;
35
+ const renderReactClientSpy = sinon . spy ( renderReactClientModule , 'renderReactClient' ) ;
137
36
138
37
global . window = window ;
139
38
global . document = window . document ;
140
39
141
40
// Calling it again for the client.
142
- renderReactClient ( 'ExampleReactComponent' , ExampleReactComponent ) ;
143
-
144
- assert ( renderMethod . calledOnce ) ;
41
+ renderReact ( 'ExampleReactComponent' , ExampleReactComponent ) ;
145
42
146
- sandbox . restore ( ) ;
43
+ assert ( renderReactClientSpy . calledOnce , `renderReactClient was not called once but ${ renderReactClientSpy . callCount } times` ) ;
147
44
148
- renderMethod . restore ( ) ;
45
+ renderReactClientSpy . restore ( ) ;
149
46
150
47
delete global . window ;
151
48
delete global . document ;
@@ -154,17 +51,3 @@ describe('renderReact client side endpoint', () => {
154
51
} ) ;
155
52
} ) ;
156
53
} ) ;
157
-
158
- describe ( 'renderReact server side endpoint' , ( ) => {
159
- it ( 'exists' , ( ) => {
160
- assert . isFunction ( renderReactServer ) ;
161
- assert . equal ( renderReactServer . length , 2 ) ;
162
- } ) ;
163
-
164
- it ( 'has correct markup on server' , ( ) => {
165
- const result = renderReactServer ( 'ExampleReactComponent' , ExampleReactComponent ) ( { name : 'Desmond' } ) ;
166
-
167
- assert . isString ( result ) ;
168
- assert . match ( result , / H e l l o D e s m o n d / ) ;
169
- } ) ;
170
- } ) ;
0 commit comments