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-04-10 23:57:09 -04:00
|
|
|
import {RenderFlags} from '../../src/render3/interfaces/definition';
|
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-29 19:41:45 -04:00
|
|
|
selectors: [['counter']],
|
2018-04-10 23:57:09 -04:00
|
|
|
template: function(rf: RenderFlags, ctx: CounterComponent) {
|
|
|
|
if (rf & RenderFlags.Create) {
|
2018-02-06 19:11:20 -05:00
|
|
|
text(0);
|
2017-12-01 17:23:03 -05:00
|
|
|
}
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Update) {
|
|
|
|
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', () => {
|
|
|
|
|
2018-04-10 23:57:09 -04:00
|
|
|
function showItems(rf: RenderFlags, ctx: {items: string[]}) {
|
|
|
|
if (rf & RenderFlags.Create) {
|
2018-02-06 19:11:20 -05:00
|
|
|
container(0);
|
2018-01-25 09:32:21 -05:00
|
|
|
}
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Update) {
|
|
|
|
containerRefreshStart(0);
|
|
|
|
{
|
|
|
|
for (const item of ctx.items) {
|
|
|
|
const rf0 = embeddedViewStart(0);
|
|
|
|
{
|
|
|
|
if (rf0 & RenderFlags.Create) {
|
|
|
|
text(0);
|
|
|
|
}
|
|
|
|
if (rf0 & RenderFlags.Update) {
|
|
|
|
textBinding(0, bind(item));
|
|
|
|
}
|
2018-01-25 09:32:21 -05:00
|
|
|
}
|
2018-04-10 23:57:09 -04:00
|
|
|
embeddedViewEnd();
|
2018-01-25 09:32:21 -05:00
|
|
|
}
|
|
|
|
}
|
2018-04-10 23:57:09 -04:00
|
|
|
containerRefreshEnd();
|
2018-01-25 09:32:21 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class WrapperComponent {
|
|
|
|
items: string[];
|
|
|
|
static ngComponentDef = defineComponent({
|
|
|
|
type: WrapperComponent,
|
2018-03-29 19:41:45 -04:00
|
|
|
selectors: [['wrapper']],
|
2018-04-10 23:57:09 -04:00
|
|
|
template: function ChildComponentTemplate(rf: RenderFlags, ctx: {items: string[]}) {
|
|
|
|
if (rf & RenderFlags.Create) {
|
2018-02-06 19:11:20 -05:00
|
|
|
container(0);
|
2018-01-25 09:32:21 -05:00
|
|
|
}
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Update) {
|
|
|
|
containerRefreshStart(0);
|
|
|
|
{
|
|
|
|
const rf0 = embeddedViewStart(0);
|
|
|
|
{ showItems(rf0, {items: ctx.items}); }
|
|
|
|
embeddedViewEnd();
|
|
|
|
}
|
|
|
|
containerRefreshEnd();
|
2018-01-25 09:32:21 -05:00
|
|
|
}
|
|
|
|
},
|
|
|
|
factory: () => new WrapperComponent,
|
|
|
|
inputs: {items: 'items'}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-04-10 23:57:09 -04:00
|
|
|
function template(rf: RenderFlags, ctx: {items: string[]}) {
|
|
|
|
if (rf & RenderFlags.Create) {
|
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-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Update) {
|
|
|
|
elementProperty(0, 'items', bind(ctx.items));
|
|
|
|
}
|
2018-01-25 09:32:21 -05:00
|
|
|
}
|
|
|
|
|
2018-03-29 15:58:41 -04:00
|
|
|
const defs = [WrapperComponent];
|
2018-03-26 00:32:39 -04:00
|
|
|
|
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-29 19:41:45 -04:00
|
|
|
selectors: [['wrapper']],
|
2018-04-10 23:57:09 -04:00
|
|
|
template: function(rf: RenderFlags, ctx: WrapperComponent) {
|
|
|
|
if (rf & RenderFlags.Create) {
|
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-29 15:58:41 -04:00
|
|
|
directives: () => [EncapsulatedComponent]
|
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-29 19:41:45 -04:00
|
|
|
selectors: [['encapsulated']],
|
2018-04-10 23:57:09 -04:00
|
|
|
template: function(rf: RenderFlags, ctx: EncapsulatedComponent) {
|
|
|
|
if (rf & RenderFlags.Create) {
|
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-29 15:58:41 -04:00
|
|
|
directives: () => [LeafComponent]
|
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-29 19:41:45 -04:00
|
|
|
selectors: [['leaf']],
|
2018-04-10 23:57:09 -04:00
|
|
|
template: function(rf: RenderFlags, ctx: LeafComponent) {
|
|
|
|
if (rf & RenderFlags.Create) {
|
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-29 19:41:45 -04:00
|
|
|
selectors: [['wrapper']],
|
2018-04-10 23:57:09 -04:00
|
|
|
template: function(rf: RenderFlags, ctx: WrapperComponentWith) {
|
|
|
|
if (rf & RenderFlags.Create) {
|
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-29 15:58:41 -04:00
|
|
|
directives: () => [LeafComponentwith]
|
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-29 19:41:45 -04:00
|
|
|
selectors: [['leaf']],
|
2018-04-10 23:57:09 -04:00
|
|
|
template: function(rf: RenderFlags, ctx: LeafComponentwith) {
|
|
|
|
if (rf & RenderFlags.Create) {
|
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-29 19:41:45 -04:00
|
|
|
selectors: [['tree-comp']],
|
2018-03-13 14:48:09 -04:00
|
|
|
factory: () => new TreeComponent(),
|
2018-04-10 23:57:09 -04:00
|
|
|
template: (rf: RenderFlags, ctx: TreeComponent) => {
|
|
|
|
if (rf & RenderFlags.Create) {
|
2018-03-13 14:48:09 -04:00
|
|
|
text(0);
|
|
|
|
container(1);
|
|
|
|
container(2);
|
|
|
|
}
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Update) {
|
|
|
|
textBinding(0, bind(ctx.data.value));
|
|
|
|
containerRefreshStart(1);
|
|
|
|
{
|
|
|
|
if (ctx.data.left != null) {
|
|
|
|
let rf0 = embeddedViewStart(0);
|
|
|
|
if (rf0 & RenderFlags.Create) {
|
|
|
|
elementStart(0, 'tree-comp');
|
|
|
|
elementEnd();
|
|
|
|
}
|
|
|
|
if (rf0 & RenderFlags.Update) {
|
|
|
|
elementProperty(0, 'data', bind(ctx.data.left));
|
|
|
|
}
|
|
|
|
embeddedViewEnd();
|
2018-03-13 14:48:09 -04:00
|
|
|
}
|
|
|
|
}
|
2018-04-10 23:57:09 -04:00
|
|
|
containerRefreshEnd();
|
|
|
|
containerRefreshStart(2);
|
|
|
|
{
|
|
|
|
if (ctx.data.right != null) {
|
|
|
|
let rf0 = embeddedViewStart(0);
|
|
|
|
if (rf0 & RenderFlags.Create) {
|
|
|
|
elementStart(0, 'tree-comp');
|
|
|
|
elementEnd();
|
|
|
|
}
|
|
|
|
if (rf0 & RenderFlags.Update) {
|
|
|
|
elementProperty(0, 'data', bind(ctx.data.right));
|
|
|
|
}
|
|
|
|
embeddedViewEnd();
|
2018-03-13 14:48:09 -04:00
|
|
|
}
|
|
|
|
}
|
2018-04-10 23:57:09 -04:00
|
|
|
containerRefreshEnd();
|
2018-03-13 14:48:09 -04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
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']);
|
|
|
|
});
|
|
|
|
});
|