@@ -419,51 +419,208 @@ describe("resource", function() {
419
419
expect ( person . name ) . toEqual ( 'misko' ) ;
420
420
} ) ;
421
421
422
- it ( "should have $q and $resolved properties for get" , function ( ) {
423
- $httpBackend . expect ( 'GET' , '/CreditCard/123' ) . respond ( { id : 123 , number : '9876' } ) ;
424
- var cc = CreditCard . get ( { id : 123 } , callback ) ;
425
- expect ( cc . $q ) . toBeDefined ( ) ;
426
- expect ( typeof cc . $q ) . toBe ( 'object' ) ;
427
- expect ( cc . $resolved ) . toBeFalsy ( ) ;
428
- $httpBackend . flush ( ) ;
429
- expect ( cc . $q ) . toBeDefined ( ) ;
430
- expect ( cc . $resolved ) . toBeTruthy ( ) ;
431
- } ) ;
432
422
433
- it ( "should have $q and $resolved properties for query" , function ( ) {
434
- $httpBackend . expect ( 'GET' , '/CreditCard?key=value' ) . respond ( [ { id : 1 } , { id : 2 } ] ) ;
423
+ describe ( 'promise api' , function ( ) {
435
424
436
- var ccs = CreditCard . query ( { key : 'value' } , callback ) ;
437
- expect ( ccs . $q ) . toBeDefined ( ) ;
438
- expect ( typeof ccs . $q ) . toBe ( 'object' ) ;
439
- expect ( ccs . $resolved ) . toBeFalsy ( ) ;
440
- $httpBackend . flush ( ) ;
441
- expect ( ccs . $q ) . toBeDefined ( ) ;
442
- expect ( ccs . $resolved ) . toBeTruthy ( ) ;
443
- } ) ;
425
+ var $rootScope ;
444
426
445
- it ( "should have $q and $resolved properties for save" , function ( ) {
446
- $httpBackend . expect ( 'POST' , '/CreditCard/123' , '{"id":{"key":123},"name":"misko"}' ) .
447
- respond ( { id : { key : 123 } , name : 'rama' } ) ;
448
427
449
- var cc = CreditCard . save ( { id : { key : 123 } , name : 'misko' } , callback ) ;
450
- expect ( cc . $q ) . toBeDefined ( ) ;
451
- expect ( typeof cc . $q ) . toBe ( 'object' ) ;
452
- expect ( cc . $resolved ) . toBeFalsy ( ) ;
453
- $httpBackend . flush ( ) ;
454
- expect ( cc . $q ) . toBeDefined ( ) ;
455
- expect ( cc . $resolved ) . toBeTruthy ( ) ;
456
- } ) ;
428
+ beforeEach ( inject ( function ( _$rootScope_ ) {
429
+ $rootScope = _$rootScope_ ;
430
+ } ) ) ;
457
431
458
- it ( 'should should have $q and $resolved properties for delete' , function ( ) {
459
- $httpBackend . expect ( 'DELETE' , '/CreditCard/123' ) . respond ( { } ) ;
460
- var removed = CreditCard . remove ( { id :123 } , callback ) ;
461
- expect ( removed . $q ) . toBeDefined ( ) ;
462
- expect ( typeof removed . $q ) . toBe ( 'object' ) ;
463
- expect ( removed . $resolved ) . toBeFalsy ( ) ;
464
- $httpBackend . flush ( ) ;
465
- expect ( removed . $q ) . toBeDefined ( ) ;
466
- expect ( removed . $resolved ) . toBeTruthy ( ) ;
432
+
433
+ describe ( 'single resource' , function ( ) {
434
+
435
+ it ( 'should add promise $then method to the result object' , function ( ) {
436
+ $httpBackend . expect ( 'GET' , '/CreditCard/123' ) . respond ( { id : 123 , number : '9876' } ) ;
437
+ var cc = CreditCard . get ( { id : 123 } ) ;
438
+
439
+ cc . $then ( callback ) ;
440
+ expect ( callback ) . not . toHaveBeenCalled ( ) ;
441
+
442
+ $httpBackend . flush ( ) ;
443
+
444
+ var response = callback . mostRecentCall . args [ 0 ] ;
445
+
446
+ expect ( response ) . toEqualData ( {
447
+ data : { id : 123 , number : '9876' } ,
448
+ status : 200 ,
449
+ config : { method : 'GET' , data : undefined , url : '/CreditCard/123' } ,
450
+ resource : { id : 123 , number : '9876' , $resolved : true }
451
+ } ) ;
452
+ expect ( typeof response . resource . $save ) . toBe ( 'function' ) ;
453
+ } ) ;
454
+
455
+
456
+ it ( 'should keep $then around after promise resolution' , function ( ) {
457
+ $httpBackend . expect ( 'GET' , '/CreditCard/123' ) . respond ( { id : 123 , number : '9876' } ) ;
458
+ var cc = CreditCard . get ( { id : 123 } ) ;
459
+
460
+ cc . $then ( callback ) ;
461
+ $httpBackend . flush ( ) ;
462
+
463
+ var response = callback . mostRecentCall . args [ 0 ] ;
464
+
465
+ callback . reset ( ) ;
466
+
467
+ cc . $then ( callback ) ;
468
+ $rootScope . $apply ( ) ; //flush async queue
469
+
470
+ expect ( callback ) . toHaveBeenCalledOnceWith ( response ) ;
471
+ } ) ;
472
+
473
+
474
+ it ( 'should allow promise chaining via $then method' , function ( ) {
475
+ $httpBackend . expect ( 'GET' , '/CreditCard/123' ) . respond ( { id : 123 , number : '9876' } ) ;
476
+ var cc = CreditCard . get ( { id : 123 } ) ;
477
+
478
+ cc . $then ( function ( response ) { return 'new value' ; } ) . then ( callback ) ;
479
+ $httpBackend . flush ( ) ;
480
+
481
+ expect ( callback ) . toHaveBeenCalledOnceWith ( 'new value' ) ;
482
+ } ) ;
483
+
484
+
485
+ it ( 'should allow error callback registration via $then method' , function ( ) {
486
+ $httpBackend . expect ( 'GET' , '/CreditCard/123' ) . respond ( 404 , 'resource not found' ) ;
487
+ var cc = CreditCard . get ( { id : 123 } ) ;
488
+
489
+ cc . $then ( null , callback ) ;
490
+ $httpBackend . flush ( ) ;
491
+
492
+ var response = callback . mostRecentCall . args [ 0 ] ;
493
+
494
+ expect ( response ) . toEqualData ( {
495
+ data : 'resource not found' ,
496
+ status : 404 ,
497
+ config : { method : 'GET' , data : undefined , url : '/CreditCard/123' }
498
+ } ) ;
499
+ } ) ;
500
+
501
+
502
+ it ( 'should add $resolved boolean field to the result object' , function ( ) {
503
+ $httpBackend . expect ( 'GET' , '/CreditCard/123' ) . respond ( { id : 123 , number : '9876' } ) ;
504
+ var cc = CreditCard . get ( { id : 123 } ) ;
505
+
506
+ expect ( cc . $resolved ) . toBe ( false ) ;
507
+
508
+ cc . $then ( callback ) ;
509
+ expect ( cc . $resolved ) . toBe ( false ) ;
510
+
511
+ $httpBackend . flush ( ) ;
512
+
513
+ expect ( cc . $resolved ) . toBe ( true ) ;
514
+ } ) ;
515
+
516
+
517
+ it ( 'should set $resolved field to true when an error occurs' , function ( ) {
518
+ $httpBackend . expect ( 'GET' , '/CreditCard/123' ) . respond ( 404 , 'resource not found' ) ;
519
+ var cc = CreditCard . get ( { id : 123 } ) ;
520
+
521
+ cc . $then ( null , callback ) ;
522
+ $httpBackend . flush ( ) ;
523
+ expect ( callback ) . toHaveBeenCalledOnce ( ) ;
524
+ expect ( cc . $resolved ) . toBe ( true ) ;
525
+ } ) ;
526
+ } ) ;
527
+
528
+
529
+ describe ( 'resource collection' , function ( ) {
530
+
531
+ it ( 'should add promise $then method to the result object' , function ( ) {
532
+ $httpBackend . expect ( 'GET' , '/CreditCard?key=value' ) . respond ( [ { id : 1 } , { id : 2 } ] ) ;
533
+ var ccs = CreditCard . query ( { key : 'value' } ) ;
534
+
535
+ ccs . $then ( callback ) ;
536
+ expect ( callback ) . not . toHaveBeenCalled ( ) ;
537
+
538
+ $httpBackend . flush ( ) ;
539
+
540
+ var response = callback . mostRecentCall . args [ 0 ] ;
541
+
542
+ expect ( response ) . toEqualData ( {
543
+ data : [ { id : 1 } , { id :2 } ] ,
544
+ status : 200 ,
545
+ config : { method : 'GET' , data : undefined , url : '/CreditCard?key=value' } ,
546
+ resource : [ { id : 1 } , { id : 2 } ]
547
+ } ) ;
548
+ expect ( typeof response . resource [ 0 ] . $save ) . toBe ( 'function' ) ;
549
+ expect ( typeof response . resource [ 1 ] . $save ) . toBe ( 'function' ) ;
550
+ } ) ;
551
+
552
+
553
+ it ( 'should keep $then around after promise resolution' , function ( ) {
554
+ $httpBackend . expect ( 'GET' , '/CreditCard?key=value' ) . respond ( [ { id : 1 } , { id : 2 } ] ) ;
555
+ var ccs = CreditCard . query ( { key : 'value' } ) ;
556
+
557
+ ccs . $then ( callback ) ;
558
+ $httpBackend . flush ( ) ;
559
+
560
+ var response = callback . mostRecentCall . args [ 0 ] ;
561
+
562
+ callback . reset ( ) ;
563
+
564
+ ccs . $then ( callback ) ;
565
+ $rootScope . $apply ( ) ; //flush async queue
566
+
567
+ expect ( callback ) . toHaveBeenCalledOnceWith ( response ) ;
568
+ } ) ;
569
+
570
+
571
+ it ( 'should allow promise chaining via $then method' , function ( ) {
572
+ $httpBackend . expect ( 'GET' , '/CreditCard?key=value' ) . respond ( [ { id : 1 } , { id : 2 } ] ) ;
573
+ var ccs = CreditCard . query ( { key : 'value' } ) ;
574
+
575
+ ccs . $then ( function ( response ) { return 'new value' ; } ) . then ( callback ) ;
576
+ $httpBackend . flush ( ) ;
577
+
578
+ expect ( callback ) . toHaveBeenCalledOnceWith ( 'new value' ) ;
579
+ } ) ;
580
+
581
+
582
+ it ( 'should allow error callback registration via $then method' , function ( ) {
583
+ $httpBackend . expect ( 'GET' , '/CreditCard?key=value' ) . respond ( 404 , 'resource not found' ) ;
584
+ var ccs = CreditCard . query ( { key : 'value' } ) ;
585
+
586
+ ccs . $then ( null , callback ) ;
587
+ $httpBackend . flush ( ) ;
588
+
589
+ var response = callback . mostRecentCall . args [ 0 ] ;
590
+
591
+ expect ( response ) . toEqualData ( {
592
+ data : 'resource not found' ,
593
+ status : 404 ,
594
+ config : { method : 'GET' , data : undefined , url : '/CreditCard?key=value' }
595
+ } ) ;
596
+ } ) ;
597
+
598
+
599
+ it ( 'should add $resolved boolean field to the result object' , function ( ) {
600
+ $httpBackend . expect ( 'GET' , '/CreditCard?key=value' ) . respond ( [ { id : 1 } , { id : 2 } ] ) ;
601
+ var ccs = CreditCard . query ( { key : 'value' } , callback ) ;
602
+
603
+ expect ( ccs . $resolved ) . toBe ( false ) ;
604
+
605
+ ccs . $then ( callback ) ;
606
+ expect ( ccs . $resolved ) . toBe ( false ) ;
607
+
608
+ $httpBackend . flush ( ) ;
609
+
610
+ expect ( ccs . $resolved ) . toBe ( true ) ;
611
+ } ) ;
612
+
613
+
614
+ it ( 'should set $resolved field to true when an error occurs' , function ( ) {
615
+ $httpBackend . expect ( 'GET' , '/CreditCard?key=value' ) . respond ( 404 , 'resource not found' ) ;
616
+ var ccs = CreditCard . query ( { key : 'value' } ) ;
617
+
618
+ ccs . $then ( null , callback ) ;
619
+ $httpBackend . flush ( ) ;
620
+ expect ( callback ) . toHaveBeenCalledOnce ( ) ;
621
+ expect ( ccs . $resolved ) . toBe ( true ) ;
622
+ } ) ;
623
+ } ) ;
467
624
} ) ;
468
625
469
626
0 commit comments