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
|
|
|
|
*/
|
|
|
|
|
2018-02-03 23:34:30 -05:00
|
|
|
|
|
|
|
import {DoCheck, ViewEncapsulation} from '../../src/core';
|
2018-03-13 14:48:09 -04:00
|
|
|
import {getRenderedText} from '../../src/render3/component';
|
|
|
|
import {LifecycleHooksFeature, defineComponent, markDirty} from '../../src/render3/index';
|
|
|
|
import {bind, container, containerRefreshEnd, containerRefreshStart, elementEnd, elementProperty, elementStart, embeddedViewEnd, embeddedViewStart, text, textBinding, tick} 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,
|
2018-03-26 00:32:39 -04:00
|
|
|
selector: [[['counter'], null]],
|
2017-12-01 17:23:03 -05:00
|
|
|
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'},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2018-02-03 23:34:30 -05:00
|
|
|
markDirty(component);
|
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();
|
2018-02-03 23:34:30 -05:00
|
|
|
markDirty(component);
|
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,
|
2018-03-26 00:32:39 -04:00
|
|
|
selector: [[['wrapper'], null]],
|
2018-01-25 09:32:21 -05:00
|
|
|
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-03-26 00:32:39 -04:00
|
|
|
elementStart(0, 'wrapper');
|
2018-02-06 19:11:20 -05:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-03-26 00:32:39 -04:00
|
|
|
const defs = [WrapperComponent.ngComponentDef];
|
|
|
|
|
2018-01-25 09:32:21 -05:00
|
|
|
it('should re-render on input change', () => {
|
|
|
|
const ctx: {items: string[]} = {items: ['a']};
|
2018-03-26 00:32:39 -04:00
|
|
|
expect(renderToHtml(template, ctx, defs)).toEqual('<wrapper>a</wrapper>');
|
2018-01-25 09:32:21 -05:00
|
|
|
|
|
|
|
ctx.items = [...ctx.items, 'b'];
|
2018-03-26 00:32:39 -04:00
|
|
|
expect(renderToHtml(template, ctx, defs)).toEqual('<wrapper>ab</wrapper>');
|
2018-01-25 09:32:21 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
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,
|
2018-03-26 00:32:39 -04:00
|
|
|
selector: [[['wrapper'], null]],
|
2017-12-11 10:30:46 -05:00
|
|
|
template: function(ctx: WrapperComponent, cm: boolean) {
|
|
|
|
if (cm) {
|
2018-03-26 00:32:39 -04:00
|
|
|
elementStart(0, 'encapsulated');
|
2018-02-06 19:11:20 -05:00
|
|
|
elementEnd();
|
2017-12-11 10:30:46 -05:00
|
|
|
}
|
|
|
|
},
|
|
|
|
factory: () => new WrapperComponent,
|
2018-03-26 00:32:39 -04:00
|
|
|
directiveDefs: () => [EncapsulatedComponent.ngComponentDef]
|
2017-12-11 10:30:46 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
class EncapsulatedComponent {
|
|
|
|
static ngComponentDef = defineComponent({
|
2018-01-22 18:27:21 -05:00
|
|
|
type: EncapsulatedComponent,
|
2018-03-26 00:32:39 -04:00
|
|
|
selector: [[['encapsulated'], null]],
|
2017-12-11 10:30:46 -05:00
|
|
|
template: function(ctx: EncapsulatedComponent, cm: boolean) {
|
|
|
|
if (cm) {
|
2018-02-06 19:11:20 -05:00
|
|
|
text(0, 'foo');
|
2018-03-26 00:32:39 -04:00
|
|
|
elementStart(1, 'leaf');
|
2018-02-06 19:11:20 -05:00
|
|
|
elementEnd();
|
2017-12-11 10:30:46 -05:00
|
|
|
}
|
|
|
|
},
|
|
|
|
factory: () => new EncapsulatedComponent,
|
|
|
|
rendererType:
|
|
|
|
createRendererType2({encapsulation: ViewEncapsulation.Emulated, styles: [], data: {}}),
|
2018-03-26 00:32:39 -04:00
|
|
|
directiveDefs: () => [LeafComponent.ngComponentDef]
|
2017-12-11 10:30:46 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
class LeafComponent {
|
|
|
|
static ngComponentDef = defineComponent({
|
2018-01-22 18:27:21 -05:00
|
|
|
type: LeafComponent,
|
2018-03-26 00:32:39 -04:00
|
|
|
selector: [[['leaf'], null]],
|
2017-12-11 10:30:46 -05:00
|
|
|
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', () => {
|
2018-03-06 13:13:49 -05:00
|
|
|
renderComponent(WrapperComponent, {rendererFactory: getRendererFactory2(document)});
|
2017-12-11 10:30:46 -05:00
|
|
|
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', () => {
|
2018-03-06 13:13:49 -05:00
|
|
|
renderComponent(EncapsulatedComponent, {rendererFactory: getRendererFactory2(document)});
|
2017-12-11 10:30:46 -05:00
|
|
|
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,
|
2018-03-26 00:32:39 -04:00
|
|
|
selector: [[['wrapper'], null]],
|
2017-12-11 10:30:46 -05:00
|
|
|
template: function(ctx: WrapperComponentWith, cm: boolean) {
|
|
|
|
if (cm) {
|
2018-03-26 00:32:39 -04:00
|
|
|
elementStart(0, 'leaf');
|
2018-02-06 19:11:20 -05:00
|
|
|
elementEnd();
|
2017-12-11 10:30:46 -05:00
|
|
|
}
|
|
|
|
},
|
|
|
|
factory: () => new WrapperComponentWith,
|
|
|
|
rendererType:
|
|
|
|
createRendererType2({encapsulation: ViewEncapsulation.Emulated, styles: [], data: {}}),
|
2018-03-26 00:32:39 -04:00
|
|
|
directiveDefs: () => [LeafComponentwith.ngComponentDef]
|
2017-12-11 10:30:46 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
class LeafComponentwith {
|
|
|
|
static ngComponentDef = defineComponent({
|
2018-01-22 18:27:21 -05:00
|
|
|
type: LeafComponentwith,
|
2018-03-26 00:32:39 -04:00
|
|
|
selector: [[['leaf'], null]],
|
2017-12-11 10:30:46 -05:00
|
|
|
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: {}}),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-03-06 13:13:49 -05:00
|
|
|
renderComponent(WrapperComponentWith, {rendererFactory: getRendererFactory2(document)});
|
2017-12-11 10:30:46 -05:00
|
|
|
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
|
|
|
});
|
2018-02-03 23:34:30 -05:00
|
|
|
|
2017-12-11 10:30:46 -05:00
|
|
|
});
|
2018-03-13 14:48:09 -04:00
|
|
|
|
|
|
|
describe('recursive components', () => {
|
|
|
|
let events: string[] = [];
|
|
|
|
let count = 0;
|
|
|
|
|
|
|
|
class TreeNode {
|
|
|
|
constructor(
|
|
|
|
public value: number, public depth: number, public left: TreeNode|null,
|
|
|
|
public right: TreeNode|null) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
class TreeComponent {
|
|
|
|
data: TreeNode = _buildTree(0);
|
|
|
|
|
|
|
|
ngDoCheck() { events.push('check' + this.data.value); }
|
|
|
|
|
|
|
|
static ngComponentDef = defineComponent({
|
|
|
|
type: TreeComponent,
|
2018-03-26 00:32:39 -04:00
|
|
|
selector: [[['tree-comp'], null]],
|
2018-03-13 14:48:09 -04:00
|
|
|
factory: () => new TreeComponent(),
|
|
|
|
template: (ctx: TreeComponent, cm: boolean) => {
|
|
|
|
if (cm) {
|
|
|
|
text(0);
|
|
|
|
container(1);
|
|
|
|
container(2);
|
|
|
|
}
|
|
|
|
textBinding(0, bind(ctx.data.value));
|
|
|
|
containerRefreshStart(1);
|
|
|
|
{
|
|
|
|
if (ctx.data.left != null) {
|
|
|
|
if (embeddedViewStart(0)) {
|
2018-03-26 00:32:39 -04:00
|
|
|
elementStart(0, 'tree-comp');
|
2018-03-13 14:48:09 -04:00
|
|
|
elementEnd();
|
|
|
|
}
|
|
|
|
elementProperty(0, 'data', bind(ctx.data.left));
|
|
|
|
embeddedViewEnd();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
containerRefreshEnd();
|
|
|
|
containerRefreshStart(2);
|
|
|
|
{
|
|
|
|
if (ctx.data.right != null) {
|
|
|
|
if (embeddedViewStart(0)) {
|
2018-03-26 00:32:39 -04:00
|
|
|
elementStart(0, 'tree-comp');
|
2018-03-13 14:48:09 -04:00
|
|
|
elementEnd();
|
|
|
|
}
|
|
|
|
elementProperty(0, 'data', bind(ctx.data.right));
|
|
|
|
embeddedViewEnd();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
containerRefreshEnd();
|
|
|
|
},
|
|
|
|
inputs: {data: 'data'}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-03-26 00:32:39 -04:00
|
|
|
TreeComponent.ngComponentDef.directiveDefs = () => [TreeComponent.ngComponentDef];
|
2018-03-13 14:48:09 -04:00
|
|
|
|
|
|
|
function _buildTree(currDepth: number): TreeNode {
|
|
|
|
const children = currDepth < 2 ? _buildTree(currDepth + 1) : null;
|
|
|
|
const children2 = currDepth < 2 ? _buildTree(currDepth + 1) : null;
|
|
|
|
return new TreeNode(count++, currDepth, children, children2);
|
|
|
|
}
|
|
|
|
|
|
|
|
it('should check each component just once', () => {
|
|
|
|
const comp = renderComponent(TreeComponent, {hostFeatures: [LifecycleHooksFeature]});
|
|
|
|
expect(getRenderedText(comp)).toEqual('6201534');
|
|
|
|
expect(events).toEqual(['check6', 'check2', 'check0', 'check1', 'check5', 'check3', 'check4']);
|
|
|
|
|
|
|
|
events = [];
|
|
|
|
tick(comp);
|
|
|
|
expect(events).toEqual(['check6', 'check2', 'check0', 'check1', 'check5', 'check3', 'check4']);
|
|
|
|
});
|
|
|
|
});
|