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
|
|
|
|
*/
|
|
|
|
|
2018-05-27 08:19:58 -04:00
|
|
|
import {DoCheck, OnChanges, SimpleChange, SimpleChanges} from '../../src/core';
|
2018-04-14 12:18:38 -04:00
|
|
|
import {DirectiveDef, NgOnChangesFeature, defineDirective} from '../../src/render3/index';
|
2018-01-17 13:09:05 -05:00
|
|
|
|
|
|
|
describe('define', () => {
|
|
|
|
describe('component', () => {
|
|
|
|
describe('NgOnChangesFeature', () => {
|
|
|
|
it('should patch class', () => {
|
|
|
|
class MyDirective implements OnChanges, DoCheck {
|
2018-05-27 08:19:58 -04:00
|
|
|
public log: Array<string|SimpleChange> = [];
|
2018-01-17 13:09:05 -05:00
|
|
|
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');
|
2018-05-27 08:19:58 -04:00
|
|
|
this.log.push('valA', changes['valA']);
|
|
|
|
this.log.push('valB', changes['valB']);
|
2018-01-17 13:09:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
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-04-14 12:18:38 -04:00
|
|
|
const myDir =
|
|
|
|
(MyDirective.ngDirectiveDef as DirectiveDef<MyDirective>).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-04-14 12:18:38 -04:00
|
|
|
(MyDirective.ngDirectiveDef as DirectiveDef<MyDirective>).doCheck !.call(myDir);
|
2018-05-27 08:19:58 -04:00
|
|
|
const changeA = new SimpleChange('initValue', 'first', false);
|
|
|
|
const changeB = new SimpleChange(undefined, 'second', true);
|
|
|
|
expect(myDir.log).toEqual(['ngOnChanges', 'valA', changeA, 'valB', changeB, 'ngDoCheck']);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('correctly computes firstChange', () => {
|
|
|
|
class MyDirective implements OnChanges {
|
|
|
|
public log: Array<string|SimpleChange> = [];
|
|
|
|
public valA: string = 'initValue';
|
|
|
|
public valB: string;
|
|
|
|
|
|
|
|
ngOnChanges(changes: SimpleChanges): void {
|
|
|
|
this.log.push('valA', changes['valA']);
|
|
|
|
this.log.push('valB', changes['valB']);
|
|
|
|
}
|
|
|
|
|
|
|
|
static ngDirectiveDef = defineDirective({
|
|
|
|
type: MyDirective,
|
|
|
|
selectors: [['', 'myDir', '']],
|
|
|
|
factory: () => new MyDirective(),
|
|
|
|
features: [NgOnChangesFeature()],
|
|
|
|
inputs: {valA: 'valA', valB: 'valB'}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const myDir =
|
|
|
|
(MyDirective.ngDirectiveDef as DirectiveDef<MyDirective>).factory() as MyDirective;
|
|
|
|
myDir.valA = 'first';
|
|
|
|
myDir.valB = 'second';
|
|
|
|
(MyDirective.ngDirectiveDef as DirectiveDef<MyDirective>).doCheck !.call(myDir);
|
|
|
|
const changeA1 = new SimpleChange('initValue', 'first', false);
|
|
|
|
const changeB1 = new SimpleChange(undefined, 'second', true);
|
|
|
|
expect(myDir.log).toEqual(['valA', changeA1, 'valB', changeB1]);
|
|
|
|
|
|
|
|
myDir.log.length = 0;
|
|
|
|
myDir.valA = 'third';
|
|
|
|
(MyDirective.ngDirectiveDef as DirectiveDef<MyDirective>).doCheck !.call(myDir);
|
|
|
|
const changeA2 = new SimpleChange('first', 'third', false);
|
|
|
|
expect(myDir.log).toEqual(['valA', changeA2, 'valB', undefined]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not create a getter when only a setter is originally defined', () => {
|
|
|
|
class MyDirective implements OnChanges {
|
|
|
|
public log: Array<string|SimpleChange> = [];
|
|
|
|
|
|
|
|
public set onlySetter(value: string) { this.log.push(value); }
|
|
|
|
|
|
|
|
ngOnChanges(changes: SimpleChanges): void {
|
|
|
|
this.log.push('ngOnChanges');
|
|
|
|
this.log.push('onlySetter', changes['onlySetter']);
|
|
|
|
}
|
|
|
|
|
|
|
|
static ngDirectiveDef = defineDirective({
|
|
|
|
type: MyDirective,
|
|
|
|
selectors: [['', 'myDir', '']],
|
|
|
|
factory: () => new MyDirective(),
|
|
|
|
features: [NgOnChangesFeature()],
|
|
|
|
inputs: {onlySetter: 'onlySetter'}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const myDir =
|
|
|
|
(MyDirective.ngDirectiveDef as DirectiveDef<MyDirective>).factory() as MyDirective;
|
|
|
|
myDir.onlySetter = 'someValue';
|
|
|
|
expect(myDir.onlySetter).toBeUndefined();
|
|
|
|
(MyDirective.ngDirectiveDef as DirectiveDef<MyDirective>).doCheck !.call(myDir);
|
|
|
|
const changeSetter = new SimpleChange(undefined, 'someValue', true);
|
|
|
|
expect(myDir.log).toEqual(['someValue', 'ngOnChanges', 'onlySetter', changeSetter]);
|
2018-01-17 13:09:05 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2018-01-23 21:39:09 -05:00
|
|
|
});
|