| 
									
										
										
										
											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
 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-02 12:12:46 -08:00
										 |  |  | import {SpyObject} from '@angular/core/testing/src/testing_internal'; | 
					
						
							| 
									
										
										
										
											2015-05-27 09:53:37 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-29 14:26:52 -04:00
										 |  |  | class TestObj { | 
					
						
							| 
									
										
										
										
											2016-06-20 14:21:01 -07:00
										 |  |  |   prop: any; | 
					
						
							|  |  |  |   constructor(prop: any) { this.prop = prop; } | 
					
						
							| 
									
										
										
										
											2015-05-27 09:53:37 -07:00
										 |  |  |   someFunc(): number { return -1; } | 
					
						
							| 
									
										
										
										
											2016-06-20 14:21:01 -07:00
										 |  |  |   someComplexFunc(a: any) { return a; } | 
					
						
							| 
									
										
										
										
											2014-10-29 14:26:52 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-14 13:26:42 -07:00
										 |  |  | class SpyTestObj extends SpyObject { | 
					
						
							| 
									
										
										
										
											2015-05-27 09:53:37 -07:00
										 |  |  |   constructor() { super(TestObj); } | 
					
						
							| 
									
										
										
										
											2015-04-14 13:26:42 -07:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2014-11-25 15:16:53 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-29 14:26:52 -04:00
										 |  |  | export function main() { | 
					
						
							| 
									
										
										
										
											2015-10-13 00:29:13 -07:00
										 |  |  |   describe('testing', () => { | 
					
						
							| 
									
										
										
										
											2014-11-17 17:39:39 -08:00
										 |  |  |     describe('equality', () => { | 
					
						
							|  |  |  |       it('should structurally compare objects', () => { | 
					
						
							| 
									
										
										
										
											2016-11-03 17:05:03 -07:00
										 |  |  |         const expected = new TestObj(new TestObj({'one': [1, 2]})); | 
					
						
							|  |  |  |         const actual = new TestObj(new TestObj({'one': [1, 2]})); | 
					
						
							|  |  |  |         const falseActual = new TestObj(new TestObj({'one': [1, 3]})); | 
					
						
							| 
									
										
										
										
											2014-10-29 14:26:52 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |         expect(actual).toEqual(expected); | 
					
						
							|  |  |  |         expect(falseActual).not.toEqual(expected); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2014-11-17 17:39:39 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     describe('toEqual for Maps', () => { | 
					
						
							|  |  |  |       it('should detect equality for same reference', () => { | 
					
						
							| 
									
										
										
										
											2016-11-03 17:05:03 -07:00
										 |  |  |         const m1: Map<string, number> = new Map(); | 
					
						
							|  |  |  |         m1.set('a', 1); | 
					
						
							| 
									
										
										
										
											2014-11-17 17:39:39 -08:00
										 |  |  |         expect(m1).toEqual(m1); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it('should detect equality for same content', () => { | 
					
						
							| 
									
										
										
										
											2016-11-03 16:58:27 -07:00
										 |  |  |         const m1: Map<string, number> = new Map(); | 
					
						
							|  |  |  |         m1.set('a', 1); | 
					
						
							|  |  |  |         const m2: Map<string, number> = new Map(); | 
					
						
							|  |  |  |         m2.set('a', 1); | 
					
						
							|  |  |  |         expect(m1).toEqual(m2); | 
					
						
							| 
									
										
										
										
											2014-11-17 17:39:39 -08:00
										 |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it('should detect missing entries', () => { | 
					
						
							| 
									
										
										
										
											2016-11-03 17:05:03 -07:00
										 |  |  |         const m1: Map<string, number> = new Map(); | 
					
						
							|  |  |  |         m1.set('a', 1); | 
					
						
							|  |  |  |         const m2: Map<string, number> = new Map(); | 
					
						
							|  |  |  |         expect(m1).not.toEqual(m2); | 
					
						
							| 
									
										
										
										
											2014-11-17 17:39:39 -08:00
										 |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it('should detect different values', () => { | 
					
						
							| 
									
										
										
										
											2016-11-03 17:05:03 -07:00
										 |  |  |         const m1: Map<string, number> = new Map(); | 
					
						
							|  |  |  |         m1.set('a', 1); | 
					
						
							|  |  |  |         const m2: Map<string, number> = new Map(); | 
					
						
							|  |  |  |         m2.set('a', 2); | 
					
						
							|  |  |  |         expect(m1).not.toEqual(m2); | 
					
						
							| 
									
										
										
										
											2014-11-17 17:39:39 -08:00
										 |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it('should detect additional entries', () => { | 
					
						
							| 
									
										
										
										
											2016-11-03 16:58:27 -07:00
										 |  |  |         const m1: Map<string, number> = new Map(); | 
					
						
							|  |  |  |         m1.set('a', 1); | 
					
						
							|  |  |  |         const m2: Map<string, number> = new Map(); | 
					
						
							|  |  |  |         m2.set('a', 1); | 
					
						
							|  |  |  |         m2.set('b', 2); | 
					
						
							|  |  |  |         expect(m1).not.toEqual(m2); | 
					
						
							| 
									
										
										
										
											2014-11-17 17:39:39 -08:00
										 |  |  |       }); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2014-11-25 15:16:53 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |     describe('spy objects', () => { | 
					
						
							| 
									
										
										
										
											2016-10-11 15:44:48 -07:00
										 |  |  |       let spyObj: any; | 
					
						
							| 
									
										
										
										
											2014-11-25 15:16:53 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-03 16:58:27 -07:00
										 |  |  |       beforeEach(() => { spyObj = new SpyTestObj(); }); | 
					
						
							| 
									
										
										
										
											2014-11-25 15:16:53 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |       it('should return a new spy func with no calls', | 
					
						
							|  |  |  |          () => { expect(spyObj.spy('someFunc')).not.toHaveBeenCalled(); }); | 
					
						
							| 
									
										
										
										
											2014-11-25 15:16:53 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |       it('should record function calls', () => { | 
					
						
							| 
									
										
										
										
											2016-10-11 15:44:48 -07:00
										 |  |  |         spyObj.spy('someFunc').and.callFake((a: any, b: any) => a + b); | 
					
						
							| 
									
										
										
										
											2014-11-25 15:16:53 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-27 09:53:37 -07:00
										 |  |  |         expect(spyObj.someFunc(1, 2)).toEqual(3); | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |         expect(spyObj.spy('someFunc')).toHaveBeenCalledWith(1, 2); | 
					
						
							| 
									
										
										
										
											2014-11-25 15:16:53 -08:00
										 |  |  |       }); | 
					
						
							| 
									
										
										
										
											2015-04-14 13:26:42 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |       it('should match multiple function calls', () => { | 
					
						
							| 
									
										
										
										
											2015-05-27 09:53:37 -07:00
										 |  |  |         spyObj.someFunc(1, 2); | 
					
						
							|  |  |  |         spyObj.someFunc(3, 4); | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |         expect(spyObj.spy('someFunc')).toHaveBeenCalledWith(1, 2); | 
					
						
							|  |  |  |         expect(spyObj.spy('someFunc')).toHaveBeenCalledWith(3, 4); | 
					
						
							| 
									
										
										
										
											2015-04-23 09:13:42 -07:00
										 |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |       it('should match null arguments', () => { | 
					
						
							|  |  |  |         spyObj.someFunc(null, 'hello'); | 
					
						
							|  |  |  |         expect(spyObj.spy('someFunc')).toHaveBeenCalledWith(null, 'hello'); | 
					
						
							| 
									
										
										
										
											2015-05-07 17:18:42 -07:00
										 |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |       it('should match using deep equality', () => { | 
					
						
							| 
									
										
										
										
											2015-04-23 09:13:42 -07:00
										 |  |  |         spyObj.someComplexFunc([1]); | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |         expect(spyObj.spy('someComplexFunc')).toHaveBeenCalledWith([1]); | 
					
						
							| 
									
										
										
										
											2015-04-23 09:13:42 -07:00
										 |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |       it('should support stubs', () => { | 
					
						
							| 
									
										
										
										
											2016-11-03 16:58:27 -07:00
										 |  |  |         const s = SpyObject.stub({'a': 1}, {'b': 2}); | 
					
						
							| 
									
										
										
										
											2015-04-17 16:08:59 -07:00
										 |  |  |         expect(s.a()).toEqual(1); | 
					
						
							|  |  |  |         expect(s.b()).toEqual(2); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-27 09:53:37 -07:00
										 |  |  |       it('should create spys for all methods', | 
					
						
							|  |  |  |          () => { expect(() => spyObj.someFunc()).not.toThrow(); }); | 
					
						
							| 
									
										
										
										
											2014-11-25 15:16:53 -08:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2014-10-29 14:26:52 -04:00
										 |  |  |   }); | 
					
						
							| 
									
										
										
										
											2015-02-16 20:04:03 -08:00
										 |  |  | } |