2018-02-05 20:31:12 -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-12-13 18:51:47 -05:00
|
|
|
import {AttributeMarker} from '@angular/compiler/src/core';
|
2018-08-03 15:20:27 -04:00
|
|
|
import {setup} from '@angular/compiler/test/aot/test_util';
|
2018-02-05 20:31:12 -05:00
|
|
|
import {compile, expectEmit} from './mock_compile';
|
|
|
|
|
2018-07-11 12:56:47 -04:00
|
|
|
|
2018-12-12 18:23:12 -05:00
|
|
|
/**
|
|
|
|
* These tests are codified version of the tests in compiler_canonical_spec.ts. Every
|
|
|
|
* test in compiler_canonical_spec.ts should have a corresponding test here.
|
|
|
|
*/
|
2018-02-05 20:31:12 -05:00
|
|
|
describe('compiler compliance', () => {
|
2018-04-10 23:57:20 -04:00
|
|
|
|
2018-02-05 20:31:12 -05:00
|
|
|
const angularFiles = setup({
|
2018-08-01 03:52:34 -04:00
|
|
|
compileAngular: false,
|
2018-02-05 20:31:12 -05:00
|
|
|
compileAnimations: false,
|
2018-08-01 03:52:34 -04:00
|
|
|
compileFakeCore: true,
|
2018-02-05 20:31:12 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('elements', () => {
|
2018-06-08 12:00:01 -04:00
|
|
|
it('should handle SVG', () => {
|
|
|
|
const files = {
|
|
|
|
app: {
|
|
|
|
'spec.ts': `
|
|
|
|
import {Component, NgModule} from '@angular/core';
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'my-component',
|
|
|
|
template: \`<div class="my-app" title="Hello"><svg><circle cx="20" cy="30" r="50"/></svg><p>test</p></div>\`
|
|
|
|
})
|
|
|
|
export class MyComponent {}
|
|
|
|
|
|
|
|
@NgModule({declarations: [MyComponent]})
|
|
|
|
export class MyModule {}
|
|
|
|
`
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// The factory should look like this:
|
2018-07-16 19:36:31 -04:00
|
|
|
const factory =
|
2019-10-11 17:18:45 -04:00
|
|
|
'MyComponent.ɵfac = function MyComponent_Factory(t) { return new (t || MyComponent)(); }';
|
2018-06-08 12:00:01 -04:00
|
|
|
|
|
|
|
// The template should look like this (where IDENT is a wild card for an identifier):
|
|
|
|
const template = `
|
|
|
|
…
|
2019-09-23 14:08:51 -04:00
|
|
|
consts: [["title", "Hello", ${AttributeMarker.Classes}, "my-app"], ["cx", "20", "cy", "30", "r", "50"]],
|
2018-07-12 18:10:55 -04:00
|
|
|
template: function MyComponent_Template(rf, ctx) {
|
2018-06-08 12:00:01 -04:00
|
|
|
if (rf & 1) {
|
2019-09-23 14:08:51 -04:00
|
|
|
$r3$.ɵɵelementStart(0, "div", 0);
|
2019-05-17 21:49:21 -04:00
|
|
|
$r3$.ɵɵnamespaceSVG();
|
|
|
|
$r3$.ɵɵelementStart(1, "svg");
|
2019-09-23 14:08:51 -04:00
|
|
|
$r3$.ɵɵelement(2, "circle", 1);
|
2019-05-17 21:49:21 -04:00
|
|
|
$r3$.ɵɵelementEnd();
|
|
|
|
$r3$.ɵɵnamespaceHTML();
|
|
|
|
$r3$.ɵɵelementStart(3, "p");
|
|
|
|
$r3$.ɵɵtext(4, "test");
|
|
|
|
$r3$.ɵɵelementEnd();
|
|
|
|
$r3$.ɵɵelementEnd();
|
2018-06-08 12:00:01 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
|
|
const result = compile(files, angularFiles);
|
|
|
|
|
|
|
|
expectEmit(result.source, factory, 'Incorrect factory');
|
|
|
|
expectEmit(result.source, template, 'Incorrect template');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should handle MathML', () => {
|
|
|
|
const files = {
|
|
|
|
app: {
|
|
|
|
'spec.ts': `
|
|
|
|
import {Component, NgModule} from '@angular/core';
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'my-component',
|
|
|
|
template: \`<div class="my-app" title="Hello"><math><infinity/></math><p>test</p></div>\`
|
|
|
|
})
|
|
|
|
export class MyComponent {}
|
|
|
|
|
|
|
|
@NgModule({declarations: [MyComponent]})
|
|
|
|
export class MyModule {}
|
|
|
|
`
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// The factory should look like this:
|
2018-07-16 19:36:31 -04:00
|
|
|
const factory =
|
2019-10-11 17:18:45 -04:00
|
|
|
'MyComponent.ɵfac = function MyComponent_Factory(t) { return new (t || MyComponent)(); }';
|
2018-06-08 12:00:01 -04:00
|
|
|
|
|
|
|
// The template should look like this (where IDENT is a wild card for an identifier):
|
|
|
|
const template = `
|
|
|
|
…
|
2019-09-23 14:08:51 -04:00
|
|
|
consts: [["title", "Hello", ${AttributeMarker.Classes}, "my-app"]],
|
2018-07-12 18:10:55 -04:00
|
|
|
template: function MyComponent_Template(rf, ctx) {
|
2018-06-08 12:00:01 -04:00
|
|
|
if (rf & 1) {
|
2019-09-23 14:08:51 -04:00
|
|
|
$r3$.ɵɵelementStart(0, "div", 0);
|
2019-05-17 21:49:21 -04:00
|
|
|
$r3$.ɵɵnamespaceMathML();
|
|
|
|
$r3$.ɵɵelementStart(1, "math");
|
|
|
|
$r3$.ɵɵelement(2, "infinity");
|
|
|
|
$r3$.ɵɵelementEnd();
|
|
|
|
$r3$.ɵɵnamespaceHTML();
|
|
|
|
$r3$.ɵɵelementStart(3, "p");
|
|
|
|
$r3$.ɵɵtext(4, "test");
|
|
|
|
$r3$.ɵɵelementEnd();
|
|
|
|
$r3$.ɵɵelementEnd();
|
2018-06-08 12:00:01 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
|
|
const result = compile(files, angularFiles);
|
|
|
|
expectEmit(result.source, factory, 'Incorrect factory');
|
|
|
|
expectEmit(result.source, template, 'Incorrect template');
|
|
|
|
});
|
|
|
|
|
2018-02-05 20:31:12 -05:00
|
|
|
it('should translate DOM structure', () => {
|
|
|
|
const files = {
|
|
|
|
app: {
|
|
|
|
'spec.ts': `
|
|
|
|
import {Component, NgModule} from '@angular/core';
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'my-component',
|
|
|
|
template: \`<div class="my-app" title="Hello">Hello <b>World</b>!</div>\`
|
|
|
|
})
|
|
|
|
export class MyComponent {}
|
|
|
|
|
|
|
|
@NgModule({declarations: [MyComponent]})
|
|
|
|
export class MyModule {}
|
|
|
|
`
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// The factory should look like this:
|
2018-07-16 19:36:31 -04:00
|
|
|
const factory =
|
2019-10-11 17:18:45 -04:00
|
|
|
'MyComponent.ɵfac = function MyComponent_Factory(t) { return new (t || MyComponent)(); }';
|
2018-02-05 20:31:12 -05:00
|
|
|
|
|
|
|
// The template should look like this (where IDENT is a wild card for an identifier):
|
|
|
|
const template = `
|
|
|
|
…
|
2019-09-23 14:08:51 -04:00
|
|
|
consts: [["title", "Hello", ${AttributeMarker.Classes}, "my-app"]],
|
2018-07-12 18:10:55 -04:00
|
|
|
template: function MyComponent_Template(rf, ctx) {
|
2018-04-10 23:57:20 -04:00
|
|
|
if (rf & 1) {
|
2019-09-23 14:08:51 -04:00
|
|
|
$r3$.ɵɵelementStart(0, "div", 0);
|
2019-05-17 21:49:21 -04:00
|
|
|
$r3$.ɵɵtext(1, "Hello ");
|
|
|
|
$r3$.ɵɵelementStart(2, "b");
|
|
|
|
$r3$.ɵɵtext(3, "World");
|
|
|
|
$r3$.ɵɵelementEnd();
|
|
|
|
$r3$.ɵɵtext(4, "!");
|
|
|
|
$r3$.ɵɵelementEnd();
|
2018-02-05 20:31:12 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
|
|
const result = compile(files, angularFiles);
|
|
|
|
|
|
|
|
expectEmit(result.source, factory, 'Incorrect factory');
|
|
|
|
expectEmit(result.source, template, 'Incorrect template');
|
|
|
|
});
|
2018-04-16 18:29:42 -04:00
|
|
|
|
2018-06-08 18:25:39 -04:00
|
|
|
// TODO(https://github.com/angular/angular/issues/24426): We need to support the parser actually
|
2019-01-10 16:24:32 -05:00
|
|
|
// building the proper attributes based off of xmlns attributes.
|
2019-09-23 14:08:51 -04:00
|
|
|
xit('should support namespaced attributes', () => {
|
2018-06-08 18:25:39 -04:00
|
|
|
const files = {
|
|
|
|
app: {
|
|
|
|
'spec.ts': `
|
|
|
|
import {Component, NgModule} from '@angular/core';
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'my-component',
|
|
|
|
template: \`<div xmlns:foo="http://someuri/foo" class="my-app" foo:bar="baz" title="Hello" foo:qux="quacks">Hello <b>World</b>!</div>\`
|
|
|
|
})
|
|
|
|
export class MyComponent {}
|
|
|
|
|
|
|
|
@NgModule({declarations: [MyComponent]})
|
|
|
|
export class MyModule {}
|
|
|
|
`
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// The factory should look like this:
|
2018-07-16 19:36:31 -04:00
|
|
|
const factory =
|
2019-10-11 17:18:45 -04:00
|
|
|
'MyComponent.ɵfac = function MyComponent_Factory(t) { return new (t || MyComponent)(); }';
|
2018-06-08 18:25:39 -04:00
|
|
|
|
|
|
|
// The template should look like this (where IDENT is a wild card for an identifier):
|
|
|
|
const template = `
|
|
|
|
…
|
2019-09-23 14:08:51 -04:00
|
|
|
consts: [["class", "my-app", 0, "http://someuri/foo", "foo:bar", "baz", "title", "Hello", 0, "http://someuri/foo", "foo:qux", "quacks"]],
|
2018-07-12 18:10:55 -04:00
|
|
|
template: function MyComponent_Template(rf, ctx) {
|
2018-06-08 18:25:39 -04:00
|
|
|
if (rf & 1) {
|
2019-09-23 14:08:51 -04:00
|
|
|
$r3$.ɵɵelementStart(0, "div", 0);
|
2019-05-17 21:49:21 -04:00
|
|
|
$r3$.ɵɵtext(1, "Hello ");
|
|
|
|
$r3$.ɵɵelementStart(2, "b");
|
|
|
|
$r3$.ɵɵtext(3, "World");
|
|
|
|
$r3$.ɵɵelementEnd();
|
|
|
|
$r3$.ɵɵtext(4, "!");
|
|
|
|
$r3$.ɵɵelementEnd();
|
2018-06-08 18:25:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
|
|
const result = compile(files, angularFiles);
|
|
|
|
|
|
|
|
expectEmit(result.source, factory, 'Incorrect factory');
|
|
|
|
expectEmit(result.source, template, 'Incorrect template');
|
|
|
|
});
|
|
|
|
|
2018-08-08 04:21:20 -04:00
|
|
|
it('should support <ng-container>', () => {
|
|
|
|
const files = {
|
|
|
|
app: {
|
|
|
|
'spec.ts': `
|
|
|
|
import {Component, NgModule} from '@angular/core';
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'my-component',
|
|
|
|
template: \`<ng-container><span>in a </span>container</ng-container>\`
|
|
|
|
})
|
|
|
|
export class MyComponent {}
|
|
|
|
|
|
|
|
@NgModule({declarations: [MyComponent]})
|
|
|
|
export class MyModule {}
|
|
|
|
`
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// The template should look like this (where IDENT is a wild card for an identifier):
|
|
|
|
const template = `
|
|
|
|
…
|
|
|
|
template: function MyComponent_Template(rf, ctx) {
|
|
|
|
if (rf & 1) {
|
2019-05-17 21:49:21 -04:00
|
|
|
i0.ɵɵelementContainerStart(0);
|
|
|
|
i0.ɵɵelementStart(1, "span");
|
|
|
|
i0.ɵɵtext(2, "in a ");
|
|
|
|
i0.ɵɵelementEnd();
|
|
|
|
i0.ɵɵtext(3, "container");
|
|
|
|
i0.ɵɵelementContainerEnd();
|
2018-08-08 04:21:20 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
`;
|
|
|
|
|
|
|
|
const result = compile(files, angularFiles);
|
|
|
|
expectEmit(result.source, template, 'Incorrect template');
|
|
|
|
});
|
|
|
|
|
2019-07-07 10:35:58 -04:00
|
|
|
it('should generate self-closing elementContainer instruction for empty <ng-container>', () => {
|
2018-08-08 04:21:20 -04:00
|
|
|
const files = {
|
|
|
|
app: {
|
|
|
|
'spec.ts': `
|
|
|
|
import {Component, NgModule} from '@angular/core';
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'my-component',
|
|
|
|
template: \`<ng-container></ng-container>\`
|
|
|
|
})
|
|
|
|
export class MyComponent {}
|
|
|
|
|
|
|
|
@NgModule({declarations: [MyComponent]})
|
|
|
|
export class MyModule {}
|
|
|
|
`
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// The template should look like this (where IDENT is a wild card for an identifier):
|
|
|
|
const template = `
|
|
|
|
…
|
|
|
|
template: function MyComponent_Template(rf, ctx) {
|
|
|
|
if (rf & 1) {
|
2019-07-07 10:35:58 -04:00
|
|
|
i0.ɵɵelementContainer(0);
|
2018-08-08 04:21:20 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
`;
|
|
|
|
|
|
|
|
const result = compile(files, angularFiles);
|
|
|
|
expectEmit(result.source, template, 'Incorrect template');
|
|
|
|
});
|
|
|
|
|
2018-04-16 18:29:42 -04:00
|
|
|
it('should bind to element properties', () => {
|
|
|
|
const files = {
|
|
|
|
app: {
|
|
|
|
'spec.ts': `
|
|
|
|
import {Component, NgModule} from '@angular/core';
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'my-component',
|
|
|
|
template: \`<div [id]="id"></div>\`
|
|
|
|
})
|
|
|
|
export class MyComponent {
|
|
|
|
id = 'one';
|
|
|
|
}
|
|
|
|
|
|
|
|
@NgModule({declarations: [MyComponent]})
|
|
|
|
export class MyModule {}
|
|
|
|
`
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-07-16 19:36:31 -04:00
|
|
|
const factory =
|
2019-10-11 17:18:45 -04:00
|
|
|
'MyComponent.ɵfac = function MyComponent_Factory(t) { return new (t || MyComponent)(); }';
|
2018-04-16 18:29:42 -04:00
|
|
|
const template = `
|
2018-08-20 09:20:12 -04:00
|
|
|
…
|
2019-09-23 14:08:51 -04:00
|
|
|
consts: [[${AttributeMarker.Bindings}, "id"]],
|
2018-07-12 18:10:55 -04:00
|
|
|
template: function MyComponent_Template(rf, ctx) {
|
2018-04-16 18:29:42 -04:00
|
|
|
if (rf & 1) {
|
2019-09-23 14:08:51 -04:00
|
|
|
$r3$.ɵɵelement(0, "div", 0);
|
2018-04-16 18:29:42 -04:00
|
|
|
}
|
|
|
|
if (rf & 2) {
|
2019-05-17 21:49:21 -04:00
|
|
|
$r3$.ɵɵproperty("id", ctx.id);
|
2018-04-16 18:29:42 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
|
|
const result = compile(files, angularFiles);
|
|
|
|
|
|
|
|
expectEmit(result.source, factory, 'Incorrect factory');
|
|
|
|
expectEmit(result.source, template, 'Incorrect template');
|
|
|
|
});
|
|
|
|
|
2018-05-21 18:59:25 -04:00
|
|
|
it('should reserve slots for pure functions', () => {
|
|
|
|
const files = {
|
|
|
|
app: {
|
|
|
|
'spec.ts': `
|
|
|
|
import {Component, NgModule} from '@angular/core';
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'my-component',
|
|
|
|
template: \`<div
|
|
|
|
[ternary]="cond ? [a] : [0]"
|
|
|
|
[pipe]="value | pipe:1:2"
|
|
|
|
[and]="cond && [b]"
|
|
|
|
[or]="cond || [c]"
|
|
|
|
></div>\`
|
|
|
|
})
|
|
|
|
export class MyComponent {
|
|
|
|
id = 'one';
|
|
|
|
}
|
|
|
|
|
|
|
|
@NgModule({declarations: [MyComponent]})
|
|
|
|
export class MyModule {}
|
|
|
|
`
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-04-16 20:17:12 -04:00
|
|
|
///////////////
|
|
|
|
// TODO(FW-1273): The code generated below is adding extra parens, and we need to stop
|
|
|
|
// generating those.
|
|
|
|
//
|
|
|
|
// For example:
|
2019-05-17 21:49:21 -04:00
|
|
|
// `$r3$.ɵɵproperty("ternary", (ctx.cond ? $r3$.ɵɵpureFunction1(8, $c0$, ctx.a): $c1$));`
|
2019-04-16 20:17:12 -04:00
|
|
|
///////////////
|
|
|
|
|
2018-07-16 19:36:31 -04:00
|
|
|
const factory =
|
2019-10-11 17:18:45 -04:00
|
|
|
'MyComponent.ɵfac = function MyComponent_Factory(t) { return new (t || MyComponent)(); }';
|
2018-05-21 18:59:25 -04:00
|
|
|
const template = `
|
2018-07-12 18:10:55 -04:00
|
|
|
template: function MyComponent_Template(rf, ctx) {
|
2018-05-21 18:59:25 -04:00
|
|
|
if (rf & 1) {
|
2019-09-23 14:08:51 -04:00
|
|
|
$r3$.ɵɵelement(0, "div", 0);
|
2019-11-13 08:26:34 -05:00
|
|
|
$r3$.ɵɵpipe(1, "pipe");
|
2018-05-21 18:59:25 -04:00
|
|
|
}
|
|
|
|
if (rf & 2) {
|
2019-11-13 08:26:34 -05:00
|
|
|
$r3$.ɵɵproperty("ternary", ctx.cond ? $r3$.ɵɵpureFunction1(8, $c0$, ctx.a): $r3$.ɵɵpureFunction0(10, $c1$))("pipe", $r3$.ɵɵpipeBind3(1, 4, ctx.value, 1, 2))("and", ctx.cond && $r3$.ɵɵpureFunction1(11, $c0$, ctx.b))("or", ctx.cond || $r3$.ɵɵpureFunction1(13, $c0$, ctx.c));
|
2018-05-21 18:59:25 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
|
|
const result = compile(files, angularFiles);
|
|
|
|
|
|
|
|
expectEmit(result.source, factory, 'Incorrect factory');
|
|
|
|
expectEmit(result.source, template, 'Incorrect template');
|
|
|
|
});
|
|
|
|
|
2018-12-10 17:53:10 -05:00
|
|
|
it('should reserve slots for pure functions in host binding function', () => {
|
|
|
|
const files = {
|
|
|
|
app: {
|
|
|
|
'spec.ts': `
|
|
|
|
import {Component, NgModule, Input} from '@angular/core';
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'my-component',
|
|
|
|
template: '...',
|
|
|
|
host: {
|
|
|
|
'[@expansionHeight]': \`{
|
|
|
|
value: getExpandedState(),
|
|
|
|
params: {
|
|
|
|
collapsedHeight: collapsedHeight,
|
|
|
|
expandedHeight: expandedHeight
|
|
|
|
}
|
|
|
|
}\`,
|
|
|
|
'[@expansionWidth]': \`{
|
|
|
|
value: getExpandedState(),
|
|
|
|
params: {
|
|
|
|
collapsedWidth: collapsedWidth,
|
|
|
|
expandedWidth: expandedWidth
|
|
|
|
}
|
|
|
|
}\`
|
|
|
|
}
|
|
|
|
})
|
|
|
|
export class MyComponent {
|
|
|
|
@Input() expandedHeight: string;
|
|
|
|
@Input() collapsedHeight: string;
|
|
|
|
|
|
|
|
@Input() expandedWidth: string;
|
|
|
|
@Input() collapsedWidth: string;
|
|
|
|
|
|
|
|
getExpandedState() {
|
|
|
|
return 'expanded';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@NgModule({
|
|
|
|
declarations: [MyComponent]
|
|
|
|
})
|
|
|
|
export class MyModule {}
|
|
|
|
`
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const hostBindingsDef = `
|
|
|
|
const $_c0$ = function (a0, a1) { return { collapsedHeight: a0, expandedHeight: a1 }; };
|
|
|
|
const $_c1$ = function (a0, a1) { return { value: a0, params: a1 }; };
|
|
|
|
const $_c2$ = function (a0, a1) { return { collapsedWidth: a0, expandedWidth: a1 }; };
|
|
|
|
…
|
2020-01-08 14:32:33 -05:00
|
|
|
hostVars: 14,
|
2020-01-25 06:38:42 -05:00
|
|
|
hostBindings: function MyComponent_HostBindings(rf, ctx) {
|
2018-12-10 17:53:10 -05:00
|
|
|
if (rf & 2) {
|
2019-05-24 20:21:41 -04:00
|
|
|
$r3$.ɵɵupdateSyntheticHostBinding("@expansionHeight",
|
2019-05-17 21:49:21 -04:00
|
|
|
$r3$.ɵɵpureFunction2(5, $_c1$, ctx.getExpandedState(),
|
|
|
|
$r3$.ɵɵpureFunction2(2, $_c0$, ctx.collapsedHeight, ctx.expandedHeight)
|
2018-12-10 17:53:10 -05:00
|
|
|
)
|
2019-06-27 14:23:15 -04:00
|
|
|
)("@expansionWidth",
|
2019-05-17 21:49:21 -04:00
|
|
|
$r3$.ɵɵpureFunction2(11, $_c1$, ctx.getExpandedState(),
|
|
|
|
$r3$.ɵɵpureFunction2(8, $_c2$, ctx.collapsedWidth, ctx.expandedWidth)
|
2018-12-10 17:53:10 -05:00
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
…
|
|
|
|
`;
|
|
|
|
const result = compile(files, angularFiles);
|
|
|
|
expectEmit(result.source, hostBindingsDef, 'Incorrect "hostBindings" function');
|
|
|
|
});
|
|
|
|
|
2018-04-16 18:29:42 -04:00
|
|
|
it('should bind to class and style names', () => {
|
|
|
|
const files = {
|
|
|
|
app: {
|
|
|
|
'spec.ts': `
|
|
|
|
import {Component, NgModule} from '@angular/core';
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'my-component',
|
|
|
|
template: \`<div [class.error]="error" [style.background-color]="color"></div>\`
|
|
|
|
})
|
|
|
|
export class MyComponent {
|
|
|
|
error = true;
|
|
|
|
color = 'red';
|
|
|
|
}
|
|
|
|
|
|
|
|
@NgModule({declarations: [MyComponent]})
|
|
|
|
export class MyModule {}
|
|
|
|
`
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-07-16 19:36:31 -04:00
|
|
|
const factory =
|
2019-10-11 17:18:45 -04:00
|
|
|
'MyComponent.ɵfac = function MyComponent_Factory(t) { return new (t || MyComponent)(); }';
|
2018-04-16 18:29:42 -04:00
|
|
|
const template = `
|
2019-10-10 17:57:15 -04:00
|
|
|
MyComponent.ɵcmp = i0.ɵɵdefineComponent({type:MyComponent,selectors:[["my-component"]],
|
2019-09-23 14:08:51 -04:00
|
|
|
decls: 1,
|
2019-12-14 22:48:24 -05:00
|
|
|
vars: 4,
|
2018-08-18 14:14:50 -04:00
|
|
|
template: function MyComponent_Template(rf,ctx){
|
2018-07-12 18:10:55 -04:00
|
|
|
if (rf & 1) {
|
2019-09-09 16:14:26 -04:00
|
|
|
$r3$.ɵɵelement(0, "div");
|
2018-07-12 18:10:55 -04:00
|
|
|
}
|
|
|
|
if (rf & 2) {
|
2019-05-28 13:31:01 -04:00
|
|
|
$r3$.ɵɵstyleProp("background-color", ctx.color);
|
|
|
|
$r3$.ɵɵclassProp("error", ctx.error);
|
2018-06-19 15:45:00 -04:00
|
|
|
}
|
2018-11-19 12:48:14 -05:00
|
|
|
},
|
|
|
|
encapsulation: 2
|
2018-07-12 18:10:55 -04:00
|
|
|
});
|
2018-04-16 18:29:42 -04:00
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
|
|
const result = compile(files, angularFiles);
|
|
|
|
|
|
|
|
expectEmit(result.source, factory, 'Incorrect factory');
|
|
|
|
expectEmit(result.source, template, 'Incorrect template');
|
|
|
|
});
|
|
|
|
|
2019-09-23 14:08:51 -04:00
|
|
|
it('should de-duplicate attribute arrays', () => {
|
|
|
|
const files = {
|
|
|
|
app: {
|
|
|
|
'spec.ts': `
|
|
|
|
import {Component, NgModule} from '@angular/core';
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'my-component',
|
|
|
|
template: \`
|
|
|
|
<div title="hi"></div>
|
|
|
|
<span title="hi"></span>
|
|
|
|
\`
|
|
|
|
})
|
|
|
|
export class MyComponent {
|
|
|
|
}
|
|
|
|
|
|
|
|
@NgModule({declarations: [MyComponent]})
|
|
|
|
export class MyModule {}
|
|
|
|
`
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const template = `
|
|
|
|
…
|
|
|
|
consts: [["title", "hi"]],
|
|
|
|
template: function MyComponent_Template(rf, ctx) {
|
|
|
|
if (rf & 1) {
|
|
|
|
$r3$.ɵɵelement(0, "div", 0);
|
|
|
|
$r3$.ɵɵelement(1, "span", 0);
|
|
|
|
}
|
|
|
|
…
|
|
|
|
}
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
|
|
const result = compile(files, angularFiles);
|
|
|
|
expectEmit(result.source, template, 'Incorrect template');
|
|
|
|
});
|
|
|
|
|
2018-02-05 20:31:12 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('components & directives', () => {
|
|
|
|
it('should instantiate directives', () => {
|
|
|
|
const files = {
|
|
|
|
app: {
|
|
|
|
'spec.ts': `
|
|
|
|
import {Component, Directive, NgModule} from '@angular/core';
|
|
|
|
|
|
|
|
@Component({selector: 'child', template: 'child-view'})
|
|
|
|
export class ChildComponent {}
|
|
|
|
|
|
|
|
@Directive({selector: '[some-directive]'})
|
|
|
|
export class SomeDirective {}
|
|
|
|
|
|
|
|
@Component({selector: 'my-component', template: '<child some-directive></child>!'})
|
|
|
|
export class MyComponent {}
|
|
|
|
|
|
|
|
@NgModule({declarations: [ChildComponent, SomeDirective, MyComponent]})
|
|
|
|
export class MyModule{}
|
|
|
|
`
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// ChildComponent definition should be:
|
|
|
|
const ChildComponentDefinition = `
|
2019-10-10 17:57:15 -04:00
|
|
|
ChildComponent.ɵcmp = $r3$.ɵɵdefineComponent({
|
2018-02-05 20:31:12 -05:00
|
|
|
type: ChildComponent,
|
2018-07-12 18:10:55 -04:00
|
|
|
selectors: [["child"]],
|
2019-09-23 14:08:51 -04:00
|
|
|
decls: 1,
|
2018-08-18 14:14:50 -04:00
|
|
|
vars: 0,
|
|
|
|
template: function ChildComponent_Template(rf, ctx) {
|
2018-04-10 23:57:20 -04:00
|
|
|
if (rf & 1) {
|
2019-05-17 21:49:21 -04:00
|
|
|
$r3$.ɵɵtext(0, "child-view");
|
2018-02-05 20:31:12 -05:00
|
|
|
}
|
2018-11-19 12:48:14 -05:00
|
|
|
},
|
|
|
|
encapsulation: 2
|
2018-02-05 20:31:12 -05:00
|
|
|
});`;
|
|
|
|
|
2019-08-12 02:26:20 -04:00
|
|
|
const ChildComponentFactory =
|
2019-10-11 17:18:45 -04:00
|
|
|
`ChildComponent.ɵfac = function ChildComponent_Factory(t) { return new (t || ChildComponent)(); };`;
|
2019-08-12 02:26:20 -04:00
|
|
|
|
2018-02-05 20:31:12 -05:00
|
|
|
// SomeDirective definition should be:
|
|
|
|
const SomeDirectiveDefinition = `
|
2019-10-11 15:28:12 -04:00
|
|
|
SomeDirective.ɵdir = $r3$.ɵɵdefineDirective({
|
2018-02-05 20:31:12 -05:00
|
|
|
type: SomeDirective,
|
2019-08-12 02:26:20 -04:00
|
|
|
selectors: [["", "some-directive", ""]]
|
2018-02-05 20:31:12 -05:00
|
|
|
});
|
|
|
|
`;
|
|
|
|
|
2019-08-12 02:26:20 -04:00
|
|
|
const SomeDirectiveFactory =
|
2019-10-11 17:18:45 -04:00
|
|
|
`SomeDirective.ɵfac = function SomeDirective_Factory(t) {return new (t || SomeDirective)(); };`;
|
2019-08-12 02:26:20 -04:00
|
|
|
|
2018-02-05 20:31:12 -05:00
|
|
|
// MyComponent definition should be:
|
|
|
|
const MyComponentDefinition = `
|
|
|
|
…
|
2019-10-10 17:57:15 -04:00
|
|
|
MyComponent.ɵcmp = $r3$.ɵɵdefineComponent({
|
2018-02-05 20:31:12 -05:00
|
|
|
type: MyComponent,
|
2018-07-12 18:10:55 -04:00
|
|
|
selectors: [["my-component"]],
|
2019-09-23 14:08:51 -04:00
|
|
|
decls: 2,
|
2018-08-18 14:14:50 -04:00
|
|
|
vars: 0,
|
2019-09-23 14:08:51 -04:00
|
|
|
consts: [["some-directive", ""]],
|
2018-08-18 14:14:50 -04:00
|
|
|
template: function MyComponent_Template(rf, ctx) {
|
2018-04-10 23:57:20 -04:00
|
|
|
if (rf & 1) {
|
2019-09-23 14:08:51 -04:00
|
|
|
$r3$.ɵɵelement(0, "child", 0);
|
2019-05-17 21:49:21 -04:00
|
|
|
$r3$.ɵɵtext(1, "!");
|
2018-02-05 20:31:12 -05:00
|
|
|
}
|
2018-03-29 15:58:41 -04:00
|
|
|
},
|
2018-11-19 12:48:14 -05:00
|
|
|
directives: [ChildComponent, SomeDirective],
|
|
|
|
encapsulation: 2
|
2018-02-05 20:31:12 -05:00
|
|
|
});
|
|
|
|
`;
|
|
|
|
|
2019-08-12 02:26:20 -04:00
|
|
|
const MyComponentFactory =
|
2019-10-11 17:18:45 -04:00
|
|
|
`MyComponent.ɵfac = function MyComponent_Factory(t) { return new (t || MyComponent)(); };`;
|
2018-02-05 20:31:12 -05:00
|
|
|
|
|
|
|
const result = compile(files, angularFiles);
|
|
|
|
const source = result.source;
|
|
|
|
|
2019-10-10 17:57:15 -04:00
|
|
|
expectEmit(source, ChildComponentDefinition, 'Incorrect ChildComponent.ɵcmp');
|
2019-10-11 17:18:45 -04:00
|
|
|
expectEmit(source, ChildComponentFactory, 'Incorrect ChildComponent.ɵfac');
|
2019-10-11 15:28:12 -04:00
|
|
|
expectEmit(source, SomeDirectiveDefinition, 'Incorrect SomeDirective.ɵdir');
|
2019-10-11 17:18:45 -04:00
|
|
|
expectEmit(source, SomeDirectiveFactory, 'Incorrect SomeDirective.ɵfac');
|
2019-10-10 17:57:15 -04:00
|
|
|
expectEmit(source, MyComponentDefinition, 'Incorrect MyComponentDefinition.ɵcmp');
|
2019-10-11 17:18:45 -04:00
|
|
|
expectEmit(source, MyComponentFactory, 'Incorrect MyComponentDefinition.ɵfac');
|
2018-02-05 20:31:12 -05:00
|
|
|
});
|
|
|
|
|
2018-03-29 19:41:53 -04:00
|
|
|
it('should support complex selectors', () => {
|
|
|
|
const files = {
|
|
|
|
app: {
|
|
|
|
'spec.ts': `
|
|
|
|
import {Directive, NgModule} from '@angular/core';
|
|
|
|
|
|
|
|
@Directive({selector: 'div.foo[some-directive]:not([title]):not(.baz)'})
|
|
|
|
export class SomeDirective {}
|
2018-04-13 19:47:27 -04:00
|
|
|
|
2018-03-29 19:41:53 -04:00
|
|
|
@Directive({selector: ':not(span[title]):not(.baz)'})
|
|
|
|
export class OtherDirective {}
|
|
|
|
|
|
|
|
@NgModule({declarations: [SomeDirective, OtherDirective]})
|
|
|
|
export class MyModule{}
|
|
|
|
`
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// SomeDirective definition should be:
|
|
|
|
const SomeDirectiveDefinition = `
|
2019-10-11 15:28:12 -04:00
|
|
|
SomeDirective.ɵdir = $r3$.ɵɵdefineDirective({
|
2018-03-29 19:41:53 -04:00
|
|
|
type: SomeDirective,
|
2019-08-12 02:26:20 -04:00
|
|
|
selectors: [["div", "some-directive", "", 8, "foo", 3, "title", "", 9, "baz"]]
|
2018-03-29 19:41:53 -04:00
|
|
|
});
|
|
|
|
`;
|
|
|
|
|
2019-08-12 02:26:20 -04:00
|
|
|
const SomeDirectiveFactory =
|
2019-10-11 17:18:45 -04:00
|
|
|
`SomeDirective.ɵfac = function SomeDirective_Factory(t) {return new (t || SomeDirective)(); };`;
|
2019-08-12 02:26:20 -04:00
|
|
|
|
2018-03-29 19:41:53 -04:00
|
|
|
// OtherDirective definition should be:
|
|
|
|
const OtherDirectiveDefinition = `
|
2019-10-11 15:28:12 -04:00
|
|
|
OtherDirective.ɵdir = $r3$.ɵɵdefineDirective({
|
2018-03-29 19:41:53 -04:00
|
|
|
type: OtherDirective,
|
2019-08-12 02:26:20 -04:00
|
|
|
selectors: [["", 5, "span", "title", "", 9, "baz"]]
|
2018-03-29 19:41:53 -04:00
|
|
|
});
|
|
|
|
`;
|
|
|
|
|
2019-08-12 02:26:20 -04:00
|
|
|
const OtherDirectiveFactory =
|
2019-10-11 17:18:45 -04:00
|
|
|
`OtherDirective.ɵfac = function OtherDirective_Factory(t) {return new (t || OtherDirective)(); };`;
|
2019-08-12 02:26:20 -04:00
|
|
|
|
2018-03-29 19:41:53 -04:00
|
|
|
const result = compile(files, angularFiles);
|
|
|
|
const source = result.source;
|
|
|
|
|
2019-10-11 15:28:12 -04:00
|
|
|
expectEmit(source, SomeDirectiveDefinition, 'Incorrect SomeDirective.ɵdir');
|
2019-10-11 17:18:45 -04:00
|
|
|
expectEmit(source, SomeDirectiveFactory, 'Incorrect SomeDirective.ɵfac');
|
2019-10-11 15:28:12 -04:00
|
|
|
expectEmit(source, OtherDirectiveDefinition, 'Incorrect OtherDirective.ɵdir');
|
2019-10-11 17:18:45 -04:00
|
|
|
expectEmit(source, OtherDirectiveFactory, 'Incorrect OtherDirective.ɵfac');
|
2018-03-29 19:41:53 -04:00
|
|
|
});
|
|
|
|
|
2019-11-13 00:32:58 -05:00
|
|
|
it('should convert #my-app selector to ["", "id", "my-app"]', () => {
|
|
|
|
const files = {
|
|
|
|
app: {
|
|
|
|
'spec.ts': `
|
|
|
|
import {Component, NgModule} from '@angular/core';
|
|
|
|
|
|
|
|
@Component({selector: '#my-app', template: ''})
|
|
|
|
export class SomeComponent {}
|
|
|
|
|
|
|
|
@NgModule({declarations: [SomeComponent]})
|
|
|
|
export class MyModule{}
|
|
|
|
`
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// SomeDirective definition should be:
|
|
|
|
const SomeDirectiveDefinition = `
|
|
|
|
SomeComponent.ɵcmp = $r3$.ɵɵdefineComponent({
|
|
|
|
type: SomeComponent,
|
|
|
|
selectors: [["", "id", "my-app"]],
|
|
|
|
…
|
|
|
|
});
|
|
|
|
`;
|
|
|
|
|
|
|
|
const result = compile(files, angularFiles);
|
|
|
|
const source = result.source;
|
|
|
|
|
|
|
|
expectEmit(source, SomeDirectiveDefinition, 'Incorrect SomeComponent.ɵcomp');
|
|
|
|
});
|
2018-11-22 09:38:28 -05:00
|
|
|
it('should support components without selector', () => {
|
|
|
|
const files = {
|
|
|
|
app: {
|
|
|
|
'spec.ts': `
|
|
|
|
import {Component, Directive, NgModule} from '@angular/core';
|
|
|
|
|
|
|
|
@Component({template: '<router-outlet></router-outlet>'})
|
|
|
|
export class EmptyOutletComponent {}
|
|
|
|
|
|
|
|
@NgModule({declarations: [EmptyOutletComponent]})
|
|
|
|
export class MyModule{}
|
|
|
|
`
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// EmptyOutletComponent definition should be:
|
|
|
|
const EmptyOutletComponentDefinition = `
|
|
|
|
…
|
2019-10-10 17:57:15 -04:00
|
|
|
EmptyOutletComponent.ɵcmp = $r3$.ɵɵdefineComponent({
|
2018-11-22 09:38:28 -05:00
|
|
|
type: EmptyOutletComponent,
|
|
|
|
selectors: [["ng-component"]],
|
2019-09-23 14:08:51 -04:00
|
|
|
decls: 1,
|
2018-11-22 09:38:28 -05:00
|
|
|
vars: 0,
|
|
|
|
template: function EmptyOutletComponent_Template(rf, ctx) {
|
|
|
|
if (rf & 1) {
|
2019-05-17 21:49:21 -04:00
|
|
|
$r3$.ɵɵelement(0, "router-outlet");
|
2018-11-22 09:38:28 -05:00
|
|
|
}
|
|
|
|
},
|
|
|
|
encapsulation: 2
|
|
|
|
});
|
|
|
|
`;
|
|
|
|
|
2019-08-12 02:26:20 -04:00
|
|
|
const EmptyOutletComponentFactory =
|
2019-10-11 17:18:45 -04:00
|
|
|
`EmptyOutletComponent.ɵfac = function EmptyOutletComponent_Factory(t) { return new (t || EmptyOutletComponent)(); };`;
|
2019-08-12 02:26:20 -04:00
|
|
|
|
2018-11-22 09:38:28 -05:00
|
|
|
const result = compile(files, angularFiles);
|
|
|
|
const source = result.source;
|
|
|
|
|
2019-10-10 17:57:15 -04:00
|
|
|
expectEmit(source, EmptyOutletComponentDefinition, 'Incorrect EmptyOutletComponent.ɵcmp');
|
2019-10-11 17:18:45 -04:00
|
|
|
expectEmit(source, EmptyOutletComponentFactory, 'Incorrect EmptyOutletComponent.ɵfac');
|
2018-11-22 09:38:28 -05:00
|
|
|
});
|
|
|
|
|
2018-09-21 21:38:13 -04:00
|
|
|
it('should not treat ElementRef, ViewContainerRef, or ChangeDetectorRef specially when injecting',
|
|
|
|
() => {
|
|
|
|
const files = {
|
|
|
|
app: {
|
|
|
|
'spec.ts': `
|
|
|
|
import {Component, NgModule, ElementRef, ChangeDetectorRef, ViewContainerRef} from '@angular/core';
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'my-component',
|
|
|
|
template: ''
|
|
|
|
})
|
|
|
|
export class MyComponent {
|
|
|
|
constructor(public el: ElementRef, public vcr: ViewContainerRef, public cdr: ChangeDetectorRef) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
@NgModule({declarations: [MyComponent]})
|
|
|
|
export class MyModule {}
|
|
|
|
`
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const MyComponentDefinition = `
|
|
|
|
…
|
2019-10-10 17:57:15 -04:00
|
|
|
MyComponent.ɵcmp = $r3$.ɵɵdefineComponent({
|
2018-09-21 21:38:13 -04:00
|
|
|
type: MyComponent,
|
|
|
|
selectors: [["my-component"]],
|
2019-09-23 14:08:51 -04:00
|
|
|
decls: 0,
|
2018-09-21 21:38:13 -04:00
|
|
|
vars: 0,
|
2018-11-19 12:48:14 -05:00
|
|
|
template: function MyComponent_Template(rf, ctx) {},
|
|
|
|
encapsulation: 2
|
2018-09-21 21:38:13 -04:00
|
|
|
});`;
|
|
|
|
|
2019-10-11 17:18:45 -04:00
|
|
|
const MyComponentFactory = `MyComponent.ɵfac = function MyComponent_Factory(t) {
|
2019-08-12 02:26:20 -04:00
|
|
|
return new (t || MyComponent)(
|
|
|
|
$r3$.ɵɵdirectiveInject($i$.ElementRef), $r3$.ɵɵdirectiveInject($i$.ViewContainerRef),
|
|
|
|
$r3$.ɵɵdirectiveInject($i$.ChangeDetectorRef));
|
|
|
|
};`;
|
|
|
|
|
2018-09-21 21:38:13 -04:00
|
|
|
const result = compile(files, angularFiles);
|
|
|
|
const source = result.source;
|
|
|
|
|
2019-10-10 17:57:15 -04:00
|
|
|
expectEmit(source, MyComponentDefinition, 'Incorrect MyComponent.ɵcmp');
|
2019-10-11 17:18:45 -04:00
|
|
|
expectEmit(source, MyComponentFactory, 'Incorrect MyComponent.ɵfac');
|
2018-09-21 21:38:13 -04:00
|
|
|
});
|
|
|
|
|
2018-02-05 20:31:12 -05:00
|
|
|
it('should support structural directives', () => {
|
|
|
|
const files = {
|
|
|
|
app: {
|
|
|
|
'spec.ts': `
|
|
|
|
import {Component, Directive, NgModule, TemplateRef} from '@angular/core';
|
|
|
|
|
|
|
|
@Directive({selector: '[if]'})
|
|
|
|
export class IfDirective {
|
|
|
|
constructor(template: TemplateRef<any>) { }
|
|
|
|
}
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'my-component',
|
|
|
|
template: '<ul #foo><li *if>{{salutation}} {{foo}}</li></ul>'
|
|
|
|
})
|
|
|
|
export class MyComponent {
|
|
|
|
salutation = 'Hello';
|
|
|
|
}
|
|
|
|
|
|
|
|
@NgModule({declarations: [IfDirective, MyComponent]})
|
|
|
|
export class MyModule {}
|
|
|
|
`
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const IfDirectiveDefinition = `
|
2019-10-11 15:28:12 -04:00
|
|
|
IfDirective.ɵdir = $r3$.ɵɵdefineDirective({
|
2018-02-05 20:31:12 -05:00
|
|
|
type: IfDirective,
|
2019-08-12 02:26:20 -04:00
|
|
|
selectors: [["", "if", ""]]
|
2018-02-05 20:31:12 -05:00
|
|
|
});`;
|
2019-08-12 02:26:20 -04:00
|
|
|
const IfDirectiveFactory =
|
2019-10-11 17:18:45 -04:00
|
|
|
`IfDirective.ɵfac = function IfDirective_Factory(t) { return new (t || IfDirective)($r3$.ɵɵdirectiveInject($i$.TemplateRef)); };`;
|
2019-08-12 02:26:20 -04:00
|
|
|
|
2018-02-05 20:31:12 -05:00
|
|
|
const MyComponentDefinition = `
|
2018-12-19 15:17:38 -05:00
|
|
|
function MyComponent_li_2_Template(rf, ctx) {
|
2018-07-17 21:59:49 -04:00
|
|
|
if (rf & 1) {
|
2019-05-17 21:49:21 -04:00
|
|
|
$r3$.ɵɵelementStart(0, "li");
|
|
|
|
$r3$.ɵɵtext(1);
|
|
|
|
$r3$.ɵɵelementEnd();
|
2018-07-17 21:59:49 -04:00
|
|
|
}
|
|
|
|
if (rf & 2) {
|
2019-05-17 21:49:21 -04:00
|
|
|
const $myComp$ = $r3$.ɵɵnextContext();
|
|
|
|
const $foo$ = $r3$.ɵɵreference(1);
|
2019-09-06 17:43:16 -04:00
|
|
|
$r3$.ɵɵadvance(1);
|
2019-04-23 23:40:05 -04:00
|
|
|
$r3$.ɵɵtextInterpolate2("", $myComp$.salutation, " ", $foo$, "");
|
2018-07-17 21:59:49 -04:00
|
|
|
}
|
|
|
|
}
|
2018-02-05 20:31:12 -05:00
|
|
|
…
|
2019-10-10 17:57:15 -04:00
|
|
|
MyComponent.ɵcmp = $r3$.ɵɵdefineComponent({
|
2018-02-05 20:31:12 -05:00
|
|
|
type: MyComponent,
|
2018-07-12 18:10:55 -04:00
|
|
|
selectors: [["my-component"]],
|
2019-09-23 14:08:51 -04:00
|
|
|
decls: 3,
|
2018-08-18 14:14:50 -04:00
|
|
|
vars: 0,
|
2019-10-29 17:20:25 -04:00
|
|
|
consts: [["foo", ""], [${AttributeMarker.Template}, "if"]],
|
2018-08-18 14:14:50 -04:00
|
|
|
template: function MyComponent_Template(rf, ctx) {
|
2018-04-10 23:57:20 -04:00
|
|
|
if (rf & 1) {
|
2019-10-29 17:20:25 -04:00
|
|
|
$r3$.ɵɵelementStart(0, "ul", null, 0);
|
|
|
|
$r3$.ɵɵtemplate(2, MyComponent_li_2_Template, 2, 2, "li", 1);
|
2019-05-17 21:49:21 -04:00
|
|
|
$r3$.ɵɵelementEnd();
|
2018-02-05 20:31:12 -05:00
|
|
|
}
|
2018-03-29 15:58:41 -04:00
|
|
|
},
|
2019-10-29 17:20:25 -04:00
|
|
|
directives: [IfDirective],
|
|
|
|
encapsulation: 2
|
2018-02-05 20:31:12 -05:00
|
|
|
});`;
|
|
|
|
|
2019-08-12 02:26:20 -04:00
|
|
|
const MyComponentFactory =
|
2019-10-11 17:18:45 -04:00
|
|
|
`MyComponent.ɵfac = function MyComponent_Factory(t) { return new (t || MyComponent)(); };`;
|
2019-08-12 02:26:20 -04:00
|
|
|
|
2018-02-05 20:31:12 -05:00
|
|
|
const result = compile(files, angularFiles);
|
|
|
|
const source = result.source;
|
|
|
|
|
2019-10-11 15:28:12 -04:00
|
|
|
expectEmit(source, IfDirectiveDefinition, 'Incorrect IfDirective.ɵdir');
|
2019-10-11 17:18:45 -04:00
|
|
|
expectEmit(source, IfDirectiveFactory, 'Incorrect IfDirective.ɵfac');
|
2019-10-10 17:57:15 -04:00
|
|
|
expectEmit(source, MyComponentDefinition, 'Incorrect MyComponent.ɵcmp');
|
2019-10-11 17:18:45 -04:00
|
|
|
expectEmit(source, MyComponentFactory, 'Incorrect MyComponent.ɵfac');
|
2018-02-05 20:31:12 -05:00
|
|
|
});
|
|
|
|
|
2018-02-14 20:12:05 -05:00
|
|
|
describe('value composition', () => {
|
|
|
|
|
|
|
|
it('should support array literals', () => {
|
|
|
|
const files = {
|
|
|
|
app: {
|
|
|
|
'spec.ts': `
|
|
|
|
import {Component, Input, NgModule} from '@angular/core';
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'my-comp',
|
|
|
|
template: \`
|
|
|
|
<p>{{ names[0] }}</p>
|
|
|
|
<p>{{ names[1] }}</p>
|
|
|
|
\`
|
|
|
|
})
|
|
|
|
export class MyComp {
|
|
|
|
@Input() names: string[];
|
|
|
|
}
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'my-app',
|
|
|
|
template: \`
|
|
|
|
<my-comp [names]="['Nancy', customName]"></my-comp>
|
|
|
|
\`
|
|
|
|
})
|
|
|
|
export class MyApp {
|
|
|
|
customName = 'Bess';
|
|
|
|
}
|
|
|
|
|
|
|
|
@NgModule({declarations: [MyComp, MyApp]})
|
|
|
|
export class MyModule { }
|
|
|
|
`
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const MyAppDeclaration = `
|
2018-07-12 18:10:55 -04:00
|
|
|
const $e0_ff$ = function ($v$) { return ["Nancy", $v$]; };
|
2018-02-14 20:12:05 -05:00
|
|
|
…
|
2019-10-10 17:57:15 -04:00
|
|
|
MyApp.ɵcmp = $r3$.ɵɵdefineComponent({
|
2018-02-14 20:12:05 -05:00
|
|
|
type: MyApp,
|
2018-07-12 18:10:55 -04:00
|
|
|
selectors: [["my-app"]],
|
2019-09-23 14:08:51 -04:00
|
|
|
decls: 1,
|
2018-08-18 14:14:50 -04:00
|
|
|
vars: 3,
|
2019-09-23 14:08:51 -04:00
|
|
|
consts: [[${AttributeMarker.Bindings}, "names"]],
|
2018-08-18 14:14:50 -04:00
|
|
|
template: function MyApp_Template(rf, ctx) {
|
2018-04-10 23:57:20 -04:00
|
|
|
if (rf & 1) {
|
2019-09-23 14:08:51 -04:00
|
|
|
$r3$.ɵɵelement(0, "my-comp", 0);
|
2018-02-14 20:12:05 -05:00
|
|
|
}
|
2018-04-10 23:57:20 -04:00
|
|
|
if (rf & 2) {
|
2019-05-17 21:49:21 -04:00
|
|
|
$r3$.ɵɵproperty("names", $r3$.ɵɵpureFunction1(1, $e0_ff$, ctx.customName));
|
2018-04-10 23:57:20 -04:00
|
|
|
}
|
2018-03-29 15:58:41 -04:00
|
|
|
},
|
2018-11-19 12:48:14 -05:00
|
|
|
directives: [MyComp],
|
|
|
|
encapsulation: 2
|
2018-02-14 20:12:05 -05:00
|
|
|
});
|
|
|
|
`;
|
|
|
|
|
|
|
|
const result = compile(files, angularFiles);
|
|
|
|
const source = result.source;
|
|
|
|
|
|
|
|
expectEmit(source, MyAppDeclaration, 'Invalid array emit');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should support 9+ bindings in array literals', () => {
|
|
|
|
const files = {
|
|
|
|
app: {
|
|
|
|
'spec.ts': `
|
|
|
|
import {Component, Input, NgModule} from '@angular/core';
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'my-comp',
|
|
|
|
template: \`
|
|
|
|
{{ names[0] }}
|
|
|
|
{{ names[1] }}
|
|
|
|
{{ names[3] }}
|
|
|
|
{{ names[4] }}
|
|
|
|
{{ names[5] }}
|
|
|
|
{{ names[6] }}
|
|
|
|
{{ names[7] }}
|
|
|
|
{{ names[8] }}
|
|
|
|
{{ names[9] }}
|
|
|
|
{{ names[10] }}
|
|
|
|
{{ names[11] }}
|
|
|
|
\`
|
|
|
|
})
|
|
|
|
export class MyComp {
|
|
|
|
@Input() names: string[];
|
|
|
|
}
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'my-app',
|
|
|
|
template: \`
|
|
|
|
<my-comp [names]="['start-', n0, n1, n2, n3, n4, '-middle-', n5, n6, n7, n8, '-end']">
|
|
|
|
</my-comp>
|
|
|
|
\`
|
|
|
|
})
|
|
|
|
export class MyApp {
|
|
|
|
n0 = 'a';
|
|
|
|
n1 = 'b';
|
|
|
|
n2 = 'c';
|
|
|
|
n3 = 'd';
|
|
|
|
n4 = 'e';
|
|
|
|
n5 = 'f';
|
|
|
|
n6 = 'g';
|
|
|
|
n7 = 'h';
|
|
|
|
n8 = 'i';
|
|
|
|
}
|
|
|
|
|
|
|
|
@NgModule({declarations: [MyComp, MyApp]})
|
|
|
|
export class MyModule {}
|
|
|
|
`
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const MyAppDefinition = `
|
2018-07-12 18:10:55 -04:00
|
|
|
const $e0_ff$ = function ($v0$, $v1$, $v2$, $v3$, $v4$, $v5$, $v6$, $v7$, $v8$) {
|
|
|
|
return ["start-", $v0$, $v1$, $v2$, $v3$, $v4$, "-middle-", $v5$, $v6$, $v7$, $v8$, "-end"];
|
2018-02-14 20:12:05 -05:00
|
|
|
}
|
|
|
|
…
|
2019-10-10 17:57:15 -04:00
|
|
|
MyApp.ɵcmp = $r3$.ɵɵdefineComponent({
|
2018-02-14 20:12:05 -05:00
|
|
|
type: MyApp,
|
2018-07-12 18:10:55 -04:00
|
|
|
selectors: [["my-app"]],
|
2019-09-23 14:08:51 -04:00
|
|
|
decls: 1,
|
2018-08-18 14:14:50 -04:00
|
|
|
vars: 11,
|
2019-09-23 14:08:51 -04:00
|
|
|
consts: [[${AttributeMarker.Bindings}, "names"]],
|
2018-08-18 14:14:50 -04:00
|
|
|
template: function MyApp_Template(rf, ctx) {
|
2018-04-10 23:57:20 -04:00
|
|
|
if (rf & 1) {
|
2019-09-23 14:08:51 -04:00
|
|
|
$r3$.ɵɵelement(0, "my-comp", 0);
|
2018-02-14 20:12:05 -05:00
|
|
|
}
|
2018-04-10 23:57:20 -04:00
|
|
|
if (rf & 2) {
|
2019-05-17 21:49:21 -04:00
|
|
|
$r3$.ɵɵproperty("names",
|
|
|
|
$r3$.ɵɵpureFunctionV(1, $e0_ff$, [ctx.n0, ctx.n1, ctx.n2, ctx.n3, ctx.n4, ctx.n5, ctx.n6, ctx.n7, ctx.n8]));
|
2018-04-10 23:57:20 -04:00
|
|
|
}
|
2018-03-29 15:58:41 -04:00
|
|
|
},
|
2018-11-19 12:48:14 -05:00
|
|
|
directives: [MyComp],
|
|
|
|
encapsulation: 2
|
2018-02-14 20:12:05 -05:00
|
|
|
});
|
|
|
|
`;
|
|
|
|
|
|
|
|
const result = compile(files, angularFiles);
|
|
|
|
const source = result.source;
|
|
|
|
|
|
|
|
expectEmit(source, MyAppDefinition, 'Invalid array binding');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should support object literals', () => {
|
|
|
|
const files = {
|
|
|
|
app: {
|
|
|
|
'spec.ts': `
|
|
|
|
import {Component, Input, NgModule} from '@angular/core';
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'object-comp',
|
|
|
|
template: \`
|
|
|
|
<p> {{ config['duration'] }} </p>
|
|
|
|
<p> {{ config.animation }} </p>
|
|
|
|
\`
|
|
|
|
})
|
|
|
|
export class ObjectComp {
|
|
|
|
@Input() config: {[key: string]: any};
|
|
|
|
}
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'my-app',
|
|
|
|
template: \`
|
|
|
|
<object-comp [config]="{'duration': 500, animation: name}"></object-comp>
|
|
|
|
\`
|
|
|
|
})
|
|
|
|
export class MyApp {
|
|
|
|
name = 'slide';
|
|
|
|
}
|
|
|
|
|
|
|
|
@NgModule({declarations: [ObjectComp, MyApp]})
|
|
|
|
export class MyModule {}
|
|
|
|
`
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const MyAppDefinition = `
|
2018-07-12 18:10:55 -04:00
|
|
|
const $e0_ff$ = function ($v$) { return {"duration": 500, animation: $v$}; };
|
2018-02-14 20:12:05 -05:00
|
|
|
…
|
2019-10-10 17:57:15 -04:00
|
|
|
MyApp.ɵcmp = $r3$.ɵɵdefineComponent({
|
2018-02-14 20:12:05 -05:00
|
|
|
type: MyApp,
|
2018-07-12 18:10:55 -04:00
|
|
|
selectors: [["my-app"]],
|
2019-09-23 14:08:51 -04:00
|
|
|
decls: 1,
|
2018-08-18 14:14:50 -04:00
|
|
|
vars: 3,
|
2019-09-23 14:08:51 -04:00
|
|
|
consts: [[${AttributeMarker.Bindings}, "config"]],
|
2018-08-18 14:14:50 -04:00
|
|
|
template: function MyApp_Template(rf, ctx) {
|
2018-04-10 23:57:20 -04:00
|
|
|
if (rf & 1) {
|
2019-09-23 14:08:51 -04:00
|
|
|
$r3$.ɵɵelement(0, "object-comp", 0);
|
2018-02-14 20:12:05 -05:00
|
|
|
}
|
2018-04-10 23:57:20 -04:00
|
|
|
if (rf & 2) {
|
2019-05-17 21:49:21 -04:00
|
|
|
$r3$.ɵɵproperty("config", $r3$.ɵɵpureFunction1(1, $e0_ff$, ctx.name));
|
2018-04-10 23:57:20 -04:00
|
|
|
}
|
2018-03-29 15:58:41 -04:00
|
|
|
},
|
2018-11-19 12:48:14 -05:00
|
|
|
directives: [ObjectComp],
|
|
|
|
encapsulation: 2
|
2018-02-14 20:12:05 -05:00
|
|
|
});
|
|
|
|
`;
|
|
|
|
|
|
|
|
const result = compile(files, angularFiles);
|
|
|
|
const source = result.source;
|
|
|
|
|
|
|
|
expectEmit(source, MyAppDefinition, 'Invalid object literal binding');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should support expressions nested deeply in object/array literals', () => {
|
|
|
|
const files = {
|
|
|
|
app: {
|
|
|
|
'spec.ts': `
|
|
|
|
import {Component, Input, NgModule} from '@angular/core';
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'nested-comp',
|
|
|
|
template: \`
|
|
|
|
<p> {{ config.animation }} </p>
|
|
|
|
<p> {{config.actions[0].opacity }} </p>
|
|
|
|
<p> {{config.actions[1].duration }} </p>
|
|
|
|
\`
|
|
|
|
})
|
|
|
|
export class NestedComp {
|
|
|
|
@Input() config: {[key: string]: any};
|
|
|
|
}
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'my-app',
|
|
|
|
template: \`
|
|
|
|
<nested-comp [config]="{animation: name, actions: [{ opacity: 0, duration: 0}, {opacity: 1, duration: duration }]}">
|
|
|
|
</nested-comp>
|
|
|
|
\`
|
|
|
|
})
|
|
|
|
export class MyApp {
|
|
|
|
name = 'slide';
|
|
|
|
duration = 100;
|
|
|
|
}
|
|
|
|
|
|
|
|
@NgModule({declarations: [NestedComp, MyApp]})
|
|
|
|
export class MyModule {}
|
|
|
|
`
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const MyAppDefinition = `
|
2019-11-13 08:26:34 -05:00
|
|
|
const $c0$ = function () { return {opacity: 0, duration: 0}; };
|
2018-07-12 18:10:55 -04:00
|
|
|
const $e0_ff$ = function ($v$) { return {opacity: 1, duration: $v$}; };
|
2019-11-13 08:26:34 -05:00
|
|
|
const $e0_ff_1$ = function ($v1$, $v2$) { return [$v1$, $v2$]; };
|
2018-07-12 18:10:55 -04:00
|
|
|
const $e0_ff_2$ = function ($v1$, $v2$) { return {animation: $v1$, actions: $v2$}; };
|
2018-02-14 20:12:05 -05:00
|
|
|
…
|
2019-10-10 17:57:15 -04:00
|
|
|
MyApp.ɵcmp = $r3$.ɵɵdefineComponent({
|
2018-02-14 20:12:05 -05:00
|
|
|
type: MyApp,
|
2018-07-12 18:10:55 -04:00
|
|
|
selectors: [["my-app"]],
|
2019-09-23 14:08:51 -04:00
|
|
|
decls: 1,
|
2019-11-13 08:26:34 -05:00
|
|
|
vars: 10,
|
2019-09-23 14:08:51 -04:00
|
|
|
consts: [[${AttributeMarker.Bindings}, "config"]],
|
2018-08-18 14:14:50 -04:00
|
|
|
template: function MyApp_Template(rf, ctx) {
|
2018-04-10 23:57:20 -04:00
|
|
|
if (rf & 1) {
|
2019-09-23 14:08:51 -04:00
|
|
|
$r3$.ɵɵelement(0, "nested-comp", 0);
|
2018-02-14 20:12:05 -05:00
|
|
|
}
|
2018-04-10 23:57:20 -04:00
|
|
|
if (rf & 2) {
|
2019-11-13 08:26:34 -05:00
|
|
|
$r3$.ɵɵproperty("config",
|
|
|
|
$r3$.ɵɵpureFunction2(7, $e0_ff_2$, ctx.name, $r3$.ɵɵpureFunction2(4, $e0_ff_1$, $r3$.ɵɵpureFunction0(1, $c0$), $r3$.ɵɵpureFunction1(2, $e0_ff$, ctx.duration))));
|
2018-04-10 23:57:20 -04:00
|
|
|
}
|
2018-03-29 15:58:41 -04:00
|
|
|
},
|
2018-11-19 12:48:14 -05:00
|
|
|
directives: [NestedComp],
|
|
|
|
encapsulation: 2
|
2018-02-14 20:12:05 -05:00
|
|
|
});
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
|
|
const result = compile(files, angularFiles);
|
|
|
|
const source = result.source;
|
|
|
|
|
|
|
|
expectEmit(source, MyAppDefinition, 'Invalid array/object literal binding');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-12-19 09:03:36 -05:00
|
|
|
describe('content projection', () => {
|
2018-02-05 20:31:12 -05:00
|
|
|
|
2018-12-19 09:03:36 -05:00
|
|
|
it('should support content projection in root template', () => {
|
|
|
|
const files = {
|
|
|
|
app: {
|
|
|
|
'spec.ts': `
|
|
|
|
import {Component, Directive, NgModule, TemplateRef} from '@angular/core';
|
2018-12-19 15:17:38 -05:00
|
|
|
|
2018-12-19 09:03:36 -05:00
|
|
|
@Component({selector: 'simple', template: '<div><ng-content></ng-content></div>'})
|
|
|
|
export class SimpleComponent {}
|
2018-12-19 15:17:38 -05:00
|
|
|
|
2018-12-19 09:03:36 -05:00
|
|
|
@Component({
|
|
|
|
selector: 'complex',
|
|
|
|
template: \`
|
|
|
|
<div id="first"><ng-content select="span[title=toFirst]"></ng-content></div>
|
|
|
|
<div id="second"><ng-content SELECT="span[title=toSecond]"></ng-content></div>\`
|
|
|
|
})
|
|
|
|
export class ComplexComponent { }
|
2018-12-19 15:17:38 -05:00
|
|
|
|
2018-12-19 09:03:36 -05:00
|
|
|
@NgModule({declarations: [SimpleComponent, ComplexComponent]})
|
|
|
|
export class MyModule {}
|
2018-12-19 15:17:38 -05:00
|
|
|
|
2018-12-19 09:03:36 -05:00
|
|
|
@Component({
|
|
|
|
selector: 'my-app',
|
|
|
|
template: '<simple>content</simple> <complex></complex>'
|
2018-02-05 20:31:12 -05:00
|
|
|
})
|
2018-12-19 09:03:36 -05:00
|
|
|
export class MyApp {}
|
|
|
|
`
|
|
|
|
}
|
|
|
|
};
|
2018-02-05 20:31:12 -05:00
|
|
|
|
2018-12-19 09:03:36 -05:00
|
|
|
const SimpleComponentDefinition = `
|
2019-10-10 17:57:15 -04:00
|
|
|
SimpleComponent.ɵcmp = $r3$.ɵɵdefineComponent({
|
2018-12-19 09:03:36 -05:00
|
|
|
type: SimpleComponent,
|
|
|
|
selectors: [["simple"]],
|
2019-05-21 16:00:47 -04:00
|
|
|
ngContentSelectors: $c0$,
|
2019-09-23 14:08:51 -04:00
|
|
|
decls: 2,
|
2018-12-19 09:03:36 -05:00
|
|
|
vars: 0,
|
|
|
|
template: function SimpleComponent_Template(rf, ctx) {
|
|
|
|
if (rf & 1) {
|
2019-05-17 21:49:21 -04:00
|
|
|
$r3$.ɵɵprojectionDef();
|
|
|
|
$r3$.ɵɵelementStart(0, "div");
|
|
|
|
$r3$.ɵɵprojection(1);
|
|
|
|
$r3$.ɵɵelementEnd();
|
2018-12-19 09:03:36 -05:00
|
|
|
}
|
|
|
|
},
|
|
|
|
encapsulation: 2
|
|
|
|
});`;
|
2018-02-05 20:31:12 -05:00
|
|
|
|
2018-12-19 09:03:36 -05:00
|
|
|
const ComplexComponentDefinition = `
|
|
|
|
const $c1$ = [[["span", "title", "tofirst"]], [["span", "title", "tosecond"]]];
|
|
|
|
…
|
2019-10-10 17:57:15 -04:00
|
|
|
ComplexComponent.ɵcmp = $r3$.ɵɵdefineComponent({
|
2018-12-19 09:03:36 -05:00
|
|
|
type: ComplexComponent,
|
|
|
|
selectors: [["complex"]],
|
2019-09-23 14:08:51 -04:00
|
|
|
ngContentSelectors: $c2$,
|
|
|
|
decls: 4,
|
2018-12-19 09:03:36 -05:00
|
|
|
vars: 0,
|
2019-09-23 14:08:51 -04:00
|
|
|
consts: [["id","first"], ["id","second"]],
|
2018-12-19 09:03:36 -05:00
|
|
|
template: function ComplexComponent_Template(rf, ctx) {
|
|
|
|
if (rf & 1) {
|
2019-05-17 21:49:21 -04:00
|
|
|
$r3$.ɵɵprojectionDef($c1$);
|
2019-09-23 14:08:51 -04:00
|
|
|
$r3$.ɵɵelementStart(0, "div", 0);
|
2019-05-21 16:00:47 -04:00
|
|
|
$r3$.ɵɵprojection(1);
|
2019-05-17 21:49:21 -04:00
|
|
|
$r3$.ɵɵelementEnd();
|
2019-09-23 14:08:51 -04:00
|
|
|
$r3$.ɵɵelementStart(2, "div", 1);
|
2019-05-21 16:00:47 -04:00
|
|
|
$r3$.ɵɵprojection(3, 1);
|
2019-05-17 21:49:21 -04:00
|
|
|
$r3$.ɵɵelementEnd();
|
2018-12-19 09:03:36 -05:00
|
|
|
}
|
|
|
|
},
|
|
|
|
encapsulation: 2
|
|
|
|
});
|
|
|
|
`;
|
2018-02-05 20:31:12 -05:00
|
|
|
|
2018-12-19 09:03:36 -05:00
|
|
|
const result = compile(files, angularFiles);
|
|
|
|
const source = result.source;
|
2018-02-05 20:31:12 -05:00
|
|
|
|
2018-12-19 09:03:36 -05:00
|
|
|
expectEmit(
|
|
|
|
result.source, SimpleComponentDefinition, 'Incorrect SimpleComponent definition');
|
|
|
|
expectEmit(
|
|
|
|
result.source, ComplexComponentDefinition, 'Incorrect ComplexComponent definition');
|
|
|
|
});
|
|
|
|
|
2019-05-21 16:00:47 -04:00
|
|
|
it('should support multi-slot content projection with multiple wildcard slots', () => {
|
|
|
|
const files = {
|
|
|
|
app: {
|
|
|
|
'spec.ts': `
|
|
|
|
import {Component, NgModule} from '@angular/core';
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
template: \`
|
|
|
|
<ng-content></ng-content>
|
|
|
|
<ng-content select="[spacer]"></ng-content>
|
|
|
|
<ng-content></ng-content>
|
|
|
|
\`,
|
|
|
|
})
|
|
|
|
class Cmp {}
|
|
|
|
|
|
|
|
@NgModule({ declarations: [Cmp] })
|
|
|
|
class Module {}
|
|
|
|
`,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const output = `
|
|
|
|
const $c0$ = ["*", [["", "spacer", ""]], "*"];
|
|
|
|
const $c1$ = ["*", "[spacer]", "*"];
|
|
|
|
…
|
2019-10-10 17:57:15 -04:00
|
|
|
Cmp.ɵcmp = $r3$.ɵɵdefineComponent({
|
2019-05-21 16:00:47 -04:00
|
|
|
type: Cmp,
|
|
|
|
selectors: [["ng-component"]],
|
|
|
|
ngContentSelectors: $c1$,
|
2019-09-23 14:08:51 -04:00
|
|
|
decls: 3,
|
2019-05-21 16:00:47 -04:00
|
|
|
vars: 0,
|
|
|
|
template: function Cmp_Template(rf, ctx) {
|
|
|
|
if (rf & 1) {
|
|
|
|
i0.ɵɵprojectionDef($c0$);
|
|
|
|
i0.ɵɵprojection(0);
|
|
|
|
i0.ɵɵprojection(1, 1);
|
|
|
|
i0.ɵɵprojection(2, 2);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
encapsulation: 2
|
|
|
|
});
|
|
|
|
`;
|
|
|
|
|
|
|
|
const {source} = compile(files, angularFiles);
|
|
|
|
expectEmit(source, output, 'Invalid content projection instructions generated');
|
|
|
|
});
|
|
|
|
|
2018-12-19 09:03:36 -05:00
|
|
|
it('should support content projection in nested templates', () => {
|
|
|
|
const files = {
|
|
|
|
app: {
|
|
|
|
'spec.ts': `
|
|
|
|
import {Component, NgModule} from '@angular/core';
|
2018-12-19 15:17:38 -05:00
|
|
|
|
2018-12-19 09:03:36 -05:00
|
|
|
@Component({
|
|
|
|
template: \`
|
|
|
|
<div id="second" *ngIf="visible">
|
|
|
|
<ng-content SELECT="span[title=toFirst]"></ng-content>
|
|
|
|
</div>
|
|
|
|
<div id="third" *ngIf="visible">
|
|
|
|
No ng-content, no instructions generated.
|
|
|
|
</div>
|
|
|
|
<ng-template>
|
|
|
|
'*' selector: <ng-content></ng-content>
|
|
|
|
</ng-template>
|
|
|
|
\`,
|
|
|
|
})
|
|
|
|
class Cmp {}
|
2018-12-19 15:17:38 -05:00
|
|
|
|
2018-12-19 09:03:36 -05:00
|
|
|
@NgModule({ declarations: [Cmp] })
|
|
|
|
class Module {}
|
|
|
|
`
|
|
|
|
}
|
|
|
|
};
|
|
|
|
const output = `
|
2018-12-19 15:17:38 -05:00
|
|
|
function Cmp_div_0_Template(rf, ctx) { if (rf & 1) {
|
2019-09-23 14:08:51 -04:00
|
|
|
$r3$.ɵɵelementStart(0, "div", 2);
|
2019-05-21 16:00:47 -04:00
|
|
|
$r3$.ɵɵprojection(1);
|
2019-05-17 21:49:21 -04:00
|
|
|
$r3$.ɵɵelementEnd();
|
2018-12-19 09:03:36 -05:00
|
|
|
} }
|
2018-12-19 15:17:38 -05:00
|
|
|
function Cmp_div_1_Template(rf, ctx) {
|
2018-12-19 09:03:36 -05:00
|
|
|
if (rf & 1) {
|
2019-09-23 14:08:51 -04:00
|
|
|
$r3$.ɵɵelementStart(0, "div", 3);
|
2019-05-17 21:49:21 -04:00
|
|
|
$r3$.ɵɵtext(1, " No ng-content, no instructions generated. ");
|
|
|
|
$r3$.ɵɵelementEnd();
|
2018-02-05 20:31:12 -05:00
|
|
|
}
|
2018-12-19 09:03:36 -05:00
|
|
|
}
|
2018-12-19 15:17:38 -05:00
|
|
|
function Cmp_ng_template_2_Template(rf, ctx) {
|
2018-12-19 09:03:36 -05:00
|
|
|
if (rf & 1) {
|
2019-05-17 21:49:21 -04:00
|
|
|
$r3$.ɵɵtext(0, " '*' selector: ");
|
2019-05-21 16:00:47 -04:00
|
|
|
$r3$.ɵɵprojection(1, 1);
|
2018-12-19 09:03:36 -05:00
|
|
|
}
|
|
|
|
}
|
2019-05-21 16:00:47 -04:00
|
|
|
const $_c4$ = [[["span", "title", "tofirst"]], "*"];
|
2018-12-19 09:03:36 -05:00
|
|
|
…
|
2019-09-23 14:08:51 -04:00
|
|
|
consts: [["id", "second", ${AttributeMarker.Template}, "ngIf"], ["id", "third", ${AttributeMarker.Template}, "ngIf"], ["id", "second"], ["id", "third"]],
|
2018-12-19 09:03:36 -05:00
|
|
|
template: function Cmp_Template(rf, ctx) {
|
|
|
|
if (rf & 1) {
|
2019-05-17 21:49:21 -04:00
|
|
|
$r3$.ɵɵprojectionDef($_c4$);
|
2019-09-23 14:08:51 -04:00
|
|
|
$r3$.ɵɵtemplate(0, Cmp_div_0_Template, 2, 0, "div", 0);
|
|
|
|
$r3$.ɵɵtemplate(1, Cmp_div_1_Template, 2, 0, "div", 1);
|
2019-05-17 21:49:21 -04:00
|
|
|
$r3$.ɵɵtemplate(2, Cmp_ng_template_2_Template, 2, 0, "ng-template");
|
2018-12-19 09:03:36 -05:00
|
|
|
}
|
|
|
|
if (rf & 2) {
|
2019-05-17 21:49:21 -04:00
|
|
|
$r3$.ɵɵproperty("ngIf", ctx.visible);
|
2019-09-06 17:43:16 -04:00
|
|
|
$r3$.ɵɵadvance(1);
|
2019-05-17 21:49:21 -04:00
|
|
|
$r3$.ɵɵproperty("ngIf", ctx.visible);
|
2018-12-19 09:03:36 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
`;
|
2018-02-05 20:31:12 -05:00
|
|
|
|
2018-12-19 09:03:36 -05:00
|
|
|
const {source} = compile(files, angularFiles);
|
|
|
|
expectEmit(source, output, 'Invalid content projection instructions generated');
|
|
|
|
});
|
2018-11-30 18:01:37 -05:00
|
|
|
|
2018-12-19 09:03:36 -05:00
|
|
|
it('should support content projection in both the root and nested templates', () => {
|
|
|
|
const files = {
|
|
|
|
app: {
|
|
|
|
'spec.ts': `
|
|
|
|
import {Component, NgModule} from '@angular/core';
|
2018-12-19 15:17:38 -05:00
|
|
|
|
2018-12-19 09:03:36 -05:00
|
|
|
@Component({
|
|
|
|
template: \`
|
|
|
|
<ng-content select="[id=toMainBefore]"></ng-content>
|
|
|
|
<ng-template>
|
|
|
|
<ng-content select="[id=toTemplate]"></ng-content>
|
|
|
|
<ng-template>
|
|
|
|
<ng-content select="[id=toNestedTemplate]"></ng-content>
|
|
|
|
</ng-template>
|
2018-12-19 15:17:38 -05:00
|
|
|
</ng-template>
|
2018-12-19 09:03:36 -05:00
|
|
|
<ng-template>
|
|
|
|
'*' selector in a template: <ng-content></ng-content>
|
|
|
|
</ng-template>
|
|
|
|
<ng-content select="[id=toMainAfter]"></ng-content>
|
|
|
|
\`,
|
|
|
|
})
|
|
|
|
class Cmp {}
|
2018-12-19 15:17:38 -05:00
|
|
|
|
2018-12-19 09:03:36 -05:00
|
|
|
@NgModule({ declarations: [Cmp] })
|
|
|
|
class Module {}
|
|
|
|
`
|
|
|
|
}
|
|
|
|
};
|
2018-11-30 18:01:37 -05:00
|
|
|
|
2018-12-19 09:03:36 -05:00
|
|
|
const output = `
|
2018-12-19 15:17:38 -05:00
|
|
|
function Cmp_ng_template_1_ng_template_1_Template(rf, ctx) {
|
2018-12-19 09:03:36 -05:00
|
|
|
if (rf & 1) {
|
2019-05-21 16:00:47 -04:00
|
|
|
$r3$.ɵɵprojection(0, 3);
|
2018-12-19 15:17:38 -05:00
|
|
|
}
|
2018-11-30 18:01:37 -05:00
|
|
|
}
|
2018-12-19 15:17:38 -05:00
|
|
|
function Cmp_ng_template_1_Template(rf, ctx) {
|
2018-12-19 09:03:36 -05:00
|
|
|
if (rf & 1) {
|
2019-05-21 16:00:47 -04:00
|
|
|
$r3$.ɵɵprojection(0, 2);
|
2019-05-17 21:49:21 -04:00
|
|
|
$r3$.ɵɵtemplate(1, Cmp_ng_template_1_ng_template_1_Template, 1, 0, "ng-template");
|
2018-12-19 15:17:38 -05:00
|
|
|
}
|
2018-11-30 18:01:37 -05:00
|
|
|
}
|
2018-12-19 15:17:38 -05:00
|
|
|
function Cmp_ng_template_2_Template(rf, ctx) {
|
2018-12-19 09:03:36 -05:00
|
|
|
if (rf & 1) {
|
2019-05-17 21:49:21 -04:00
|
|
|
$r3$.ɵɵtext(0, " '*' selector in a template: ");
|
2019-05-21 16:00:47 -04:00
|
|
|
$r3$.ɵɵprojection(1, 4);
|
2018-12-19 09:03:36 -05:00
|
|
|
}
|
2018-11-30 18:01:37 -05:00
|
|
|
}
|
2019-05-21 16:00:47 -04:00
|
|
|
const $_c0$ = [[["", "id", "tomainbefore"]], [["", "id", "tomainafter"]], [["", "id", "totemplate"]], [["", "id", "tonestedtemplate"]], "*"];
|
|
|
|
const $_c1$ = ["[id=toMainBefore]", "[id=toMainAfter]", "[id=toTemplate]", "[id=toNestedTemplate]", "*"];
|
2018-12-19 09:03:36 -05:00
|
|
|
…
|
|
|
|
template: function Cmp_Template(rf, ctx) {
|
|
|
|
if (rf & 1) {
|
2019-05-21 16:00:47 -04:00
|
|
|
$r3$.ɵɵprojectionDef($_c0$);
|
|
|
|
$r3$.ɵɵprojection(0);
|
2019-05-17 21:49:21 -04:00
|
|
|
$r3$.ɵɵtemplate(1, Cmp_ng_template_1_Template, 2, 0, "ng-template");
|
|
|
|
$r3$.ɵɵtemplate(2, Cmp_ng_template_2_Template, 2, 0, "ng-template");
|
2019-05-21 16:00:47 -04:00
|
|
|
$r3$.ɵɵprojection(3, 1);
|
2018-12-19 15:17:38 -05:00
|
|
|
}
|
2018-11-30 18:01:37 -05:00
|
|
|
}
|
2018-12-19 09:03:36 -05:00
|
|
|
`;
|
|
|
|
|
|
|
|
const {source} = compile(files, angularFiles);
|
|
|
|
expectEmit(source, output, 'Invalid content projection instructions generated');
|
|
|
|
});
|
2019-04-08 16:47:23 -04:00
|
|
|
|
|
|
|
it('should parse the selector that is passed into ngProjectAs', () => {
|
|
|
|
const files = {
|
|
|
|
app: {
|
|
|
|
'spec.ts': `
|
|
|
|
import {Component, NgModule} from '@angular/core';
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'simple',
|
|
|
|
template: '<div><ng-content select="[title]"></ng-content></div>'
|
|
|
|
})
|
|
|
|
export class SimpleComponent {}
|
|
|
|
|
|
|
|
@NgModule({declarations: [SimpleComponent]})
|
|
|
|
export class MyModule {}
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'my-app',
|
|
|
|
template: '<simple><h1 ngProjectAs="[title]"></h1></simple>'
|
|
|
|
})
|
|
|
|
export class MyApp {}
|
|
|
|
`
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Note that the c0 and c1 constants aren't being used in this particular test,
|
|
|
|
// but they are used in some of the logic that is folded under the ellipsis.
|
|
|
|
const SimpleComponentDefinition = `
|
|
|
|
const $_c0$ = [[["", "title", ""]]];
|
|
|
|
const $_c1$ = ["[title]"];
|
|
|
|
…
|
2019-10-10 17:57:15 -04:00
|
|
|
MyApp.ɵcmp = $r3$.ɵɵdefineComponent({
|
2019-04-08 16:47:23 -04:00
|
|
|
type: MyApp,
|
|
|
|
selectors: [["my-app"]],
|
2019-09-23 14:08:51 -04:00
|
|
|
decls: 2,
|
2019-04-08 16:47:23 -04:00
|
|
|
vars: 0,
|
2019-09-23 14:08:51 -04:00
|
|
|
consts: [["ngProjectAs", "[title]", 5, ["", "title", ""]]],
|
2019-04-08 16:47:23 -04:00
|
|
|
template: function MyApp_Template(rf, ctx) {
|
|
|
|
if (rf & 1) {
|
2019-05-17 21:49:21 -04:00
|
|
|
$r3$.ɵɵelementStart(0, "simple");
|
2019-09-23 14:08:51 -04:00
|
|
|
$r3$.ɵɵelement(1, "h1", 0);
|
2019-05-17 21:49:21 -04:00
|
|
|
$r3$.ɵɵelementEnd();
|
2019-04-08 16:47:23 -04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
encapsulation: 2
|
|
|
|
})`;
|
|
|
|
|
|
|
|
const result = compile(files, angularFiles);
|
|
|
|
|
|
|
|
expectEmit(
|
|
|
|
result.source, SimpleComponentDefinition, 'Incorrect SimpleComponent definition');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should take the first selector if multiple values are passed into ngProjectAs', () => {
|
|
|
|
const files = {
|
|
|
|
app: {
|
|
|
|
'spec.ts': `
|
|
|
|
import {Component, NgModule} from '@angular/core';
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'simple',
|
|
|
|
template: '<div><ng-content select="[title]"></ng-content></div>'
|
|
|
|
})
|
|
|
|
export class SimpleComponent {}
|
|
|
|
|
|
|
|
@NgModule({declarations: [SimpleComponent]})
|
|
|
|
export class MyModule {}
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'my-app',
|
|
|
|
template: '<simple><h1 ngProjectAs="[title],[header]"></h1></simple>'
|
|
|
|
})
|
|
|
|
export class MyApp {}
|
|
|
|
`
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Note that the c0 and c1 constants aren't being used in this particular test,
|
|
|
|
// but they are used in some of the logic that is folded under the ellipsis.
|
|
|
|
const SimpleComponentDefinition = `
|
|
|
|
const $_c0$ = [[["", "title", ""]]];
|
|
|
|
const $_c1$ = ["[title]"];
|
|
|
|
…
|
2019-10-10 17:57:15 -04:00
|
|
|
MyApp.ɵcmp = $r3$.ɵɵdefineComponent({
|
2019-04-08 16:47:23 -04:00
|
|
|
type: MyApp,
|
|
|
|
selectors: [["my-app"]],
|
2019-09-23 14:08:51 -04:00
|
|
|
decls: 2,
|
2019-04-08 16:47:23 -04:00
|
|
|
vars: 0,
|
2019-09-23 14:08:51 -04:00
|
|
|
consts: [["ngProjectAs", "[title],[header]", 5, ["", "title", ""]]],
|
2019-04-08 16:47:23 -04:00
|
|
|
template: function MyApp_Template(rf, ctx) {
|
|
|
|
if (rf & 1) {
|
2019-05-17 21:49:21 -04:00
|
|
|
$r3$.ɵɵelementStart(0, "simple");
|
2019-09-23 14:08:51 -04:00
|
|
|
$r3$.ɵɵelement(1, "h1", 0);
|
2019-05-17 21:49:21 -04:00
|
|
|
$r3$.ɵɵelementEnd();
|
2019-04-08 16:47:23 -04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
encapsulation: 2
|
|
|
|
})`;
|
|
|
|
|
|
|
|
const result = compile(files, angularFiles);
|
|
|
|
|
|
|
|
expectEmit(
|
|
|
|
result.source, SimpleComponentDefinition, 'Incorrect SimpleComponent definition');
|
|
|
|
});
|
|
|
|
|
2019-12-02 18:34:48 -05:00
|
|
|
it('should include parsed ngProjectAs selectors into template attrs', () => {
|
|
|
|
const files = {
|
|
|
|
app: {
|
|
|
|
'spec.ts': `
|
|
|
|
import {Component} from '@angular/core';
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'my-app',
|
|
|
|
template: '<div *ngIf="show" ngProjectAs=".someclass"></div>'
|
|
|
|
})
|
|
|
|
export class MyApp {
|
|
|
|
show = true;
|
|
|
|
}
|
|
|
|
`
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const SimpleComponentDefinition = `
|
|
|
|
MyApp.ɵcmp = i0.ɵɵdefineComponent({
|
|
|
|
type: MyApp,
|
|
|
|
selectors: [
|
|
|
|
["my-app"]
|
|
|
|
],
|
|
|
|
decls: 1,
|
|
|
|
vars: 1,
|
|
|
|
consts: [
|
2020-01-02 12:49:18 -05:00
|
|
|
["ngProjectAs", ".someclass", ${AttributeMarker.ProjectAs}, ["", 8, "someclass"], ${AttributeMarker.Template}, "ngIf"],
|
2019-12-02 18:34:48 -05:00
|
|
|
["ngProjectAs", ".someclass", ${AttributeMarker.ProjectAs}, ["", 8, "someclass"]]
|
|
|
|
],
|
|
|
|
template: function MyApp_Template(rf, ctx) {
|
|
|
|
if (rf & 1) {
|
|
|
|
i0.ɵɵtemplate(0, MyApp_div_0_Template, 1, 0, "div", 0);
|
|
|
|
}
|
|
|
|
if (rf & 2) {
|
|
|
|
i0.ɵɵproperty("ngIf", ctx.show);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
encapsulation: 2
|
|
|
|
});
|
|
|
|
`;
|
|
|
|
|
|
|
|
const result = compile(files, angularFiles);
|
|
|
|
expectEmit(result.source, SimpleComponentDefinition, 'Incorrect MyApp definition');
|
|
|
|
});
|
|
|
|
|
2018-11-30 18:01:37 -05:00
|
|
|
});
|
|
|
|
|
2018-02-13 13:48:22 -05:00
|
|
|
describe('queries', () => {
|
|
|
|
const directive = {
|
|
|
|
'some.directive.ts': `
|
|
|
|
import {Directive} from '@angular/core';
|
|
|
|
|
|
|
|
@Directive({
|
|
|
|
selector: '[someDir]',
|
|
|
|
})
|
|
|
|
export class SomeDirective { }
|
|
|
|
`
|
|
|
|
};
|
|
|
|
|
2018-09-28 20:20:43 -04:00
|
|
|
it('should support view queries with directives', () => {
|
2018-02-13 13:48:22 -05:00
|
|
|
const files = {
|
|
|
|
app: {
|
|
|
|
...directive,
|
|
|
|
'view_query.component.ts': `
|
2018-12-06 00:21:19 -05:00
|
|
|
import {Component, NgModule, ViewChild, ViewChildren} from '@angular/core';
|
2018-02-13 13:48:22 -05:00
|
|
|
import {SomeDirective} from './some.directive';
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'view-query-component',
|
|
|
|
template: \`
|
2018-10-03 16:49:24 -04:00
|
|
|
<div someDir></div>
|
2018-02-13 13:48:22 -05:00
|
|
|
\`
|
|
|
|
})
|
|
|
|
export class ViewQueryComponent {
|
|
|
|
@ViewChild(SomeDirective) someDir: SomeDirective;
|
2018-10-03 16:49:24 -04:00
|
|
|
@ViewChildren(SomeDirective) someDirs: QueryList<SomeDirective>;
|
2018-02-13 13:48:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@NgModule({declarations: [SomeDirective, ViewQueryComponent]})
|
|
|
|
export class MyModule {}
|
2018-09-28 20:20:43 -04:00
|
|
|
`
|
2018-02-13 13:48:22 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const ViewQueryComponentDefinition = `
|
|
|
|
…
|
2019-10-10 17:57:15 -04:00
|
|
|
ViewQueryComponent.ɵcmp = $r3$.ɵɵdefineComponent({
|
2018-02-13 13:48:22 -05:00
|
|
|
type: ViewQueryComponent,
|
2018-07-12 18:10:55 -04:00
|
|
|
selectors: [["view-query-component"]],
|
2018-07-24 05:56:35 -04:00
|
|
|
viewQuery: function ViewQueryComponent_Query(rf, ctx) {
|
2018-04-10 23:57:20 -04:00
|
|
|
if (rf & 1) {
|
2019-07-20 06:32:29 -04:00
|
|
|
$r3$.ɵɵviewQuery(SomeDirective, true);
|
|
|
|
$r3$.ɵɵviewQuery(SomeDirective, true);
|
2018-02-13 13:48:22 -05:00
|
|
|
}
|
2018-04-10 23:57:20 -04:00
|
|
|
if (rf & 2) {
|
2018-07-24 05:56:35 -04:00
|
|
|
var $tmp$;
|
2019-10-30 19:09:38 -04:00
|
|
|
$r3$.ɵɵqueryRefresh($tmp$ = $r3$.ɵɵloadQuery()) && (ctx.someDir = $tmp$.first);
|
|
|
|
$r3$.ɵɵqueryRefresh($tmp$ = $r3$.ɵɵloadQuery()) && (ctx.someDirs = $tmp$);
|
2018-04-10 23:57:20 -04:00
|
|
|
}
|
2018-03-29 15:58:41 -04:00
|
|
|
},
|
2019-09-23 14:08:51 -04:00
|
|
|
decls: 1,
|
2018-08-18 14:14:50 -04:00
|
|
|
vars: 0,
|
2019-09-23 14:08:51 -04:00
|
|
|
consts: [["someDir",""]],
|
2018-08-18 14:14:50 -04:00
|
|
|
template: function ViewQueryComponent_Template(rf, ctx) {
|
2018-07-24 05:56:35 -04:00
|
|
|
if (rf & 1) {
|
2019-09-23 14:08:51 -04:00
|
|
|
$r3$.ɵɵelement(0, "div", 0);
|
2018-07-24 05:56:35 -04:00
|
|
|
}
|
|
|
|
},
|
2018-11-19 12:48:14 -05:00
|
|
|
directives: function () { return [SomeDirective]; },
|
|
|
|
encapsulation: 2
|
2018-02-13 13:48:22 -05:00
|
|
|
});`;
|
|
|
|
|
|
|
|
const result = compile(files, angularFiles);
|
|
|
|
const source = result.source;
|
|
|
|
|
|
|
|
expectEmit(source, ViewQueryComponentDefinition, 'Invalid ViewQuery declaration');
|
|
|
|
});
|
|
|
|
|
2018-09-28 20:20:43 -04:00
|
|
|
it('should support view queries with local refs', () => {
|
|
|
|
const files = {
|
|
|
|
app: {
|
|
|
|
'view_query.component.ts': `
|
|
|
|
import {Component, NgModule, ViewChild, ViewChildren, QueryList} from '@angular/core';
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'view-query-component',
|
|
|
|
template: \`
|
2018-10-03 16:49:24 -04:00
|
|
|
<div #myRef></div>
|
|
|
|
<div #myRef1></div>
|
2018-09-28 20:20:43 -04:00
|
|
|
\`
|
|
|
|
})
|
|
|
|
export class ViewQueryComponent {
|
|
|
|
@ViewChild('myRef') myRef: any;
|
|
|
|
@ViewChildren('myRef1, myRef2, myRef3') myRefs: QueryList<any>;
|
|
|
|
}
|
|
|
|
|
|
|
|
@NgModule({declarations: [ViewQueryComponent]})
|
|
|
|
export class MyModule {}
|
|
|
|
`
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const ViewQueryComponentDefinition = `
|
2018-10-03 16:49:24 -04:00
|
|
|
const $e0_attrs$ = ["myRef"];
|
|
|
|
const $e1_attrs$ = ["myRef1", "myRef2", "myRef3"];
|
2018-09-28 20:20:43 -04:00
|
|
|
…
|
2019-10-10 17:57:15 -04:00
|
|
|
ViewQueryComponent.ɵcmp = $r3$.ɵɵdefineComponent({
|
2018-09-28 20:20:43 -04:00
|
|
|
…
|
|
|
|
viewQuery: function ViewQueryComponent_Query(rf, ctx) {
|
|
|
|
if (rf & 1) {
|
2019-07-20 06:32:29 -04:00
|
|
|
$r3$.ɵɵviewQuery($e0_attrs$, true);
|
|
|
|
$r3$.ɵɵviewQuery($e1_attrs$, true);
|
2018-09-28 20:20:43 -04:00
|
|
|
}
|
|
|
|
if (rf & 2) {
|
|
|
|
var $tmp$;
|
2019-10-30 19:09:38 -04:00
|
|
|
$r3$.ɵɵqueryRefresh($tmp$ = $r3$.ɵɵloadQuery()) && (ctx.myRef = $tmp$.first);
|
|
|
|
$r3$.ɵɵqueryRefresh($tmp$ = $r3$.ɵɵloadQuery()) && (ctx.myRefs = $tmp$);
|
2018-09-28 20:20:43 -04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
…
|
|
|
|
});`;
|
|
|
|
|
|
|
|
const result = compile(files, angularFiles);
|
|
|
|
const source = result.source;
|
|
|
|
|
|
|
|
expectEmit(source, ViewQueryComponentDefinition, 'Invalid ViewQuery declaration');
|
|
|
|
});
|
|
|
|
|
2019-02-18 20:33:59 -05:00
|
|
|
it('should support static view queries', () => {
|
|
|
|
const files = {
|
|
|
|
app: {
|
|
|
|
...directive,
|
|
|
|
'view_query.component.ts': `
|
|
|
|
import {Component, NgModule, ViewChild} from '@angular/core';
|
|
|
|
import {SomeDirective} from './some.directive';
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'view-query-component',
|
|
|
|
template: \`
|
|
|
|
<div someDir></div>
|
|
|
|
\`
|
|
|
|
})
|
|
|
|
export class ViewQueryComponent {
|
|
|
|
@ViewChild(SomeDirective, {static: true}) someDir !: SomeDirective;
|
2019-10-05 04:04:54 -04:00
|
|
|
@ViewChild('foo') foo !: ElementRef;
|
2019-02-18 20:33:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@NgModule({declarations: [SomeDirective, ViewQueryComponent]})
|
|
|
|
export class MyModule {}
|
|
|
|
`
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const ViewQueryComponentDefinition = `
|
|
|
|
const $refs$ = ["foo"];
|
|
|
|
…
|
2019-10-10 17:57:15 -04:00
|
|
|
ViewQueryComponent.ɵcmp = $r3$.ɵɵdefineComponent({
|
2019-02-18 20:33:59 -05:00
|
|
|
type: ViewQueryComponent,
|
|
|
|
selectors: [["view-query-component"]],
|
|
|
|
viewQuery: function ViewQueryComponent_Query(rf, ctx) {
|
|
|
|
if (rf & 1) {
|
2019-07-20 06:32:29 -04:00
|
|
|
$r3$.ɵɵstaticViewQuery(SomeDirective, true);
|
|
|
|
$r3$.ɵɵviewQuery($refs$, true);
|
2019-02-18 20:33:59 -05:00
|
|
|
}
|
|
|
|
if (rf & 2) {
|
|
|
|
var $tmp$;
|
2019-10-30 19:09:38 -04:00
|
|
|
$r3$.ɵɵqueryRefresh($tmp$ = $r3$.ɵɵloadQuery()) && (ctx.someDir = $tmp$.first);
|
|
|
|
$r3$.ɵɵqueryRefresh($tmp$ = $r3$.ɵɵloadQuery()) && (ctx.foo = $tmp$.first);
|
2019-02-18 20:33:59 -05:00
|
|
|
}
|
|
|
|
},
|
2019-09-23 14:08:51 -04:00
|
|
|
decls: 1,
|
2019-02-18 20:33:59 -05:00
|
|
|
vars: 0,
|
2019-09-23 14:08:51 -04:00
|
|
|
consts: [["someDir",""]],
|
2019-02-18 20:33:59 -05:00
|
|
|
template: function ViewQueryComponent_Template(rf, ctx) {
|
|
|
|
if (rf & 1) {
|
2019-09-23 14:08:51 -04:00
|
|
|
$r3$.ɵɵelement(0, "div", 0);
|
2019-02-18 20:33:59 -05:00
|
|
|
}
|
|
|
|
},
|
|
|
|
directives: function () { return [SomeDirective]; },
|
|
|
|
encapsulation: 2
|
|
|
|
});`;
|
|
|
|
|
|
|
|
const result = compile(files, angularFiles);
|
|
|
|
const source = result.source;
|
|
|
|
|
|
|
|
expectEmit(source, ViewQueryComponentDefinition, 'Invalid ViewQuery declaration');
|
|
|
|
});
|
|
|
|
|
2018-09-28 20:20:43 -04:00
|
|
|
it('should support view queries with read tokens specified', () => {
|
|
|
|
const files = {
|
|
|
|
app: {
|
2018-10-03 16:49:24 -04:00
|
|
|
...directive,
|
2018-09-28 20:20:43 -04:00
|
|
|
'view_query.component.ts': `
|
2018-10-03 16:49:24 -04:00
|
|
|
import {Component, NgModule, ViewChild, ViewChildren, QueryList, ElementRef, TemplateRef} from '@angular/core';
|
|
|
|
import {SomeDirective} from './some.directive';
|
2018-09-28 20:20:43 -04:00
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'view-query-component',
|
|
|
|
template: \`
|
2018-10-03 16:49:24 -04:00
|
|
|
<div someDir></div>
|
|
|
|
<div #myRef></div>
|
|
|
|
<div #myRef1></div>
|
2018-09-28 20:20:43 -04:00
|
|
|
\`
|
|
|
|
})
|
|
|
|
export class ViewQueryComponent {
|
2018-10-03 16:49:24 -04:00
|
|
|
@ViewChild('myRef', {read: TemplateRef}) myRef: TemplateRef;
|
|
|
|
@ViewChildren('myRef1, myRef2, myRef3', {read: ElementRef}) myRefs: QueryList<ElementRef>;
|
|
|
|
@ViewChild(SomeDirective, {read: ElementRef}) someDir: ElementRef;
|
|
|
|
@ViewChildren(SomeDirective, {read: TemplateRef}) someDirs: QueryList<TemplateRef>;
|
2018-09-28 20:20:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
@NgModule({declarations: [ViewQueryComponent]})
|
|
|
|
export class MyModule {}
|
|
|
|
`
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const ViewQueryComponentDefinition = `
|
2018-10-03 16:49:24 -04:00
|
|
|
const $e0_attrs$ = ["myRef"];
|
|
|
|
const $e1_attrs$ = ["myRef1", "myRef2", "myRef3"];
|
2018-09-28 20:20:43 -04:00
|
|
|
…
|
2019-10-10 17:57:15 -04:00
|
|
|
ViewQueryComponent.ɵcmp = $r3$.ɵɵdefineComponent({
|
2018-09-28 20:20:43 -04:00
|
|
|
…
|
|
|
|
viewQuery: function ViewQueryComponent_Query(rf, ctx) {
|
|
|
|
if (rf & 1) {
|
2019-05-17 21:49:21 -04:00
|
|
|
$r3$.ɵɵviewQuery($e0_attrs$, true, TemplateRef);
|
|
|
|
$r3$.ɵɵviewQuery(SomeDirective, true, ElementRef);
|
|
|
|
$r3$.ɵɵviewQuery($e1_attrs$, true, ElementRef);
|
|
|
|
$r3$.ɵɵviewQuery(SomeDirective, true, TemplateRef);
|
2018-09-28 20:20:43 -04:00
|
|
|
}
|
|
|
|
if (rf & 2) {
|
|
|
|
var $tmp$;
|
2019-10-30 19:09:38 -04:00
|
|
|
$r3$.ɵɵqueryRefresh($tmp$ = $r3$.ɵɵloadQuery()) && (ctx.myRef = $tmp$.first);
|
|
|
|
$r3$.ɵɵqueryRefresh($tmp$ = $r3$.ɵɵloadQuery()) && (ctx.someDir = $tmp$.first);
|
|
|
|
$r3$.ɵɵqueryRefresh($tmp$ = $r3$.ɵɵloadQuery()) && (ctx.myRefs = $tmp$);
|
|
|
|
$r3$.ɵɵqueryRefresh($tmp$ = $r3$.ɵɵloadQuery()) && (ctx.someDirs = $tmp$);
|
2018-09-28 20:20:43 -04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
…
|
|
|
|
});`;
|
|
|
|
|
|
|
|
const result = compile(files, angularFiles);
|
|
|
|
const source = result.source;
|
|
|
|
|
|
|
|
expectEmit(source, ViewQueryComponentDefinition, 'Invalid ViewQuery declaration');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should support content queries with directives', () => {
|
2018-02-13 13:48:22 -05:00
|
|
|
const files = {
|
|
|
|
app: {
|
|
|
|
...directive,
|
2018-09-28 20:20:43 -04:00
|
|
|
'content_query.ts': `
|
2018-07-18 03:42:42 -04:00
|
|
|
import {Component, ContentChild, ContentChildren, NgModule, QueryList} from '@angular/core';
|
2018-02-13 13:48:22 -05:00
|
|
|
import {SomeDirective} from './some.directive';
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'content-query-component',
|
|
|
|
template: \`
|
|
|
|
<div><ng-content></ng-content></div>
|
|
|
|
\`
|
|
|
|
})
|
|
|
|
export class ContentQueryComponent {
|
2019-10-05 04:04:54 -04:00
|
|
|
@ContentChild(SomeDirective) someDir: SomeDirective;
|
2018-07-18 03:42:42 -04:00
|
|
|
@ContentChildren(SomeDirective) someDirList !: QueryList<SomeDirective>;
|
2018-02-13 13:48:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'my-app',
|
|
|
|
template: \`
|
|
|
|
<content-query-component>
|
|
|
|
<div someDir></div>
|
|
|
|
</content-query-component>
|
|
|
|
\`
|
|
|
|
})
|
|
|
|
export class MyApp { }
|
|
|
|
|
|
|
|
@NgModule({declarations: [SomeDirective, ContentQueryComponent, MyApp]})
|
|
|
|
export class MyModule { }
|
|
|
|
`
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const ContentQueryComponentDefinition = `
|
2019-10-10 17:57:15 -04:00
|
|
|
ContentQueryComponent.ɵcmp = $r3$.ɵɵdefineComponent({
|
2018-02-13 13:48:22 -05:00
|
|
|
type: ContentQueryComponent,
|
2018-07-12 18:10:55 -04:00
|
|
|
selectors: [["content-query-component"]],
|
2019-02-02 14:20:33 -05:00
|
|
|
contentQueries: function ContentQueryComponent_ContentQueries(rf, ctx, dirIndex) {
|
|
|
|
if (rf & 1) {
|
2019-07-20 06:32:29 -04:00
|
|
|
$r3$.ɵɵcontentQuery(dirIndex, SomeDirective, true);
|
|
|
|
$r3$.ɵɵcontentQuery(dirIndex, SomeDirective, false);
|
2019-02-02 14:20:33 -05:00
|
|
|
}
|
|
|
|
if (rf & 2) {
|
2018-07-12 18:10:55 -04:00
|
|
|
var $tmp$;
|
2019-10-30 19:09:38 -04:00
|
|
|
$r3$.ɵɵqueryRefresh($tmp$ = $r3$.ɵɵloadQuery()) && (ctx.someDir = $tmp$.first);
|
|
|
|
$r3$.ɵɵqueryRefresh($tmp$ = $r3$.ɵɵloadQuery()) && (ctx.someDirList = $tmp$);
|
2019-02-02 14:20:33 -05:00
|
|
|
}
|
2018-02-13 13:48:22 -05:00
|
|
|
},
|
2018-12-22 11:02:34 -05:00
|
|
|
ngContentSelectors: _c0,
|
2019-09-23 14:08:51 -04:00
|
|
|
decls: 2,
|
2018-08-18 14:14:50 -04:00
|
|
|
vars: 0,
|
|
|
|
template: function ContentQueryComponent_Template(rf, ctx) {
|
2018-04-10 23:57:20 -04:00
|
|
|
if (rf & 1) {
|
2019-05-17 21:49:21 -04:00
|
|
|
$r3$.ɵɵprojectionDef();
|
|
|
|
$r3$.ɵɵelementStart(0, "div");
|
|
|
|
$r3$.ɵɵprojection(1);
|
|
|
|
$r3$.ɵɵelementEnd();
|
2018-02-13 13:48:22 -05:00
|
|
|
}
|
2018-11-19 12:48:14 -05:00
|
|
|
},
|
|
|
|
encapsulation: 2
|
2018-02-13 13:48:22 -05:00
|
|
|
});`;
|
|
|
|
|
|
|
|
const result = compile(files, angularFiles);
|
|
|
|
const source = result.source;
|
2018-09-28 20:20:43 -04:00
|
|
|
|
2018-02-13 13:48:22 -05:00
|
|
|
expectEmit(source, ContentQueryComponentDefinition, 'Invalid ContentQuery declaration');
|
|
|
|
});
|
|
|
|
|
2018-10-03 16:49:24 -04:00
|
|
|
it('should support content queries with local refs', () => {
|
|
|
|
const files = {
|
|
|
|
app: {
|
|
|
|
'content_query.component.ts': `
|
|
|
|
import {Component, ContentChild, ContentChildren, NgModule, QueryList} from '@angular/core';
|
2018-09-28 20:20:43 -04:00
|
|
|
|
2018-10-03 16:49:24 -04:00
|
|
|
@Component({
|
|
|
|
selector: 'content-query-component',
|
|
|
|
template: \`
|
|
|
|
<div #myRef></div>
|
|
|
|
<div #myRef1></div>
|
|
|
|
\`
|
|
|
|
})
|
|
|
|
export class ContentQueryComponent {
|
2019-10-05 04:04:54 -04:00
|
|
|
@ContentChild('myRef') myRef: any;
|
2018-10-03 16:49:24 -04:00
|
|
|
@ContentChildren('myRef1, myRef2, myRef3') myRefs: QueryList<any>;
|
|
|
|
}
|
|
|
|
@NgModule({declarations: [ContentQueryComponent]})
|
|
|
|
export class MyModule {}
|
|
|
|
`
|
|
|
|
}
|
|
|
|
};
|
2018-09-28 20:20:43 -04:00
|
|
|
|
2018-10-03 16:49:24 -04:00
|
|
|
const ContentQueryComponentDefinition = `
|
|
|
|
const $e0_attrs$ = ["myRef"];
|
|
|
|
const $e1_attrs$ = ["myRef1", "myRef2", "myRef3"];
|
2018-09-28 20:20:43 -04:00
|
|
|
…
|
2019-10-10 17:57:15 -04:00
|
|
|
ContentQueryComponent.ɵcmp = $r3$.ɵɵdefineComponent({
|
2018-10-03 16:49:24 -04:00
|
|
|
…
|
2019-02-02 14:20:33 -05:00
|
|
|
contentQueries: function ContentQueryComponent_ContentQueries(rf, ctx, dirIndex) {
|
|
|
|
if (rf & 1) {
|
2019-07-20 06:32:29 -04:00
|
|
|
$r3$.ɵɵcontentQuery(dirIndex, $e0_attrs$, true);
|
|
|
|
$r3$.ɵɵcontentQuery(dirIndex, $e1_attrs$, false);
|
2019-02-02 14:20:33 -05:00
|
|
|
}
|
|
|
|
if (rf & 2) {
|
2018-10-03 16:49:24 -04:00
|
|
|
var $tmp$;
|
2019-10-30 19:09:38 -04:00
|
|
|
$r3$.ɵɵqueryRefresh($tmp$ = $r3$.ɵɵloadQuery()) && (ctx.myRef = $tmp$.first);
|
|
|
|
$r3$.ɵɵqueryRefresh($tmp$ = $r3$.ɵɵloadQuery()) && (ctx.myRefs = $tmp$);
|
2019-02-02 14:20:33 -05:00
|
|
|
}
|
2018-10-03 16:49:24 -04:00
|
|
|
},
|
|
|
|
…
|
|
|
|
});`;
|
2018-09-28 20:20:43 -04:00
|
|
|
|
2018-10-03 16:49:24 -04:00
|
|
|
const result = compile(files, angularFiles);
|
|
|
|
const source = result.source;
|
2018-09-28 20:20:43 -04:00
|
|
|
|
2018-10-03 16:49:24 -04:00
|
|
|
expectEmit(source, ContentQueryComponentDefinition, 'Invalid ContentQuery declaration');
|
|
|
|
});
|
2018-09-28 20:20:43 -04:00
|
|
|
|
2019-02-18 21:18:56 -05:00
|
|
|
it('should support static content queries', () => {
|
|
|
|
const files = {
|
|
|
|
app: {
|
|
|
|
...directive,
|
|
|
|
'content_query.ts': `
|
|
|
|
import {Component, ContentChild, NgModule} from '@angular/core';
|
|
|
|
import {SomeDirective} from './some.directive';
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'content-query-component',
|
|
|
|
template: \`
|
|
|
|
<div><ng-content></ng-content></div>
|
|
|
|
\`
|
|
|
|
})
|
|
|
|
export class ContentQueryComponent {
|
|
|
|
@ContentChild(SomeDirective, {static: true}) someDir !: SomeDirective;
|
2019-10-05 04:04:54 -04:00
|
|
|
@ContentChild('foo') foo !: ElementRef;
|
2019-02-18 21:18:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'my-app',
|
|
|
|
template: \`
|
|
|
|
<content-query-component>
|
|
|
|
<div someDir></div>
|
|
|
|
</content-query-component>
|
|
|
|
\`
|
|
|
|
})
|
|
|
|
export class MyApp { }
|
|
|
|
|
|
|
|
@NgModule({declarations: [SomeDirective, ContentQueryComponent, MyApp]})
|
|
|
|
export class MyModule { }
|
|
|
|
`
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const ContentQueryComponentDefinition = `
|
2019-10-10 17:57:15 -04:00
|
|
|
ContentQueryComponent.ɵcmp = $r3$.ɵɵdefineComponent({
|
2019-02-18 21:18:56 -05:00
|
|
|
type: ContentQueryComponent,
|
|
|
|
selectors: [["content-query-component"]],
|
|
|
|
contentQueries: function ContentQueryComponent_ContentQueries(rf, ctx, dirIndex) {
|
|
|
|
if (rf & 1) {
|
2019-07-20 06:32:29 -04:00
|
|
|
$r3$.ɵɵstaticContentQuery(dirIndex, SomeDirective, true);
|
|
|
|
$r3$.ɵɵcontentQuery(dirIndex, $ref0$, true);
|
2019-02-18 21:18:56 -05:00
|
|
|
}
|
|
|
|
if (rf & 2) {
|
|
|
|
var $tmp$;
|
2019-10-30 19:09:38 -04:00
|
|
|
$r3$.ɵɵqueryRefresh($tmp$ = $r3$.ɵɵloadQuery()) && (ctx.someDir = $tmp$.first);
|
|
|
|
$r3$.ɵɵqueryRefresh($tmp$ = $r3$.ɵɵloadQuery()) && (ctx.foo = $tmp$.first);
|
2019-02-18 21:18:56 -05:00
|
|
|
}
|
|
|
|
},
|
|
|
|
ngContentSelectors: $_c1$,
|
2019-09-23 14:08:51 -04:00
|
|
|
decls: 2,
|
2019-02-18 21:18:56 -05:00
|
|
|
vars: 0,
|
|
|
|
template: function ContentQueryComponent_Template(rf, ctx) {
|
|
|
|
if (rf & 1) {
|
2019-05-17 21:49:21 -04:00
|
|
|
$r3$.ɵɵprojectionDef();
|
|
|
|
$r3$.ɵɵelementStart(0, "div");
|
|
|
|
$r3$.ɵɵprojection(1);
|
|
|
|
$r3$.ɵɵelementEnd();
|
2019-02-18 21:18:56 -05:00
|
|
|
}
|
|
|
|
},
|
|
|
|
encapsulation: 2
|
|
|
|
});`;
|
|
|
|
|
|
|
|
const result = compile(files, angularFiles);
|
|
|
|
const source = result.source;
|
|
|
|
|
|
|
|
expectEmit(source, ContentQueryComponentDefinition, 'Invalid ContentQuery declaration');
|
|
|
|
});
|
|
|
|
|
2018-10-03 16:49:24 -04:00
|
|
|
it('should support content queries with read tokens specified', () => {
|
|
|
|
const files = {
|
|
|
|
app: {
|
|
|
|
...directive,
|
|
|
|
'content_query.component.ts': `
|
|
|
|
import {Component, ContentChild, ContentChildren, NgModule, QueryList, ElementRef, TemplateRef} from '@angular/core';
|
|
|
|
import {SomeDirective} from './some.directive';
|
2018-09-28 20:20:43 -04:00
|
|
|
|
2018-10-03 16:49:24 -04:00
|
|
|
@Component({
|
|
|
|
selector: 'content-query-component',
|
|
|
|
template: \`
|
|
|
|
<div someDir></div>
|
|
|
|
<div #myRef></div>
|
|
|
|
<div #myRef1></div>
|
|
|
|
\`
|
|
|
|
})
|
|
|
|
export class ContentQueryComponent {
|
2019-10-05 04:04:54 -04:00
|
|
|
@ContentChild('myRef', {read: TemplateRef}) myRef: TemplateRef;
|
2018-10-03 16:49:24 -04:00
|
|
|
@ContentChildren('myRef1, myRef2, myRef3', {read: ElementRef}) myRefs: QueryList<ElementRef>;
|
2019-10-05 04:04:54 -04:00
|
|
|
@ContentChild(SomeDirective, {read: ElementRef}) someDir: ElementRef;
|
2018-10-03 16:49:24 -04:00
|
|
|
@ContentChildren(SomeDirective, {read: TemplateRef}) someDirs: QueryList<TemplateRef>;
|
|
|
|
}
|
|
|
|
@NgModule({declarations: [ContentQueryComponent]})
|
|
|
|
export class MyModule {}
|
|
|
|
`
|
|
|
|
}
|
|
|
|
};
|
2018-09-28 20:20:43 -04:00
|
|
|
|
2018-10-03 16:49:24 -04:00
|
|
|
const ContentQueryComponentDefinition = `
|
|
|
|
const $e0_attrs$ = ["myRef"];
|
|
|
|
const $e1_attrs$ = ["myRef1", "myRef2", "myRef3"];
|
2018-09-28 20:20:43 -04:00
|
|
|
…
|
2019-10-10 17:57:15 -04:00
|
|
|
ContentQueryComponent.ɵcmp = $r3$.ɵɵdefineComponent({
|
2018-10-03 16:49:24 -04:00
|
|
|
…
|
2019-02-02 14:20:33 -05:00
|
|
|
contentQueries: function ContentQueryComponent_ContentQueries(rf, ctx, dirIndex) {
|
|
|
|
if (rf & 1) {
|
2019-05-17 21:49:21 -04:00
|
|
|
$r3$.ɵɵcontentQuery(dirIndex, $e0_attrs$, true, TemplateRef);
|
|
|
|
$r3$.ɵɵcontentQuery(dirIndex, SomeDirective, true, ElementRef);
|
|
|
|
$r3$.ɵɵcontentQuery(dirIndex, $e1_attrs$, false, ElementRef);
|
|
|
|
$r3$.ɵɵcontentQuery(dirIndex, SomeDirective, false, TemplateRef);
|
2019-02-02 14:20:33 -05:00
|
|
|
}
|
|
|
|
if (rf & 2) {
|
2018-10-03 16:49:24 -04:00
|
|
|
var $tmp$;
|
2019-10-30 19:09:38 -04:00
|
|
|
$r3$.ɵɵqueryRefresh($tmp$ = $r3$.ɵɵloadQuery()) && (ctx.myRef = $tmp$.first);
|
|
|
|
$r3$.ɵɵqueryRefresh($tmp$ = $r3$.ɵɵloadQuery()) && (ctx.someDir = $tmp$.first);
|
|
|
|
$r3$.ɵɵqueryRefresh($tmp$ = $r3$.ɵɵloadQuery()) && (ctx.myRefs = $tmp$);
|
|
|
|
$r3$.ɵɵqueryRefresh($tmp$ = $r3$.ɵɵloadQuery()) && (ctx.someDirs = $tmp$);
|
2019-02-02 14:20:33 -05:00
|
|
|
}
|
2018-10-03 16:49:24 -04:00
|
|
|
},
|
|
|
|
…
|
|
|
|
});`;
|
2018-09-28 20:20:43 -04:00
|
|
|
|
2018-10-03 16:49:24 -04:00
|
|
|
const result = compile(files, angularFiles);
|
|
|
|
const source = result.source;
|
|
|
|
|
|
|
|
expectEmit(source, ContentQueryComponentDefinition, 'Invalid ContentQuery declaration');
|
|
|
|
});
|
2018-09-28 20:20:43 -04:00
|
|
|
});
|
|
|
|
|
2018-02-05 20:31:12 -05:00
|
|
|
describe('pipes', () => {
|
|
|
|
|
2018-10-15 11:51:21 -04:00
|
|
|
it('should render pipes', () => {
|
2018-02-05 20:31:12 -05:00
|
|
|
|
2018-10-15 11:51:21 -04:00
|
|
|
const files = {
|
|
|
|
app: {
|
|
|
|
'spec.ts': `
|
|
|
|
import {Component, NgModule, Pipe, PipeTransform, OnDestroy} from '@angular/core';
|
2018-10-25 14:13:14 -04:00
|
|
|
|
2018-10-15 11:51:21 -04:00
|
|
|
@Pipe({
|
|
|
|
name: 'myPipe',
|
|
|
|
pure: false
|
|
|
|
})
|
|
|
|
export class MyPipe implements PipeTransform,
|
|
|
|
OnDestroy {
|
|
|
|
transform(value: any, ...args: any[]) { return value; }
|
|
|
|
ngOnDestroy(): void { }
|
|
|
|
}
|
2018-10-25 14:13:14 -04:00
|
|
|
|
2018-10-15 11:51:21 -04:00
|
|
|
@Pipe({
|
|
|
|
name: 'myPurePipe',
|
|
|
|
pure: true,
|
|
|
|
})
|
|
|
|
export class MyPurePipe implements PipeTransform {
|
|
|
|
transform(value: any, ...args: any[]) { return value; }
|
|
|
|
}
|
2018-10-25 14:13:14 -04:00
|
|
|
|
2018-10-15 11:51:21 -04:00
|
|
|
@Component({
|
|
|
|
selector: 'my-app',
|
2019-02-10 02:41:00 -05:00
|
|
|
template: '{{name | myPipe:size | myPurePipe:size }}<p>{{ name | myPipe:1:2:3:4:5 }} {{ name ? 1 : 2 | myPipe }}</p>'
|
2018-10-15 11:51:21 -04:00
|
|
|
})
|
|
|
|
export class MyApp {
|
|
|
|
name = 'World';
|
|
|
|
size = 0;
|
|
|
|
}
|
2018-10-25 14:13:14 -04:00
|
|
|
|
2018-10-15 11:51:21 -04:00
|
|
|
@NgModule({declarations:[MyPipe, MyPurePipe, MyApp]})
|
|
|
|
export class MyModule {}
|
|
|
|
`
|
|
|
|
}
|
|
|
|
};
|
2018-02-05 20:31:12 -05:00
|
|
|
|
|
|
|
const MyPipeDefinition = `
|
2019-10-11 22:19:59 -04:00
|
|
|
MyPipe.ɵpipe = $r3$.ɵɵdefinePipe({
|
2018-08-03 15:20:27 -04:00
|
|
|
name: "myPipe",
|
|
|
|
type: MyPipe,
|
|
|
|
pure: false
|
|
|
|
});
|
2018-02-05 20:31:12 -05:00
|
|
|
`;
|
|
|
|
|
2019-10-11 17:18:45 -04:00
|
|
|
const MyPipeFactoryDef = `
|
|
|
|
MyPipe.ɵfac = function MyPipe_Factory(t) { return new (t || MyPipe)(); };
|
2019-08-12 02:26:20 -04:00
|
|
|
`;
|
|
|
|
|
2018-02-05 20:31:12 -05:00
|
|
|
const MyPurePipeDefinition = `
|
2019-10-11 22:19:59 -04:00
|
|
|
MyPurePipe.ɵpipe = $r3$.ɵɵdefinePipe({
|
2018-07-12 18:10:55 -04:00
|
|
|
name: "myPurePipe",
|
2018-02-05 20:31:12 -05:00
|
|
|
type: MyPurePipe,
|
|
|
|
pure: true
|
|
|
|
});`;
|
|
|
|
|
2019-10-11 17:18:45 -04:00
|
|
|
const MyPurePipeFactoryDef = `
|
|
|
|
MyPurePipe.ɵfac = function MyPurePipe_Factory(t) { return new (t || MyPurePipe)(); };
|
2019-08-12 02:26:20 -04:00
|
|
|
`;
|
|
|
|
|
2018-02-05 20:31:12 -05:00
|
|
|
const MyAppDefinition = `
|
2018-07-12 18:10:55 -04:00
|
|
|
const $c0$ = function ($a0$) {
|
2018-05-22 13:48:11 -04:00
|
|
|
return [$a0$, 1, 2, 3, 4, 5];
|
|
|
|
};
|
|
|
|
// ...
|
2019-10-10 17:57:15 -04:00
|
|
|
MyApp.ɵcmp = $r3$.ɵɵdefineComponent({
|
2018-02-05 20:31:12 -05:00
|
|
|
type: MyApp,
|
2018-07-12 18:10:55 -04:00
|
|
|
selectors: [["my-app"]],
|
2019-09-23 14:08:51 -04:00
|
|
|
decls: 7,
|
2019-02-10 02:41:00 -05:00
|
|
|
vars: 20,
|
2018-08-18 14:14:50 -04:00
|
|
|
template: function MyApp_Template(rf, ctx) {
|
2018-04-10 23:57:20 -04:00
|
|
|
if (rf & 1) {
|
2019-05-17 21:49:21 -04:00
|
|
|
$r3$.ɵɵtext(0);
|
|
|
|
$r3$.ɵɵpipe(1, "myPurePipe");
|
|
|
|
$r3$.ɵɵpipe(2, "myPipe");
|
|
|
|
$r3$.ɵɵelementStart(3, "p");
|
|
|
|
$r3$.ɵɵtext(4);
|
|
|
|
$r3$.ɵɵpipe(5, "myPipe");
|
|
|
|
$r3$.ɵɵpipe(6, "myPipe");
|
|
|
|
$r3$.ɵɵelementEnd();
|
2018-02-05 20:31:12 -05:00
|
|
|
}
|
2018-04-10 23:57:20 -04:00
|
|
|
if (rf & 2) {
|
2019-04-23 23:40:05 -04:00
|
|
|
$r3$.ɵɵtextInterpolate($r3$.ɵɵpipeBind2(1, 3, $r3$.ɵɵpipeBind2(2, 6, ctx.name, ctx.size), ctx.size));
|
2019-09-06 17:43:16 -04:00
|
|
|
$r3$.ɵɵadvance(4);
|
2019-04-23 23:40:05 -04:00
|
|
|
$r3$.ɵɵtextInterpolate2("", $r3$.ɵɵpipeBindV(5, 9, $r3$.ɵɵpureFunction1(18, $c0$, ctx.name)), " ", ctx.name ? 1 : $r3$.ɵɵpipeBind1(6, 16, 2), "");
|
2018-04-10 23:57:20 -04:00
|
|
|
}
|
2018-03-30 19:07:37 -04:00
|
|
|
},
|
2018-11-19 12:48:14 -05:00
|
|
|
pipes: [MyPurePipe, MyPipe],
|
|
|
|
encapsulation: 2
|
2018-02-05 20:31:12 -05:00
|
|
|
});`;
|
|
|
|
|
|
|
|
const result = compile(files, angularFiles);
|
|
|
|
const source = result.source;
|
|
|
|
|
|
|
|
expectEmit(source, MyPipeDefinition, 'Invalid pipe definition');
|
2019-10-11 17:18:45 -04:00
|
|
|
expectEmit(source, MyPipeFactoryDef, 'Invalid pipe factory function');
|
2018-02-05 20:31:12 -05:00
|
|
|
expectEmit(source, MyPurePipeDefinition, 'Invalid pure pipe definition');
|
2019-10-11 17:18:45 -04:00
|
|
|
expectEmit(source, MyPurePipeFactoryDef, 'Invalid pure pipe factory function');
|
2018-02-05 20:31:12 -05:00
|
|
|
expectEmit(source, MyAppDefinition, 'Invalid MyApp definition');
|
|
|
|
});
|
2018-10-15 11:51:21 -04:00
|
|
|
|
|
|
|
it('should use appropriate function for a given no of pipe arguments', () => {
|
|
|
|
const files = {
|
|
|
|
app: {
|
|
|
|
'spec.ts': `
|
|
|
|
import {Component, NgModule, Pipe, PipeTransform, OnDestroy} from '@angular/core';
|
2018-10-25 14:13:14 -04:00
|
|
|
|
2018-10-15 11:51:21 -04:00
|
|
|
@Pipe({
|
|
|
|
name: 'myPipe',
|
|
|
|
pure: false
|
|
|
|
})
|
|
|
|
export class MyPipe implements PipeTransform,
|
|
|
|
OnDestroy {
|
|
|
|
transform(value: any, ...args: any[]) { return value; }
|
|
|
|
ngOnDestroy(): void { }
|
|
|
|
}
|
2018-10-25 14:13:14 -04:00
|
|
|
|
2018-10-15 11:51:21 -04:00
|
|
|
@Component({
|
|
|
|
selector: 'my-app',
|
|
|
|
template: '0:{{name | myPipe}}1:{{name | myPipe:1}}2:{{name | myPipe:1:2}}3:{{name | myPipe:1:2:3}}4:{{name | myPipe:1:2:3:4}}'
|
|
|
|
})
|
|
|
|
export class MyApp {
|
|
|
|
}
|
2018-10-25 14:13:14 -04:00
|
|
|
|
2018-10-15 11:51:21 -04:00
|
|
|
@NgModule({declarations:[MyPipe, MyApp]})
|
|
|
|
export class MyModule {}
|
|
|
|
`
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const MyAppDefinition = `
|
|
|
|
// ...
|
2019-10-10 17:57:15 -04:00
|
|
|
MyApp.ɵcmp = $r3$.ɵɵdefineComponent({
|
2018-10-15 11:51:21 -04:00
|
|
|
type: MyApp,
|
|
|
|
selectors: [["my-app"]],
|
2019-09-23 14:08:51 -04:00
|
|
|
decls: 6,
|
2018-10-15 11:51:21 -04:00
|
|
|
vars: 27,
|
|
|
|
template: function MyApp_Template(rf, ctx) {
|
|
|
|
if (rf & 1) {
|
2019-05-17 21:49:21 -04:00
|
|
|
$r3$.ɵɵtext(0);
|
|
|
|
$r3$.ɵɵpipe(1, "myPipe");
|
|
|
|
$r3$.ɵɵpipe(2, "myPipe");
|
|
|
|
$r3$.ɵɵpipe(3, "myPipe");
|
|
|
|
$r3$.ɵɵpipe(4, "myPipe");
|
|
|
|
$r3$.ɵɵpipe(5, "myPipe");
|
2018-10-15 11:51:21 -04:00
|
|
|
}
|
|
|
|
if (rf & 2) {
|
2019-04-23 23:40:05 -04:00
|
|
|
$r3$.ɵɵtextInterpolate5(
|
2019-05-17 21:49:21 -04:00
|
|
|
"0:", i0.ɵɵpipeBind1(1, 5, ctx.name),
|
|
|
|
"1:", i0.ɵɵpipeBind2(2, 7, ctx.name, 1),
|
|
|
|
"2:", i0.ɵɵpipeBind3(3, 10, ctx.name, 1, 2),
|
|
|
|
"3:", i0.ɵɵpipeBind4(4, 14, ctx.name, 1, 2, 3),
|
|
|
|
"4:", i0.ɵɵpipeBindV(5, 19, $r3$.ɵɵpureFunction1(25, $c0$, ctx.name)),
|
2018-10-15 11:51:21 -04:00
|
|
|
""
|
2019-04-23 23:40:05 -04:00
|
|
|
);
|
2018-10-15 11:51:21 -04:00
|
|
|
}
|
|
|
|
},
|
2018-11-19 12:48:14 -05:00
|
|
|
pipes: [MyPipe],
|
|
|
|
encapsulation: 2
|
2018-10-15 11:51:21 -04:00
|
|
|
});`;
|
|
|
|
|
|
|
|
const result = compile(files, angularFiles);
|
|
|
|
const source = result.source;
|
|
|
|
|
|
|
|
expectEmit(source, MyAppDefinition, 'Invalid MyApp definition');
|
|
|
|
});
|
2019-07-12 14:15:12 -04:00
|
|
|
|
|
|
|
it('should generate the proper instruction when injecting ChangeDetectorRef into a pipe',
|
|
|
|
() => {
|
|
|
|
|
|
|
|
const files = {
|
|
|
|
app: {
|
|
|
|
'spec.ts': `
|
|
|
|
import {Component, NgModule, Pipe, PipeTransform, ChangeDetectorRef, Optional} from '@angular/core';
|
|
|
|
|
|
|
|
@Pipe({name: 'myPipe'})
|
|
|
|
export class MyPipe implements PipeTransform {
|
|
|
|
constructor(changeDetectorRef: ChangeDetectorRef) {}
|
|
|
|
|
|
|
|
transform(value: any, ...args: any[]) { return value; }
|
|
|
|
}
|
|
|
|
|
|
|
|
@Pipe({name: 'myOtherPipe'})
|
|
|
|
export class MyOtherPipe implements PipeTransform {
|
|
|
|
constructor(@Optional() changeDetectorRef: ChangeDetectorRef) {}
|
|
|
|
|
|
|
|
transform(value: any, ...args: any[]) { return value; }
|
|
|
|
}
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'my-app',
|
|
|
|
template: '{{name | myPipe }}<p>{{ name | myOtherPipe }}</p>'
|
|
|
|
})
|
|
|
|
export class MyApp {
|
|
|
|
name = 'World';
|
|
|
|
}
|
|
|
|
|
|
|
|
@NgModule({declarations:[MyPipe, MyOtherPipe, MyApp]})
|
|
|
|
export class MyModule {}
|
|
|
|
`
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const MyPipeDefinition = `
|
2019-10-11 22:19:59 -04:00
|
|
|
MyPipe.ɵpipe = $r3$.ɵɵdefinePipe({
|
2019-07-12 14:15:12 -04:00
|
|
|
name: "myPipe",
|
|
|
|
type: MyPipe,
|
|
|
|
pure: true
|
|
|
|
});
|
|
|
|
`;
|
|
|
|
|
2019-08-12 02:26:20 -04:00
|
|
|
const MyPipeFactory = `
|
2019-10-11 17:18:45 -04:00
|
|
|
MyPipe.ɵfac = function MyPipe_Factory(t) { return new (t || MyPipe)($r3$.ɵɵinjectPipeChangeDetectorRef()); };
|
2019-08-12 02:26:20 -04:00
|
|
|
`;
|
|
|
|
|
2019-07-12 14:15:12 -04:00
|
|
|
const MyOtherPipeDefinition = `
|
2019-10-11 22:19:59 -04:00
|
|
|
MyOtherPipe.ɵpipe = $r3$.ɵɵdefinePipe({
|
2019-07-12 14:15:12 -04:00
|
|
|
name: "myOtherPipe",
|
|
|
|
type: MyOtherPipe,
|
|
|
|
pure: true
|
|
|
|
});`;
|
|
|
|
|
2019-08-12 02:26:20 -04:00
|
|
|
const MyOtherPipeFactory = `
|
2019-10-11 17:18:45 -04:00
|
|
|
MyOtherPipe.ɵfac = function MyOtherPipe_Factory(t) { return new (t || MyOtherPipe)($r3$.ɵɵinjectPipeChangeDetectorRef(8)); };
|
2019-08-12 02:26:20 -04:00
|
|
|
`;
|
|
|
|
|
2019-07-12 14:15:12 -04:00
|
|
|
const result = compile(files, angularFiles);
|
|
|
|
const source = result.source;
|
|
|
|
|
|
|
|
expectEmit(source, MyPipeDefinition, 'Invalid pipe definition');
|
2019-08-12 02:26:20 -04:00
|
|
|
expectEmit(source, MyPipeFactory, 'Invalid pipe factory function');
|
2019-07-12 14:15:12 -04:00
|
|
|
expectEmit(source, MyOtherPipeDefinition, 'Invalid alternate pipe definition');
|
2019-08-12 02:26:20 -04:00
|
|
|
expectEmit(source, MyOtherPipeFactory, 'Invalid alternate pipe factory function');
|
2019-07-12 14:15:12 -04:00
|
|
|
});
|
|
|
|
|
2018-02-05 20:31:12 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('local reference', () => {
|
|
|
|
const files = {
|
|
|
|
app: {
|
|
|
|
'spec.ts': `
|
|
|
|
import {Component, NgModule} from '@angular/core';
|
|
|
|
|
|
|
|
@Component({selector: 'my-component', template: '<input #user>Hello {{user.value}}!'})
|
|
|
|
export class MyComponent {}
|
|
|
|
|
|
|
|
@NgModule({declarations: [MyComponent]})
|
|
|
|
export class MyModule {}
|
|
|
|
`
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const MyComponentDefinition = `
|
|
|
|
…
|
2019-10-10 17:57:15 -04:00
|
|
|
MyComponent.ɵcmp = $r3$.ɵɵdefineComponent({
|
2018-02-05 20:31:12 -05:00
|
|
|
type: MyComponent,
|
2018-07-12 18:10:55 -04:00
|
|
|
selectors: [["my-component"]],
|
2019-09-23 14:08:51 -04:00
|
|
|
decls: 3,
|
2018-08-18 14:14:50 -04:00
|
|
|
vars: 1,
|
2019-10-29 17:20:25 -04:00
|
|
|
consts: [["user", ""]],
|
2018-08-18 14:14:50 -04:00
|
|
|
template: function MyComponent_Template(rf, ctx) {
|
2018-04-10 23:57:20 -04:00
|
|
|
if (rf & 1) {
|
2019-10-29 17:20:25 -04:00
|
|
|
$r3$.ɵɵelement(0, "input", null, 0);
|
2019-05-17 21:49:21 -04:00
|
|
|
$r3$.ɵɵtext(2);
|
2018-02-05 20:31:12 -05:00
|
|
|
}
|
2018-04-10 23:57:20 -04:00
|
|
|
if (rf & 2) {
|
2019-05-17 21:49:21 -04:00
|
|
|
const $user$ = $r3$.ɵɵreference(1);
|
2019-09-06 17:43:16 -04:00
|
|
|
$r3$.ɵɵadvance(2);
|
2019-04-23 23:40:05 -04:00
|
|
|
$r3$.ɵɵtextInterpolate1("Hello ", $user$.value, "!");
|
2018-04-10 23:57:20 -04:00
|
|
|
}
|
2018-11-19 12:48:14 -05:00
|
|
|
},
|
|
|
|
encapsulation: 2
|
2018-02-05 20:31:12 -05:00
|
|
|
});
|
|
|
|
`;
|
|
|
|
|
|
|
|
const result = compile(files, angularFiles);
|
|
|
|
const source = result.source;
|
|
|
|
|
2019-10-10 17:57:15 -04:00
|
|
|
expectEmit(source, MyComponentDefinition, 'Incorrect MyComponent.ɵcmp');
|
2018-02-05 20:31:12 -05:00
|
|
|
});
|
|
|
|
|
2018-07-17 21:59:49 -04:00
|
|
|
it('local references in nested views', () => {
|
|
|
|
const files = {
|
|
|
|
app: {
|
|
|
|
'spec.ts': `
|
|
|
|
import {Component, Directive, NgModule, TemplateRef} from '@angular/core';
|
|
|
|
|
|
|
|
@Directive({selector: '[if]'})
|
|
|
|
export class IfDirective {
|
|
|
|
constructor(template: TemplateRef<any>) { }
|
|
|
|
}
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'my-component',
|
|
|
|
template: \`
|
|
|
|
<div #foo></div>
|
|
|
|
{{foo}}
|
|
|
|
<div *if>
|
|
|
|
{{foo}}-{{bar}}
|
|
|
|
<span *if>{{foo}}-{{bar}}-{{baz}}</span>
|
|
|
|
<span #bar></span>
|
|
|
|
</div>
|
|
|
|
<div #baz></div>
|
|
|
|
\`
|
|
|
|
})
|
|
|
|
export class MyComponent {}
|
|
|
|
|
|
|
|
@NgModule({declarations: [IfDirective, MyComponent]})
|
|
|
|
export class MyModule {}
|
|
|
|
`
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const MyComponentDefinition = `
|
2018-12-19 15:17:38 -05:00
|
|
|
function MyComponent_div_3_span_2_Template(rf, ctx) {
|
2018-07-17 21:59:49 -04:00
|
|
|
if (rf & 1) {
|
2019-05-17 21:49:21 -04:00
|
|
|
$r3$.ɵɵelementStart(0, "span");
|
|
|
|
$r3$.ɵɵtext(1);
|
|
|
|
$r3$.ɵɵelementEnd();
|
2018-07-17 21:59:49 -04:00
|
|
|
}
|
|
|
|
if (rf & 2) {
|
2019-05-17 21:49:21 -04:00
|
|
|
$r3$.ɵɵnextContext();
|
|
|
|
const $bar$ = $r3$.ɵɵreference(4);
|
|
|
|
$r3$.ɵɵnextContext();
|
|
|
|
const $foo$ = $r3$.ɵɵreference(1);
|
|
|
|
const $baz$ = $r3$.ɵɵreference(5);
|
2019-09-06 17:43:16 -04:00
|
|
|
$r3$.ɵɵadvance(1);
|
2019-04-23 23:40:05 -04:00
|
|
|
$r3$.ɵɵtextInterpolate3("", $foo$, "-", $bar$, "-", $baz$, "");
|
2018-07-17 21:59:49 -04:00
|
|
|
}
|
|
|
|
}
|
2018-12-19 15:17:38 -05:00
|
|
|
function MyComponent_div_3_Template(rf, ctx) {
|
2018-07-17 21:59:49 -04:00
|
|
|
if (rf & 1) {
|
2019-05-17 21:49:21 -04:00
|
|
|
$r3$.ɵɵelementStart(0, "div");
|
|
|
|
$r3$.ɵɵtext(1);
|
2019-10-29 17:20:25 -04:00
|
|
|
$r3$.ɵɵtemplate(2, MyComponent_div_3_span_2_Template, 2, 3, "span", 1);
|
|
|
|
$r3$.ɵɵelement(3, "span", null, 3);
|
2019-05-17 21:49:21 -04:00
|
|
|
$r3$.ɵɵelementEnd();
|
2018-07-17 21:59:49 -04:00
|
|
|
}
|
|
|
|
if (rf & 2) {
|
2019-05-17 21:49:21 -04:00
|
|
|
const $bar$ = $r3$.ɵɵreference(4);
|
|
|
|
$r3$.ɵɵnextContext();
|
|
|
|
const $foo$ = $r3$.ɵɵreference(1);
|
2019-09-06 17:43:16 -04:00
|
|
|
$r3$.ɵɵadvance(1);
|
2019-04-23 23:40:05 -04:00
|
|
|
$r3$.ɵɵtextInterpolate2(" ", $foo$, "-", $bar$, " ");
|
2018-07-17 21:59:49 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
…
|
2019-10-10 17:57:15 -04:00
|
|
|
MyComponent.ɵcmp = $r3$.ɵɵdefineComponent({
|
2018-07-17 21:59:49 -04:00
|
|
|
type: MyComponent,
|
|
|
|
selectors: [["my-component"]],
|
2019-09-23 14:08:51 -04:00
|
|
|
decls: 6,
|
2018-08-18 14:14:50 -04:00
|
|
|
vars: 1,
|
2019-10-29 17:20:25 -04:00
|
|
|
consts: [["foo", ""], [${AttributeMarker.Template}, "if"], ["baz", ""], ["bar", ""]],
|
2018-08-18 14:14:50 -04:00
|
|
|
template: function MyComponent_Template(rf, ctx) {
|
2018-07-17 21:59:49 -04:00
|
|
|
if (rf & 1) {
|
2019-10-29 17:20:25 -04:00
|
|
|
$r3$.ɵɵelement(0, "div", null, 0);
|
2019-05-17 21:49:21 -04:00
|
|
|
$r3$.ɵɵtext(2);
|
2019-10-29 17:20:25 -04:00
|
|
|
$r3$.ɵɵtemplate(3, MyComponent_div_3_Template, 5, 2, "div", 1);
|
|
|
|
$r3$.ɵɵelement(4, "div", null, 2);
|
2018-07-17 21:59:49 -04:00
|
|
|
}
|
|
|
|
if (rf & 2) {
|
2019-05-17 21:49:21 -04:00
|
|
|
const $foo$ = $r3$.ɵɵreference(1);
|
2019-09-06 17:43:16 -04:00
|
|
|
$r3$.ɵɵadvance(2);
|
2019-04-23 23:40:05 -04:00
|
|
|
$r3$.ɵɵtextInterpolate1(" ", $foo$, " ");
|
2018-07-17 21:59:49 -04:00
|
|
|
}
|
|
|
|
},
|
2018-11-19 12:48:14 -05:00
|
|
|
directives:[IfDirective],
|
|
|
|
encapsulation: 2
|
2018-07-17 21:59:49 -04:00
|
|
|
});`;
|
|
|
|
|
|
|
|
const result = compile(files, angularFiles);
|
|
|
|
const source = result.source;
|
2019-10-10 17:57:15 -04:00
|
|
|
expectEmit(source, MyComponentDefinition, 'Incorrect MyComponent.ɵcmp');
|
2018-07-17 21:59:49 -04:00
|
|
|
});
|
|
|
|
|
2018-07-25 20:25:22 -04:00
|
|
|
it('should support local refs mixed with context assignments', () => {
|
|
|
|
const files = {
|
|
|
|
app: {
|
|
|
|
'spec.ts': `
|
|
|
|
import {Component, NgModule} from '@angular/core';
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'my-component',
|
|
|
|
template: \`
|
|
|
|
<div *ngFor="let item of items">
|
|
|
|
<div #foo></div>
|
|
|
|
<span *ngIf="showing">
|
|
|
|
{{ foo }} - {{ item }}
|
|
|
|
</span>
|
|
|
|
</div>\`
|
|
|
|
})
|
|
|
|
export class MyComponent {}
|
|
|
|
|
2018-08-01 03:52:34 -04:00
|
|
|
@NgModule({declarations: [MyComponent]})
|
2018-07-25 20:25:22 -04:00
|
|
|
export class MyModule {}
|
|
|
|
`
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const template = `
|
2018-12-19 15:17:38 -05:00
|
|
|
function MyComponent_div_0_span_3_Template(rf, ctx) {
|
2018-07-25 20:25:22 -04:00
|
|
|
if (rf & 1) {
|
2019-05-17 21:49:21 -04:00
|
|
|
$i0$.ɵɵelementStart(0, "span");
|
|
|
|
$i0$.ɵɵtext(1);
|
|
|
|
$i0$.ɵɵelementEnd();
|
2018-07-25 20:25:22 -04:00
|
|
|
}
|
|
|
|
if (rf & 2) {
|
2019-05-17 21:49:21 -04:00
|
|
|
const $item$ = $i0$.ɵɵnextContext().$implicit;
|
|
|
|
const $foo$ = $i0$.ɵɵreference(2);
|
2019-09-06 17:43:16 -04:00
|
|
|
$r3$.ɵɵadvance(1);
|
2019-04-23 23:40:05 -04:00
|
|
|
$i0$.ɵɵtextInterpolate2(" ", $foo$, " - ", $item$, " ");
|
2018-07-25 20:25:22 -04:00
|
|
|
}
|
|
|
|
}
|
2018-08-03 15:20:27 -04:00
|
|
|
|
2018-12-19 15:17:38 -05:00
|
|
|
function MyComponent_div_0_Template(rf, ctx) {
|
2018-07-25 20:25:22 -04:00
|
|
|
if (rf & 1) {
|
2019-05-17 21:49:21 -04:00
|
|
|
$i0$.ɵɵelementStart(0, "div");
|
2019-10-29 17:20:25 -04:00
|
|
|
$i0$.ɵɵelement(1, "div", null, 1);
|
|
|
|
$i0$.ɵɵtemplate(3, MyComponent_div_0_span_3_Template, 2, 2, "span", 2);
|
2019-05-17 21:49:21 -04:00
|
|
|
$i0$.ɵɵelementEnd();
|
2018-07-25 20:25:22 -04:00
|
|
|
}
|
|
|
|
if (rf & 2) {
|
2019-05-17 21:49:21 -04:00
|
|
|
const $app$ = $i0$.ɵɵnextContext();
|
2019-09-06 17:43:16 -04:00
|
|
|
$r3$.ɵɵadvance(3);
|
2019-05-17 21:49:21 -04:00
|
|
|
$i0$.ɵɵproperty("ngIf", $app$.showing);
|
2018-07-25 20:25:22 -04:00
|
|
|
}
|
|
|
|
}
|
2018-08-03 15:20:27 -04:00
|
|
|
|
2018-07-25 20:25:22 -04:00
|
|
|
// ...
|
2019-10-29 17:20:25 -04:00
|
|
|
consts: [[${AttributeMarker.Template}, "ngFor", "ngForOf"], ["foo", ""], [${AttributeMarker.Template}, "ngIf"]],
|
2018-07-25 20:25:22 -04:00
|
|
|
template:function MyComponent_Template(rf, ctx){
|
|
|
|
if (rf & 1) {
|
2019-09-23 14:08:51 -04:00
|
|
|
$i0$.ɵɵtemplate(0, MyComponent_div_0_Template, 4, 1, "div", 0);
|
2018-07-25 20:25:22 -04:00
|
|
|
}
|
|
|
|
if (rf & 2) {
|
2019-05-17 21:49:21 -04:00
|
|
|
$i0$.ɵɵproperty("ngForOf", ctx.items);
|
2018-07-25 20:25:22 -04:00
|
|
|
}
|
|
|
|
}`;
|
|
|
|
|
|
|
|
const result = compile(files, angularFiles);
|
|
|
|
|
|
|
|
expectEmit(result.source, template, 'Incorrect template');
|
|
|
|
});
|
|
|
|
|
2018-02-05 20:31:12 -05:00
|
|
|
describe('lifecycle hooks', () => {
|
|
|
|
const files = {
|
|
|
|
app: {
|
|
|
|
'spec.ts': `
|
|
|
|
import {Component, Input, NgModule} from '@angular/core';
|
|
|
|
|
|
|
|
let events: string[] = [];
|
|
|
|
|
|
|
|
@Component({selector: 'lifecycle-comp', template: ''})
|
|
|
|
export class LifecycleComp {
|
|
|
|
@Input('name') nameMin: string;
|
|
|
|
|
|
|
|
ngOnChanges() { events.push('changes' + this.nameMin); }
|
|
|
|
|
|
|
|
ngOnInit() { events.push('init' + this.nameMin); }
|
|
|
|
ngDoCheck() { events.push('check' + this.nameMin); }
|
|
|
|
|
|
|
|
ngAfterContentInit() { events.push('content init' + this.nameMin); }
|
|
|
|
ngAfterContentChecked() { events.push('content check' + this.nameMin); }
|
|
|
|
|
|
|
|
ngAfterViewInit() { events.push('view init' + this.nameMin); }
|
|
|
|
ngAfterViewChecked() { events.push('view check' + this.nameMin); }
|
|
|
|
|
|
|
|
ngOnDestroy() { events.push(this.nameMin); }
|
|
|
|
}
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'simple-layout',
|
|
|
|
template: \`
|
|
|
|
<lifecycle-comp [name]="name1"></lifecycle-comp>
|
|
|
|
<lifecycle-comp [name]="name2"></lifecycle-comp>
|
|
|
|
\`
|
|
|
|
})
|
|
|
|
export class SimpleLayout {
|
|
|
|
name1 = '1';
|
|
|
|
name2 = '2';
|
|
|
|
}
|
|
|
|
|
|
|
|
@NgModule({declarations: [LifecycleComp, SimpleLayout]})
|
|
|
|
export class LifecycleModule {}
|
|
|
|
`
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
it('should gen hooks with a few simple components', () => {
|
|
|
|
const LifecycleCompDefinition = `
|
2019-10-10 17:57:15 -04:00
|
|
|
LifecycleComp.ɵcmp = $r3$.ɵɵdefineComponent({
|
2018-02-05 20:31:12 -05:00
|
|
|
type: LifecycleComp,
|
2018-07-12 18:10:55 -04:00
|
|
|
selectors: [["lifecycle-comp"]],
|
2018-10-26 02:05:15 -04:00
|
|
|
inputs: {nameMin: ["name", "nameMin"]},
|
2020-02-21 14:34:39 -05:00
|
|
|
features: [$r3$.ɵɵNgOnChangesFeature],
|
2019-09-23 14:08:51 -04:00
|
|
|
decls: 0,
|
2018-08-18 14:14:50 -04:00
|
|
|
vars: 0,
|
2018-11-19 12:48:14 -05:00
|
|
|
template: function LifecycleComp_Template(rf, ctx) {},
|
|
|
|
encapsulation: 2
|
2018-02-05 20:31:12 -05:00
|
|
|
});`;
|
|
|
|
|
|
|
|
const SimpleLayoutDefinition = `
|
2019-10-10 17:57:15 -04:00
|
|
|
SimpleLayout.ɵcmp = $r3$.ɵɵdefineComponent({
|
2018-02-05 20:31:12 -05:00
|
|
|
type: SimpleLayout,
|
2018-07-12 18:10:55 -04:00
|
|
|
selectors: [["simple-layout"]],
|
2019-09-23 14:08:51 -04:00
|
|
|
decls: 2,
|
2018-08-18 14:14:50 -04:00
|
|
|
vars: 2,
|
2019-09-23 14:08:51 -04:00
|
|
|
consts: [[3, "name"]],
|
2018-08-18 14:14:50 -04:00
|
|
|
template: function SimpleLayout_Template(rf, ctx) {
|
2018-04-10 23:57:20 -04:00
|
|
|
if (rf & 1) {
|
2019-09-23 14:08:51 -04:00
|
|
|
$r3$.ɵɵelement(0, "lifecycle-comp", 0);
|
|
|
|
$r3$.ɵɵelement(1, "lifecycle-comp", 0);
|
2018-02-05 20:31:12 -05:00
|
|
|
}
|
2018-04-10 23:57:20 -04:00
|
|
|
if (rf & 2) {
|
2019-05-17 21:49:21 -04:00
|
|
|
$r3$.ɵɵproperty("name", ctx.name1);
|
2019-09-06 17:43:16 -04:00
|
|
|
$r3$.ɵɵadvance(1);
|
2019-05-17 21:49:21 -04:00
|
|
|
$r3$.ɵɵproperty("name", ctx.name2);
|
2018-04-10 23:57:20 -04:00
|
|
|
}
|
2018-03-29 15:58:41 -04:00
|
|
|
},
|
2018-11-19 12:48:14 -05:00
|
|
|
directives: [LifecycleComp],
|
|
|
|
encapsulation: 2
|
2018-02-05 20:31:12 -05:00
|
|
|
});`;
|
|
|
|
|
|
|
|
const result = compile(files, angularFiles);
|
|
|
|
const source = result.source;
|
|
|
|
|
|
|
|
expectEmit(source, LifecycleCompDefinition, 'Invalid LifecycleComp definition');
|
|
|
|
expectEmit(source, SimpleLayoutDefinition, 'Invalid SimpleLayout definition');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('template variables', () => {
|
|
|
|
const shared = {
|
|
|
|
shared: {
|
|
|
|
'for_of.ts': `
|
|
|
|
import {Directive, Input, SimpleChanges, TemplateRef, ViewContainerRef} from '@angular/core';
|
|
|
|
|
|
|
|
export interface ForOfContext {
|
|
|
|
$implicit: any;
|
|
|
|
index: number;
|
|
|
|
even: boolean;
|
|
|
|
odd: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Directive({selector: '[forOf]'})
|
|
|
|
export class ForOfDirective {
|
|
|
|
private previous: any[];
|
|
|
|
|
|
|
|
constructor(private view: ViewContainerRef, private template: TemplateRef<any>) {}
|
|
|
|
|
|
|
|
@Input() forOf: any[];
|
|
|
|
|
|
|
|
ngOnChanges(simpleChanges: SimpleChanges) {
|
|
|
|
if ('forOf' in simpleChanges) {
|
|
|
|
this.update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ngDoCheck(): void {
|
|
|
|
const previous = this.previous;
|
|
|
|
const current = this.forOf;
|
|
|
|
if (!previous || previous.length != current.length ||
|
|
|
|
previous.some((value: any, index: number) => current[index] !== previous[index])) {
|
|
|
|
this.update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private update() {
|
|
|
|
// TODO(chuckj): Not implemented yet
|
|
|
|
// this.view.clear();
|
|
|
|
if (this.forOf) {
|
|
|
|
const current = this.forOf;
|
|
|
|
for (let i = 0; i < current.length; i++) {
|
|
|
|
const context = {$implicit: current[i], index: i, even: i % 2 == 0, odd: i % 2 == 1};
|
|
|
|
// TODO(chuckj): Not implemented yet
|
|
|
|
// this.view.createEmbeddedView(this.template, context);
|
|
|
|
}
|
|
|
|
this.previous = [...this.forOf];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-06-08 12:00:01 -04:00
|
|
|
it('should support embedded views in the SVG namespace', () => {
|
|
|
|
const files = {
|
|
|
|
app: {
|
|
|
|
...shared,
|
|
|
|
'spec.ts': `
|
|
|
|
import {Component, NgModule} from '@angular/core';
|
|
|
|
import {ForOfDirective} from './shared/for_of';
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'my-component',
|
|
|
|
template: \`<svg><g *for="let item of items"><circle></circle></g></svg>\`
|
|
|
|
})
|
|
|
|
export class MyComponent {
|
|
|
|
items = [{ data: 42 }, { data: 42 }];
|
|
|
|
}
|
|
|
|
|
|
|
|
@NgModule({
|
|
|
|
declarations: [MyComponent, ForOfDirective]
|
|
|
|
})
|
|
|
|
export class MyModule {}
|
|
|
|
`
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// TODO(benlesh): Enforce this when the directives are specified
|
|
|
|
const ForDirectiveDefinition = `
|
2019-10-11 15:28:12 -04:00
|
|
|
ForOfDirective.ɵdir = $r3$.ɵɵdefineDirective({
|
2018-06-08 12:00:01 -04:00
|
|
|
type: ForOfDirective,
|
2018-07-12 18:10:55 -04:00
|
|
|
selectors: [["", "forOf", ""]],
|
2020-02-21 14:34:39 -05:00
|
|
|
features: [$r3$.ɵɵNgOnChangesFeature],
|
2018-07-12 18:10:55 -04:00
|
|
|
inputs: {forOf: "forOf"}
|
2018-06-08 12:00:01 -04:00
|
|
|
});
|
|
|
|
`;
|
|
|
|
|
2019-10-11 17:18:45 -04:00
|
|
|
const ForDirectiveFactory = `ForOfDirective.ɵfac = function ForOfDirective_Factory(t) {
|
2019-08-12 02:26:20 -04:00
|
|
|
return new (t || ForOfDirective)($r3$.ɵɵdirectiveInject(ViewContainerRef), $r3$.ɵɵdirectiveInject(TemplateRef));
|
|
|
|
};`;
|
|
|
|
|
2018-06-08 12:00:01 -04:00
|
|
|
const MyComponentDefinition = `
|
2018-12-19 15:17:38 -05:00
|
|
|
function MyComponent__svg_g_1_Template(rf, ctx) {
|
2018-07-17 21:59:49 -04:00
|
|
|
if (rf & 1) {
|
2019-05-17 21:49:21 -04:00
|
|
|
$r3$.ɵɵnamespaceSVG();
|
|
|
|
$r3$.ɵɵelementStart(0,"g");
|
|
|
|
$r3$.ɵɵelement(1,"circle");
|
|
|
|
$r3$.ɵɵelementEnd();
|
2018-07-17 21:59:49 -04:00
|
|
|
}
|
|
|
|
}
|
2018-06-08 12:00:01 -04:00
|
|
|
…
|
2019-10-10 17:57:15 -04:00
|
|
|
MyComponent.ɵcmp = $r3$.ɵɵdefineComponent({
|
2018-06-08 12:00:01 -04:00
|
|
|
type: MyComponent,
|
2018-07-12 18:10:55 -04:00
|
|
|
selectors: [["my-component"]],
|
2019-09-23 14:08:51 -04:00
|
|
|
decls: 2,
|
2018-08-18 14:14:50 -04:00
|
|
|
vars: 1,
|
2019-09-23 14:08:51 -04:00
|
|
|
consts: [[${AttributeMarker.Template}, "for", "forOf"]],
|
2018-08-18 14:14:50 -04:00
|
|
|
template: function MyComponent_Template(rf, ctx){
|
2018-06-08 12:00:01 -04:00
|
|
|
if (rf & 1) {
|
2019-05-17 21:49:21 -04:00
|
|
|
$r3$.ɵɵnamespaceSVG();
|
|
|
|
$r3$.ɵɵelementStart(0,"svg");
|
2019-09-23 14:08:51 -04:00
|
|
|
$r3$.ɵɵtemplate(1, MyComponent__svg_g_1_Template, 2, 0, "g", 0);
|
2019-05-17 21:49:21 -04:00
|
|
|
$r3$.ɵɵelementEnd();
|
2018-06-08 12:00:01 -04:00
|
|
|
}
|
2019-03-01 08:39:28 -05:00
|
|
|
if (rf & 2) {
|
2019-09-06 17:43:16 -04:00
|
|
|
$r3$.ɵɵadvance(1);
|
2019-05-17 21:49:21 -04:00
|
|
|
$r3$.ɵɵproperty("forOf", ctx.items);
|
2019-03-01 08:39:28 -05:00
|
|
|
}
|
2018-06-08 12:00:01 -04:00
|
|
|
},
|
2018-11-19 12:48:14 -05:00
|
|
|
directives: function() { return [ForOfDirective]; },
|
|
|
|
encapsulation: 2
|
2018-06-08 12:00:01 -04:00
|
|
|
});
|
|
|
|
`;
|
|
|
|
|
|
|
|
const result = compile(files, angularFiles);
|
|
|
|
const source = result.source;
|
|
|
|
|
|
|
|
// TODO(benlesh): Enforce this when the directives are specified
|
|
|
|
// expectEmit(source, ForDirectiveDefinition, 'Invalid directive definition');
|
2019-08-12 02:26:20 -04:00
|
|
|
// expectEmit(source, ForDirectiveFactory, 'Invalid directive factory');
|
2018-06-08 12:00:01 -04:00
|
|
|
expectEmit(source, MyComponentDefinition, 'Invalid component definition');
|
|
|
|
});
|
|
|
|
|
2018-02-05 20:31:12 -05:00
|
|
|
it('should support a let variable and reference', () => {
|
|
|
|
const files = {
|
|
|
|
app: {
|
|
|
|
...shared,
|
|
|
|
'spec.ts': `
|
|
|
|
import {Component, NgModule} from '@angular/core';
|
|
|
|
import {ForOfDirective} from './shared/for_of';
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'my-component',
|
|
|
|
template: \`<ul><li *for="let item of items">{{item.name}}</li></ul>\`
|
|
|
|
})
|
|
|
|
export class MyComponent {
|
|
|
|
items = [{name: 'one'}, {name: 'two'}];
|
|
|
|
}
|
|
|
|
|
|
|
|
@NgModule({
|
|
|
|
declarations: [MyComponent, ForOfDirective]
|
|
|
|
})
|
|
|
|
export class MyModule {}
|
|
|
|
`
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// TODO(chuckj): Enforce this when the directives are specified
|
|
|
|
const ForDirectiveDefinition = `
|
2019-10-11 15:28:12 -04:00
|
|
|
ForOfDirective.ɵdir = $r3$.ɵɵdefineDirective({
|
2018-02-05 20:31:12 -05:00
|
|
|
type: ForOfDirective,
|
2018-07-12 18:10:55 -04:00
|
|
|
selectors: [["", "forOf", ""]],
|
2020-02-21 14:34:39 -05:00
|
|
|
features: [$r3$.ɵɵNgOnChangesFeature],
|
2018-07-12 18:10:55 -04:00
|
|
|
inputs: {forOf: "forOf"}
|
2018-02-05 20:31:12 -05:00
|
|
|
});
|
|
|
|
`;
|
|
|
|
|
2019-08-12 02:26:20 -04:00
|
|
|
const ForDirectiveFactory = `
|
2019-10-11 17:18:45 -04:00
|
|
|
ForOfDirective.ɵfac = function ForOfDirective_Factory(t) {
|
2019-08-12 02:26:20 -04:00
|
|
|
return new (t || ForOfDirective)($r3$.ɵɵdirectiveInject(ViewContainerRef), $r3$.ɵɵdirectiveInject(TemplateRef));
|
|
|
|
};
|
|
|
|
`;
|
|
|
|
|
2018-02-05 20:31:12 -05:00
|
|
|
const MyComponentDefinition = `
|
2018-12-19 15:17:38 -05:00
|
|
|
function MyComponent_li_1_Template(rf, ctx) {
|
2018-07-17 21:59:49 -04:00
|
|
|
if (rf & 1) {
|
2019-05-17 21:49:21 -04:00
|
|
|
$r3$.ɵɵelementStart(0, "li");
|
|
|
|
$r3$.ɵɵtext(1);
|
|
|
|
$r3$.ɵɵelementEnd();
|
2018-07-17 21:59:49 -04:00
|
|
|
}
|
|
|
|
if (rf & 2) {
|
2018-07-25 20:25:22 -04:00
|
|
|
const $item$ = ctx.$implicit;
|
2019-09-06 17:43:16 -04:00
|
|
|
$r3$.ɵɵadvance(1);
|
2019-04-23 23:40:05 -04:00
|
|
|
$r3$.ɵɵtextInterpolate($item$.name);
|
2018-07-17 21:59:49 -04:00
|
|
|
}
|
|
|
|
}
|
2018-02-05 20:31:12 -05:00
|
|
|
…
|
2019-10-10 17:57:15 -04:00
|
|
|
MyComponent.ɵcmp = $r3$.ɵɵdefineComponent({
|
2018-02-05 20:31:12 -05:00
|
|
|
type: MyComponent,
|
2018-07-12 18:10:55 -04:00
|
|
|
selectors: [["my-component"]],
|
2019-09-23 14:08:51 -04:00
|
|
|
decls: 2,
|
2018-08-18 14:14:50 -04:00
|
|
|
vars: 1,
|
2019-09-23 14:08:51 -04:00
|
|
|
consts: [[${AttributeMarker.Template}, "for", "forOf"]],
|
2018-08-18 14:14:50 -04:00
|
|
|
template: function MyComponent_Template(rf, ctx) {
|
2018-04-10 23:57:20 -04:00
|
|
|
if (rf & 1) {
|
2019-05-17 21:49:21 -04:00
|
|
|
$r3$.ɵɵelementStart(0, "ul");
|
2019-09-23 14:08:51 -04:00
|
|
|
$r3$.ɵɵtemplate(1, MyComponent_li_1_Template, 2, 1, "li", 0);
|
2019-05-17 21:49:21 -04:00
|
|
|
$r3$.ɵɵelementEnd();
|
2018-02-05 20:31:12 -05:00
|
|
|
}
|
2018-04-10 23:57:20 -04:00
|
|
|
if (rf & 2) {
|
2019-09-06 17:43:16 -04:00
|
|
|
$r3$.ɵɵadvance(1);
|
2019-05-17 21:49:21 -04:00
|
|
|
$r3$.ɵɵproperty("forOf", ctx.items);
|
2018-04-10 23:57:20 -04:00
|
|
|
}
|
2018-03-29 15:58:41 -04:00
|
|
|
},
|
2018-11-19 12:48:14 -05:00
|
|
|
directives: function() { return [ForOfDirective]; },
|
|
|
|
encapsulation: 2
|
2018-02-05 20:31:12 -05:00
|
|
|
});
|
|
|
|
`;
|
|
|
|
|
|
|
|
const result = compile(files, angularFiles);
|
|
|
|
const source = result.source;
|
|
|
|
|
|
|
|
// TODO(chuckj): Enforce this when the directives are specified
|
|
|
|
// expectEmit(source, ForDirectiveDefinition, 'Invalid directive definition');
|
2019-08-12 02:26:20 -04:00
|
|
|
// expectEmit(source, ForDirectiveFactory, 'Invalid directive factory');
|
2018-02-05 20:31:12 -05:00
|
|
|
expectEmit(source, MyComponentDefinition, 'Invalid component definition');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should support accessing parent template variables', () => {
|
|
|
|
const files = {
|
|
|
|
app: {
|
|
|
|
...shared,
|
|
|
|
'spec.ts': `
|
|
|
|
import {Component, NgModule} from '@angular/core';
|
|
|
|
import {ForOfDirective} from './shared/for_of';
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'my-component',
|
|
|
|
template: \`
|
|
|
|
<ul>
|
|
|
|
<li *for="let item of items">
|
|
|
|
<div>{{item.name}}</div>
|
|
|
|
<ul>
|
|
|
|
<li *for="let info of item.infos">
|
|
|
|
{{item.name}}: {{info.description}}
|
|
|
|
</li>
|
|
|
|
</ul>
|
|
|
|
</li>
|
|
|
|
</ul>\`
|
|
|
|
})
|
|
|
|
export class MyComponent {
|
|
|
|
items = [
|
|
|
|
{name: 'one', infos: [{description: '11'}, {description: '12'}]},
|
|
|
|
{name: 'two', infos: [{description: '21'}, {description: '22'}]}
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
@NgModule({
|
|
|
|
declarations: [MyComponent, ForOfDirective]
|
|
|
|
})
|
|
|
|
export class MyModule {}
|
|
|
|
`
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const MyComponentDefinition = `
|
2018-12-19 15:17:38 -05:00
|
|
|
function MyComponent_li_1_li_4_Template(rf, ctx) {
|
2018-07-17 21:59:49 -04:00
|
|
|
if (rf & 1) {
|
2019-05-17 21:49:21 -04:00
|
|
|
$r3$.ɵɵelementStart(0, "li");
|
|
|
|
$r3$.ɵɵtext(1);
|
|
|
|
$r3$.ɵɵelementEnd();
|
2018-07-17 21:59:49 -04:00
|
|
|
}
|
|
|
|
if (rf & 2) {
|
2018-07-25 20:25:22 -04:00
|
|
|
const $info$ = ctx.$implicit;
|
2019-05-17 21:49:21 -04:00
|
|
|
const $item$ = $r3$.ɵɵnextContext().$implicit;
|
2019-09-06 17:43:16 -04:00
|
|
|
$r3$.ɵɵadvance(1);
|
2019-04-23 23:40:05 -04:00
|
|
|
$r3$.ɵɵtextInterpolate2(" ", $item$.name, ": ", $info$.description, " ");
|
2018-07-17 21:59:49 -04:00
|
|
|
}
|
|
|
|
}
|
2018-08-03 15:20:27 -04:00
|
|
|
|
2018-12-19 15:17:38 -05:00
|
|
|
function MyComponent_li_1_Template(rf, ctx) {
|
2018-07-17 21:59:49 -04:00
|
|
|
if (rf & 1) {
|
2019-05-17 21:49:21 -04:00
|
|
|
$r3$.ɵɵelementStart(0, "li");
|
|
|
|
$r3$.ɵɵelementStart(1, "div");
|
|
|
|
$r3$.ɵɵtext(2);
|
|
|
|
$r3$.ɵɵelementEnd();
|
|
|
|
$r3$.ɵɵelementStart(3, "ul");
|
2019-09-23 14:08:51 -04:00
|
|
|
$r3$.ɵɵtemplate(4, MyComponent_li_1_li_4_Template, 2, 2, "li", 0);
|
2019-05-17 21:49:21 -04:00
|
|
|
$r3$.ɵɵelementEnd();
|
|
|
|
$r3$.ɵɵelementEnd();
|
2018-07-17 21:59:49 -04:00
|
|
|
}
|
|
|
|
if (rf & 2) {
|
2018-07-25 20:25:22 -04:00
|
|
|
const $item$ = ctx.$implicit;
|
2019-09-06 17:43:16 -04:00
|
|
|
$r3$.ɵɵadvance(2);
|
2019-04-23 23:40:05 -04:00
|
|
|
$r3$.ɵɵtextInterpolate(IDENT.name);
|
2019-09-06 17:43:16 -04:00
|
|
|
$r3$.ɵɵadvance(2);
|
2019-05-17 21:49:21 -04:00
|
|
|
$r3$.ɵɵproperty("forOf", IDENT.infos);
|
2018-07-17 21:59:49 -04:00
|
|
|
}
|
|
|
|
}
|
2018-08-03 15:20:27 -04:00
|
|
|
|
2018-02-05 20:31:12 -05:00
|
|
|
…
|
2019-10-10 17:57:15 -04:00
|
|
|
MyComponent.ɵcmp = $r3$.ɵɵdefineComponent({
|
2018-02-05 20:31:12 -05:00
|
|
|
type: MyComponent,
|
2018-07-12 18:10:55 -04:00
|
|
|
selectors: [["my-component"]],
|
2019-09-23 14:08:51 -04:00
|
|
|
decls: 2,
|
2018-08-18 14:14:50 -04:00
|
|
|
vars: 1,
|
2019-09-23 14:08:51 -04:00
|
|
|
consts: [[${AttributeMarker.Template}, "for", "forOf"]],
|
2018-08-18 14:14:50 -04:00
|
|
|
template: function MyComponent_Template(rf, ctx) {
|
2018-04-10 23:57:20 -04:00
|
|
|
if (rf & 1) {
|
2019-05-17 21:49:21 -04:00
|
|
|
$r3$.ɵɵelementStart(0, "ul");
|
2019-09-23 14:08:51 -04:00
|
|
|
$r3$.ɵɵtemplate(1, MyComponent_li_1_Template, 5, 2, "li", 0);
|
2019-05-17 21:49:21 -04:00
|
|
|
$r3$.ɵɵelementEnd();
|
2018-02-05 20:31:12 -05:00
|
|
|
}
|
2018-04-10 23:57:20 -04:00
|
|
|
if (rf & 2) {
|
2019-09-06 17:43:16 -04:00
|
|
|
$r3$.ɵɵadvance(1);
|
2019-05-17 21:49:21 -04:00
|
|
|
$r3$.ɵɵproperty("forOf", ctx.items);
|
2018-04-10 23:57:20 -04:00
|
|
|
}
|
2018-03-29 15:58:41 -04:00
|
|
|
},
|
2018-11-19 12:48:14 -05:00
|
|
|
directives: function () { return [ForOfDirective]; },
|
|
|
|
encapsulation: 2
|
2018-02-05 20:31:12 -05:00
|
|
|
});`;
|
|
|
|
|
|
|
|
const result = compile(files, angularFiles);
|
|
|
|
const source = result.source;
|
|
|
|
expectEmit(source, MyComponentDefinition, 'Invalid component definition');
|
|
|
|
});
|
|
|
|
});
|
2018-10-25 14:13:14 -04:00
|
|
|
|
|
|
|
it('should instantiate directives in a closure when they are forward referenced', () => {
|
|
|
|
const files = {
|
|
|
|
app: {
|
|
|
|
'spec.ts': `
|
|
|
|
import {Component, NgModule, Directive} from '@angular/core';
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'host-binding-comp',
|
|
|
|
template: \`
|
|
|
|
<my-forward-directive></my-forward-directive>
|
|
|
|
\`
|
|
|
|
})
|
|
|
|
export class HostBindingComp {
|
|
|
|
}
|
|
|
|
|
|
|
|
@Directive({
|
|
|
|
selector: 'my-forward-directive'
|
|
|
|
})
|
|
|
|
class MyForwardDirective {}
|
|
|
|
|
|
|
|
@NgModule({declarations: [HostBindingComp, MyForwardDirective]})
|
|
|
|
export class MyModule {}
|
|
|
|
`
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const MyAppDefinition = `
|
|
|
|
…
|
|
|
|
directives: function () { return [MyForwardDirective]; }
|
|
|
|
…
|
|
|
|
`;
|
|
|
|
|
|
|
|
const result = compile(files, angularFiles);
|
|
|
|
const source = result.source;
|
|
|
|
expectEmit(source, MyAppDefinition, 'Invalid component definition');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should instantiate pipes in a closure when they are forward referenced', () => {
|
|
|
|
const files = {
|
|
|
|
app: {
|
|
|
|
'spec.ts': `
|
|
|
|
import {Component, NgModule, Pipe} from '@angular/core';
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'host-binding-comp',
|
|
|
|
template: \`
|
|
|
|
<div [attr.style]="{} | my_forward_pipe">...</div>
|
|
|
|
\`
|
|
|
|
})
|
|
|
|
export class HostBindingComp {
|
|
|
|
}
|
|
|
|
|
|
|
|
@Pipe({
|
|
|
|
name: 'my_forward_pipe'
|
|
|
|
})
|
|
|
|
class MyForwardPipe {}
|
|
|
|
|
|
|
|
@NgModule({declarations: [HostBindingComp, MyForwardPipe]})
|
|
|
|
export class MyModule {}
|
|
|
|
`
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const MyAppDefinition = `
|
|
|
|
…
|
|
|
|
pipes: function () { return [MyForwardPipe]; }
|
|
|
|
…
|
|
|
|
`;
|
|
|
|
|
|
|
|
const result = compile(files, angularFiles);
|
|
|
|
const source = result.source;
|
|
|
|
expectEmit(source, MyAppDefinition, 'Invalid component definition');
|
|
|
|
});
|
2019-01-10 16:24:32 -05:00
|
|
|
|
|
|
|
it('should split multiple `exportAs` values into an array', () => {
|
|
|
|
const files = {
|
|
|
|
app: {
|
|
|
|
'spec.ts': `
|
|
|
|
import {Directive, NgModule} from '@angular/core';
|
|
|
|
|
|
|
|
@Directive({selector: '[some-directive]', exportAs: 'someDir, otherDir'})
|
|
|
|
export class SomeDirective {}
|
|
|
|
|
2018-11-16 11:52:55 -05:00
|
|
|
@NgModule({declarations: [SomeDirective]})
|
2019-01-10 16:24:32 -05:00
|
|
|
export class MyModule{}
|
|
|
|
`
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// SomeDirective definition should be:
|
|
|
|
const SomeDirectiveDefinition = `
|
2019-10-11 15:28:12 -04:00
|
|
|
SomeDirective.ɵdir = $r3$.ɵɵdefineDirective({
|
2019-01-10 16:24:32 -05:00
|
|
|
type: SomeDirective,
|
|
|
|
selectors: [["", "some-directive", ""]],
|
|
|
|
exportAs: ["someDir", "otherDir"]
|
|
|
|
});
|
|
|
|
`;
|
|
|
|
|
|
|
|
const result = compile(files, angularFiles);
|
|
|
|
const source = result.source;
|
|
|
|
|
2019-10-11 15:28:12 -04:00
|
|
|
expectEmit(source, SomeDirectiveDefinition, 'Incorrect SomeDirective.ɵdir');
|
2019-01-10 16:24:32 -05:00
|
|
|
});
|
|
|
|
|
2019-06-04 16:03:32 -04:00
|
|
|
it('should not throw for empty property bindings on ng-template', () => {
|
|
|
|
const files = {
|
|
|
|
app: {
|
|
|
|
'example.ts': `
|
|
|
|
import {Component, NgModule} from '@angular/core';
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'my-app',
|
|
|
|
template: '<ng-template [id]=""></ng-template>'
|
|
|
|
})
|
|
|
|
export class MyComponent {
|
|
|
|
}
|
|
|
|
|
|
|
|
@NgModule({declarations: [MyComponent]})
|
|
|
|
export class MyModule {}`
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
expect(() => compile(files, angularFiles)).not.toThrow();
|
|
|
|
});
|
|
|
|
|
2019-10-27 05:59:23 -04:00
|
|
|
it('should not generate a selectors array if the directive does not have a selector', () => {
|
|
|
|
const files = {
|
|
|
|
app: {
|
|
|
|
'spec.ts': `
|
|
|
|
import {Directive} from '@angular/core';
|
|
|
|
|
|
|
|
@Directive()
|
|
|
|
export class AbstractDirective {
|
|
|
|
}
|
|
|
|
`
|
|
|
|
}
|
|
|
|
};
|
|
|
|
const expectedOutput = `
|
|
|
|
// ...
|
|
|
|
AbstractDirective.ɵdir = $r3$.ɵɵdefineDirective({
|
|
|
|
type: AbstractDirective
|
|
|
|
});
|
|
|
|
// ...
|
|
|
|
`;
|
|
|
|
const result = compile(files, angularFiles);
|
|
|
|
expectEmit(result.source, expectedOutput, 'Invalid directive definition');
|
|
|
|
});
|
2019-06-04 16:03:32 -04:00
|
|
|
|
2019-11-13 08:26:34 -05:00
|
|
|
it('should generate a pure function for constant object literals', () => {
|
|
|
|
const files = {
|
|
|
|
app: {
|
|
|
|
'spec.ts': `
|
|
|
|
import {Component} from '@angular/core';
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
template: '<some-comp [prop]="{}" [otherProp]="{a: 1, b: 2}"></some-comp>'
|
|
|
|
})
|
|
|
|
export class MyApp {
|
|
|
|
}
|
|
|
|
`
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const MyAppDeclaration = `
|
|
|
|
const $c0$ = function () { return {}; };
|
|
|
|
const $c1$ = function () { return { a: 1, b: 2 }; };
|
|
|
|
…
|
|
|
|
MyApp.ɵcmp = $r3$.ɵɵdefineComponent({
|
|
|
|
type: MyApp,
|
|
|
|
selectors: [["ng-component"]],
|
|
|
|
decls: 1,
|
|
|
|
vars: 4,
|
|
|
|
consts: [[${AttributeMarker.Bindings}, "prop", "otherProp"]],
|
|
|
|
template: function MyApp_Template(rf, ctx) {
|
|
|
|
if (rf & 1) {
|
|
|
|
$r3$.ɵɵelement(0, "some-comp", 0);
|
|
|
|
}
|
|
|
|
if (rf & 2) {
|
|
|
|
$r3$.ɵɵproperty("prop", $r3$.ɵɵpureFunction0(2, $c0$))("otherProp", $r3$.ɵɵpureFunction0(3, $c1$));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
encapsulation: 2
|
|
|
|
});
|
|
|
|
`;
|
|
|
|
|
|
|
|
const result = compile(files, angularFiles);
|
|
|
|
expectEmit(result.source, MyAppDeclaration, 'Invalid component definition');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should generate a pure function for constant array literals', () => {
|
|
|
|
const files = {
|
|
|
|
app: {
|
|
|
|
'spec.ts': `
|
|
|
|
import {Component} from '@angular/core';
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
template: '<some-comp [prop]="[]" [otherProp]="[0, 1, 2]"></some-comp>'
|
|
|
|
})
|
|
|
|
export class MyApp {
|
|
|
|
}
|
|
|
|
`
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const MyAppDeclaration = `
|
|
|
|
const $c0$ = function () { return []; };
|
|
|
|
const $c1$ = function () { return [0, 1, 2]; };
|
|
|
|
…
|
|
|
|
MyApp.ɵcmp = $r3$.ɵɵdefineComponent({
|
|
|
|
type: MyApp,
|
|
|
|
selectors: [["ng-component"]],
|
|
|
|
decls: 1,
|
|
|
|
vars: 4,
|
|
|
|
consts: [[${AttributeMarker.Bindings}, "prop", "otherProp"]],
|
|
|
|
template: function MyApp_Template(rf, ctx) {
|
|
|
|
if (rf & 1) {
|
|
|
|
$r3$.ɵɵelement(0, "some-comp", 0);
|
|
|
|
}
|
|
|
|
if (rf & 2) {
|
|
|
|
$r3$.ɵɵproperty("prop", $r3$.ɵɵpureFunction0(2, $c0$))("otherProp", $r3$.ɵɵpureFunction0(3, $c1$));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
encapsulation: 2
|
|
|
|
});
|
|
|
|
`;
|
|
|
|
|
2020-02-16 06:49:23 -05:00
|
|
|
const result = compile(files, angularFiles);
|
|
|
|
expectEmit(result.source, MyAppDeclaration, 'Invalid component definition');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not share pure functions between null and object literals', () => {
|
|
|
|
const files = {
|
|
|
|
app: {
|
|
|
|
'spec.ts': `
|
|
|
|
import {Component, NgModule} from '@angular/core';
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
template: \`
|
|
|
|
<div [dir]="{foo: null}"></div>
|
|
|
|
<div [dir]="{foo: {}}"></div>
|
|
|
|
\`
|
|
|
|
})
|
|
|
|
export class MyApp {}
|
|
|
|
|
|
|
|
@NgModule({declarations: [MyApp]})
|
|
|
|
export class MyModule {}
|
|
|
|
`
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const MyAppDeclaration = `
|
|
|
|
const $c0$ = function () { return { foo: null }; };
|
|
|
|
const $c1$ = function () { return {}; };
|
|
|
|
const $c2$ = function (a0) { return { foo: a0 }; };
|
|
|
|
…
|
|
|
|
MyApp.ɵcmp = $r3$.ɵɵdefineComponent({
|
|
|
|
type: MyApp,
|
|
|
|
selectors: [["ng-component"]],
|
|
|
|
decls: 2,
|
|
|
|
vars: 6,
|
|
|
|
consts: [[${AttributeMarker.Bindings}, "dir"]],
|
|
|
|
template: function MyApp_Template(rf, ctx) {
|
|
|
|
if (rf & 1) {
|
|
|
|
$r3$.ɵɵelement(0, "div", 0);
|
|
|
|
$r3$.ɵɵelement(1, "div", 0);
|
|
|
|
}
|
|
|
|
if (rf & 2) {
|
|
|
|
$r3$.ɵɵproperty("dir", $r3$.ɵɵpureFunction0(2, $c0$));
|
|
|
|
$r3$.ɵɵadvance(1);
|
|
|
|
$r3$.ɵɵproperty("dir", $r3$.ɵɵpureFunction1(4, $c2$, $r3$.ɵɵpureFunction0(3, $c1$)));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
encapsulation: 2
|
|
|
|
});
|
|
|
|
`;
|
|
|
|
|
|
|
|
const result = compile(files, angularFiles);
|
|
|
|
expectEmit(result.source, MyAppDeclaration, 'Invalid component definition');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not share pure functions between null and array literals', () => {
|
|
|
|
const files = {
|
|
|
|
app: {
|
|
|
|
'spec.ts': `
|
|
|
|
import {Component, NgModule} from '@angular/core';
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
template: \`
|
|
|
|
<div [dir]="{foo: null}"></div>
|
|
|
|
<div [dir]="{foo: []}"></div>
|
|
|
|
\`
|
|
|
|
})
|
|
|
|
export class MyApp {}
|
|
|
|
|
|
|
|
@NgModule({declarations: [MyApp]})
|
|
|
|
export class MyModule {}
|
|
|
|
`
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const MyAppDeclaration = `
|
|
|
|
const $c0$ = function () { return { foo: null }; };
|
|
|
|
const $c1$ = function () { return []; };
|
|
|
|
const $c2$ = function (a0) { return { foo: a0 }; };
|
|
|
|
…
|
|
|
|
MyApp.ɵcmp = $r3$.ɵɵdefineComponent({
|
|
|
|
type: MyApp,
|
|
|
|
selectors: [["ng-component"]],
|
|
|
|
decls: 2,
|
|
|
|
vars: 6,
|
|
|
|
consts: [[${AttributeMarker.Bindings}, "dir"]],
|
|
|
|
template: function MyApp_Template(rf, ctx) {
|
|
|
|
if (rf & 1) {
|
|
|
|
$r3$.ɵɵelement(0, "div", 0);
|
|
|
|
$r3$.ɵɵelement(1, "div", 0);
|
|
|
|
}
|
|
|
|
if (rf & 2) {
|
|
|
|
$r3$.ɵɵproperty("dir", $r3$.ɵɵpureFunction0(2, $c0$));
|
|
|
|
$r3$.ɵɵadvance(1);
|
|
|
|
$r3$.ɵɵproperty("dir", $r3$.ɵɵpureFunction1(4, $c2$, $r3$.ɵɵpureFunction0(3, $c1$)));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
encapsulation: 2
|
|
|
|
});
|
|
|
|
`;
|
|
|
|
|
|
|
|
const result = compile(files, angularFiles);
|
|
|
|
expectEmit(result.source, MyAppDeclaration, 'Invalid component definition');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not share pure functions between null and function calls', () => {
|
|
|
|
const files = {
|
|
|
|
app: {
|
|
|
|
'spec.ts': `
|
|
|
|
import {Component, NgModule} from '@angular/core';
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
template: \`
|
|
|
|
<div [dir]="{foo: null}"></div>
|
|
|
|
<div [dir]="{foo: getFoo()}"></div>
|
|
|
|
\`
|
|
|
|
})
|
|
|
|
export class MyApp {
|
|
|
|
getFoo() {
|
|
|
|
return 'foo!';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@NgModule({declarations: [MyApp]})
|
|
|
|
export class MyModule {}
|
|
|
|
`
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const MyAppDeclaration = `
|
|
|
|
const $c0$ = function () { return { foo: null }; };
|
|
|
|
const $c1$ = function (a0) { return { foo: a0 }; };
|
|
|
|
…
|
|
|
|
MyApp.ɵcmp = $r3$.ɵɵdefineComponent({
|
|
|
|
type: MyApp,
|
|
|
|
selectors: [["ng-component"]],
|
|
|
|
decls: 2,
|
|
|
|
vars: 5,
|
|
|
|
consts: [[${AttributeMarker.Bindings}, "dir"]],
|
|
|
|
template: function MyApp_Template(rf, ctx) {
|
|
|
|
if (rf & 1) {
|
|
|
|
$r3$.ɵɵelement(0, "div", 0);
|
|
|
|
$r3$.ɵɵelement(1, "div", 0);
|
|
|
|
}
|
|
|
|
if (rf & 2) {
|
|
|
|
$r3$.ɵɵproperty("dir", $r3$.ɵɵpureFunction0(2, $c0$));
|
|
|
|
$r3$.ɵɵadvance(1);
|
|
|
|
$r3$.ɵɵproperty("dir", $r3$.ɵɵpureFunction1(3, $c1$, ctx.getFoo()));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
encapsulation: 2
|
|
|
|
});
|
|
|
|
`;
|
|
|
|
|
2019-11-13 08:26:34 -05:00
|
|
|
const result = compile(files, angularFiles);
|
|
|
|
expectEmit(result.source, MyAppDeclaration, 'Invalid component definition');
|
|
|
|
});
|
|
|
|
|
2018-02-05 20:31:12 -05:00
|
|
|
});
|
2018-08-07 15:04:39 -04:00
|
|
|
|
2018-10-26 02:05:15 -04:00
|
|
|
describe('inherited base classes', () => {
|
2019-04-21 11:37:15 -04:00
|
|
|
const directive = {
|
|
|
|
'some.directive.ts': `
|
|
|
|
import {Directive} from '@angular/core';
|
|
|
|
|
|
|
|
@Directive({
|
|
|
|
selector: '[someDir]',
|
|
|
|
})
|
|
|
|
export class SomeDirective { }
|
|
|
|
`
|
|
|
|
};
|
|
|
|
|
2019-10-25 13:45:08 -04:00
|
|
|
it('should add an abstract directive if one or more @Input is present', () => {
|
2018-08-07 15:04:39 -04:00
|
|
|
const files = {
|
|
|
|
app: {
|
|
|
|
'spec.ts': `
|
|
|
|
import {Component, NgModule, Input} from '@angular/core';
|
|
|
|
export class BaseClass {
|
|
|
|
@Input()
|
|
|
|
input1 = 'test';
|
|
|
|
|
|
|
|
@Input('alias2')
|
|
|
|
input2 = 'whatever';
|
|
|
|
}
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'my-component',
|
|
|
|
template: \`<div>{{input1}} {{input2}}</div>\`
|
|
|
|
})
|
|
|
|
export class MyComponent extends BaseClass {
|
|
|
|
}
|
|
|
|
|
|
|
|
@NgModule({
|
|
|
|
declarations: [MyComponent]
|
|
|
|
})
|
|
|
|
export class MyModule {}
|
|
|
|
`
|
|
|
|
}
|
|
|
|
};
|
|
|
|
const expectedOutput = `
|
|
|
|
// ...
|
2019-10-25 13:45:08 -04:00
|
|
|
BaseClass.ɵdir = $r3$.ɵɵdefineDirective({
|
|
|
|
type: BaseClass,
|
2018-08-07 15:04:39 -04:00
|
|
|
inputs: {
|
|
|
|
input1: "input1",
|
|
|
|
input2: ["alias2", "input2"]
|
|
|
|
}
|
|
|
|
});
|
|
|
|
// ...
|
|
|
|
`;
|
|
|
|
const result = compile(files, angularFiles);
|
2019-10-27 05:59:23 -04:00
|
|
|
expectEmit(result.source, expectedOutput, 'Invalid directive definition');
|
2018-08-07 15:04:39 -04:00
|
|
|
});
|
|
|
|
|
2019-10-25 13:45:08 -04:00
|
|
|
it('should add an abstract directive if one or more @Output is present', () => {
|
2018-08-07 15:04:39 -04:00
|
|
|
const files = {
|
|
|
|
app: {
|
|
|
|
'spec.ts': `
|
|
|
|
import {Component, NgModule, Output, EventEmitter} from '@angular/core';
|
|
|
|
export class BaseClass {
|
|
|
|
@Output()
|
|
|
|
output1 = new EventEmitter<string>();
|
|
|
|
|
|
|
|
@Output()
|
|
|
|
output2 = new EventEmitter<string>();
|
|
|
|
|
|
|
|
clicked() {
|
|
|
|
this.output1.emit('test');
|
|
|
|
this.output2.emit('test');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'my-component',
|
|
|
|
template: \`<button (click)="clicked()">Click Me</button>\`
|
|
|
|
})
|
|
|
|
export class MyComponent extends BaseClass {
|
|
|
|
}
|
|
|
|
|
|
|
|
@NgModule({
|
|
|
|
declarations: [MyComponent]
|
|
|
|
})
|
|
|
|
export class MyModule {}
|
|
|
|
`
|
|
|
|
}
|
|
|
|
};
|
|
|
|
const expectedOutput = `
|
|
|
|
// ...
|
2019-10-25 13:45:08 -04:00
|
|
|
BaseClass.ɵdir = $r3$.ɵɵdefineDirective({
|
|
|
|
type: BaseClass,
|
2018-08-07 15:04:39 -04:00
|
|
|
outputs: {
|
|
|
|
output1: "output1",
|
|
|
|
output2: "output2"
|
|
|
|
}
|
|
|
|
});
|
|
|
|
// ...
|
|
|
|
`;
|
|
|
|
const result = compile(files, angularFiles);
|
2019-10-27 05:59:23 -04:00
|
|
|
expectEmit(result.source, expectedOutput, 'Invalid directive definition');
|
2018-08-07 15:04:39 -04:00
|
|
|
});
|
|
|
|
|
2019-10-25 13:45:08 -04:00
|
|
|
it('should add an abstract directive if a mixture of @Input and @Output props are present',
|
|
|
|
() => {
|
|
|
|
const files = {
|
|
|
|
app: {
|
|
|
|
'spec.ts': `
|
2018-08-07 15:04:39 -04:00
|
|
|
import {Component, NgModule, Input, Output, EventEmitter} from '@angular/core';
|
|
|
|
export class BaseClass {
|
|
|
|
@Output()
|
|
|
|
output1 = new EventEmitter<string>();
|
|
|
|
|
|
|
|
@Output()
|
|
|
|
output2 = new EventEmitter<string>();
|
|
|
|
|
|
|
|
@Input()
|
|
|
|
input1 = 'test';
|
|
|
|
|
|
|
|
@Input('whatever')
|
|
|
|
input2 = 'blah';
|
|
|
|
|
|
|
|
clicked() {
|
|
|
|
this.output1.emit('test');
|
|
|
|
this.output2.emit('test');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'my-component',
|
|
|
|
template: \`<button (click)="clicked()">Click Me</button>\`
|
|
|
|
})
|
|
|
|
export class MyComponent extends BaseClass {
|
|
|
|
}
|
|
|
|
|
|
|
|
@NgModule({
|
|
|
|
declarations: [MyComponent]
|
|
|
|
})
|
|
|
|
export class MyModule {}
|
|
|
|
`
|
2019-10-25 13:45:08 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
const expectedOutput = `
|
2018-08-07 15:04:39 -04:00
|
|
|
// ...
|
2019-10-25 13:45:08 -04:00
|
|
|
BaseClass.ɵdir = $r3$.ɵɵdefineDirective({
|
|
|
|
type: BaseClass,
|
2018-08-07 15:04:39 -04:00
|
|
|
inputs: {
|
|
|
|
input1: "input1",
|
|
|
|
input2: ["whatever", "input2"]
|
|
|
|
},
|
|
|
|
outputs: {
|
|
|
|
output1: "output1",
|
|
|
|
output2: "output2"
|
|
|
|
}
|
|
|
|
});
|
|
|
|
// ...
|
|
|
|
`;
|
2019-10-25 13:45:08 -04:00
|
|
|
const result = compile(files, angularFiles);
|
2019-10-27 05:59:23 -04:00
|
|
|
expectEmit(result.source, expectedOutput, 'Invalid directive definition');
|
2019-10-25 13:45:08 -04:00
|
|
|
});
|
2018-08-07 15:04:39 -04:00
|
|
|
|
2019-10-25 13:45:08 -04:00
|
|
|
it('should add an abstract directive if a ViewChild query is present', () => {
|
2019-04-21 11:37:15 -04:00
|
|
|
const files = {
|
|
|
|
app: {
|
|
|
|
'spec.ts': `
|
|
|
|
import {Component, NgModule, ViewChild} from '@angular/core';
|
|
|
|
export class BaseClass {
|
|
|
|
@ViewChild('something') something: any;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'my-component',
|
|
|
|
template: ''
|
|
|
|
})
|
|
|
|
export class MyComponent extends BaseClass {
|
|
|
|
}
|
|
|
|
|
|
|
|
@NgModule({
|
|
|
|
declarations: [MyComponent]
|
|
|
|
})
|
|
|
|
export class MyModule {}
|
|
|
|
`
|
|
|
|
}
|
|
|
|
};
|
|
|
|
const expectedOutput = `
|
|
|
|
const $e0_attrs$ = ["something"];
|
|
|
|
// ...
|
2019-10-25 13:45:08 -04:00
|
|
|
BaseClass.ɵdir = $r3$.ɵɵdefineDirective({
|
|
|
|
type: BaseClass,
|
|
|
|
viewQuery: function BaseClass_Query(rf, ctx) {
|
2019-04-21 11:37:15 -04:00
|
|
|
if (rf & 1) {
|
2019-07-20 06:32:29 -04:00
|
|
|
$r3$.ɵɵviewQuery($e0_attrs$, true);
|
2019-04-21 11:37:15 -04:00
|
|
|
}
|
|
|
|
if (rf & 2) {
|
|
|
|
var $tmp$;
|
2019-10-30 19:09:38 -04:00
|
|
|
$r3$.ɵɵqueryRefresh($tmp$ = $r3$.ɵɵloadQuery()) && (ctx.something = $tmp$.first);
|
2019-04-21 11:37:15 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
// ...
|
|
|
|
`;
|
|
|
|
const result = compile(files, angularFiles);
|
2019-10-27 05:59:23 -04:00
|
|
|
expectEmit(result.source, expectedOutput, 'Invalid directive definition');
|
2019-04-21 11:37:15 -04:00
|
|
|
});
|
|
|
|
|
2019-10-25 13:45:08 -04:00
|
|
|
it('should add an abstract directive if a ViewChildren query is present', () => {
|
2019-04-21 11:37:15 -04:00
|
|
|
const files = {
|
|
|
|
app: {
|
|
|
|
...directive,
|
|
|
|
'spec.ts': `
|
|
|
|
import {Component, NgModule, ViewChildren} from '@angular/core';
|
|
|
|
import {SomeDirective} from './some.directive';
|
|
|
|
|
|
|
|
export class BaseClass {
|
|
|
|
@ViewChildren(SomeDirective) something: QueryList<SomeDirective>;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'my-component',
|
|
|
|
template: ''
|
|
|
|
})
|
|
|
|
export class MyComponent extends BaseClass {
|
|
|
|
}
|
|
|
|
|
|
|
|
@NgModule({
|
|
|
|
declarations: [MyComponent, SomeDirective]
|
|
|
|
})
|
|
|
|
export class MyModule {}
|
|
|
|
`
|
|
|
|
}
|
|
|
|
};
|
|
|
|
const expectedOutput = `
|
|
|
|
// ...
|
2019-10-25 13:45:08 -04:00
|
|
|
BaseClass.ɵdir = $r3$.ɵɵdefineDirective({
|
|
|
|
type: BaseClass,
|
|
|
|
viewQuery: function BaseClass_Query(rf, ctx) {
|
2019-04-21 11:37:15 -04:00
|
|
|
if (rf & 1) {
|
2019-07-20 06:32:29 -04:00
|
|
|
$r3$.ɵɵviewQuery(SomeDirective, true);
|
2019-04-21 11:37:15 -04:00
|
|
|
}
|
|
|
|
if (rf & 2) {
|
|
|
|
var $tmp$;
|
2019-10-30 19:09:38 -04:00
|
|
|
$r3$.ɵɵqueryRefresh($tmp$ = $r3$.ɵɵloadQuery()) && (ctx.something = $tmp$);
|
2019-04-21 11:37:15 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
// ...
|
|
|
|
`;
|
|
|
|
const result = compile(files, angularFiles);
|
2019-10-27 05:59:23 -04:00
|
|
|
expectEmit(result.source, expectedOutput, 'Invalid directive definition');
|
2019-04-21 11:37:15 -04:00
|
|
|
});
|
|
|
|
|
2019-10-25 13:45:08 -04:00
|
|
|
it('should add an abstract directive if a ContentChild query is present', () => {
|
2019-04-21 11:37:15 -04:00
|
|
|
const files = {
|
|
|
|
app: {
|
|
|
|
'spec.ts': `
|
|
|
|
import {Component, NgModule, ContentChild} from '@angular/core';
|
|
|
|
export class BaseClass {
|
2019-10-05 04:04:54 -04:00
|
|
|
@ContentChild('something') something: any;
|
2019-04-21 11:37:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'my-component',
|
|
|
|
template: ''
|
|
|
|
})
|
|
|
|
export class MyComponent extends BaseClass {
|
|
|
|
}
|
|
|
|
|
|
|
|
@NgModule({
|
|
|
|
declarations: [MyComponent]
|
|
|
|
})
|
|
|
|
export class MyModule {}
|
|
|
|
`
|
|
|
|
}
|
|
|
|
};
|
|
|
|
const expectedOutput = `
|
|
|
|
const $e0_attrs$ = ["something"];
|
|
|
|
// ...
|
2019-10-25 13:45:08 -04:00
|
|
|
BaseClass.ɵdir = $r3$.ɵɵdefineDirective({
|
|
|
|
type: BaseClass,
|
|
|
|
contentQueries: function BaseClass_ContentQueries(rf, ctx, dirIndex) {
|
2019-04-21 11:37:15 -04:00
|
|
|
if (rf & 1) {
|
2019-07-20 06:32:29 -04:00
|
|
|
$r3$.ɵɵcontentQuery(dirIndex, $e0_attrs$, true);
|
2019-04-21 11:37:15 -04:00
|
|
|
}
|
|
|
|
if (rf & 2) {
|
|
|
|
var $tmp$;
|
2019-10-30 19:09:38 -04:00
|
|
|
$r3$.ɵɵqueryRefresh($tmp$ = $r3$.ɵɵloadQuery()) && (ctx.something = $tmp$.first);
|
2019-04-21 11:37:15 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
// ...
|
|
|
|
`;
|
|
|
|
const result = compile(files, angularFiles);
|
2019-10-27 05:59:23 -04:00
|
|
|
expectEmit(result.source, expectedOutput, 'Invalid directive definition');
|
2019-04-21 11:37:15 -04:00
|
|
|
});
|
|
|
|
|
2019-10-25 13:45:08 -04:00
|
|
|
it('should add an abstract directive if a ContentChildren query is present', () => {
|
2019-04-21 11:37:15 -04:00
|
|
|
const files = {
|
|
|
|
app: {
|
|
|
|
...directive,
|
|
|
|
'spec.ts': `
|
|
|
|
import {Component, NgModule, ContentChildren} from '@angular/core';
|
|
|
|
import {SomeDirective} from './some.directive';
|
|
|
|
|
|
|
|
export class BaseClass {
|
|
|
|
@ContentChildren(SomeDirective) something: QueryList<SomeDirective>;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'my-component',
|
|
|
|
template: ''
|
|
|
|
})
|
|
|
|
export class MyComponent extends BaseClass {
|
|
|
|
}
|
|
|
|
|
|
|
|
@NgModule({
|
|
|
|
declarations: [MyComponent, SomeDirective]
|
|
|
|
})
|
|
|
|
export class MyModule {}
|
|
|
|
`
|
|
|
|
}
|
|
|
|
};
|
|
|
|
const expectedOutput = `
|
|
|
|
// ...
|
2019-10-25 13:45:08 -04:00
|
|
|
BaseClass.ɵdir = $r3$.ɵɵdefineDirective({
|
|
|
|
type: BaseClass,
|
|
|
|
contentQueries: function BaseClass_ContentQueries(rf, ctx, dirIndex) {
|
2019-04-21 11:37:15 -04:00
|
|
|
if (rf & 1) {
|
2019-07-20 06:32:29 -04:00
|
|
|
$r3$.ɵɵcontentQuery(dirIndex, SomeDirective, false);
|
2019-04-21 11:37:15 -04:00
|
|
|
}
|
|
|
|
if (rf & 2) {
|
|
|
|
var $tmp$;
|
2019-10-30 19:09:38 -04:00
|
|
|
$r3$.ɵɵqueryRefresh($tmp$ = $r3$.ɵɵloadQuery()) && (ctx.something = $tmp$);
|
2019-04-21 11:37:15 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
// ...
|
|
|
|
`;
|
|
|
|
const result = compile(files, angularFiles);
|
2019-10-27 05:59:23 -04:00
|
|
|
expectEmit(result.source, expectedOutput, 'Invalid directive definition');
|
2019-04-21 11:37:15 -04:00
|
|
|
});
|
|
|
|
|
2019-10-25 13:45:08 -04:00
|
|
|
it('should add an abstract directive if a host binding is present', () => {
|
2019-04-27 03:33:10 -04:00
|
|
|
const files = {
|
|
|
|
app: {
|
|
|
|
'spec.ts': `
|
|
|
|
import {Component, NgModule, HostBinding} from '@angular/core';
|
|
|
|
export class BaseClass {
|
|
|
|
@HostBinding('attr.tabindex')
|
|
|
|
tabindex = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'my-component',
|
|
|
|
template: ''
|
|
|
|
})
|
|
|
|
export class MyComponent extends BaseClass {
|
|
|
|
}
|
|
|
|
|
|
|
|
@NgModule({
|
|
|
|
declarations: [MyComponent]
|
|
|
|
})
|
|
|
|
export class MyModule {}
|
|
|
|
`
|
|
|
|
}
|
|
|
|
};
|
|
|
|
const expectedOutput = `
|
|
|
|
// ...
|
2019-10-25 13:45:08 -04:00
|
|
|
BaseClass.ɵdir = $r3$.ɵɵdefineDirective({
|
|
|
|
type: BaseClass,
|
2020-01-08 14:32:33 -05:00
|
|
|
hostVars: 1,
|
2020-01-25 06:38:42 -05:00
|
|
|
hostBindings: function BaseClass_HostBindings(rf, ctx) {
|
2019-04-27 03:33:10 -04:00
|
|
|
if (rf & 2) {
|
2019-05-15 23:21:24 -04:00
|
|
|
$r3$.ɵɵattribute("tabindex", ctx.tabindex);
|
2019-04-27 03:33:10 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
// ...
|
|
|
|
`;
|
|
|
|
const result = compile(files, angularFiles);
|
2019-10-27 05:59:23 -04:00
|
|
|
expectEmit(result.source, expectedOutput, 'Invalid directive definition');
|
2019-04-27 03:33:10 -04:00
|
|
|
});
|
|
|
|
|
2019-10-25 13:45:08 -04:00
|
|
|
it('should add an abstract directive if a host listener is present', () => {
|
2019-04-27 03:33:10 -04:00
|
|
|
const files = {
|
|
|
|
app: {
|
|
|
|
'spec.ts': `
|
|
|
|
import {Component, NgModule, HostListener} from '@angular/core';
|
|
|
|
export class BaseClass {
|
|
|
|
@HostListener('mousedown', ['$event'])
|
|
|
|
handleMousedown(event: any) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'my-component',
|
|
|
|
template: ''
|
|
|
|
})
|
|
|
|
export class MyComponent extends BaseClass {
|
|
|
|
}
|
|
|
|
|
|
|
|
@NgModule({
|
|
|
|
declarations: [MyComponent]
|
|
|
|
})
|
|
|
|
export class MyModule {}
|
|
|
|
`
|
|
|
|
}
|
|
|
|
};
|
|
|
|
const expectedOutput = `
|
|
|
|
// ...
|
2019-10-25 13:45:08 -04:00
|
|
|
BaseClass.ɵdir = $r3$.ɵɵdefineDirective({
|
|
|
|
type: BaseClass,
|
2020-01-25 06:38:42 -05:00
|
|
|
hostBindings: function BaseClass_HostBindings(rf, ctx) {
|
2019-04-27 03:33:10 -04:00
|
|
|
if (rf & 1) {
|
2019-10-25 13:45:08 -04:00
|
|
|
$r3$.ɵɵlistener("mousedown", function BaseClass_mousedown_HostBindingHandler($event) {
|
2019-04-27 03:33:10 -04:00
|
|
|
return ctx.handleMousedown($event);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
// ...
|
|
|
|
`;
|
|
|
|
const result = compile(files, angularFiles);
|
2019-10-27 05:59:23 -04:00
|
|
|
expectEmit(result.source, expectedOutput, 'Invalid directive definition');
|
2019-04-27 03:33:10 -04:00
|
|
|
});
|
|
|
|
|
2019-10-25 13:45:08 -04:00
|
|
|
it('should add an abstract directive when using any lifecycle hook', () => {
|
|
|
|
const files = {
|
|
|
|
app: {
|
|
|
|
'spec.ts': `
|
|
|
|
import {Component, NgModule, Input} from '@angular/core';
|
|
|
|
export class BaseClass {
|
|
|
|
ngAfterContentChecked() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'my-component',
|
|
|
|
template: \`<div>{{input1}} {{input2}}</div>\`
|
|
|
|
})
|
|
|
|
export class MyComponent extends BaseClass {
|
|
|
|
}
|
|
|
|
|
|
|
|
@NgModule({
|
|
|
|
declarations: [MyComponent]
|
|
|
|
})
|
|
|
|
export class MyModule {}
|
|
|
|
`
|
|
|
|
}
|
|
|
|
};
|
|
|
|
const expectedOutput = `
|
|
|
|
// ...
|
|
|
|
BaseClass.ɵdir = $r3$.ɵɵdefineDirective({
|
2019-10-27 05:59:23 -04:00
|
|
|
type: BaseClass
|
2019-10-25 13:45:08 -04:00
|
|
|
});
|
|
|
|
// ...
|
|
|
|
`;
|
|
|
|
const result = compile(files, angularFiles);
|
2019-10-27 05:59:23 -04:00
|
|
|
expectEmit(result.source, expectedOutput, 'Invalid directive definition');
|
2019-10-25 13:45:08 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
it('should add an abstract directive when using ngOnChanges', () => {
|
|
|
|
const files = {
|
|
|
|
app: {
|
|
|
|
'spec.ts': `
|
|
|
|
import {Component, NgModule, Input} from '@angular/core';
|
|
|
|
export class BaseClass {
|
|
|
|
ngOnChanges() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'my-component',
|
|
|
|
template: \`<div>{{input1}} {{input2}}</div>\`
|
|
|
|
})
|
|
|
|
export class MyComponent extends BaseClass {
|
|
|
|
}
|
|
|
|
|
|
|
|
@NgModule({
|
|
|
|
declarations: [MyComponent]
|
|
|
|
})
|
|
|
|
export class MyModule {}
|
|
|
|
`
|
|
|
|
}
|
|
|
|
};
|
|
|
|
const expectedOutput = `
|
|
|
|
// ...
|
|
|
|
BaseClass.ɵdir = $r3$.ɵɵdefineDirective({
|
|
|
|
type: BaseClass,
|
2020-02-21 14:34:39 -05:00
|
|
|
features: [$r3$.ɵɵNgOnChangesFeature]
|
2019-10-25 13:45:08 -04:00
|
|
|
});
|
|
|
|
// ...
|
|
|
|
`;
|
|
|
|
const result = compile(files, angularFiles);
|
2019-10-27 05:59:23 -04:00
|
|
|
expectEmit(result.source, expectedOutput, 'Invalid directive definition');
|
2019-10-25 13:45:08 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should NOT add an abstract directive if @Component is present', () => {
|
2018-08-07 15:04:39 -04:00
|
|
|
const files = {
|
|
|
|
app: {
|
|
|
|
'spec.ts': `
|
|
|
|
import {Component, NgModule, Output, EventEmitter} from '@angular/core';
|
|
|
|
@Component({
|
|
|
|
selector: 'whatever',
|
|
|
|
template: '<button (click)="clicked()">Click {{input1}}</button>'
|
|
|
|
})
|
|
|
|
export class BaseClass {
|
|
|
|
@Output()
|
|
|
|
output1 = new EventEmitter<string>();
|
|
|
|
|
|
|
|
@Input()
|
|
|
|
input1 = 'whatever';
|
|
|
|
|
|
|
|
clicked() {
|
|
|
|
this.output1.emit('test');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'my-component',
|
|
|
|
template: \`<div>What is this developer doing?</div>\`
|
|
|
|
})
|
|
|
|
export class MyComponent extends BaseClass {
|
|
|
|
}
|
|
|
|
|
|
|
|
@NgModule({
|
|
|
|
declarations: [MyComponent]
|
|
|
|
})
|
|
|
|
export class MyModule {}
|
|
|
|
`
|
|
|
|
}
|
|
|
|
};
|
|
|
|
const result = compile(files, angularFiles);
|
2019-10-25 13:45:08 -04:00
|
|
|
expect(result.source).not.toContain('ɵdir');
|
2018-08-07 15:04:39 -04:00
|
|
|
});
|
|
|
|
|
2019-10-25 13:45:08 -04:00
|
|
|
it('should NOT add an abstract directive if @Directive is present', () => {
|
2018-08-07 15:04:39 -04:00
|
|
|
const files = {
|
|
|
|
app: {
|
|
|
|
'spec.ts': `
|
|
|
|
import {Component, Directive, NgModule, Output, EventEmitter} from '@angular/core';
|
|
|
|
@Directive({
|
|
|
|
selector: 'whatever',
|
|
|
|
})
|
|
|
|
export class BaseClass {
|
|
|
|
@Output()
|
|
|
|
output1 = new EventEmitter<string>();
|
|
|
|
|
|
|
|
@Input()
|
|
|
|
input1 = 'whatever';
|
|
|
|
|
|
|
|
clicked() {
|
|
|
|
this.output1.emit('test');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'my-component',
|
|
|
|
template: '<button (click)="clicked()">Click {{input1}}</button>'
|
|
|
|
})
|
|
|
|
export class MyComponent extends BaseClass {
|
|
|
|
}
|
|
|
|
|
|
|
|
@NgModule({
|
|
|
|
declarations: [MyComponent]
|
|
|
|
})
|
|
|
|
export class MyModule {}
|
|
|
|
`
|
|
|
|
}
|
|
|
|
};
|
|
|
|
const result = compile(files, angularFiles);
|
2019-10-25 13:45:08 -04:00
|
|
|
expect(result.source.match(/ɵdir/g) !.length).toBe(1);
|
2018-08-07 15:04:39 -04:00
|
|
|
});
|
|
|
|
});
|
2018-03-26 00:32:39 -04:00
|
|
|
});
|