test(ivy): run view_injector_integration tests on node (#28593)
There is nothing browser specific in those tests and fakeAsync is supported on node. Testing / debugging on node is often faster than on Karma. PR Close #28593
This commit is contained in:
parent
6e4ef91ceb
commit
0e4705aec3
@ -8,7 +8,6 @@
|
|||||||
|
|
||||||
import {Attribute, ChangeDetectionStrategy, ChangeDetectorRef, Component, ComponentFactoryResolver, DebugElement, Directive, ElementRef, EmbeddedViewRef, Host, Inject, InjectionToken, Injector, Input, NgModule, Optional, Pipe, PipeTransform, Provider, Self, SkipSelf, TemplateRef, Type, ViewContainerRef} from '@angular/core';
|
import {Attribute, ChangeDetectionStrategy, ChangeDetectorRef, Component, ComponentFactoryResolver, DebugElement, Directive, ElementRef, EmbeddedViewRef, Host, Inject, InjectionToken, Injector, Input, NgModule, Optional, Pipe, PipeTransform, Provider, Self, SkipSelf, TemplateRef, Type, ViewContainerRef} from '@angular/core';
|
||||||
import {ComponentFixture, TestBed, fakeAsync} from '@angular/core/testing';
|
import {ComponentFixture, TestBed, fakeAsync} from '@angular/core/testing';
|
||||||
import {getDOM} from '@angular/platform-browser/src/dom/dom_adapter';
|
|
||||||
import {expect} from '@angular/platform-browser/testing/src/matchers';
|
import {expect} from '@angular/platform-browser/testing/src/matchers';
|
||||||
import {ivyEnabled, modifiedInIvy, obsoleteInIvy, onlyInIvy} from '@angular/private/testing';
|
import {ivyEnabled, modifiedInIvy, obsoleteInIvy, onlyInIvy} from '@angular/private/testing';
|
||||||
|
|
||||||
@ -183,7 +182,6 @@ export class DuplicatePipe2 implements PipeTransform {
|
|||||||
class TestComp {
|
class TestComp {
|
||||||
}
|
}
|
||||||
|
|
||||||
(function() {
|
|
||||||
function createComponentFixture<T>(
|
function createComponentFixture<T>(
|
||||||
template: string, providers?: Provider[] | null, comp?: Type<T>): ComponentFixture<T> {
|
template: string, providers?: Provider[] | null, comp?: Type<T>): ComponentFixture<T> {
|
||||||
if (!comp) {
|
if (!comp) {
|
||||||
@ -196,17 +194,13 @@ class TestComp {
|
|||||||
return TestBed.createComponent(comp !);
|
return TestBed.createComponent(comp !);
|
||||||
}
|
}
|
||||||
|
|
||||||
function createComponent(
|
function createComponent(template: string, providers?: Provider[], comp?: Type<any>): DebugElement {
|
||||||
template: string, providers?: Provider[], comp?: Type<any>): DebugElement {
|
|
||||||
const fixture = createComponentFixture(template, providers, comp);
|
const fixture = createComponentFixture(template, providers, comp);
|
||||||
fixture.detectChanges();
|
fixture.detectChanges();
|
||||||
return fixture.debugElement;
|
return fixture.debugElement;
|
||||||
}
|
}
|
||||||
|
|
||||||
describe('View injector', () => {
|
describe('View injector', () => {
|
||||||
// On CJS fakeAsync is not supported...
|
|
||||||
if (!getDOM().supportsDOMEvents()) return;
|
|
||||||
|
|
||||||
const TOKEN = new InjectionToken<string>('token');
|
const TOKEN = new InjectionToken<string>('token');
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
@ -260,8 +254,7 @@ class TestComp {
|
|||||||
it('should instantiate providers that have dependencies with SkipSelf', () => {
|
it('should instantiate providers that have dependencies with SkipSelf', () => {
|
||||||
TestBed.configureTestingModule({declarations: [SimpleDirective, SomeOtherDirective]});
|
TestBed.configureTestingModule({declarations: [SimpleDirective, SomeOtherDirective]});
|
||||||
TestBed.overrideDirective(
|
TestBed.overrideDirective(
|
||||||
SimpleDirective,
|
SimpleDirective, {add: {providers: [{provide: 'injectable1', useValue: 'injectable1'}]}});
|
||||||
{add: {providers: [{provide: 'injectable1', useValue: 'injectable1'}]}});
|
|
||||||
TestBed.overrideDirective(SomeOtherDirective, {
|
TestBed.overrideDirective(SomeOtherDirective, {
|
||||||
add: {
|
add: {
|
||||||
providers: [
|
providers: [
|
||||||
@ -322,9 +315,7 @@ class TestComp {
|
|||||||
];
|
];
|
||||||
TestBed.overrideDirective(SimpleDirective, {set: {providers}});
|
TestBed.overrideDirective(SimpleDirective, {set: {providers}});
|
||||||
const el = createComponent('<div simpleDirective></div>');
|
const el = createComponent('<div simpleDirective></div>');
|
||||||
expect(el.children[0].injector.get('injectable1')).toEqual([
|
expect(el.children[0].injector.get('injectable1')).toEqual(['injectable11', 'injectable12']);
|
||||||
'injectable11', 'injectable12'
|
|
||||||
]);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should instantiate providers lazily', () => {
|
it('should instantiate providers lazily', () => {
|
||||||
@ -516,8 +507,7 @@ class TestComp {
|
|||||||
() => {
|
() => {
|
||||||
TestBed.configureTestingModule({declarations: [SimpleComponent, NeedsService]});
|
TestBed.configureTestingModule({declarations: [SimpleComponent, NeedsService]});
|
||||||
TestBed.overrideComponent(
|
TestBed.overrideComponent(
|
||||||
SimpleComponent,
|
SimpleComponent, {set: {viewProviders: [{provide: 'service', useValue: 'service'}]}});
|
||||||
{set: {viewProviders: [{provide: 'service', useValue: 'service'}]}});
|
|
||||||
expect(() => createComponent('<div simpleComponent needsService></div>'))
|
expect(() => createComponent('<div simpleComponent needsService></div>'))
|
||||||
.toThrowError(/No provider for service!/);
|
.toThrowError(/No provider for service!/);
|
||||||
});
|
});
|
||||||
@ -526,8 +516,7 @@ class TestComp {
|
|||||||
() => {
|
() => {
|
||||||
TestBed.configureTestingModule({declarations: [SimpleComponent, NeedsService]});
|
TestBed.configureTestingModule({declarations: [SimpleComponent, NeedsService]});
|
||||||
TestBed.overrideComponent(
|
TestBed.overrideComponent(
|
||||||
SimpleComponent,
|
SimpleComponent, {set: {viewProviders: [{provide: 'service', useValue: 'service'}]}});
|
||||||
{set: {viewProviders: [{provide: 'service', useValue: 'service'}]}});
|
|
||||||
expect(() => createComponent('<div simpleComponent><div needsService></div></div>'))
|
expect(() => createComponent('<div simpleComponent><div needsService></div></div>'))
|
||||||
.toThrowError(/No provider for service!/);
|
.toThrowError(/No provider for service!/);
|
||||||
});
|
});
|
||||||
@ -558,8 +547,7 @@ class TestComp {
|
|||||||
SimpleComponent, {set: {providers: [{provide: 'service', useValue: 'hostService'}]}});
|
SimpleComponent, {set: {providers: [{provide: 'service', useValue: 'hostService'}]}});
|
||||||
TestBed.overrideComponent(SimpleComponent, {set: {template: '<div needsService></div>'}});
|
TestBed.overrideComponent(SimpleComponent, {set: {template: '<div needsService></div>'}});
|
||||||
const el = createComponent('<div simpleComponent></div>');
|
const el = createComponent('<div simpleComponent></div>');
|
||||||
expect(el.children[0].children[0].injector.get(NeedsService).service)
|
expect(el.children[0].children[0].injector.get(NeedsService).service).toEqual('hostService');
|
||||||
.toEqual('hostService');
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should instantiate directives that depend on view providers of a component', () => {
|
it('should instantiate directives that depend on view providers of a component', () => {
|
||||||
@ -568,16 +556,14 @@ class TestComp {
|
|||||||
SimpleComponent, {set: {providers: [{provide: 'service', useValue: 'hostService'}]}});
|
SimpleComponent, {set: {providers: [{provide: 'service', useValue: 'hostService'}]}});
|
||||||
TestBed.overrideComponent(SimpleComponent, {set: {template: '<div needsService></div>'}});
|
TestBed.overrideComponent(SimpleComponent, {set: {template: '<div needsService></div>'}});
|
||||||
const el = createComponent('<div simpleComponent></div>');
|
const el = createComponent('<div simpleComponent></div>');
|
||||||
expect(el.children[0].children[0].injector.get(NeedsService).service)
|
expect(el.children[0].children[0].injector.get(NeedsService).service).toEqual('hostService');
|
||||||
.toEqual('hostService');
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should instantiate directives in a root embedded view that depend on view providers of a component',
|
it('should instantiate directives in a root embedded view that depend on view providers of a component',
|
||||||
() => {
|
() => {
|
||||||
TestBed.configureTestingModule({declarations: [SimpleComponent, NeedsService]});
|
TestBed.configureTestingModule({declarations: [SimpleComponent, NeedsService]});
|
||||||
TestBed.overrideComponent(
|
TestBed.overrideComponent(
|
||||||
SimpleComponent,
|
SimpleComponent, {set: {providers: [{provide: 'service', useValue: 'hostService'}]}});
|
||||||
{set: {providers: [{provide: 'service', useValue: 'hostService'}]}});
|
|
||||||
TestBed.overrideComponent(
|
TestBed.overrideComponent(
|
||||||
SimpleComponent, {set: {template: '<div *ngIf="true" needsService></div>'}});
|
SimpleComponent, {set: {template: '<div *ngIf="true" needsService></div>'}});
|
||||||
const el = createComponent('<div simpleComponent></div>');
|
const el = createComponent('<div simpleComponent></div>');
|
||||||
@ -771,8 +757,7 @@ class TestComp {
|
|||||||
})
|
})
|
||||||
.createComponent(MyComp);
|
.createComponent(MyComp);
|
||||||
|
|
||||||
expect(compFixture.componentInstance.vc.parentInjector.get('someToken'))
|
expect(compFixture.componentInstance.vc.parentInjector.get('someToken')).toBe('someValue');
|
||||||
.toBe('someValue');
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -906,8 +891,7 @@ class TestComp {
|
|||||||
it('should inject ViewContainerRef', () => {
|
it('should inject ViewContainerRef', () => {
|
||||||
TestBed.configureTestingModule({declarations: [NeedsViewContainerRef]});
|
TestBed.configureTestingModule({declarations: [NeedsViewContainerRef]});
|
||||||
const el = createComponent('<div needsViewContainerRef></div>');
|
const el = createComponent('<div needsViewContainerRef></div>');
|
||||||
expect(
|
expect(el.children[0].injector.get(NeedsViewContainerRef).viewContainer.element.nativeElement)
|
||||||
el.children[0].injector.get(NeedsViewContainerRef).viewContainer.element.nativeElement)
|
|
||||||
.toBe(el.children[0].nativeElement);
|
.toBe(el.children[0].nativeElement);
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -982,8 +966,7 @@ class TestComp {
|
|||||||
});
|
});
|
||||||
const el = createComponent(
|
const el = createComponent(
|
||||||
'<div [simpleDirective]="true | pipeNeedsChangeDetectorRef" directiveNeedsChangeDetectorRef></div>');
|
'<div [simpleDirective]="true | pipeNeedsChangeDetectorRef" directiveNeedsChangeDetectorRef></div>');
|
||||||
const cdRef =
|
const cdRef = el.children[0].injector.get(DirectiveNeedsChangeDetectorRef).changeDetectorRef;
|
||||||
el.children[0].injector.get(DirectiveNeedsChangeDetectorRef).changeDetectorRef;
|
|
||||||
expect(el.children[0].injector.get(SimpleDirective).value.changeDetectorRef).toEqual(cdRef);
|
expect(el.children[0].injector.get(SimpleDirective).value.changeDetectorRef).toEqual(cdRef);
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -1027,11 +1010,8 @@ class TestComp {
|
|||||||
class SomeComponent {
|
class SomeComponent {
|
||||||
}
|
}
|
||||||
|
|
||||||
@NgModule({
|
@NgModule(
|
||||||
declarations: [SomeComponent],
|
{declarations: [SomeComponent], exports: [SomeComponent], entryComponents: [SomeComponent]})
|
||||||
exports: [SomeComponent],
|
|
||||||
entryComponents: [SomeComponent]
|
|
||||||
})
|
|
||||||
class SomeModule {
|
class SomeModule {
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1065,8 +1045,7 @@ class TestComp {
|
|||||||
imports: [SomeModule],
|
imports: [SomeModule],
|
||||||
declarations: [ComponentThatLoadsAnotherComponentThenMovesIt],
|
declarations: [ComponentThatLoadsAnotherComponentThenMovesIt],
|
||||||
});
|
});
|
||||||
const fixture =
|
const fixture = createComponentFixture(`<listener-and-on-destroy></listener-and-on-destroy>`);
|
||||||
createComponentFixture(`<listener-and-on-destroy></listener-and-on-destroy>`);
|
|
||||||
fixture.detectChanges();
|
fixture.detectChanges();
|
||||||
|
|
||||||
// This test will fail if the ngOnInit of ComponentThatLoadsAnotherComponentThenMovesIt
|
// This test will fail if the ngOnInit of ComponentThatLoadsAnotherComponentThenMovesIt
|
||||||
@ -1074,7 +1053,6 @@ class TestComp {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
})();
|
|
||||||
|
|
||||||
class TestValue {
|
class TestValue {
|
||||||
constructor(public value: string) {}
|
constructor(public value: string) {}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user