1
+ 'use strict' ;
2
+
3
+ var _typeof = typeof Symbol === "function" && typeof Symbol . iterator === "symbol" ? function ( obj ) { return typeof obj ; } : function ( obj ) { return obj && typeof Symbol === "function" && obj . constructor === Symbol && obj !== Symbol . prototype ? "symbol" : typeof obj ; } ;
4
+
5
+ var _react = require ( 'react' ) ;
6
+
7
+ var _react2 = _interopRequireDefault ( _react ) ;
8
+
9
+ var _enzyme = require ( 'enzyme' ) ;
10
+
11
+ var _helpers = require ( '../../tools/tests/helpers' ) ;
12
+
13
+ var _withJob = require ( '../withJob' ) ;
14
+
15
+ var _withJob2 = _interopRequireDefault ( _withJob ) ;
16
+
17
+ function _interopRequireDefault ( obj ) { return obj && obj . __esModule ? obj : { default : obj } ; }
18
+
19
+ var workTime = 10 ; // ms
20
+
21
+ describe ( 'withJob()' , function ( ) {
22
+ ( 0 , _helpers . warningsToErrors ) ( ) ;
23
+
24
+ describe ( 'arguments' , function ( ) {
25
+ it ( 'returns a function' , function ( ) {
26
+ var actual = _typeof ( ( 0 , _withJob2 . default ) ( function ( ) {
27
+ return undefined ;
28
+ } ) ) ;
29
+ var expected = 'function' ;
30
+ expect ( actual ) . toEqual ( expected ) ;
31
+ } ) ;
32
+
33
+ it ( 'should throws if no work is provided' , function ( ) {
34
+ // $FlowIgnore: we expect this to flow error
35
+ expect ( function ( ) {
36
+ return ( 0 , _withJob2 . default ) ( ) ;
37
+ } ) . toThrowError ( 'You must provide a "work" function to the "withJob".' ) ;
38
+ } ) ;
39
+
40
+ it ( 'should throws if the work is invalid' , function ( ) {
41
+ // $FlowIgnore: we expect this to flow error
42
+ expect ( function ( ) {
43
+ return ( 0 , _withJob2 . default ) ( 1 ) ;
44
+ } ) . toThrowError ( 'You must provide a "work" function to the "withJob".' ) ;
45
+ } ) ;
46
+ } ) ;
47
+
48
+ describe ( 'higher order component' , function ( ) {
49
+ var hoc = ( 0 , _withJob2 . default ) ( function ( ) {
50
+ return ( 0 , _helpers . resolveAfter ) ( 1 ) ;
51
+ } ) ;
52
+ var Actual = hoc ( _helpers . Foo ) ;
53
+
54
+ it ( 'should return a renderable component' , function ( ) {
55
+ expect ( function ( ) {
56
+ return ( 0 , _enzyme . mount ) ( _react2 . default . createElement ( Actual , null ) ) ;
57
+ } ) . not . toThrowError ( ) ;
58
+ } ) ;
59
+ } ) ;
60
+
61
+ describe ( 'rendering' , function ( ) {
62
+ it ( 'should set the "result" immediately if the work does not return a promise' , function ( ) {
63
+ var FooWithJob = ( 0 , _withJob2 . default ) ( function ( ) {
64
+ return 'bob' ;
65
+ } ) ( _helpers . Foo ) ;
66
+ expect ( ( 0 , _enzyme . mount ) ( _react2 . default . createElement ( FooWithJob , null ) ) ) . toMatchSnapshot ( ) ;
67
+ } ) ;
68
+
69
+ it ( 'should provide the props to the work function' , function ( ) {
70
+ var expected = { foo : 'bar' , baz : 'qux' } ;
71
+ var actual = void 0 ;
72
+ var FooWithJob = ( 0 , _withJob2 . default ) ( function ( props ) {
73
+ actual = props ;
74
+ } ) ( _helpers . Foo ) ;
75
+ ( 0 , _enzyme . mount ) ( _react2 . default . createElement ( FooWithJob , expected ) ) ;
76
+ expect ( actual ) . toMatchObject ( expected ) ;
77
+ } ) ;
78
+
79
+ it ( 'should set "inProgress" when processing work' , function ( ) {
80
+ var FooWithJob = ( 0 , _withJob2 . default ) ( function ( ) {
81
+ return ( 0 , _helpers . resolveAfter ) ( workTime ) ;
82
+ } ) ( _helpers . Foo ) ;
83
+ var actual = ( 0 , _enzyme . mount ) ( _react2 . default . createElement ( FooWithJob , null ) ) . find ( _helpers . Foo ) . props ( ) ;
84
+ var expected = { job : { completed : false , inProgress : true } } ;
85
+ expect ( actual ) . toMatchObject ( expected ) ;
86
+ } ) ;
87
+
88
+ it ( 'should set "result" when work completes successfully' , function ( ) {
89
+ var FooWithJob = ( 0 , _withJob2 . default ) ( function ( ) {
90
+ return ( 0 , _helpers . resolveAfter ) ( workTime , 'result' ) ;
91
+ } ) ( _helpers . Foo ) ;
92
+ var renderWrapper = ( 0 , _enzyme . mount ) ( _react2 . default . createElement ( FooWithJob , null ) ) ;
93
+ // Allow enough time for work to complete
94
+ return ( 0 , _helpers . resolveAfter ) ( workTime + 5 ) . then ( function ( ) {
95
+ var actual = renderWrapper . find ( _helpers . Foo ) . props ( ) ;
96
+ var expected = { job : { completed : true , inProgress : false , result : 'result' } } ;
97
+ expect ( actual ) . toMatchObject ( expected ) ;
98
+ } )
99
+ // swallow other errors
100
+ . catch ( function ( ) {
101
+ return undefined ;
102
+ } ) ;
103
+ } ) ;
104
+
105
+ it ( 'should set "error" when asynchronous work fails' , function ( ) {
106
+ var error = new Error ( 'poop' ) ;
107
+ var FooWithJob = ( 0 , _withJob2 . default ) ( function ( ) {
108
+ return ( 0 , _helpers . rejectAfter ) ( workTime , error ) ;
109
+ } ) ( _helpers . Foo ) ;
110
+ var renderWrapper = ( 0 , _enzyme . mount ) ( _react2 . default . createElement ( FooWithJob , null ) ) ;
111
+ // Allow enough time for work to complete
112
+ return ( 0 , _helpers . resolveAfter ) ( workTime + 5 ) . then ( function ( ) {
113
+ var actual = renderWrapper . find ( _helpers . Foo ) . props ( ) ;
114
+ var expected = { job : { completed : true , inProgress : false , error : error } } ;
115
+ expect ( actual ) . toMatchObject ( expected ) ;
116
+ } )
117
+ // swallow other errors
118
+ . catch ( function ( ) {
119
+ return undefined ;
120
+ } ) ;
121
+ } ) ;
122
+
123
+ it ( 'should set "error" when synchronous work fails' , function ( ) {
124
+ var error = new Error ( 'poop' ) ;
125
+ var FooWithJob = ( 0 , _withJob2 . default ) ( function ( ) {
126
+ throw error ;
127
+ } ) ( _helpers . Foo ) ;
128
+ var renderWrapper = ( 0 , _enzyme . mount ) ( _react2 . default . createElement ( FooWithJob , null ) ) ;
129
+ var actual = renderWrapper . find ( _helpers . Foo ) . props ( ) ;
130
+ var expected = { job : { completed : true , inProgress : false , error : error } } ;
131
+ expect ( actual ) . toMatchObject ( expected ) ;
132
+ } ) ;
133
+
134
+ it ( 'should not fire again when no "config.shouldWorkAgain" is provided' , function ( ) {
135
+ var fireCount = 0 ;
136
+ var Component = ( 0 , _withJob2 . default ) ( function ( ) {
137
+ fireCount += 1 ;
138
+ return 'foo' ;
139
+ } ) ( function ( ) {
140
+ return _react2 . default . createElement (
141
+ 'div' ,
142
+ null ,
143
+ 'bob'
144
+ ) ;
145
+ } ) ;
146
+ var renderWrapper = ( 0 , _enzyme . mount ) ( _react2 . default . createElement ( Component , { foo : 'foo' } ) ) ;
147
+ expect ( fireCount ) . toEqual ( 1 ) ;
148
+ // Set props to cause a re-render
149
+ renderWrapper . setProps ( { foo : 'bar' } ) ;
150
+ expect ( fireCount ) . toEqual ( 1 ) ;
151
+ } ) ;
152
+
153
+ it ( 'should fire again for a remount' , function ( ) {
154
+ var fireCount = 0 ;
155
+ var Component = ( 0 , _withJob2 . default ) ( function ( ) {
156
+ fireCount += 1 ;
157
+ return true ;
158
+ } ) ( function ( ) {
159
+ return _react2 . default . createElement (
160
+ 'div' ,
161
+ null ,
162
+ 'bob'
163
+ ) ;
164
+ } ) ;
165
+ ( 0 , _enzyme . mount ) ( _react2 . default . createElement ( Component , null ) ) ;
166
+ expect ( fireCount ) . toEqual ( 1 ) ;
167
+ ( 0 , _enzyme . mount ) ( _react2 . default . createElement ( Component , null ) ) ;
168
+ expect ( fireCount ) . toEqual ( 2 ) ;
169
+ } ) ;
170
+
171
+ it ( 'should fire expectantly for a "shouldWorkAgain" implementation' , function ( ) {
172
+ var prevProductIds = [ ] ;
173
+ var fireCount = 0 ;
174
+ var Component = ( 0 , _withJob2 . default ) ( function ( _ref ) {
175
+ var productId = _ref . productId ;
176
+
177
+ prevProductIds . push ( productId ) ;
178
+ fireCount += 1 ;
179
+ return true ;
180
+ } , {
181
+ shouldWorkAgain : function shouldWorkAgain ( prevProps , nextProps , currentJob ) {
182
+ return ( currentJob . inProgress || currentJob . completed ) && prevProductIds . indexOf ( nextProps . productId ) === - 1 ;
183
+ }
184
+ } ) ( function ( ) {
185
+ return _react2 . default . createElement (
186
+ 'div' ,
187
+ null ,
188
+ 'bob'
189
+ ) ;
190
+ } ) ;
191
+ var renderWrapper = ( 0 , _enzyme . mount ) ( _react2 . default . createElement ( Component , { productId : 1 } ) ) ;
192
+ expect ( fireCount ) . toEqual ( 1 ) ;
193
+ renderWrapper . setProps ( { productId : 2 } ) ;
194
+ expect ( fireCount ) . toEqual ( 2 ) ;
195
+ renderWrapper . setProps ( { productId : 3 } ) ;
196
+ expect ( fireCount ) . toEqual ( 3 ) ;
197
+ renderWrapper . setProps ( { productId : 2 } ) ;
198
+ expect ( fireCount ) . toEqual ( 3 ) ;
199
+ renderWrapper . setProps ( { productId : 1 } ) ;
200
+ expect ( fireCount ) . toEqual ( 3 ) ;
201
+ } ) ;
202
+ } ) ;
203
+ } ) ;
0 commit comments