@@ -2,9 +2,10 @@ import { expect } from 'chai';
2
2
import { stub } from 'sinon' ;
3
3
4
4
import {
5
- combineResolvers , and , or ,
5
+ combineResolvers , and , or , compose , Composable
6
6
} from '../../dist/helper' ;
7
7
import { createResolver } from '../../dist/resolver' ;
8
+ import { resolveAll } from 'jspm-config' ;
8
9
9
10
describe ( '(unit) src/helper.js' , ( ) => {
10
11
describe ( 'combineResolvers' , ( ) => {
@@ -174,4 +175,100 @@ describe('(unit) src/helper.js', () => {
174
175
} ) ;
175
176
176
177
} ) ;
178
+
179
+ describe ( 'Compose resolvers' , ( ) => {
180
+ const compositionErr = new Error ( 'composition error' ) ;
181
+ const successResolver = createResolver ( ( ) => null , ( ) => null ) ;
182
+ const failureResolver = createResolver ( ( ) => { throw compositionErr ; } , ( ) => null ) ;
183
+
184
+ it ( 'composed resolvers are chained, and base resolver is called for each' , ( ) => {
185
+
186
+ const b = {
187
+ resolve : ( ) => { } ,
188
+ error : d => compositionErr
189
+ } ;
190
+
191
+ stub ( b , 'resolve' , b . resolve ) ;
192
+
193
+ const base = new Composable ( b . resolve , b . error ) ;
194
+ const comp = base . compose ( {
195
+ r1 : ( ) => true ,
196
+ r2 : ( ) => true ,
197
+ r3 : ( ) => true ,
198
+ } ) ;
199
+
200
+ return Promise . all ( [
201
+
202
+ comp . r1 ( ) . then ( r => {
203
+ expect ( b . resolve . calledThrice ) . to . be . true ;
204
+ expect ( r ) . to . be . true ;
205
+ } ) ,
206
+
207
+ comp . r2 ( ) . then ( r => {
208
+ expect ( b . resolve . calledThrice ) . to . be . true ;
209
+ expect ( r ) . to . be . true ;
210
+ } ) ,
211
+
212
+ comp . r3 ( ) . then ( r => {
213
+ expect ( r ) . to . be . true ;
214
+ expect ( b . resolve . calledThrice ) . to . be . true ;
215
+ } )
216
+
217
+ ] ) ;
218
+ } ) ;
219
+
220
+ it ( 'when base throws, child is not called ' , ( ) => {
221
+
222
+ const b = {
223
+ resolve : null ,
224
+ error : d => compositionErr
225
+ } ;
226
+
227
+ const r1 = {
228
+ resolve : ( ) => true ,
229
+ error : ( ) => compositionErr
230
+ } ;
231
+
232
+ stub ( b , 'error' , b . error ) ;
233
+ stub ( r1 , 'error' , r1 . error ) ;
234
+
235
+ const base = new Composable ( b . resolve , b . error ) ;
236
+ const comp = base . compose ( { r1 : r1 } ) ;
237
+
238
+ comp . r1 ( )
239
+ . catch ( e => {
240
+ expect ( b . error . calledOnce ) . to . be . true ;
241
+ expect ( r1 . resolve . notCalled ) . to . be . true ;
242
+ expect ( r1 . error . notCalled ) . to . be . true ;
243
+ expect ( e ) . to . equal ( compositionErr ) ;
244
+ } ) ;
245
+ } ) ;
246
+
247
+ it ( 'when child throws, parent error is called ' , ( ) => {
248
+
249
+ const b = {
250
+ resolve : null ,
251
+ error : d => null
252
+ } ;
253
+
254
+ const r1 = {
255
+ resolve : ( ) => true ,
256
+ error : ( ) => compositionErr
257
+ } ;
258
+
259
+ stub ( b , 'error' , b . error ) ;
260
+ stub ( r1 , 'error' , r1 . error ) ;
261
+
262
+ const base = new Composable ( b . resolve , b . error ) ;
263
+ const comp = base . compose ( { r1 : r1 } ) ;
264
+
265
+ comp . r1 ( )
266
+ . catch ( e => {
267
+ expect ( b . error . calledOnce ) . to . be . true ;
268
+ expect ( r1 . error . calledOnce ) . to . be . true ;
269
+ expect ( e ) . to . equal ( compositionErr ) ;
270
+ } ) ;
271
+ } ) ;
272
+
273
+ } ) ;
177
274
} ) ;
0 commit comments