2016-07-21 20:12:00 -04: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
|
|
|
|
*/
|
|
|
|
|
2016-08-17 12:24:44 -04:00
|
|
|
import {DirectiveResolver, ResourceLoader} from '@angular/compiler';
|
2017-08-16 12:00:03 -04:00
|
|
|
import {Compiler, Component, Injector, NgModule, NgModuleFactory, ɵstringify as stringify} from '@angular/core';
|
2016-11-10 17:07:30 -05:00
|
|
|
import {TestBed, async, fakeAsync, inject, tick} from '@angular/core/testing';
|
2017-03-02 15:12:46 -05:00
|
|
|
import {expect} from '@angular/platform-browser/testing/src/matchers';
|
|
|
|
import {MockDirectiveResolver} from '../testing';
|
2016-08-17 12:24:44 -04:00
|
|
|
import {SpyResourceLoader} from './spies';
|
2016-06-24 11:46:43 -04:00
|
|
|
|
2016-08-18 14:20:02 -04:00
|
|
|
@Component({selector: 'child-cmp'})
|
2016-06-24 11:46:43 -04:00
|
|
|
class ChildComp {
|
|
|
|
}
|
|
|
|
|
|
|
|
@Component({selector: 'some-cmp', template: 'someComp'})
|
|
|
|
class SomeComp {
|
|
|
|
}
|
|
|
|
|
|
|
|
@Component({selector: 'some-cmp', templateUrl: './someTpl'})
|
|
|
|
class SomeCompWithUrlTemplate {
|
|
|
|
}
|
|
|
|
|
2017-12-16 17:42:55 -05:00
|
|
|
{
|
2016-08-18 14:20:02 -04:00
|
|
|
describe('RuntimeCompiler', () => {
|
|
|
|
|
|
|
|
describe('compilerComponentSync', () => {
|
|
|
|
describe('never resolving loader', () => {
|
|
|
|
class StubResourceLoader {
|
|
|
|
get(url: string) { return new Promise(() => {}); }
|
|
|
|
}
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
TestBed.configureCompiler(
|
perf: switch angular to use StaticInjector instead of ReflectiveInjector
This change allows ReflectiveInjector to be tree shaken resulting
in not needed Reflect polyfil and smaller bundles.
Code savings for HelloWorld using Closure:
Reflective: bundle.js: 105,864(34,190 gzip)
Static: bundle.js: 154,889(33,555 gzip)
645( 2%)
BREAKING CHANGE:
`platformXXXX()` no longer accepts providers which depend on reflection.
Specifically the method signature when from `Provider[]` to
`StaticProvider[]`.
Example:
Before:
```
[
MyClass,
{provide: ClassA, useClass: SubClassA}
]
```
After:
```
[
{provide: MyClass, deps: [Dep1,...]},
{provide: ClassA, useClass: SubClassA, deps: [Dep1,...]}
]
```
NOTE: This only applies to platform creation and providers for the JIT
compiler. It does not apply to `@Compotent` or `@NgModule` provides
declarations.
Benchpress note: Previously Benchpress also supported reflective
provides, which now require static providers.
DEPRECATION:
- `ReflectiveInjector` is now deprecated as it will be remove. Use
`Injector.create` as a replacement.
closes #18496
2017-08-03 15:33:29 -04:00
|
|
|
{providers: [{provide: ResourceLoader, useClass: StubResourceLoader, deps: []}]});
|
2016-08-18 14:20:02 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should throw when using a templateUrl that has not been compiled before', async(() => {
|
|
|
|
TestBed.configureTestingModule({declarations: [SomeCompWithUrlTemplate]});
|
|
|
|
TestBed.compileComponents().then(() => {
|
|
|
|
expect(() => TestBed.createComponent(SomeCompWithUrlTemplate))
|
|
|
|
.toThrowError(
|
|
|
|
`Can't compile synchronously as ${stringify(SomeCompWithUrlTemplate)} is still being loaded!`);
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('should throw when using a templateUrl in a nested component that has not been compiled before',
|
|
|
|
() => {
|
|
|
|
TestBed.configureTestingModule({declarations: [SomeComp, ChildComp]});
|
|
|
|
TestBed.overrideComponent(ChildComp, {set: {templateUrl: '/someTpl.html'}});
|
|
|
|
TestBed.overrideComponent(SomeComp, {set: {template: '<child-cmp></child-cmp>'}});
|
|
|
|
TestBed.compileComponents().then(() => {
|
|
|
|
expect(() => TestBed.createComponent(SomeComp))
|
|
|
|
.toThrowError(
|
|
|
|
`Can't compile synchronously as ${stringify(ChildComp)} is still being loaded!`);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('resolving loader', () => {
|
|
|
|
class StubResourceLoader {
|
|
|
|
get(url: string) { return Promise.resolve('hello'); }
|
|
|
|
}
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
TestBed.configureCompiler(
|
perf: switch angular to use StaticInjector instead of ReflectiveInjector
This change allows ReflectiveInjector to be tree shaken resulting
in not needed Reflect polyfil and smaller bundles.
Code savings for HelloWorld using Closure:
Reflective: bundle.js: 105,864(34,190 gzip)
Static: bundle.js: 154,889(33,555 gzip)
645( 2%)
BREAKING CHANGE:
`platformXXXX()` no longer accepts providers which depend on reflection.
Specifically the method signature when from `Provider[]` to
`StaticProvider[]`.
Example:
Before:
```
[
MyClass,
{provide: ClassA, useClass: SubClassA}
]
```
After:
```
[
{provide: MyClass, deps: [Dep1,...]},
{provide: ClassA, useClass: SubClassA, deps: [Dep1,...]}
]
```
NOTE: This only applies to platform creation and providers for the JIT
compiler. It does not apply to `@Compotent` or `@NgModule` provides
declarations.
Benchpress note: Previously Benchpress also supported reflective
provides, which now require static providers.
DEPRECATION:
- `ReflectiveInjector` is now deprecated as it will be remove. Use
`Injector.create` as a replacement.
closes #18496
2017-08-03 15:33:29 -04:00
|
|
|
{providers: [{provide: ResourceLoader, useClass: StubResourceLoader, deps: []}]});
|
2016-08-18 14:20:02 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should allow to use templateUrl components that have been loaded before', async(() => {
|
|
|
|
TestBed.configureTestingModule({declarations: [SomeCompWithUrlTemplate]});
|
|
|
|
TestBed.compileComponents().then(() => {
|
|
|
|
const fixture = TestBed.createComponent(SomeCompWithUrlTemplate);
|
|
|
|
expect(fixture.nativeElement).toHaveText('hello');
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-06-24 11:46:43 -04:00
|
|
|
describe('RuntimeCompiler', () => {
|
|
|
|
let compiler: Compiler;
|
2016-08-17 12:24:44 -04:00
|
|
|
let resourceLoader: SpyResourceLoader;
|
2016-07-28 09:31:26 -04:00
|
|
|
let dirResolver: MockDirectiveResolver;
|
2016-06-24 11:46:43 -04:00
|
|
|
let injector: Injector;
|
|
|
|
|
perf: switch angular to use StaticInjector instead of ReflectiveInjector
This change allows ReflectiveInjector to be tree shaken resulting
in not needed Reflect polyfil and smaller bundles.
Code savings for HelloWorld using Closure:
Reflective: bundle.js: 105,864(34,190 gzip)
Static: bundle.js: 154,889(33,555 gzip)
645( 2%)
BREAKING CHANGE:
`platformXXXX()` no longer accepts providers which depend on reflection.
Specifically the method signature when from `Provider[]` to
`StaticProvider[]`.
Example:
Before:
```
[
MyClass,
{provide: ClassA, useClass: SubClassA}
]
```
After:
```
[
{provide: MyClass, deps: [Dep1,...]},
{provide: ClassA, useClass: SubClassA, deps: [Dep1,...]}
]
```
NOTE: This only applies to platform creation and providers for the JIT
compiler. It does not apply to `@Compotent` or `@NgModule` provides
declarations.
Benchpress note: Previously Benchpress also supported reflective
provides, which now require static providers.
DEPRECATION:
- `ReflectiveInjector` is now deprecated as it will be remove. Use
`Injector.create` as a replacement.
closes #18496
2017-08-03 15:33:29 -04:00
|
|
|
beforeEach(() => { TestBed.configureCompiler({providers: [SpyResourceLoader.PROVIDE]}); });
|
2016-06-24 11:46:43 -04:00
|
|
|
|
2016-08-02 05:32:27 -04:00
|
|
|
beforeEach(fakeAsync(inject(
|
2016-08-18 14:20:02 -04:00
|
|
|
[Compiler, ResourceLoader, DirectiveResolver, Injector],
|
|
|
|
(_compiler: Compiler, _resourceLoader: SpyResourceLoader,
|
2016-07-28 09:31:26 -04:00
|
|
|
_dirResolver: MockDirectiveResolver, _injector: Injector) => {
|
2016-06-24 11:46:43 -04:00
|
|
|
compiler = _compiler;
|
2016-08-17 12:24:44 -04:00
|
|
|
resourceLoader = _resourceLoader;
|
2016-07-28 09:31:26 -04:00
|
|
|
dirResolver = _dirResolver;
|
2016-06-24 11:46:43 -04:00
|
|
|
injector = _injector;
|
2016-08-02 05:32:27 -04:00
|
|
|
})));
|
2016-06-24 11:46:43 -04:00
|
|
|
|
2016-07-18 06:50:31 -04:00
|
|
|
describe('compileModuleAsync', () => {
|
2016-06-28 12:54:42 -04:00
|
|
|
it('should allow to use templateUrl components', fakeAsync(() => {
|
2016-07-25 03:36:30 -04:00
|
|
|
@NgModule({
|
|
|
|
declarations: [SomeCompWithUrlTemplate],
|
|
|
|
entryComponents: [SomeCompWithUrlTemplate]
|
|
|
|
})
|
2016-07-18 06:50:31 -04:00
|
|
|
class SomeModule {
|
|
|
|
}
|
|
|
|
|
2016-10-11 18:44:48 -04:00
|
|
|
resourceLoader.spy('get').and.callFake(() => Promise.resolve('hello'));
|
2017-03-24 12:59:58 -04:00
|
|
|
let ngModuleFactory: NgModuleFactory<any> = undefined !;
|
2016-07-18 06:50:31 -04:00
|
|
|
compiler.compileModuleAsync(SomeModule).then((f) => ngModuleFactory = f);
|
2016-06-28 12:54:42 -04:00
|
|
|
tick();
|
2016-07-18 06:50:31 -04:00
|
|
|
expect(ngModuleFactory.moduleType).toBe(SomeModule);
|
2016-06-28 12:54:42 -04:00
|
|
|
}));
|
|
|
|
});
|
|
|
|
|
2016-07-18 06:50:31 -04:00
|
|
|
describe('compileModuleSync', () => {
|
2016-06-28 12:54:42 -04:00
|
|
|
it('should throw when using a templateUrl that has not been compiled before', () => {
|
2016-07-25 03:36:30 -04:00
|
|
|
@NgModule(
|
|
|
|
{declarations: [SomeCompWithUrlTemplate], entryComponents: [SomeCompWithUrlTemplate]})
|
2016-07-18 06:50:31 -04:00
|
|
|
class SomeModule {
|
|
|
|
}
|
|
|
|
|
2016-10-11 18:44:48 -04:00
|
|
|
resourceLoader.spy('get').and.callFake(() => Promise.resolve(''));
|
2016-07-18 06:50:31 -04:00
|
|
|
expect(() => compiler.compileModuleSync(SomeModule))
|
2016-06-28 12:54:42 -04:00
|
|
|
.toThrowError(
|
|
|
|
`Can't compile synchronously as ${stringify(SomeCompWithUrlTemplate)} is still being loaded!`);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should throw when using a templateUrl in a nested component that has not been compiled before',
|
|
|
|
() => {
|
2016-08-19 16:51:45 -04:00
|
|
|
@NgModule({declarations: [SomeComp, ChildComp], entryComponents: [SomeComp]})
|
2016-07-18 06:50:31 -04:00
|
|
|
class SomeModule {
|
|
|
|
}
|
|
|
|
|
2016-10-11 18:44:48 -04:00
|
|
|
resourceLoader.spy('get').and.callFake(() => Promise.resolve(''));
|
2017-08-16 12:00:03 -04:00
|
|
|
dirResolver.setDirective(SomeComp, new Component({selector: 'some-cmp', template: ''}));
|
|
|
|
dirResolver.setDirective(
|
|
|
|
ChildComp, new Component({selector: 'child-cmp', templateUrl: '/someTpl.html'}));
|
2016-07-18 06:50:31 -04:00
|
|
|
expect(() => compiler.compileModuleSync(SomeModule))
|
2016-06-28 12:54:42 -04:00
|
|
|
.toThrowError(
|
|
|
|
`Can't compile synchronously as ${stringify(ChildComp)} is still being loaded!`);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should allow to use templateUrl components that have been loaded before',
|
|
|
|
fakeAsync(() => {
|
2016-07-25 03:36:30 -04:00
|
|
|
@NgModule({
|
|
|
|
declarations: [SomeCompWithUrlTemplate],
|
|
|
|
entryComponents: [SomeCompWithUrlTemplate]
|
|
|
|
})
|
2016-07-18 06:50:31 -04:00
|
|
|
class SomeModule {
|
|
|
|
}
|
|
|
|
|
2016-10-11 18:44:48 -04:00
|
|
|
resourceLoader.spy('get').and.callFake(() => Promise.resolve('hello'));
|
2016-07-18 06:50:31 -04:00
|
|
|
compiler.compileModuleAsync(SomeModule);
|
|
|
|
tick();
|
|
|
|
|
2016-11-12 08:08:58 -05:00
|
|
|
const ngModuleFactory = compiler.compileModuleSync(SomeModule);
|
2016-07-18 06:50:31 -04:00
|
|
|
expect(ngModuleFactory).toBeTruthy();
|
2016-06-28 12:54:42 -04:00
|
|
|
}));
|
|
|
|
});
|
2016-06-24 11:46:43 -04:00
|
|
|
});
|
2016-07-22 12:20:51 -04:00
|
|
|
}
|