| 
									
										
										
										
											2015-05-26 17:12:38 -07:00
										 |  |  | import {ddescribe, describe, it, iit, xit, expect, beforeEach, afterEach} from 'angular2/test_lib'; | 
					
						
							| 
									
										
										
										
											2015-07-06 11:50:40 -07:00
										 |  |  | import {isBlank} from 'angular2/src/facade/lang'; | 
					
						
							| 
									
										
										
										
											2015-05-26 17:12:38 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | import {coalesce} from 'angular2/src/change_detection/coalesce'; | 
					
						
							| 
									
										
										
										
											2015-06-09 15:45:45 +02:00
										 |  |  | import {RecordType, ProtoRecord} from 'angular2/src/change_detection/proto_record'; | 
					
						
							| 
									
										
										
										
											2015-07-06 11:50:40 -07:00
										 |  |  | import {DirectiveIndex} from 'angular2/src/change_detection/directive_record'; | 
					
						
							| 
									
										
										
										
											2015-05-26 17:12:38 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | export function main() { | 
					
						
							| 
									
										
										
										
											2015-07-10 11:29:41 +02:00
										 |  |  |   function r(funcOrValue, args, contextIndex, selfIndex, | 
					
						
							|  |  |  |              {lastInBinding, mode, name, directiveIndex}: | 
					
						
							|  |  |  |                  {lastInBinding?: any, mode?: any, name?: any, directiveIndex?: any} = {}) { | 
					
						
							| 
									
										
										
										
											2015-07-06 11:50:40 -07:00
										 |  |  |     if (isBlank(lastInBinding)) lastInBinding = false; | 
					
						
							|  |  |  |     if (isBlank(mode)) mode = RecordType.PROPERTY; | 
					
						
							|  |  |  |     if (isBlank(name)) name = "name"; | 
					
						
							|  |  |  |     if (isBlank(directiveIndex)) directiveIndex = null; | 
					
						
							|  |  |  |     return new ProtoRecord(mode, name, funcOrValue, args, null, contextIndex, directiveIndex, | 
					
						
							|  |  |  |                            selfIndex, null, null, lastInBinding, false); | 
					
						
							| 
									
										
										
										
											2015-05-26 17:12:38 -07:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe("change detection - coalesce", () => { | 
					
						
							|  |  |  |     it("should work with an empty list", () => { expect(coalesce([])).toEqual([]); }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it("should remove non-terminal duplicate records" + | 
					
						
							|  |  |  |            " and update the context indices referencing them", | 
					
						
							|  |  |  |        () => { | 
					
						
							| 
									
										
										
										
											2015-06-03 13:42:57 -07:00
										 |  |  |          var rs = coalesce( | 
					
						
							|  |  |  |              [r("user", [], 0, 1), r("first", [], 1, 2), r("user", [], 0, 3), r("last", [], 3, 4)]); | 
					
						
							| 
									
										
										
										
											2015-05-26 17:12:38 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |          expect(rs).toEqual([r("user", [], 0, 1), r("first", [], 1, 2), r("last", [], 1, 3)]); | 
					
						
							|  |  |  |        }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it("should update indices of other records", () => { | 
					
						
							|  |  |  |       var rs = coalesce( | 
					
						
							|  |  |  |           [r("dup", [], 0, 1), r("dup", [], 0, 2), r("user", [], 0, 3), r("first", [3], 3, 4)]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       expect(rs).toEqual([r("dup", [], 0, 1), r("user", [], 0, 2), r("first", [2], 2, 3)]); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it("should remove non-terminal duplicate records" + | 
					
						
							|  |  |  |            " and update the args indices referencing them", | 
					
						
							|  |  |  |        () => { | 
					
						
							|  |  |  |          var rs = coalesce([ | 
					
						
							|  |  |  |            r("user1", [], 0, 1), | 
					
						
							|  |  |  |            r("user2", [], 0, 2), | 
					
						
							|  |  |  |            r("hi", [1], 0, 3), | 
					
						
							|  |  |  |            r("hi", [1], 0, 4), | 
					
						
							|  |  |  |            r("hi", [2], 0, 5) | 
					
						
							|  |  |  |          ]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |          expect(rs).toEqual( | 
					
						
							|  |  |  |              [r("user1", [], 0, 1), r("user2", [], 0, 2), r("hi", [1], 0, 3), r("hi", [2], 0, 4)]); | 
					
						
							|  |  |  |        }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-06 11:50:40 -07:00
										 |  |  |     it("should replace duplicate terminal records with self records", () => { | 
					
						
							|  |  |  |       var rs = coalesce( | 
					
						
							|  |  |  |           [r("user", [], 0, 1, {lastInBinding: true}), r("user", [], 0, 2, {lastInBinding: true})]); | 
					
						
							| 
									
										
										
										
											2015-05-26 17:12:38 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-09 15:45:45 +02:00
										 |  |  |       expect(rs[1]).toEqual(new ProtoRecord(RecordType.SELF, "self", null, [], null, 1, null, 2, | 
					
						
							| 
									
										
										
										
											2015-05-26 17:12:38 -07:00
										 |  |  |                                             null, null, true, false)); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2015-05-27 10:14:37 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     it("should not coalesce directive lifecycle records", () => { | 
					
						
							|  |  |  |       var rs = coalesce([ | 
					
						
							| 
									
										
										
										
											2015-07-06 11:50:40 -07:00
										 |  |  |         r("onCheck", [], 0, 1, {mode: RecordType.DIRECTIVE_LIFECYCLE}), | 
					
						
							|  |  |  |         r("onCheck", [], 0, 1, {mode: RecordType.DIRECTIVE_LIFECYCLE}) | 
					
						
							| 
									
										
										
										
											2015-05-27 10:14:37 -07:00
										 |  |  |       ]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       expect(rs.length).toEqual(2); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2015-06-30 14:13:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     it("should not coalesce protos with different names but same value", () => { | 
					
						
							|  |  |  |       var nullFunc = () => {}; | 
					
						
							|  |  |  |       var rs = coalesce([ | 
					
						
							| 
									
										
										
										
											2015-07-06 11:50:40 -07:00
										 |  |  |         r(nullFunc, [], 0, 1, {name: "foo"}), | 
					
						
							|  |  |  |         r(nullFunc, [], 0, 1, {name: "bar"}), | 
					
						
							| 
									
										
										
										
											2015-06-30 14:13:50 -07:00
										 |  |  |       ]); | 
					
						
							|  |  |  |       expect(rs.length).toEqual(2); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2015-07-06 11:50:40 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     it("should not coalesce protos with the same context index but different directive indices", | 
					
						
							|  |  |  |        () => { | 
					
						
							|  |  |  |          var nullFunc = () => {}; | 
					
						
							|  |  |  |          var rs = coalesce([ | 
					
						
							|  |  |  |            r(nullFunc, [], 0, 1, {directiveIndex: new DirectiveIndex(0, 0)}), | 
					
						
							|  |  |  |            r(nullFunc, [], 0, 1, {directiveIndex: new DirectiveIndex(0, 1)}), | 
					
						
							|  |  |  |            r(nullFunc, [], 0, 1, {directiveIndex: new DirectiveIndex(1, 0)}), | 
					
						
							|  |  |  |            r(nullFunc, [], 0, 1, {directiveIndex: null}), | 
					
						
							|  |  |  |          ]); | 
					
						
							|  |  |  |          expect(rs.length).toEqual(4); | 
					
						
							|  |  |  |        }); | 
					
						
							| 
									
										
										
										
											2015-05-26 17:12:38 -07:00
										 |  |  |   }); | 
					
						
							|  |  |  | } |