/** * @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 */ import {AttributeMarker, InitialStylingFlags, ViewEncapsulation} from '@angular/compiler/src/core'; import {setup} from '@angular/compiler/test/aot/test_util'; import {compile, expectEmit} from './mock_compile'; describe('compiler compliance: styling', () => { const angularFiles = setup({ compileAngular: false, compileFakeCore: true, compileAnimations: false, }); describe('@Component.styles', () => { it('should pass in the component metadata styles into the component definition and shim them using style encapsulation', () => { const files = { app: { 'spec.ts': ` import {Component, NgModule} from '@angular/core'; @Component({ selector: "my-component", styles: ["div.foo { color: red; }", ":host p:nth-child(even) { --webkit-transition: 1s linear all; }"], template: "..." }) export class MyComponent { } @NgModule({declarations: [MyComponent]}) export class MyModule {} ` } }; const template = 'styles: ["div.foo[_ngcontent-%COMP%] { color: red; }", "[_nghost-%COMP%] p[_ngcontent-%COMP%]:nth-child(even) { --webkit-transition: 1s linear all; }"]'; const result = compile(files, angularFiles); expectEmit(result.source, template, 'Incorrect template'); }); it('should pass in styles, but skip shimming the styles if the view encapsulation signals not to', () => { const files = { app: { 'spec.ts': ` import {Component, NgModule} from '@angular/core'; @Component({ selector: "my-component", encapsulation: ${ViewEncapsulation.None}, styles: ["div.tall { height: 123px; }", ":host.small p { height:5px; }"], template: "..." }) export class MyComponent { } @NgModule({declarations: [MyComponent]}) export class MyModule {} ` } }; const template = 'div.tall { height: 123px; }", ":host.small p { height:5px; }'; const result = compile(files, angularFiles); expectEmit(result.source, template, 'Incorrect template'); }); it('should pass in the component metadata styles into the component definition but skip shimming when style encapsulation is set to native', () => { const files = { app: { 'spec.ts': ` import {Component, NgModule} from '@angular/core'; @Component({ encapsulation: ${ViewEncapsulation.Native}, selector: "my-component", styles: ["div.cool { color: blue; }", ":host.nice p { color: gold; }"], template: "..." }) export class MyComponent { } @NgModule({declarations: [MyComponent]}) export class MyModule {} ` } }; const template = ` MyComponent.ngComponentDef = $r3$.ɵdefineComponent({ … styles: ["div.cool { color: blue; }", ":host.nice p { color: gold; }"], encapsulation: 1 }) `; const result = compile(files, angularFiles); expectEmit(result.source, template, 'Incorrect template'); }); }); describe('@Component.animations', () => { it('should pass in the component metadata animations into the component definition', () => { const files = { app: { 'spec.ts': ` import {Component, NgModule} from '@angular/core'; @Component({ selector: "my-component", animations: [{name: 'foo123'}, {name: 'trigger123'}], template: "" }) export class MyComponent { } @NgModule({declarations: [MyComponent]}) export class MyModule {} ` } }; const template = ` MyComponent.ngComponentDef = $r3$.ɵdefineComponent({ type: MyComponent, selectors:[["my-component"]], factory:function MyComponent_Factory(t){ return new (t || MyComponent)(); }, consts: 0, vars: 0, template: function MyComponent_Template(rf, $ctx$) { }, encapsulation: 2, data: { animation: [{name: 'foo123'}, {name: 'trigger123'}] } }); `; const result = compile(files, angularFiles); expectEmit(result.source, template, 'Incorrect template'); }); it('should include animations even if the provided array is empty', () => { const files = { app: { 'spec.ts': ` import {Component, NgModule} from '@angular/core'; @Component({ selector: "my-component", animations: [], template: "" }) export class MyComponent { } @NgModule({declarations: [MyComponent]}) export class MyModule {} ` } }; const template = ` MyComponent.ngComponentDef = $r3$.ɵdefineComponent({ type: MyComponent, selectors:[["my-component"]], factory:function MyComponent_Factory(t){ return new (t || MyComponent)(); }, consts: 0, vars: 0, template: function MyComponent_Template(rf, $ctx$) { }, encapsulation: 2, data: { animation: [] } }); `; const result = compile(files, angularFiles); expectEmit(result.source, template, 'Incorrect template'); }); it('should generate any animation triggers into the component template', () => { const files = { app: { 'spec.ts': ` import {Component, NgModule} from '@angular/core'; @Component({ selector: "my-component", template: \`
\`, }) export class MyComponent { } @NgModule({declarations: [MyComponent]}) export class MyModule {} ` } }; const template = ` const $e0_attrs$ = ["@foo", ""]; const $e1_attrs$ = ["@bar", ""]; const $e2_attrs$ = ["@baz", ""]; … MyComponent.ngComponentDef = $r3$.ɵdefineComponent({ … consts: 3, vars: 1, template: function MyComponent_Template(rf, $ctx$) { if (rf & 1) { $r3$.ɵelement(0, "div", $e0_attrs$); $r3$.ɵelement(1, "div", $e1_attrs$); $r3$.ɵelement(2, "div", $e2_attrs$); } if (rf & 2) { $r3$.ɵelementProperty(0, "@foo", $r3$.ɵbind(ctx.exp)); } }, encapsulation: 2 }); `; const result = compile(files, angularFiles); expectEmit(result.source, template, 'Incorrect template'); }); }); describe('[style] and [style.prop]', () => { it('should create style instructions on the element', () => { const files = { app: { 'spec.ts': ` import {Component, NgModule} from '@angular/core'; @Component({ selector: 'my-component', template: \`\` }) export class MyComponent { myStyleExp = [{color:'red'}, {color:'blue', duration:1000}] } @NgModule({declarations: [MyComponent]}) export class MyModule {} ` } }; const template = ` template: function MyComponent_Template(rf, $ctx$) { if (rf & 1) { $r3$.ɵelementStart(0, "div"); $r3$.ɵelementStyling(null, null, $r3$.ɵdefaultStyleSanitizer); $r3$.ɵelementEnd(); } if (rf & 2) { $r3$.ɵelementStylingMap(0, null, $ctx$.myStyleExp); $r3$.ɵelementStylingApply(0); } } `; const result = compile(files, angularFiles); expectEmit(result.source, template, 'Incorrect template'); }); it('should place initial, multi, singular and application followed by attribute style instructions in the template code in that order', () => { const files = { app: { 'spec.ts': ` import {Component, NgModule} from '@angular/core'; @Component({ selector: 'my-component', template: \`\` }) export class MyComponent { myStyleExp = [{color:'red'}, {color:'blue', duration:1000}] myWidth = '100px'; myHeight = '100px'; } @NgModule({declarations: [MyComponent]}) export class MyModule {} ` } }; const template = ` const $e0_attrs$ = [${AttributeMarker.SelectOnly}, "style"]; const $e0_styling$ = ["opacity","width","height",${InitialStylingFlags.VALUES_MODE},"opacity","1"]; … MyComponent.ngComponentDef = $r3$.ɵdefineComponent({ type: MyComponent, selectors:[["my-component"]], factory:function MyComponent_Factory(t){ return new (t || MyComponent)(); }, consts: 1, vars: 1, template: function MyComponent_Template(rf, $ctx$) { if (rf & 1) { $r3$.ɵelementStart(0, "div", $e0_attrs$); $r3$.ɵelementStyling(null, $e0_styling$, $r3$.ɵdefaultStyleSanitizer); $r3$.ɵelementEnd(); } if (rf & 2) { $r3$.ɵelementStylingMap(0, null, $ctx$.myStyleExp); $r3$.ɵelementStyleProp(0, 1, $ctx$.myWidth); $r3$.ɵelementStyleProp(0, 2, $ctx$.myHeight); $r3$.ɵelementStylingApply(0); $r3$.ɵelementAttribute(0, "style", $r3$.ɵbind("border-width: 10px"), $r3$.ɵsanitizeStyle); } }, encapsulation: 2 }); `; const result = compile(files, angularFiles); expectEmit(result.source, template, 'Incorrect template'); }); it('should assign a sanitizer instance to the element style allocation instruction if any url-based properties are detected', () => { const files = { app: { 'spec.ts': ` import {Component, NgModule} from '@angular/core'; @Component({ selector: 'my-component', template: \`