2018-01-17 13:09:05 -05: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
|
|
|
|
*/
|
|
|
|
|
|
|
|
import {DoCheck, OnChanges, SimpleChanges} from '../../src/core';
|
|
|
|
import {NgOnChangesFeature, defineDirective} from '../../src/render3/index';
|
|
|
|
|
|
|
|
describe('define', () => {
|
|
|
|
describe('component', () => {
|
|
|
|
describe('NgOnChangesFeature', () => {
|
|
|
|
it('should patch class', () => {
|
|
|
|
class MyDirective implements OnChanges, DoCheck {
|
|
|
|
public log: string[] = [];
|
|
|
|
public valA: string = 'initValue';
|
|
|
|
public set valB(value: string) { this.log.push(value); }
|
|
|
|
|
|
|
|
public get valB() { return 'works'; }
|
|
|
|
|
|
|
|
ngDoCheck(): void { this.log.push('ngDoCheck'); }
|
|
|
|
ngOnChanges(changes: SimpleChanges): void {
|
|
|
|
this.log.push('ngOnChanges');
|
|
|
|
this.log.push('valA', changes['valA'].previousValue, changes['valA'].currentValue);
|
|
|
|
this.log.push('valB', changes['valB'].previousValue, changes['valB'].currentValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
static ngDirectiveDef = defineDirective({
|
2018-01-23 13:57:48 -05:00
|
|
|
type: MyDirective,
|
2018-03-29 19:41:45 -04:00
|
|
|
selectors: [['', 'myDir', '']],
|
2018-01-17 13:09:05 -05:00
|
|
|
factory: () => new MyDirective(),
|
2018-03-12 14:18:50 -04:00
|
|
|
features: [NgOnChangesFeature()],
|
2018-01-17 13:09:05 -05:00
|
|
|
inputs: {valA: 'valA', valB: 'valB'}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-03-16 19:42:13 -04:00
|
|
|
const myDir = MyDirective.ngDirectiveDef.factory() as MyDirective;
|
2018-01-17 13:09:05 -05:00
|
|
|
myDir.valA = 'first';
|
|
|
|
expect(myDir.valA).toEqual('first');
|
|
|
|
myDir.valB = 'second';
|
|
|
|
expect(myDir.log).toEqual(['second']);
|
|
|
|
expect(myDir.valB).toEqual('works');
|
|
|
|
myDir.log.length = 0;
|
2018-01-23 21:39:09 -05:00
|
|
|
MyDirective.ngDirectiveDef.doCheck !.call(myDir);
|
2018-01-17 13:09:05 -05:00
|
|
|
expect(myDir.log).toEqual([
|
|
|
|
'ngOnChanges', 'valA', 'initValue', 'first', 'valB', undefined, 'second', 'ngDoCheck'
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2018-01-23 21:39:09 -05:00
|
|
|
});
|