@@ -111,4 +111,71 @@ describe("__with__", function() {
111111 expect ( callWithFunction ( { } ) ) . to . throwError ( expectTypeError ) ;
112112 expect ( callWithFunction ( function ( ) { } ) ) . to . not . throwError ( expectTypeError ) ;
113113 } ) ;
114+
115+ describe ( "using promises" , function ( ) {
116+ var promiseFake ;
117+
118+ beforeEach ( function ( ) {
119+ promiseFake = {
120+ then : function ( onResolve , onReject ) {
121+ promiseFake . onResolve = onResolve ;
122+ promiseFake . onReject = onReject ;
123+ }
124+ } ;
125+ } ) ;
126+
127+ it ( "should pass the returned promise through" , function ( ) {
128+ var fn = moduleFake . __with__ ( { } ) ;
129+
130+ expect ( fn ( function ( ) {
131+ return promiseFake ;
132+ } ) ) . to . equal ( promiseFake ) ;
133+ } ) ;
134+
135+ it ( "should not undo any changes until the promise has been resolved" , function ( ) {
136+ expect ( moduleFake . getValue ( ) ) . to . be ( 0 ) ;
137+ expect ( moduleFake . getReference ( ) ) . to . eql ( { } ) ;
138+
139+ moduleFake . __with__ ( {
140+ myValue : 2 ,
141+ myReference : newObj
142+ } ) ( function ( ) {
143+ return promiseFake ;
144+ } ) ;
145+
146+ // the change should still be present at this point
147+ expect ( moduleFake . getValue ( ) ) . to . be ( 2 ) ;
148+ expect ( moduleFake . getReference ( ) ) . to . be ( newObj ) ;
149+
150+ promiseFake . onResolve ( ) ;
151+
152+ // now everything should be back to normal
153+ expect ( moduleFake . getValue ( ) ) . to . be ( 0 ) ;
154+ expect ( moduleFake . getReference ( ) ) . to . eql ( { } ) ;
155+ } ) ;
156+
157+ it ( "should also undo any changes if the promise has been rejected" , function ( ) {
158+ expect ( moduleFake . getValue ( ) ) . to . be ( 0 ) ;
159+ expect ( moduleFake . getReference ( ) ) . to . eql ( { } ) ;
160+
161+ moduleFake . __with__ ( {
162+ myValue : 2 ,
163+ myReference : newObj
164+ } ) ( function ( ) {
165+ return promiseFake ;
166+ } ) ;
167+
168+ // the change should still be present at this point
169+ expect ( moduleFake . getValue ( ) ) . to . be ( 2 ) ;
170+ expect ( moduleFake . getReference ( ) ) . to . be ( newObj ) ;
171+
172+ promiseFake . onReject ( ) ;
173+
174+ // now everything should be back to normal
175+ expect ( moduleFake . getValue ( ) ) . to . be ( 0 ) ;
176+ expect ( moduleFake . getReference ( ) ) . to . eql ( { } ) ;
177+ } ) ;
178+
179+ } ) ;
180+
114181} ) ;
0 commit comments