@@ -25,8 +25,20 @@ describe("resource", function() {
25
25
headers : {
26
26
'If-None-Match' : '*'
27
27
}
28
+ } ,
29
+ cancellableQuery : {
30
+ method : 'GET' ,
31
+ isArray : true ,
32
+ timeout : 'promise'
33
+ } ,
34
+ cancellableGet : {
35
+ method : 'GET' ,
36
+ timeout : 'promise'
37
+ } ,
38
+ cancellablePut : {
39
+ method : 'PUT' ,
40
+ timeout : 'promise'
28
41
}
29
-
30
42
} ) ;
31
43
callback = jasmine . createSpy ( ) ;
32
44
} ) ) ;
@@ -674,21 +686,25 @@ describe("resource", function() {
674
686
expect ( person2 ) . toEqual ( jasmine . any ( Person ) ) ;
675
687
} ) ;
676
688
677
- it ( 'should not include $promise and $resolved when resource is toJson\'ed' , function ( ) {
678
- $httpBackend . expect ( 'GET' , '/CreditCard/123' ) . respond ( { id : 123 , number : '9876' } ) ;
679
- var cc = CreditCard . get ( { id : 123 } ) ;
680
- $httpBackend . flush ( ) ;
689
+ it ( 'should not include $promise, $resolved and $timeoutDeferred when resource is toJson\'ed' ,
690
+ function ( ) {
691
+ $httpBackend . expect ( 'GET' , '/CreditCard/123' ) . respond ( { id : 123 , number : '9876' } ) ;
692
+ var cc = CreditCard . cancellableGet ( { id : 123 } ) ;
693
+ $httpBackend . flush ( ) ;
681
694
682
- cc . $myProp = 'still here' ;
695
+ cc . $myProp = 'still here' ;
683
696
684
- expect ( cc . $promise ) . toBeDefined ( ) ;
685
- expect ( cc . $resolved ) . toBe ( true ) ;
697
+ expect ( cc . $promise ) . toBeDefined ( ) ;
698
+ expect ( cc . $resolved ) . toBe ( true ) ;
699
+ expect ( cc . $timeoutDeferred ) . toBeDefined ( ) ;
686
700
687
- var json = JSON . parse ( angular . toJson ( cc ) ) ;
688
- expect ( json . $promise ) . not . toBeDefined ( ) ;
689
- expect ( json . $resolved ) . not . toBeDefined ( ) ;
690
- expect ( json ) . toEqual ( { id : 123 , number : '9876' , $myProp : 'still here' } ) ;
691
- } ) ;
701
+ var json = JSON . parse ( angular . toJson ( cc ) ) ;
702
+ expect ( json . $promise ) . not . toBeDefined ( ) ;
703
+ expect ( json . $resolved ) . not . toBeDefined ( ) ;
704
+ expect ( json . $timeoutDeferred ) . not . toBeDefined ( ) ;
705
+ expect ( json ) . toEqual ( { id : 123 , number : '9876' , $myProp : 'still here' } ) ;
706
+ }
707
+ ) ;
692
708
693
709
describe ( 'promise api' , function ( ) {
694
710
@@ -1033,6 +1049,120 @@ describe("resource", function() {
1033
1049
} ) ;
1034
1050
} ) ;
1035
1051
1052
+ describe ( 'timeoutDeferred api' , function ( ) {
1053
+ var $rootScope ;
1054
+
1055
+
1056
+ beforeEach ( inject ( function ( _$rootScope_ ) {
1057
+ $rootScope = _$rootScope_ ;
1058
+ } ) ) ;
1059
+
1060
+
1061
+ describe ( 'single resource' , function ( ) {
1062
+ beforeEach ( inject ( function ( _$rootScope_ ) {
1063
+ $httpBackend . whenGET ( '/CreditCard/123' ) . respond ( { id : { key : 123 } , number : '9876' } ) ;
1064
+ } ) ) ;
1065
+
1066
+
1067
+ it ( 'should add $timeoutDeferred to the result object iff `timeout === "promise"`' ,
1068
+ function ( ) {
1069
+ var cc = CreditCard . cancellableGet ( { id : 123 } ) ;
1070
+ expect ( cc . $timeoutDeferred ) . toBeDefined ( ) ;
1071
+
1072
+ cc = CreditCard . get ( { id : 123 } ) ;
1073
+ expect ( cc . $timeoutDeferred ) . not . toBeDefined ( ) ;
1074
+ }
1075
+ ) ;
1076
+
1077
+
1078
+ it ( 'should keep $timeoutDeferred around after resolution' , function ( ) {
1079
+ var cc = CreditCard . cancellableGet ( { id : 123 } ) ;
1080
+ var deferred = cc . $timeoutDeferred ;
1081
+
1082
+ $httpBackend . flush ( ) ;
1083
+
1084
+ expect ( cc . $timeoutDeferred ) . toBeDefined ( ) ;
1085
+ expect ( cc . $timeoutDeferred ) . toBe ( deferred ) ;
1086
+ } ) ;
1087
+
1088
+
1089
+ it ( 'should create a new $timeoutDeferred for each request' , function ( ) {
1090
+ $httpBackend . whenPUT ( '/CreditCard/123' ) . respond ( { id : { key : 123 } , number : '9876' } ) ;
1091
+
1092
+ var cc = CreditCard . cancellableGet ( { id : 123 } ) ;
1093
+ $httpBackend . flush ( ) ;
1094
+ var deferred1 = cc . $timeoutDeferred ;
1095
+
1096
+ cc . $cancellablePut ( ) ;
1097
+ $httpBackend . flush ( ) ;
1098
+ var deferred2 = cc . $timeoutDeferred ;
1099
+
1100
+ cc . $cancellablePut ( ) ;
1101
+ $httpBackend . flush ( ) ;
1102
+ var deferred3 = cc . $timeoutDeferred ;
1103
+
1104
+ expect ( deferred1 ) . toBeDefined ( ) ;
1105
+ expect ( deferred2 ) . toBeDefined ( ) ;
1106
+ expect ( deferred3 ) . toBeDefined ( ) ;
1107
+ expect ( deferred1 ) . not . toBe ( deferred2 ) ;
1108
+ expect ( deferred1 ) . not . toBe ( deferred3 ) ;
1109
+ expect ( deferred2 ) . not . toBe ( deferred3 ) ;
1110
+ } ) ;
1111
+
1112
+
1113
+ it ( 'should allow cancelling the request' , function ( ) {
1114
+ var cc = new CreditCard ( { id : { key : 123 } } ) ;
1115
+
1116
+ $httpBackend . expectPUT ( '/CreditCard/123' ) . respond ( { id : { key : 123 } } ) ;
1117
+ cc . $cancellablePut ( ) ;
1118
+ expect ( $httpBackend . flush ) . not . toThrow ( ) ;
1119
+
1120
+ $httpBackend . expectPUT ( '/CreditCard/123' ) . respond ( { id : { key : 123 } } ) ;
1121
+ cc . $cancellablePut ( ) ;
1122
+ cc . $timeoutDeferred . resolve ( ) ;
1123
+ expect ( $httpBackend . flush ) . toThrow ( 'No pending request to flush !' ) ;
1124
+ } ) ;
1125
+ } ) ;
1126
+
1127
+
1128
+ describe ( 'resource collection' , function ( ) {
1129
+ beforeEach ( inject ( function ( _$rootScope_ ) {
1130
+ $httpBackend . whenGET ( '/CreditCard' ) . respond ( [ { id : { key : 1 } } , { id : { key : 2 } } ] ) ;
1131
+ } ) ) ;
1132
+
1133
+
1134
+ it ( 'should add $timeoutDeferred to the result object iff `timeout === "promise"`' ,
1135
+ function ( ) {
1136
+ var ccs = CreditCard . query ( ) ;
1137
+ expect ( ccs . $timeoutDeferred ) . not . toBeDefined ( ) ;
1138
+
1139
+ ccs = CreditCard . cancellableQuery ( ) ;
1140
+ expect ( ccs . $timeoutDeferred ) . toBeDefined ( ) ;
1141
+ }
1142
+ ) ;
1143
+
1144
+
1145
+ it ( 'should keep $timeoutDeferred around after resolution' , function ( ) {
1146
+ var ccs = CreditCard . cancellableQuery ( ) ;
1147
+ var deferred = ccs . $timeoutDeferred ;
1148
+
1149
+ $httpBackend . flush ( ) ;
1150
+
1151
+ expect ( ccs . $timeoutDeferred ) . toBeDefined ( ) ;
1152
+ expect ( ccs . $timeoutDeferred ) . toBe ( deferred ) ;
1153
+ } ) ;
1154
+
1155
+
1156
+ it ( 'should allow cancelling the request' , function ( ) {
1157
+ var ccs = CreditCard . cancellableQuery ( ) ;
1158
+ expect ( $httpBackend . flush ) . not . toThrow ( ) ;
1159
+
1160
+ ccs = CreditCard . cancellableQuery ( ) ;
1161
+ ccs . $timeoutDeferred . resolve ( ) ;
1162
+ expect ( $httpBackend . flush ) . toThrow ( 'No pending request to flush !' ) ;
1163
+ } ) ;
1164
+ } ) ;
1165
+ } ) ;
1036
1166
1037
1167
describe ( 'failure mode' , function ( ) {
1038
1168
var ERROR_CODE = 500 ,
0 commit comments