2017-12-01 17:23:03 -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
|
|
|
|
*/
|
|
|
|
|
2017-12-11 10:30:46 -05:00
|
|
|
import {ViewEncapsulation} from '../../src/core';
|
2018-02-06 19:11:20 -05:00
|
|
|
import {defineComponent, markDirty} from '../../src/render3/index';
|
2018-02-06 20:27:16 -05:00
|
|
|
import {bind, componentRefresh, container, containerRefreshEnd, containerRefreshStart, elementEnd, elementProperty, elementStart, embeddedViewEnd, embeddedViewStart, text, textBinding} from '../../src/render3/instructions';
|
2018-01-03 04:45:09 -05:00
|
|
|
import {createRendererType2} from '../../src/view/index';
|
2017-12-01 17:23:03 -05:00
|
|
|
|
2017-12-11 10:30:46 -05:00
|
|
|
import {getRendererFactory2} from './imported_renderer2';
|
2018-01-25 09:32:21 -05:00
|
|
|
import {containerEl, renderComponent, renderToHtml, requestAnimationFrame, toHtml} from './render_util';
|
2017-12-01 17:23:03 -05:00
|
|
|
|
|
|
|
describe('component', () => {
|
|
|
|
class CounterComponent {
|
|
|
|
count = 0;
|
|
|
|
|
|
|
|
increment() { this.count++; }
|
|
|
|
|
|
|
|
static ngComponentDef = defineComponent({
|
2018-01-22 18:27:21 -05:00
|
|
|
type: CounterComponent,
|
2017-12-01 17:23:03 -05:00
|
|
|
tag: 'counter',
|
|
|
|
template: function(ctx: CounterComponent, cm: boolean) {
|
|
|
|
if (cm) {
|
2018-02-06 19:11:20 -05:00
|
|
|
text(0);
|
2017-12-01 17:23:03 -05:00
|
|
|
}
|
2018-02-06 19:11:20 -05:00
|
|
|
textBinding(0, bind(ctx.count));
|
2017-12-01 17:23:03 -05:00
|
|
|
},
|
|
|
|
factory: () => new CounterComponent,
|
|
|
|
inputs: {count: 'count'},
|
|
|
|
methods: {increment: 'increment'}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
describe('renderComponent', () => {
|
|
|
|
it('should render on initial call', () => {
|
|
|
|
renderComponent(CounterComponent);
|
2018-01-03 05:42:48 -05:00
|
|
|
expect(toHtml(containerEl)).toEqual('0');
|
2017-12-01 17:23:03 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should re-render on input change or method invocation', () => {
|
|
|
|
const component = renderComponent(CounterComponent);
|
2018-01-03 05:42:48 -05:00
|
|
|
expect(toHtml(containerEl)).toEqual('0');
|
2017-12-01 17:23:03 -05:00
|
|
|
component.count = 123;
|
|
|
|
markDirty(component, requestAnimationFrame);
|
2018-01-03 05:42:48 -05:00
|
|
|
expect(toHtml(containerEl)).toEqual('0');
|
2017-12-01 17:23:03 -05:00
|
|
|
requestAnimationFrame.flush();
|
2018-01-03 05:42:48 -05:00
|
|
|
expect(toHtml(containerEl)).toEqual('123');
|
2017-12-01 17:23:03 -05:00
|
|
|
component.increment();
|
|
|
|
markDirty(component, requestAnimationFrame);
|
2018-01-03 05:42:48 -05:00
|
|
|
expect(toHtml(containerEl)).toEqual('123');
|
2017-12-01 17:23:03 -05:00
|
|
|
requestAnimationFrame.flush();
|
2018-01-03 05:42:48 -05:00
|
|
|
expect(toHtml(containerEl)).toEqual('124');
|
2017-12-01 17:23:03 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
2017-12-11 10:30:46 -05:00
|
|
|
|
2018-01-25 09:32:21 -05:00
|
|
|
describe('component with a container', () => {
|
|
|
|
|
|
|
|
function showItems(ctx: {items: string[]}, cm: boolean) {
|
|
|
|
if (cm) {
|
2018-02-06 19:11:20 -05:00
|
|
|
container(0);
|
2018-01-25 09:32:21 -05:00
|
|
|
}
|
2018-02-06 19:11:20 -05:00
|
|
|
containerRefreshStart(0);
|
2018-01-25 09:32:21 -05:00
|
|
|
{
|
|
|
|
for (const item of ctx.items) {
|
2018-02-06 20:27:16 -05:00
|
|
|
const cm0 = embeddedViewStart(0);
|
2018-01-25 09:32:21 -05:00
|
|
|
{
|
|
|
|
if (cm0) {
|
2018-02-06 19:11:20 -05:00
|
|
|
text(0);
|
2018-01-25 09:32:21 -05:00
|
|
|
}
|
2018-02-06 19:11:20 -05:00
|
|
|
textBinding(0, bind(item));
|
2018-01-25 09:32:21 -05:00
|
|
|
}
|
2018-02-06 20:27:16 -05:00
|
|
|
embeddedViewEnd();
|
2018-01-25 09:32:21 -05:00
|
|
|
}
|
|
|
|
}
|
2018-02-06 19:11:20 -05:00
|
|
|
containerRefreshEnd();
|
2018-01-25 09:32:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
class WrapperComponent {
|
|
|
|
items: string[];
|
|
|
|
static ngComponentDef = defineComponent({
|
|
|
|
type: WrapperComponent,
|
|
|
|
tag: 'wrapper',
|
|
|
|
template: function ChildComponentTemplate(ctx: {items: string[]}, cm: boolean) {
|
|
|
|
if (cm) {
|
2018-02-06 19:11:20 -05:00
|
|
|
container(0);
|
2018-01-25 09:32:21 -05:00
|
|
|
}
|
2018-02-06 19:11:20 -05:00
|
|
|
containerRefreshStart(0);
|
2018-01-25 09:32:21 -05:00
|
|
|
{
|
2018-02-06 20:27:16 -05:00
|
|
|
const cm0 = embeddedViewStart(0);
|
2018-01-25 09:32:21 -05:00
|
|
|
{ showItems({items: ctx.items}, cm0); }
|
2018-02-06 20:27:16 -05:00
|
|
|
embeddedViewEnd();
|
2018-01-25 09:32:21 -05:00
|
|
|
}
|
2018-02-06 19:11:20 -05:00
|
|
|
containerRefreshEnd();
|
2018-01-25 09:32:21 -05:00
|
|
|
},
|
|
|
|
factory: () => new WrapperComponent,
|
|
|
|
inputs: {items: 'items'}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function template(ctx: {items: string[]}, cm: boolean) {
|
|
|
|
if (cm) {
|
2018-02-06 19:11:20 -05:00
|
|
|
elementStart(0, WrapperComponent);
|
|
|
|
elementEnd();
|
2018-01-25 09:32:21 -05:00
|
|
|
}
|
2018-02-06 19:11:20 -05:00
|
|
|
elementProperty(0, 'items', bind(ctx.items));
|
2018-01-25 09:32:21 -05:00
|
|
|
WrapperComponent.ngComponentDef.h(1, 0);
|
2018-02-06 19:11:20 -05:00
|
|
|
componentRefresh(1, 0);
|
2018-01-25 09:32:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
it('should re-render on input change', () => {
|
|
|
|
const ctx: {items: string[]} = {items: ['a']};
|
|
|
|
expect(renderToHtml(template, ctx)).toEqual('<wrapper>a</wrapper>');
|
|
|
|
|
|
|
|
ctx.items = [...ctx.items, 'b'];
|
|
|
|
expect(renderToHtml(template, ctx)).toEqual('<wrapper>ab</wrapper>');
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2017-12-11 10:30:46 -05:00
|
|
|
// TODO: add tests with Native once tests are run in real browser (domino doesn't support shadow
|
|
|
|
// root)
|
|
|
|
describe('encapsulation', () => {
|
|
|
|
class WrapperComponent {
|
|
|
|
static ngComponentDef = defineComponent({
|
2018-01-22 18:27:21 -05:00
|
|
|
type: WrapperComponent,
|
2017-12-11 10:30:46 -05:00
|
|
|
tag: 'wrapper',
|
|
|
|
template: function(ctx: WrapperComponent, cm: boolean) {
|
|
|
|
if (cm) {
|
2018-02-06 19:11:20 -05:00
|
|
|
elementStart(0, EncapsulatedComponent);
|
|
|
|
elementEnd();
|
2017-12-11 10:30:46 -05:00
|
|
|
}
|
2017-12-20 19:26:07 -05:00
|
|
|
EncapsulatedComponent.ngComponentDef.h(1, 0);
|
2018-02-06 19:11:20 -05:00
|
|
|
componentRefresh(1, 0);
|
2017-12-11 10:30:46 -05:00
|
|
|
},
|
|
|
|
factory: () => new WrapperComponent,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
class EncapsulatedComponent {
|
|
|
|
static ngComponentDef = defineComponent({
|
2018-01-22 18:27:21 -05:00
|
|
|
type: EncapsulatedComponent,
|
2017-12-11 10:30:46 -05:00
|
|
|
tag: 'encapsulated',
|
|
|
|
template: function(ctx: EncapsulatedComponent, cm: boolean) {
|
|
|
|
if (cm) {
|
2018-02-06 19:11:20 -05:00
|
|
|
text(0, 'foo');
|
|
|
|
elementStart(1, LeafComponent);
|
|
|
|
elementEnd();
|
2017-12-11 10:30:46 -05:00
|
|
|
}
|
2017-12-20 19:26:07 -05:00
|
|
|
LeafComponent.ngComponentDef.h(2, 1);
|
2018-02-06 19:11:20 -05:00
|
|
|
componentRefresh(2, 1);
|
2017-12-11 10:30:46 -05:00
|
|
|
},
|
|
|
|
factory: () => new EncapsulatedComponent,
|
|
|
|
rendererType:
|
|
|
|
createRendererType2({encapsulation: ViewEncapsulation.Emulated, styles: [], data: {}}),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
class LeafComponent {
|
|
|
|
static ngComponentDef = defineComponent({
|
2018-01-22 18:27:21 -05:00
|
|
|
type: LeafComponent,
|
2017-12-11 10:30:46 -05:00
|
|
|
tag: 'leaf',
|
|
|
|
template: function(ctx: LeafComponent, cm: boolean) {
|
|
|
|
if (cm) {
|
2018-02-06 19:11:20 -05:00
|
|
|
elementStart(0, 'span');
|
|
|
|
{ text(1, 'bar'); }
|
|
|
|
elementEnd();
|
2017-12-11 10:30:46 -05:00
|
|
|
}
|
|
|
|
},
|
|
|
|
factory: () => new LeafComponent,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
it('should encapsulate children, but not host nor grand children', () => {
|
|
|
|
renderComponent(WrapperComponent, getRendererFactory2(document));
|
|
|
|
expect(containerEl.outerHTML)
|
2018-01-03 04:45:09 -05:00
|
|
|
.toMatch(
|
|
|
|
/<div host=""><encapsulated _nghost-c(\d+)="">foo<leaf _ngcontent-c\1=""><span>bar<\/span><\/leaf><\/encapsulated><\/div>/);
|
2017-12-11 10:30:46 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should encapsulate host', () => {
|
|
|
|
renderComponent(EncapsulatedComponent, getRendererFactory2(document));
|
|
|
|
expect(containerEl.outerHTML)
|
2018-01-03 04:45:09 -05:00
|
|
|
.toMatch(
|
|
|
|
/<div host="" _nghost-c(\d+)="">foo<leaf _ngcontent-c\1=""><span>bar<\/span><\/leaf><\/div>/);
|
2017-12-11 10:30:46 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should encapsulate host and children with different attributes', () => {
|
|
|
|
class WrapperComponentWith {
|
|
|
|
static ngComponentDef = defineComponent({
|
2018-01-22 18:27:21 -05:00
|
|
|
type: WrapperComponentWith,
|
2017-12-11 10:30:46 -05:00
|
|
|
tag: 'wrapper',
|
|
|
|
template: function(ctx: WrapperComponentWith, cm: boolean) {
|
|
|
|
if (cm) {
|
2018-02-06 19:11:20 -05:00
|
|
|
elementStart(0, LeafComponentwith);
|
|
|
|
elementEnd();
|
2017-12-11 10:30:46 -05:00
|
|
|
}
|
2017-12-20 19:26:07 -05:00
|
|
|
LeafComponentwith.ngComponentDef.h(1, 0);
|
2018-02-06 19:11:20 -05:00
|
|
|
componentRefresh(1, 0);
|
2017-12-11 10:30:46 -05:00
|
|
|
},
|
|
|
|
factory: () => new WrapperComponentWith,
|
|
|
|
rendererType:
|
|
|
|
createRendererType2({encapsulation: ViewEncapsulation.Emulated, styles: [], data: {}}),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
class LeafComponentwith {
|
|
|
|
static ngComponentDef = defineComponent({
|
2018-01-22 18:27:21 -05:00
|
|
|
type: LeafComponentwith,
|
2017-12-11 10:30:46 -05:00
|
|
|
tag: 'leaf',
|
|
|
|
template: function(ctx: LeafComponentwith, cm: boolean) {
|
|
|
|
if (cm) {
|
2018-02-06 19:11:20 -05:00
|
|
|
elementStart(0, 'span');
|
|
|
|
{ text(1, 'bar'); }
|
|
|
|
elementEnd();
|
2017-12-11 10:30:46 -05:00
|
|
|
}
|
|
|
|
},
|
|
|
|
factory: () => new LeafComponentwith,
|
|
|
|
rendererType:
|
|
|
|
createRendererType2({encapsulation: ViewEncapsulation.Emulated, styles: [], data: {}}),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
renderComponent(WrapperComponentWith, getRendererFactory2(document));
|
|
|
|
expect(containerEl.outerHTML)
|
2018-01-03 04:45:09 -05:00
|
|
|
.toMatch(
|
|
|
|
/<div host="" _nghost-c(\d+)=""><leaf _ngcontent-c\1="" _nghost-c(\d+)=""><span _ngcontent-c\2="">bar<\/span><\/leaf><\/div>/);
|
2017-12-11 10:30:46 -05:00
|
|
|
});
|
|
|
|
});
|