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-05-09 14:47:25 -04:00
|
|
|
import {ΔdefineComponent, ΔdefineDirective} from '../../src/render3/index';
|
|
|
|
import {Δcontainer, ΔcontainerRefreshEnd, ΔcontainerRefreshStart, ΔelementEnd, Δ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
|
|
|
|
2019-05-09 22:08:31 -04:00
|
|
|
import {renderToHtml} from './render_util';
|
2017-12-01 17:23:03 -05:00
|
|
|
|
|
|
|
describe('outputs', () => {
|
|
|
|
let buttonToggle: ButtonToggle;
|
|
|
|
|
|
|
|
class ButtonToggle {
|
|
|
|
change = new EventEmitter();
|
|
|
|
resetStream = new EventEmitter();
|
|
|
|
|
2019-05-09 14:47:25 -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-05-09 14:47:25 -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
|
|
|
|
2019-05-09 22:08:31 -04:00
|
|
|
const deps = [ButtonToggle, OtherDir];
|
2017-12-01 17:23:03 -05:00
|
|
|
|
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-05-09 14:47:25 -04:00
|
|
|
ΔelementStart(0, 'button');
|
2017-12-08 14:48:54 -05:00
|
|
|
{
|
2019-05-09 14:47:25 -04:00
|
|
|
Δlistener('click', function() { return ctx.onClick(); });
|
|
|
|
Δtext(1, 'Click me');
|
2017-12-08 14:48:54 -05:00
|
|
|
}
|
2019-05-09 14:47:25 -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-05-09 14:47:25 -04:00
|
|
|
ΔcontainerRefreshStart(2);
|
2018-04-10 23:57:09 -04:00
|
|
|
{
|
|
|
|
if (ctx.condition) {
|
2019-05-09 14:47:25 -04:00
|
|
|
let rf1 = ΔembeddedViewStart(0, 1, 0);
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf1 & RenderFlags.Create) {
|
2019-05-09 14:47:25 -04:00
|
|
|
ΔelementStart(0, 'button-toggle');
|
2018-04-10 23:57:09 -04:00
|
|
|
{
|
2019-05-09 14:47:25 -04:00
|
|
|
Δlistener('change', function() { return ctx.onChange(); });
|
2018-04-10 23:57:09 -04:00
|
|
|
}
|
2019-05-09 14:47:25 -04:00
|
|
|
ΔelementEnd();
|
2018-02-16 14:36:50 -05:00
|
|
|
}
|
2019-05-09 14:47:25 -04:00
|
|
|
ΔembeddedViewEnd();
|
2018-04-10 23:57:09 -04:00
|
|
|
} else {
|
2019-05-09 14:47:25 -04:00
|
|
|
if (ΔembeddedViewStart(1, 1, 0)) {
|
|
|
|
ΔelementStart(0, 'div', ['otherDir', '']);
|
2018-04-10 23:57:09 -04:00
|
|
|
{
|
2019-05-09 14:47:25 -04:00
|
|
|
Δlistener('change', function() { return ctx.onChange(); });
|
2018-04-10 23:57:09 -04:00
|
|
|
}
|
2019-05-09 14:47:25 -04:00
|
|
|
ΔelementEnd();
|
2018-02-16 14:36:50 -05:00
|
|
|
}
|
2019-05-09 14:47:25 -04:00
|
|
|
ΔembeddedViewEnd();
|
2017-12-08 14:48:54 -05:00
|
|
|
}
|
|
|
|
}
|
2019-05-09 14:47:25 -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
|
|
|
});
|