Skip to content

Commit e49bfb3

Browse files
chore: store verbatim copy of testing files from angular repository
1 parent 9d92f68 commit e49bfb3

10 files changed

+409
-0
lines changed

nativescript-angular/testing/index.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* @license
3+
* Copyright Google Inc. All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
// This file is not used to build this module. It is only used during editing
10+
// by the TypeScript language service and during build for verification. `ngc`
11+
// replaces this file with production index.ts when it rewrites private symbol
12+
// names.
13+
14+
export * from './public_api';
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"name": "@angular/platform-browser-dynamic/testing",
3+
"typings": "./testing.d.ts",
4+
"main": "../bundles/platform-browser-dynamic-testing.umd.js",
5+
"module": "../esm5/testing.js",
6+
"es2015": "../esm2015/testing.js"
7+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* @license
3+
* Copyright Google Inc. All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
/**
10+
* @module
11+
* @description
12+
* Entry point for all public APIs of this package.
13+
*/
14+
export * from './src/testing';
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/**
2+
* @license
3+
* Copyright Google Inc. All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
import {CompileReflector, DirectiveResolver, ERROR_COMPONENT_TYPE, NgModuleResolver, PipeResolver} from '@angular/compiler';
10+
import {MockDirectiveResolver, MockNgModuleResolver, MockPipeResolver} from '@angular/compiler/testing';
11+
import {Compiler, CompilerFactory, CompilerOptions, Component, ComponentFactory, Directive, Injectable, Injector, ModuleWithComponentFactories, NgModule, NgModuleFactory, Pipe, PlatformRef, StaticProvider, Type, createPlatformFactory, ɵstringify} from '@angular/core';
12+
import {MetadataOverride, ɵTestingCompiler as TestingCompiler, ɵTestingCompilerFactory as TestingCompilerFactory} from '@angular/core/testing';
13+
import {ɵCompilerImpl as CompilerImpl, ɵplatformCoreDynamic as platformCoreDynamic} from '@angular/platform-browser-dynamic';
14+
15+
import {MetadataOverrider} from './metadata_overrider';
16+
17+
export const COMPILER_PROVIDERS: StaticProvider[] = [
18+
{provide: MockPipeResolver, deps: [CompileReflector]},
19+
{provide: PipeResolver, useExisting: MockPipeResolver},
20+
{provide: MockDirectiveResolver, deps: [CompileReflector]},
21+
{provide: DirectiveResolver, useExisting: MockDirectiveResolver},
22+
{provide: MockNgModuleResolver, deps: [CompileReflector]},
23+
{provide: NgModuleResolver, useExisting: MockNgModuleResolver},
24+
];
25+
26+
export class TestingCompilerFactoryImpl implements TestingCompilerFactory {
27+
constructor(private _injector: Injector, private _compilerFactory: CompilerFactory) {}
28+
29+
createTestingCompiler(options: CompilerOptions[]): TestingCompiler {
30+
const compiler = <CompilerImpl>this._compilerFactory.createCompiler(options);
31+
return new TestingCompilerImpl(
32+
compiler, compiler.injector.get(MockDirectiveResolver),
33+
compiler.injector.get(MockPipeResolver), compiler.injector.get(MockNgModuleResolver));
34+
}
35+
}
36+
37+
export class TestingCompilerImpl implements TestingCompiler {
38+
private _overrider = new MetadataOverrider();
39+
constructor(
40+
private _compiler: CompilerImpl, private _directiveResolver: MockDirectiveResolver,
41+
private _pipeResolver: MockPipeResolver, private _moduleResolver: MockNgModuleResolver) {}
42+
get injector(): Injector { return this._compiler.injector; }
43+
44+
compileModuleSync<T>(moduleType: Type<T>): NgModuleFactory<T> {
45+
return this._compiler.compileModuleSync(moduleType);
46+
}
47+
48+
compileModuleAsync<T>(moduleType: Type<T>): Promise<NgModuleFactory<T>> {
49+
return this._compiler.compileModuleAsync(moduleType);
50+
}
51+
compileModuleAndAllComponentsSync<T>(moduleType: Type<T>): ModuleWithComponentFactories<T> {
52+
return this._compiler.compileModuleAndAllComponentsSync(moduleType);
53+
}
54+
55+
compileModuleAndAllComponentsAsync<T>(moduleType: Type<T>):
56+
Promise<ModuleWithComponentFactories<T>> {
57+
return this._compiler.compileModuleAndAllComponentsAsync(moduleType);
58+
}
59+
60+
getComponentFactory<T>(component: Type<T>): ComponentFactory<T> {
61+
return this._compiler.getComponentFactory(component);
62+
}
63+
64+
checkOverrideAllowed(type: Type<any>) {
65+
if (this._compiler.hasAotSummary(type)) {
66+
throw new Error(`${ɵstringify(type)} was AOT compiled, so its metadata cannot be changed.`);
67+
}
68+
}
69+
70+
overrideModule(ngModule: Type<any>, override: MetadataOverride<NgModule>): void {
71+
this.checkOverrideAllowed(ngModule);
72+
const oldMetadata = this._moduleResolver.resolve(ngModule, false);
73+
this._moduleResolver.setNgModule(
74+
ngModule, this._overrider.overrideMetadata(NgModule, oldMetadata, override));
75+
this.clearCacheFor(ngModule);
76+
}
77+
overrideDirective(directive: Type<any>, override: MetadataOverride<Directive>): void {
78+
this.checkOverrideAllowed(directive);
79+
const oldMetadata = this._directiveResolver.resolve(directive, false);
80+
this._directiveResolver.setDirective(
81+
directive, this._overrider.overrideMetadata(Directive, oldMetadata !, override));
82+
this.clearCacheFor(directive);
83+
}
84+
overrideComponent(component: Type<any>, override: MetadataOverride<Component>): void {
85+
this.checkOverrideAllowed(component);
86+
const oldMetadata = this._directiveResolver.resolve(component, false);
87+
this._directiveResolver.setDirective(
88+
component, this._overrider.overrideMetadata(Component, oldMetadata !, override));
89+
this.clearCacheFor(component);
90+
}
91+
overridePipe(pipe: Type<any>, override: MetadataOverride<Pipe>): void {
92+
this.checkOverrideAllowed(pipe);
93+
const oldMetadata = this._pipeResolver.resolve(pipe, false);
94+
this._pipeResolver.setPipe(pipe, this._overrider.overrideMetadata(Pipe, oldMetadata, override));
95+
this.clearCacheFor(pipe);
96+
}
97+
loadAotSummaries(summaries: () => any[]) { this._compiler.loadAotSummaries(summaries); }
98+
clearCache(): void { this._compiler.clearCache(); }
99+
clearCacheFor(type: Type<any>) { this._compiler.clearCacheFor(type); }
100+
101+
getComponentFromError(error: Error) { return (error as any)[ERROR_COMPONENT_TYPE] || null; }
102+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* @license
3+
* Copyright Google Inc. All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
import {Inject, Injectable} from '@angular/core';
10+
import {TestComponentRenderer} from '@angular/core/testing';
11+
import {DOCUMENT, ɵgetDOM as getDOM} from '@angular/platform-browser';
12+
13+
/**
14+
* A DOM based implementation of the TestComponentRenderer.
15+
*/
16+
@Injectable()
17+
export class DOMTestComponentRenderer extends TestComponentRenderer {
18+
constructor(@Inject(DOCUMENT) private _doc: any /** TODO #9100 */) { super(); }
19+
20+
insertRootElement(rootElId: string) {
21+
const rootEl = <HTMLElement>getDOM().firstChild(
22+
getDOM().content(getDOM().createTemplate(`<div id="${rootElId}"></div>`)));
23+
24+
// TODO(juliemr): can/should this be optional?
25+
const oldRoots = getDOM().querySelectorAll(this._doc, '[id^=root]');
26+
for (let i = 0; i < oldRoots.length; i++) {
27+
getDOM().remove(oldRoots[i]);
28+
}
29+
getDOM().appendChild(this._doc.body, rootEl);
30+
}
31+
}
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
/**
2+
* @license
3+
* Copyright Google Inc. All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
import {ɵstringify as stringify} from '@angular/core';
10+
import {MetadataOverride} from '@angular/core/testing';
11+
12+
type StringMap = {
13+
[key: string]: any
14+
};
15+
16+
let _nextReferenceId = 0;
17+
18+
export class MetadataOverrider {
19+
private _references = new Map<any, string>();
20+
/**
21+
* Creates a new instance for the given metadata class
22+
* based on an old instance and overrides.
23+
*/
24+
overrideMetadata<C extends T, T>(
25+
metadataClass: {new (options: T): C;}, oldMetadata: C, override: MetadataOverride<T>): C {
26+
const props: StringMap = {};
27+
if (oldMetadata) {
28+
_valueProps(oldMetadata).forEach((prop) => props[prop] = (<any>oldMetadata)[prop]);
29+
}
30+
31+
if (override.set) {
32+
if (override.remove || override.add) {
33+
throw new Error(`Cannot set and add/remove ${stringify(metadataClass)} at the same time!`);
34+
}
35+
setMetadata(props, override.set);
36+
}
37+
if (override.remove) {
38+
removeMetadata(props, override.remove, this._references);
39+
}
40+
if (override.add) {
41+
addMetadata(props, override.add);
42+
}
43+
return new metadataClass(<any>props);
44+
}
45+
}
46+
47+
function removeMetadata(metadata: StringMap, remove: any, references: Map<any, string>) {
48+
const removeObjects = new Set<string>();
49+
for (const prop in remove) {
50+
const removeValue = remove[prop];
51+
if (removeValue instanceof Array) {
52+
removeValue.forEach(
53+
(value: any) => { removeObjects.add(_propHashKey(prop, value, references)); });
54+
} else {
55+
removeObjects.add(_propHashKey(prop, removeValue, references));
56+
}
57+
}
58+
59+
for (const prop in metadata) {
60+
const propValue = metadata[prop];
61+
if (propValue instanceof Array) {
62+
metadata[prop] = propValue.filter(
63+
(value: any) => !removeObjects.has(_propHashKey(prop, value, references)));
64+
} else {
65+
if (removeObjects.has(_propHashKey(prop, propValue, references))) {
66+
metadata[prop] = undefined;
67+
}
68+
}
69+
}
70+
}
71+
72+
function addMetadata(metadata: StringMap, add: any) {
73+
for (const prop in add) {
74+
const addValue = add[prop];
75+
const propValue = metadata[prop];
76+
if (propValue != null && propValue instanceof Array) {
77+
metadata[prop] = propValue.concat(addValue);
78+
} else {
79+
metadata[prop] = addValue;
80+
}
81+
}
82+
}
83+
84+
function setMetadata(metadata: StringMap, set: any) {
85+
for (const prop in set) {
86+
metadata[prop] = set[prop];
87+
}
88+
}
89+
90+
function _propHashKey(propName: any, propValue: any, references: Map<any, string>): string {
91+
const replacer = (key: any, value: any) => {
92+
if (typeof value === 'function') {
93+
value = _serializeReference(value, references);
94+
}
95+
return value;
96+
};
97+
98+
return `${propName}:${JSON.stringify(propValue, replacer)}`;
99+
}
100+
101+
function _serializeReference(ref: any, references: Map<any, string>): string {
102+
let id = references.get(ref);
103+
if (!id) {
104+
id = `${stringify(ref)}${_nextReferenceId++}`;
105+
references.set(ref, id);
106+
}
107+
return id;
108+
}
109+
110+
111+
function _valueProps(obj: any): string[] {
112+
const props: string[] = [];
113+
// regular public props
114+
Object.keys(obj).forEach((prop) => {
115+
if (!prop.startsWith('_')) {
116+
props.push(prop);
117+
}
118+
});
119+
120+
// getters
121+
let proto = obj;
122+
while (proto = Object.getPrototypeOf(proto)) {
123+
Object.keys(proto).forEach((protoProp) => {
124+
const desc = Object.getOwnPropertyDescriptor(proto, protoProp);
125+
if (!protoProp.startsWith('_') && desc && 'get' in desc) {
126+
props.push(protoProp);
127+
}
128+
});
129+
}
130+
return props;
131+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* @license
3+
* Copyright Google Inc. All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
import {COMPILER_OPTIONS, CompilerFactory, Injector, PlatformRef, StaticProvider, createPlatformFactory} from '@angular/core';
10+
import {TestComponentRenderer, ɵTestingCompilerFactory as TestingCompilerFactory} from '@angular/core/testing';
11+
import {ɵplatformCoreDynamic as platformCoreDynamic} from '@angular/platform-browser-dynamic';
12+
13+
import {COMPILER_PROVIDERS, TestingCompilerFactoryImpl} from './compiler_factory';
14+
15+
/**
16+
* Platform for dynamic tests
17+
*
18+
* @experimental
19+
*/
20+
export const platformCoreDynamicTesting: (extraProviders?: any[]) => PlatformRef =
21+
createPlatformFactory(platformCoreDynamic, 'coreDynamicTesting', [
22+
{provide: COMPILER_OPTIONS, useValue: {providers: COMPILER_PROVIDERS}, multi: true}, {
23+
provide: TestingCompilerFactory,
24+
useClass: TestingCompilerFactoryImpl,
25+
deps: [Injector, CompilerFactory]
26+
}
27+
]);
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
* @license
3+
* Copyright Google Inc. All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
export {DOMTestComponentRenderer as ɵDOMTestComponentRenderer} from './dom_test_component_renderer';
10+
export {platformCoreDynamicTesting as ɵplatformCoreDynamicTesting} from './platform_core_dynamic_testing';
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* @license
3+
* Copyright Google Inc. All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
import {NgModule, createPlatformFactory} from '@angular/core';
10+
import {TestComponentRenderer} from '@angular/core/testing';
11+
import {
12+
ɵINTERNAL_BROWSER_DYNAMIC_PLATFORM_PROVIDERS as INTERNAL_BROWSER_DYNAMIC_PLATFORM_PROVIDERS
13+
} from '@angular/platform-browser-dynamic';
14+
import {BrowserTestingModule} from '@angular/platform-browser/testing';
15+
import {DOMTestComponentRenderer} from './dom_test_component_renderer';
16+
import {platformCoreDynamicTesting} from './platform_core_dynamic_testing';
17+
18+
export * from './private_export_testing';
19+
20+
/**
21+
* @stable
22+
*/
23+
export const platformBrowserDynamicTesting = createPlatformFactory(
24+
platformCoreDynamicTesting, 'browserDynamicTesting',
25+
INTERNAL_BROWSER_DYNAMIC_PLATFORM_PROVIDERS);
26+
27+
/**
28+
* NgModule for testing.
29+
*
30+
* @stable
31+
*/
32+
@NgModule({
33+
exports: [BrowserTestingModule],
34+
providers: [
35+
{provide: TestComponentRenderer, useClass: DOMTestComponentRenderer},
36+
]
37+
})
38+
export class BrowserDynamicTestingModule {
39+
}

0 commit comments

Comments
 (0)