2019-02-12 15:04:44 -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
|
|
|
|
*/
|
2019-03-01 16:05:49 -05:00
|
|
|
import {Component, Directive, HostBinding, HostListener, Input, QueryList, ViewChildren} from '@angular/core';
|
2019-02-12 15:04:44 -05:00
|
|
|
import {TestBed} from '@angular/core/testing';
|
2019-02-21 16:45:37 -05:00
|
|
|
import {By} from '@angular/platform-browser';
|
2019-02-12 15:04:44 -05:00
|
|
|
import {expect} from '@angular/platform-browser/testing/src/matchers';
|
|
|
|
import {onlyInIvy} from '@angular/private/testing';
|
|
|
|
|
|
|
|
describe('acceptance integration tests', () => {
|
|
|
|
onlyInIvy('[style] and [class] bindings are a new feature')
|
|
|
|
.it('should render host bindings on the root component', () => {
|
|
|
|
@Component({template: '...'})
|
|
|
|
class MyApp {
|
|
|
|
@HostBinding('style') public myStylesExp = {};
|
|
|
|
@HostBinding('class') public myClassesExp = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
TestBed.configureTestingModule({declarations: [MyApp]});
|
|
|
|
const fixture = TestBed.createComponent(MyApp);
|
|
|
|
const element = fixture.nativeElement;
|
|
|
|
fixture.detectChanges();
|
|
|
|
|
|
|
|
const component = fixture.componentInstance;
|
|
|
|
component.myStylesExp = {width: '100px'};
|
|
|
|
component.myClassesExp = 'foo';
|
|
|
|
fixture.detectChanges();
|
|
|
|
|
|
|
|
expect(element.style['width']).toEqual('100px');
|
|
|
|
expect(element.classList.contains('foo')).toBeTruthy();
|
|
|
|
|
|
|
|
component.myStylesExp = {width: '200px'};
|
|
|
|
component.myClassesExp = 'bar';
|
|
|
|
fixture.detectChanges();
|
|
|
|
|
|
|
|
expect(element.style['width']).toEqual('200px');
|
|
|
|
expect(element.classList.contains('foo')).toBeFalsy();
|
|
|
|
expect(element.classList.contains('bar')).toBeTruthy();
|
|
|
|
});
|
2019-02-21 16:45:37 -05:00
|
|
|
|
|
|
|
it('should only call inherited host listeners once', () => {
|
|
|
|
let clicks = 0;
|
|
|
|
|
|
|
|
@Component({template: ''})
|
|
|
|
class ButtonSuperClass {
|
|
|
|
@HostListener('click')
|
|
|
|
clicked() { clicks++; }
|
|
|
|
}
|
|
|
|
|
|
|
|
@Component({selector: 'button[custom-button]', template: ''})
|
|
|
|
class ButtonSubClass extends ButtonSuperClass {
|
|
|
|
}
|
|
|
|
|
|
|
|
@Component({template: '<button custom-button></button>'})
|
|
|
|
class MyApp {
|
|
|
|
}
|
|
|
|
|
|
|
|
TestBed.configureTestingModule({declarations: [MyApp, ButtonSuperClass, ButtonSubClass]});
|
|
|
|
const fixture = TestBed.createComponent(MyApp);
|
|
|
|
const button = fixture.debugElement.query(By.directive(ButtonSubClass));
|
|
|
|
fixture.detectChanges();
|
|
|
|
|
|
|
|
button.nativeElement.click();
|
|
|
|
fixture.detectChanges();
|
|
|
|
|
|
|
|
expect(clicks).toBe(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should support inherited view queries', () => {
|
|
|
|
@Directive({selector: '[someDir]'})
|
|
|
|
class SomeDir {
|
|
|
|
}
|
|
|
|
|
|
|
|
@Component({template: '<div someDir></div>'})
|
|
|
|
class SuperComp {
|
|
|
|
@ViewChildren(SomeDir) dirs !: QueryList<SomeDir>;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Component({selector: 'button[custom-button]', template: '<div someDir></div>'})
|
|
|
|
class SubComp extends SuperComp {
|
|
|
|
}
|
|
|
|
|
|
|
|
@Component({template: '<button custom-button></button>'})
|
|
|
|
class MyApp {
|
|
|
|
}
|
|
|
|
|
|
|
|
TestBed.configureTestingModule({declarations: [MyApp, SuperComp, SubComp, SomeDir]});
|
|
|
|
const fixture = TestBed.createComponent(MyApp);
|
|
|
|
const subInstance = fixture.debugElement.query(By.directive(SubComp)).componentInstance;
|
|
|
|
fixture.detectChanges();
|
|
|
|
|
|
|
|
expect(subInstance.dirs.length).toBe(1);
|
|
|
|
expect(subInstance.dirs.first).toBeAnInstanceOf(SomeDir);
|
|
|
|
});
|
|
|
|
|
2019-02-22 11:44:58 -05:00
|
|
|
it('should render host class and style on the root component', () => {
|
|
|
|
@Component({template: '...', host: {class: 'foo', style: 'color: red'}})
|
|
|
|
class MyApp {
|
|
|
|
}
|
|
|
|
|
|
|
|
TestBed.configureTestingModule({declarations: [MyApp]});
|
|
|
|
const fixture = TestBed.createComponent(MyApp);
|
|
|
|
const element = fixture.nativeElement;
|
|
|
|
fixture.detectChanges();
|
|
|
|
|
|
|
|
expect(element.style['color']).toEqual('red');
|
|
|
|
expect(element.classList.contains('foo')).toBeTruthy();
|
|
|
|
});
|
|
|
|
|
2019-03-01 16:05:49 -05:00
|
|
|
it('should not set inputs after destroy', () => {
|
|
|
|
@Directive({
|
|
|
|
selector: '[no-assign-after-destroy]',
|
|
|
|
})
|
|
|
|
class NoAssignAfterDestroy {
|
|
|
|
private _isDestroyed = false;
|
|
|
|
|
|
|
|
@Input()
|
|
|
|
get value() { return this._value; }
|
|
|
|
set value(newValue: any) {
|
|
|
|
if (this._isDestroyed) {
|
|
|
|
throw Error('Cannot assign to value after destroy.');
|
|
|
|
}
|
|
|
|
|
|
|
|
this._value = newValue;
|
|
|
|
}
|
|
|
|
private _value: any;
|
|
|
|
|
|
|
|
ngOnDestroy() { this._isDestroyed = true; }
|
|
|
|
}
|
|
|
|
|
|
|
|
@Component({template: '<div no-assign-after-destroy [value]="directiveValue"></div>'})
|
|
|
|
class App {
|
|
|
|
directiveValue = 'initial-value';
|
|
|
|
}
|
|
|
|
|
|
|
|
TestBed.configureTestingModule({declarations: [NoAssignAfterDestroy, App]});
|
|
|
|
let fixture = TestBed.createComponent(App);
|
|
|
|
fixture.destroy();
|
|
|
|
|
|
|
|
expect(() => {
|
|
|
|
fixture = TestBed.createComponent(App);
|
|
|
|
fixture.detectChanges();
|
|
|
|
}).not.toThrow();
|
|
|
|
});
|
|
|
|
|
2019-02-21 16:45:37 -05:00
|
|
|
});
|