| 
									
										
										
										
											2016-06-23 09:47:54 -07:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @license | 
					
						
							|  |  |  |  * Copyright Google Inc. All Rights Reserved. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Use of this source code is governed by an MIT-style license that can be | 
					
						
							|  |  |  |  * found in the LICENSE file at https://angular.io/license
 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-13 10:10:02 -07:00
										 |  |  | import {verifyNoBrowserErrors} from 'e2e_util/e2e_util'; | 
					
						
							| 
									
										
										
										
											2015-07-24 12:46:12 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | describe('async', () => { | 
					
						
							| 
									
										
										
										
											2016-05-01 22:54:19 -07:00
										 |  |  |   var URL = 'all/playground/src/async/index.html'; | 
					
						
							| 
									
										
										
										
											2015-07-24 12:46:12 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |   beforeEach(() => browser.get(URL)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it('should work with synchronous actions', () => { | 
					
						
							|  |  |  |     var increment = $('#increment'); | 
					
						
							|  |  |  |     increment.$('.action').click(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     expect(increment.$('.val').getText()).toEqual('1'); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it('should wait for asynchronous actions', () => { | 
					
						
							|  |  |  |     var timeout = $('#delayedIncrement'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // At this point, the async action is still pending, so the count should
 | 
					
						
							|  |  |  |     // still be 0.
 | 
					
						
							|  |  |  |     expect(timeout.$('.val').getText()).toEqual('0'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-06 21:00:26 -07:00
										 |  |  |     timeout.$('.action').click(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // whenStable should only be called when the async action finished,
 | 
					
						
							|  |  |  |     // so the count should be 1 at this point.
 | 
					
						
							|  |  |  |     expect(timeout.$('.val').getText()).toEqual('1'); | 
					
						
							| 
									
										
										
										
											2015-07-24 12:46:12 -07:00
										 |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it('should notice when asynchronous actions are cancelled', () => { | 
					
						
							|  |  |  |     var timeout = $('#delayedIncrement'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // At this point, the async action is still pending, so the count should
 | 
					
						
							|  |  |  |     // still be 0.
 | 
					
						
							|  |  |  |     expect(timeout.$('.val').getText()).toEqual('0'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-06 21:00:26 -07:00
										 |  |  |     browser.ignoreSynchronization = true; | 
					
						
							|  |  |  |     timeout.$('.action').click(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-24 12:46:12 -07:00
										 |  |  |     timeout.$('.cancel').click(); | 
					
						
							| 
									
										
										
										
											2015-10-06 21:00:26 -07:00
										 |  |  |     browser.ignoreSynchronization = false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // whenStable should be called since the async action is cancelled. The
 | 
					
						
							|  |  |  |     // count should still be 0;
 | 
					
						
							|  |  |  |     expect(timeout.$('.val').getText()).toEqual('0'); | 
					
						
							| 
									
										
										
										
											2015-07-24 12:46:12 -07:00
										 |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it('should wait for a series of asynchronous actions', () => { | 
					
						
							|  |  |  |     var timeout = $('#multiDelayedIncrements'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // At this point, the async action is still pending, so the count should
 | 
					
						
							|  |  |  |     // still be 0.
 | 
					
						
							|  |  |  |     expect(timeout.$('.val').getText()).toEqual('0'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-06 21:00:26 -07:00
										 |  |  |     timeout.$('.action').click(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // whenStable should only be called when all the async actions
 | 
					
						
							|  |  |  |     // finished, so the count should be 10 at this point.
 | 
					
						
							|  |  |  |     expect(timeout.$('.val').getText()).toEqual('10'); | 
					
						
							| 
									
										
										
										
											2015-07-24 12:46:12 -07:00
										 |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-05 12:56:24 -08:00
										 |  |  |   it('should wait via frameworkStabilizer', () => { | 
					
						
							|  |  |  |     var whenAllStable = function() { | 
					
						
							|  |  |  |       return browser.executeAsyncScript('window.frameworkStabilizers[0](arguments[0]);'); | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // This disables protractor's wait mechanism
 | 
					
						
							|  |  |  |     browser.ignoreSynchronization = true; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     var timeout = $('#multiDelayedIncrements'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // At this point, the async action is still pending, so the count should
 | 
					
						
							|  |  |  |     // still be 0.
 | 
					
						
							|  |  |  |     expect(timeout.$('.val').getText()).toEqual('0'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     timeout.$('.action').click(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     whenAllStable().then((didWork) => { | 
					
						
							|  |  |  |       // whenAllStable should only be called when all the async actions
 | 
					
						
							|  |  |  |       // finished, so the count should be 10 at this point.
 | 
					
						
							|  |  |  |       expect(timeout.$('.val').getText()).toEqual('10'); | 
					
						
							|  |  |  |       expect(didWork).toBeTruthy();  // Work was done.
 | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     whenAllStable().then((didWork) => { | 
					
						
							|  |  |  |       // whenAllStable should be called immediately since nothing is pending.
 | 
					
						
							|  |  |  |       expect(didWork).toBeFalsy();  // No work was done.
 | 
					
						
							|  |  |  |       browser.ignoreSynchronization = false; | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-24 12:46:12 -07:00
										 |  |  |   afterEach(verifyNoBrowserErrors); | 
					
						
							|  |  |  | }); |