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
|
|
|
|
*/
|
|
|
|
|
|
|
|
import {EventEmitter} from '@angular/core';
|
|
|
|
|
2019-04-04 14:41:52 -04:00
|
|
|
import {ΔdefineComponent, ΔdefineDirective} from '../../src/render3/index';
|
|
|
|
import {Δbind, Δcontainer, ΔcontainerRefreshEnd, ΔcontainerRefreshStart, Δelement, ΔelementEnd, ΔelementProperty, ΔelementStart, ΔembeddedViewEnd, ΔembeddedViewStart, Δlistener, Δtext} from '../../src/render3/instructions/all';
|
2018-04-10 23:57:09 -04:00
|
|
|
import {RenderFlags} from '../../src/render3/interfaces/definition';
|
2018-07-27 17:28:22 -04:00
|
|
|
|
2017-12-01 17:23:03 -05:00
|
|
|
import {containerEl, renderToHtml} from './render_util';
|
|
|
|
|
|
|
|
describe('outputs', () => {
|
|
|
|
let buttonToggle: ButtonToggle;
|
2018-03-26 00:32:39 -04:00
|
|
|
let destroyComp: DestroyComp;
|
|
|
|
let buttonDir: MyButton;
|
2017-12-01 17:23:03 -05:00
|
|
|
|
|
|
|
class ButtonToggle {
|
|
|
|
change = new EventEmitter();
|
|
|
|
resetStream = new EventEmitter();
|
|
|
|
|
2019-04-04 14:41:52 -04:00
|
|
|
static ngComponentDef = ΔdefineComponent({
|
2018-01-22 18:27:21 -05:00
|
|
|
type: ButtonToggle,
|
2018-03-29 19:41:45 -04:00
|
|
|
selectors: [['button-toggle']],
|
2018-04-10 23:57:09 -04:00
|
|
|
template: function(rf: RenderFlags, ctx: any) {},
|
2018-08-16 21:53:21 -04:00
|
|
|
consts: 0,
|
2018-08-18 14:14:50 -04:00
|
|
|
vars: 0,
|
2017-12-01 17:23:03 -05:00
|
|
|
factory: () => buttonToggle = new ButtonToggle(),
|
|
|
|
outputs: {change: 'change', resetStream: 'reset'}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-12-08 14:48:54 -05:00
|
|
|
let otherDir: OtherDir;
|
|
|
|
|
|
|
|
class OtherDir {
|
|
|
|
changeStream = new EventEmitter();
|
|
|
|
|
2019-04-04 14:41:52 -04:00
|
|
|
static ngDirectiveDef = ΔdefineDirective({
|
2018-01-22 18:27:21 -05:00
|
|
|
type: OtherDir,
|
2018-03-29 19:41:45 -04:00
|
|
|
selectors: [['', 'otherDir', '']],
|
2018-01-22 18:27:21 -05:00
|
|
|
factory: () => otherDir = new OtherDir,
|
|
|
|
outputs: {changeStream: 'change'}
|
|
|
|
});
|
2017-12-08 14:48:54 -05:00
|
|
|
}
|
2017-12-01 17:23:03 -05:00
|
|
|
|
2018-03-26 00:32:39 -04:00
|
|
|
class DestroyComp {
|
|
|
|
events: string[] = [];
|
|
|
|
ngOnDestroy() { this.events.push('destroy'); }
|
|
|
|
|
2019-04-04 14:41:52 -04:00
|
|
|
static ngComponentDef = ΔdefineComponent({
|
2018-03-26 00:32:39 -04:00
|
|
|
type: DestroyComp,
|
2018-03-29 19:41:45 -04:00
|
|
|
selectors: [['destroy-comp']],
|
2018-08-16 21:53:21 -04:00
|
|
|
consts: 0,
|
2018-08-18 14:14:50 -04:00
|
|
|
vars: 0,
|
2018-04-10 23:57:09 -04:00
|
|
|
template: function(rf: RenderFlags, ctx: any) {},
|
2018-03-26 00:32:39 -04:00
|
|
|
factory: () => destroyComp = new DestroyComp()
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/** <button myButton (click)="onClick()">Click me</button> */
|
|
|
|
class MyButton {
|
|
|
|
click = new EventEmitter();
|
|
|
|
|
2019-04-04 14:41:52 -04:00
|
|
|
static ngDirectiveDef = ΔdefineDirective({
|
2018-03-26 00:32:39 -04:00
|
|
|
type: MyButton,
|
2018-03-29 19:41:45 -04:00
|
|
|
selectors: [['', 'myButton', '']],
|
2018-03-26 00:32:39 -04:00
|
|
|
factory: () => buttonDir = new MyButton,
|
|
|
|
outputs: {click: 'click'}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-03-29 15:58:41 -04:00
|
|
|
const deps = [ButtonToggle, OtherDir, DestroyComp, MyButton];
|
2018-03-26 00:32:39 -04:00
|
|
|
|
2017-12-01 17:23:03 -05:00
|
|
|
it('should call component output function when event is emitted', () => {
|
|
|
|
/** <button-toggle (change)="onChange()"></button-toggle> */
|
2018-04-10 23:57:09 -04:00
|
|
|
function Template(rf: RenderFlags, ctx: any) {
|
|
|
|
if (rf & RenderFlags.Create) {
|
2019-04-04 14:41:52 -04:00
|
|
|
ΔelementStart(0, 'button-toggle');
|
2018-02-16 14:36:50 -05:00
|
|
|
{
|
2019-04-04 14:41:52 -04:00
|
|
|
Δlistener('change', function() { return ctx.onChange(); });
|
2018-02-16 14:36:50 -05:00
|
|
|
}
|
2019-04-04 14:41:52 -04:00
|
|
|
ΔelementEnd();
|
2017-12-01 17:23:03 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let counter = 0;
|
|
|
|
const ctx = {onChange: () => counter++};
|
2018-08-18 14:14:50 -04:00
|
|
|
renderToHtml(Template, ctx, 1, 0, deps);
|
2017-12-01 17:23:03 -05:00
|
|
|
|
|
|
|
buttonToggle !.change.next();
|
|
|
|
expect(counter).toEqual(1);
|
|
|
|
|
|
|
|
buttonToggle !.change.next();
|
|
|
|
expect(counter).toEqual(2);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should support more than 1 output function on the same node', () => {
|
|
|
|
/** <button-toggle (change)="onChange()" (reset)="onReset()"></button-toggle> */
|
2018-04-10 23:57:09 -04:00
|
|
|
function Template(rf: RenderFlags, ctx: any) {
|
|
|
|
if (rf & RenderFlags.Create) {
|
2019-04-04 14:41:52 -04:00
|
|
|
ΔelementStart(0, 'button-toggle');
|
2017-12-01 17:23:03 -05:00
|
|
|
{
|
2019-04-04 14:41:52 -04:00
|
|
|
Δlistener('change', function() { return ctx.onChange(); });
|
|
|
|
Δlistener('reset', function() { return ctx.onReset(); });
|
2017-12-01 17:23:03 -05:00
|
|
|
}
|
2019-04-04 14:41:52 -04:00
|
|
|
ΔelementEnd();
|
2017-12-01 17:23:03 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let counter = 0;
|
|
|
|
let resetCounter = 0;
|
|
|
|
const ctx = {onChange: () => counter++, onReset: () => resetCounter++};
|
2018-08-18 14:14:50 -04:00
|
|
|
renderToHtml(Template, ctx, 1, 0, deps);
|
2017-12-01 17:23:03 -05:00
|
|
|
|
|
|
|
buttonToggle !.change.next();
|
|
|
|
expect(counter).toEqual(1);
|
|
|
|
|
|
|
|
buttonToggle !.resetStream.next();
|
|
|
|
expect(resetCounter).toEqual(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should eval component output expression when event is emitted', () => {
|
|
|
|
/** <button-toggle (change)="counter++"></button-toggle> */
|
2018-04-10 23:57:09 -04:00
|
|
|
function Template(rf: RenderFlags, ctx: any) {
|
|
|
|
if (rf & RenderFlags.Create) {
|
2019-04-04 14:41:52 -04:00
|
|
|
ΔelementStart(0, 'button-toggle');
|
2018-02-16 14:36:50 -05:00
|
|
|
{
|
2019-04-04 14:41:52 -04:00
|
|
|
Δlistener('change', function() { return ctx.counter++; });
|
2018-02-16 14:36:50 -05:00
|
|
|
}
|
2019-04-04 14:41:52 -04:00
|
|
|
ΔelementEnd();
|
2017-12-01 17:23:03 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const ctx = {counter: 0};
|
2018-08-18 14:14:50 -04:00
|
|
|
renderToHtml(Template, ctx, 1, 0, deps);
|
2017-12-01 17:23:03 -05:00
|
|
|
|
|
|
|
buttonToggle !.change.next();
|
|
|
|
expect(ctx.counter).toEqual(1);
|
|
|
|
|
|
|
|
buttonToggle !.change.next();
|
|
|
|
expect(ctx.counter).toEqual(2);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should unsubscribe from output when view is destroyed', () => {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* % if (condition) {
|
|
|
|
* <button-toggle (change)="onChange()"></button-toggle>
|
|
|
|
* % }
|
|
|
|
*/
|
2018-04-10 23:57:09 -04:00
|
|
|
function Template(rf: RenderFlags, ctx: any) {
|
|
|
|
if (rf & RenderFlags.Create) {
|
2019-04-04 14:41:52 -04:00
|
|
|
Δcontainer(0);
|
2017-12-01 17:23:03 -05:00
|
|
|
}
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Update) {
|
2019-04-04 14:41:52 -04:00
|
|
|
ΔcontainerRefreshStart(0);
|
2018-04-10 23:57:09 -04:00
|
|
|
{
|
|
|
|
if (ctx.condition) {
|
2019-04-04 14:41:52 -04:00
|
|
|
let rf1 = ΔembeddedViewStart(0, 1, 0);
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf1 & RenderFlags.Create) {
|
2019-04-04 14:41:52 -04:00
|
|
|
ΔelementStart(0, 'button-toggle');
|
2018-04-10 23:57:09 -04:00
|
|
|
{
|
2019-04-04 14:41:52 -04:00
|
|
|
Δlistener('change', function() { return ctx.onChange(); });
|
2018-04-10 23:57:09 -04:00
|
|
|
}
|
2019-04-04 14:41:52 -04:00
|
|
|
ΔelementEnd();
|
2018-02-16 14:36:50 -05:00
|
|
|
}
|
2019-04-04 14:41:52 -04:00
|
|
|
ΔembeddedViewEnd();
|
2017-12-01 17:23:03 -05:00
|
|
|
}
|
|
|
|
}
|
2019-04-04 14:41:52 -04:00
|
|
|
ΔcontainerRefreshEnd();
|
2017-12-01 17:23:03 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let counter = 0;
|
|
|
|
const ctx = {onChange: () => counter++, condition: true};
|
2018-08-18 14:14:50 -04:00
|
|
|
renderToHtml(Template, ctx, 1, 0, deps);
|
2017-12-01 17:23:03 -05:00
|
|
|
|
|
|
|
buttonToggle !.change.next();
|
|
|
|
expect(counter).toEqual(1);
|
|
|
|
|
|
|
|
ctx.condition = false;
|
2018-08-18 14:14:50 -04:00
|
|
|
renderToHtml(Template, ctx, 1, 0, deps);
|
2017-12-01 17:23:03 -05:00
|
|
|
|
|
|
|
buttonToggle !.change.next();
|
|
|
|
expect(counter).toEqual(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should unsubscribe from output in nested view', () => {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* % if (condition) {
|
|
|
|
* % if (condition2) {
|
|
|
|
* <button-toggle (change)="onChange()"></button-toggle>
|
|
|
|
* % }
|
|
|
|
* % }
|
|
|
|
*/
|
2018-04-10 23:57:09 -04:00
|
|
|
function Template(rf: RenderFlags, ctx: any) {
|
|
|
|
if (rf & RenderFlags.Create) {
|
2019-04-04 14:41:52 -04:00
|
|
|
Δcontainer(0);
|
2017-12-01 17:23:03 -05:00
|
|
|
}
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Update) {
|
2019-04-04 14:41:52 -04:00
|
|
|
ΔcontainerRefreshStart(0);
|
2018-04-10 23:57:09 -04:00
|
|
|
{
|
|
|
|
if (ctx.condition) {
|
2019-04-04 14:41:52 -04:00
|
|
|
let rf1 = ΔembeddedViewStart(0, 1, 0);
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf1 & RenderFlags.Create) {
|
2019-04-04 14:41:52 -04:00
|
|
|
Δcontainer(0);
|
2018-04-10 23:57:09 -04:00
|
|
|
}
|
2019-04-04 14:41:52 -04:00
|
|
|
ΔcontainerRefreshStart(0);
|
2018-04-10 23:57:09 -04:00
|
|
|
{
|
|
|
|
if (ctx.condition2) {
|
2019-04-04 14:41:52 -04:00
|
|
|
let rf1 = ΔembeddedViewStart(0, 1, 0);
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf1 & RenderFlags.Create) {
|
2019-04-04 14:41:52 -04:00
|
|
|
ΔelementStart(0, 'button-toggle');
|
2018-04-10 23:57:09 -04:00
|
|
|
{
|
2019-04-04 14:41:52 -04:00
|
|
|
Δlistener('change', function() { return ctx.onChange(); });
|
2018-04-10 23:57:09 -04:00
|
|
|
}
|
2019-04-04 14:41:52 -04:00
|
|
|
ΔelementEnd();
|
2018-02-16 14:36:50 -05:00
|
|
|
}
|
2019-04-04 14:41:52 -04:00
|
|
|
ΔembeddedViewEnd();
|
2017-12-01 17:23:03 -05:00
|
|
|
}
|
|
|
|
}
|
2019-04-04 14:41:52 -04:00
|
|
|
ΔcontainerRefreshEnd();
|
|
|
|
ΔembeddedViewEnd();
|
2017-12-01 17:23:03 -05:00
|
|
|
}
|
|
|
|
}
|
2019-04-04 14:41:52 -04:00
|
|
|
ΔcontainerRefreshEnd();
|
2017-12-01 17:23:03 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let counter = 0;
|
|
|
|
const ctx = {onChange: () => counter++, condition: true, condition2: true};
|
2018-08-18 14:14:50 -04:00
|
|
|
renderToHtml(Template, ctx, 1, 0, deps);
|
2017-12-01 17:23:03 -05:00
|
|
|
|
|
|
|
buttonToggle !.change.next();
|
|
|
|
expect(counter).toEqual(1);
|
|
|
|
|
|
|
|
ctx.condition = false;
|
2018-08-18 14:14:50 -04:00
|
|
|
renderToHtml(Template, ctx, 1, 0, deps);
|
2017-12-01 17:23:03 -05:00
|
|
|
|
|
|
|
buttonToggle !.change.next();
|
|
|
|
expect(counter).toEqual(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should work properly when view also has listeners and destroys', () => {
|
|
|
|
/**
|
|
|
|
* % if (condition) {
|
|
|
|
* <button (click)="onClick()">Click me</button>
|
|
|
|
* <button-toggle (change)="onChange()"></button-toggle>
|
|
|
|
* <destroy-comp></destroy-comp>
|
|
|
|
* % }
|
|
|
|
*/
|
2018-04-10 23:57:09 -04:00
|
|
|
function Template(rf: RenderFlags, ctx: any) {
|
|
|
|
if (rf & RenderFlags.Create) {
|
2019-04-04 14:41:52 -04:00
|
|
|
Δcontainer(0);
|
2017-12-01 17:23:03 -05:00
|
|
|
}
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Update) {
|
2019-04-04 14:41:52 -04:00
|
|
|
ΔcontainerRefreshStart(0);
|
2018-04-10 23:57:09 -04:00
|
|
|
{
|
|
|
|
if (ctx.condition) {
|
2019-04-04 14:41:52 -04:00
|
|
|
let rf1 = ΔembeddedViewStart(0, 4, 0);
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf1 & RenderFlags.Create) {
|
2019-04-04 14:41:52 -04:00
|
|
|
ΔelementStart(0, 'button');
|
2018-04-10 23:57:09 -04:00
|
|
|
{
|
2019-04-04 14:41:52 -04:00
|
|
|
Δlistener('click', function() { return ctx.onClick(); });
|
|
|
|
Δtext(1, 'Click me');
|
2018-04-10 23:57:09 -04:00
|
|
|
}
|
2019-04-04 14:41:52 -04:00
|
|
|
ΔelementEnd();
|
|
|
|
ΔelementStart(2, 'button-toggle');
|
2018-04-10 23:57:09 -04:00
|
|
|
{
|
2019-04-04 14:41:52 -04:00
|
|
|
Δlistener('change', function() { return ctx.onChange(); });
|
2018-04-10 23:57:09 -04:00
|
|
|
}
|
2019-04-04 14:41:52 -04:00
|
|
|
ΔelementEnd();
|
|
|
|
Δelement(3, 'destroy-comp');
|
2018-02-16 14:36:50 -05:00
|
|
|
}
|
2019-04-04 14:41:52 -04:00
|
|
|
ΔembeddedViewEnd();
|
2017-12-01 17:23:03 -05:00
|
|
|
}
|
|
|
|
}
|
2019-04-04 14:41:52 -04:00
|
|
|
ΔcontainerRefreshEnd();
|
2017-12-01 17:23:03 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let clickCounter = 0;
|
|
|
|
let changeCounter = 0;
|
|
|
|
const ctx = {condition: true, onChange: () => changeCounter++, onClick: () => clickCounter++};
|
2018-08-18 14:14:50 -04:00
|
|
|
renderToHtml(Template, ctx, 1, 0, deps);
|
2017-12-01 17:23:03 -05:00
|
|
|
|
|
|
|
buttonToggle !.change.next();
|
|
|
|
expect(changeCounter).toEqual(1);
|
|
|
|
expect(clickCounter).toEqual(0);
|
|
|
|
|
|
|
|
const button = containerEl.querySelector('button');
|
|
|
|
button !.click();
|
|
|
|
expect(changeCounter).toEqual(1);
|
|
|
|
expect(clickCounter).toEqual(1);
|
|
|
|
|
|
|
|
ctx.condition = false;
|
2018-08-18 14:14:50 -04:00
|
|
|
renderToHtml(Template, ctx, 1, 0, deps);
|
2017-12-01 17:23:03 -05:00
|
|
|
|
|
|
|
expect(destroyComp !.events).toEqual(['destroy']);
|
|
|
|
|
|
|
|
buttonToggle !.change.next();
|
|
|
|
button !.click();
|
|
|
|
expect(changeCounter).toEqual(1);
|
|
|
|
expect(clickCounter).toEqual(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should fire event listeners along with outputs if they match', () => {
|
2018-04-10 23:57:09 -04:00
|
|
|
function Template(rf: RenderFlags, ctx: any) {
|
|
|
|
if (rf & RenderFlags.Create) {
|
2019-04-04 14:41:52 -04:00
|
|
|
ΔelementStart(0, 'button', ['myButton', '']);
|
2018-02-16 14:36:50 -05:00
|
|
|
{
|
2019-04-04 14:41:52 -04:00
|
|
|
Δlistener('click', function() { return ctx.onClick(); });
|
2018-02-16 14:36:50 -05:00
|
|
|
}
|
2019-04-04 14:41:52 -04:00
|
|
|
ΔelementEnd();
|
2017-12-01 17:23:03 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let counter = 0;
|
2018-08-18 14:14:50 -04:00
|
|
|
renderToHtml(Template, {counter, onClick: () => counter++}, 1, 0, deps);
|
2017-12-01 17:23:03 -05:00
|
|
|
|
|
|
|
// To match current Angular behavior, the click listener is still
|
|
|
|
// set up in addition to any matching outputs.
|
|
|
|
const button = containerEl.querySelector('button') !;
|
|
|
|
button.click();
|
|
|
|
expect(counter).toEqual(1);
|
|
|
|
|
|
|
|
buttonDir !.click.next();
|
|
|
|
expect(counter).toEqual(2);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should work with two outputs of the same name', () => {
|
|
|
|
/** <button-toggle (change)="onChange()" otherDir></button-toggle> */
|
2018-04-10 23:57:09 -04:00
|
|
|
function Template(rf: RenderFlags, ctx: any) {
|
|
|
|
if (rf & RenderFlags.Create) {
|
2019-04-04 14:41:52 -04:00
|
|
|
ΔelementStart(0, 'button-toggle', ['otherDir', '']);
|
2018-02-16 14:36:50 -05:00
|
|
|
{
|
2019-04-04 14:41:52 -04:00
|
|
|
Δlistener('change', function() { return ctx.onChange(); });
|
2018-02-16 14:36:50 -05:00
|
|
|
}
|
2019-04-04 14:41:52 -04:00
|
|
|
ΔelementEnd();
|
2017-12-01 17:23:03 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let counter = 0;
|
2018-08-18 14:14:50 -04:00
|
|
|
renderToHtml(Template, {counter, onChange: () => counter++}, 1, 0, deps);
|
2017-12-01 17:23:03 -05:00
|
|
|
|
|
|
|
buttonToggle !.change.next();
|
|
|
|
expect(counter).toEqual(1);
|
|
|
|
|
2017-12-08 14:48:54 -05:00
|
|
|
otherDir !.changeStream.next();
|
2017-12-01 17:23:03 -05:00
|
|
|
expect(counter).toEqual(2);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should work with an input and output of the same name', () => {
|
2018-03-26 00:32:39 -04:00
|
|
|
let otherDir: OtherChangeDir;
|
2017-12-01 17:23:03 -05:00
|
|
|
|
2018-03-26 00:32:39 -04:00
|
|
|
class OtherChangeDir {
|
2018-06-18 19:38:33 -04:00
|
|
|
// TODO(issue/24571): remove '!'.
|
|
|
|
change !: boolean;
|
2017-12-01 17:23:03 -05:00
|
|
|
|
2019-04-04 14:41:52 -04:00
|
|
|
static ngDirectiveDef = ΔdefineDirective({
|
2018-03-26 00:32:39 -04:00
|
|
|
type: OtherChangeDir,
|
2018-03-29 19:41:45 -04:00
|
|
|
selectors: [['', 'otherChangeDir', '']],
|
2018-03-26 00:32:39 -04:00
|
|
|
factory: () => otherDir = new OtherChangeDir,
|
|
|
|
inputs: {change: 'change'}
|
|
|
|
});
|
2017-12-01 17:23:03 -05:00
|
|
|
}
|
|
|
|
|
2018-03-26 00:32:39 -04:00
|
|
|
/** <button-toggle (change)="onChange()" otherChangeDir [change]="change"></button-toggle> */
|
2018-04-10 23:57:09 -04:00
|
|
|
function Template(rf: RenderFlags, ctx: any) {
|
|
|
|
if (rf & RenderFlags.Create) {
|
2019-04-04 14:41:52 -04:00
|
|
|
ΔelementStart(0, 'button-toggle', ['otherChangeDir', '']);
|
2018-02-16 14:36:50 -05:00
|
|
|
{
|
2019-04-04 14:41:52 -04:00
|
|
|
Δlistener('change', function() { return ctx.onChange(); });
|
2018-02-16 14:36:50 -05:00
|
|
|
}
|
2019-04-04 14:41:52 -04:00
|
|
|
ΔelementEnd();
|
2017-12-01 17:23:03 -05:00
|
|
|
}
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Update) {
|
2019-04-04 14:41:52 -04:00
|
|
|
ΔelementProperty(0, 'change', Δbind(ctx.change));
|
2018-04-10 23:57:09 -04:00
|
|
|
}
|
2017-12-01 17:23:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
let counter = 0;
|
2018-03-29 15:58:41 -04:00
|
|
|
const deps = [ButtonToggle, OtherChangeDir];
|
2018-08-18 14:14:50 -04:00
|
|
|
renderToHtml(Template, {counter, onChange: () => counter++, change: true}, 1, 1, deps);
|
2017-12-01 17:23:03 -05:00
|
|
|
expect(otherDir !.change).toEqual(true);
|
|
|
|
|
2018-08-18 14:14:50 -04:00
|
|
|
renderToHtml(Template, {counter, onChange: () => counter++, change: false}, 1, 1, deps);
|
2017-12-01 17:23:03 -05:00
|
|
|
expect(otherDir !.change).toEqual(false);
|
|
|
|
|
|
|
|
buttonToggle !.change.next();
|
|
|
|
expect(counter).toEqual(1);
|
|
|
|
});
|
|
|
|
|
2017-12-08 14:48:54 -05:00
|
|
|
it('should work with outputs at same index in if block', () => {
|
|
|
|
/**
|
|
|
|
* <button (click)="onClick()">Click me</button> // outputs: null
|
|
|
|
* % if (condition) {
|
|
|
|
* <button-toggle (change)="onChange()"></button-toggle> // outputs: {change: [0, 'change']}
|
|
|
|
* % } else {
|
|
|
|
* <div otherDir (change)="onChange()"></div> // outputs: {change: [0,
|
|
|
|
* 'changeStream']}
|
|
|
|
* % }
|
|
|
|
*/
|
2018-04-10 23:57:09 -04:00
|
|
|
function Template(rf: RenderFlags, ctx: any) {
|
|
|
|
if (rf & RenderFlags.Create) {
|
2019-04-04 14:41:52 -04:00
|
|
|
ΔelementStart(0, 'button');
|
2017-12-08 14:48:54 -05:00
|
|
|
{
|
2019-04-04 14:41:52 -04:00
|
|
|
Δlistener('click', function() { return ctx.onClick(); });
|
|
|
|
Δtext(1, 'Click me');
|
2017-12-08 14:48:54 -05:00
|
|
|
}
|
2019-04-04 14:41:52 -04:00
|
|
|
ΔelementEnd();
|
|
|
|
Δcontainer(2);
|
2017-12-08 14:48:54 -05:00
|
|
|
}
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf & RenderFlags.Update) {
|
2019-04-04 14:41:52 -04:00
|
|
|
ΔcontainerRefreshStart(2);
|
2018-04-10 23:57:09 -04:00
|
|
|
{
|
|
|
|
if (ctx.condition) {
|
2019-04-04 14:41:52 -04:00
|
|
|
let rf1 = ΔembeddedViewStart(0, 1, 0);
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf1 & RenderFlags.Create) {
|
2019-04-04 14:41:52 -04:00
|
|
|
ΔelementStart(0, 'button-toggle');
|
2018-04-10 23:57:09 -04:00
|
|
|
{
|
2019-04-04 14:41:52 -04:00
|
|
|
Δlistener('change', function() { return ctx.onChange(); });
|
2018-04-10 23:57:09 -04:00
|
|
|
}
|
2019-04-04 14:41:52 -04:00
|
|
|
ΔelementEnd();
|
2018-02-16 14:36:50 -05:00
|
|
|
}
|
2019-04-04 14:41:52 -04:00
|
|
|
ΔembeddedViewEnd();
|
2018-04-10 23:57:09 -04:00
|
|
|
} else {
|
2019-04-04 14:41:52 -04:00
|
|
|
if (ΔembeddedViewStart(1, 1, 0)) {
|
|
|
|
ΔelementStart(0, 'div', ['otherDir', '']);
|
2018-04-10 23:57:09 -04:00
|
|
|
{
|
2019-04-04 14:41:52 -04:00
|
|
|
Δlistener('change', function() { return ctx.onChange(); });
|
2018-04-10 23:57:09 -04:00
|
|
|
}
|
2019-04-04 14:41:52 -04:00
|
|
|
ΔelementEnd();
|
2018-02-16 14:36:50 -05:00
|
|
|
}
|
2019-04-04 14:41:52 -04:00
|
|
|
ΔembeddedViewEnd();
|
2017-12-08 14:48:54 -05:00
|
|
|
}
|
|
|
|
}
|
2019-04-04 14:41:52 -04:00
|
|
|
ΔcontainerRefreshEnd();
|
2017-12-08 14:48:54 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let counter = 0;
|
|
|
|
const ctx = {condition: true, onChange: () => counter++, onClick: () => {}};
|
2018-08-18 14:14:50 -04:00
|
|
|
renderToHtml(Template, ctx, 3, 0, deps);
|
2017-12-08 14:48:54 -05:00
|
|
|
|
|
|
|
buttonToggle !.change.next();
|
|
|
|
expect(counter).toEqual(1);
|
|
|
|
|
|
|
|
ctx.condition = false;
|
2018-08-18 14:14:50 -04:00
|
|
|
renderToHtml(Template, ctx, 3, 0, deps);
|
2017-12-08 14:48:54 -05:00
|
|
|
expect(counter).toEqual(1);
|
|
|
|
|
|
|
|
otherDir !.changeStream.next();
|
|
|
|
expect(counter).toEqual(2);
|
|
|
|
});
|
|
|
|
|
2017-12-01 17:23:03 -05:00
|
|
|
});
|