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-17 21:49:21 -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-10-11 17:18:45 -04:00
|
|
|
static ɵfac = () => buttonToggle = new ButtonToggle();
|
2019-10-10 17:57:15 -04:00
|
|
|
static ɵcmp = ɵɵ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) {},
|
2019-09-23 14:08:51 -04:00
|
|
|
decls: 0,
|
2018-08-18 14:14:50 -04:00
|
|
|
vars: 0,
|
2017-12-01 17:23:03 -05:00
|
|
|
outputs: {change: 'change', resetStream: 'reset'}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-12-08 14:48:54 -05:00
|
|
|
let otherDir: OtherDir;
|
|
|
|
|
|
|
|
class OtherDir {
|
|
|
|
changeStream = new EventEmitter();
|
|
|
|
|
2019-10-11 17:18:45 -04:00
|
|
|
static ɵfac = () => otherDir = new OtherDir;
|
2019-10-11 15:28:12 -04:00
|
|
|
static ɵdir = ɵɵdefineDirective(
|
2019-08-12 02:26:20 -04:00
|
|
|
{type: OtherDir, selectors: [['', '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-17 21:49:21 -04:00
|
|
|
ɵɵelementStart(0, 'button');
|
2017-12-08 14:48:54 -05:00
|
|
|
{
|
2019-05-17 21:49:21 -04:00
|
|
|
ɵɵlistener('click', function() { return ctx.onClick(); });
|
|
|
|
ɵɵtext(1, 'Click me');
|
2017-12-08 14:48:54 -05:00
|
|
|
}
|
2019-05-17 21:49:21 -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-17 21:49:21 -04:00
|
|
|
ɵɵcontainerRefreshStart(2);
|
2018-04-10 23:57:09 -04:00
|
|
|
{
|
|
|
|
if (ctx.condition) {
|
2019-05-17 21:49:21 -04:00
|
|
|
let rf1 = ɵɵembeddedViewStart(0, 1, 0);
|
2018-04-10 23:57:09 -04:00
|
|
|
if (rf1 & RenderFlags.Create) {
|
2019-05-17 21:49:21 -04:00
|
|
|
ɵɵelementStart(0, 'button-toggle');
|
2018-04-10 23:57:09 -04:00
|
|
|
{
|
2019-05-17 21:49:21 -04:00
|
|
|
ɵɵlistener('change', function() { return ctx.onChange(); });
|
2018-04-10 23:57:09 -04:00
|
|
|
}
|
2019-05-17 21:49:21 -04:00
|
|
|
ɵɵelementEnd();
|
2018-02-16 14:36:50 -05:00
|
|
|
}
|
2019-05-17 21:49:21 -04:00
|
|
|
ɵɵembeddedViewEnd();
|
2018-04-10 23:57:09 -04:00
|
|
|
} else {
|
2019-05-17 21:49:21 -04:00
|
|
|
if (ɵɵembeddedViewStart(1, 1, 0)) {
|
2019-09-23 14:08:51 -04:00
|
|
|
ɵɵelementStart(0, 'div', 0);
|
2018-04-10 23:57:09 -04:00
|
|
|
{
|
2019-05-17 21:49:21 -04:00
|
|
|
ɵɵlistener('change', function() { return ctx.onChange(); });
|
2018-04-10 23:57:09 -04:00
|
|
|
}
|
2019-05-17 21:49:21 -04:00
|
|
|
ɵɵelementEnd();
|
2018-02-16 14:36:50 -05:00
|
|
|
}
|
2019-05-17 21:49:21 -04:00
|
|
|
ɵɵembeddedViewEnd();
|
2017-12-08 14:48:54 -05:00
|
|
|
}
|
|
|
|
}
|
2019-05-17 21:49:21 -04:00
|
|
|
ɵɵcontainerRefreshEnd();
|
2017-12-08 14:48:54 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let counter = 0;
|
|
|
|
const ctx = {condition: true, onChange: () => counter++, onClick: () => {}};
|
2019-09-23 14:08:51 -04:00
|
|
|
const attrs = [['otherDir', '']];
|
|
|
|
renderToHtml(Template, ctx, 3, 0, deps, null, null, false, attrs);
|
2017-12-08 14:48:54 -05:00
|
|
|
|
|
|
|
buttonToggle !.change.next();
|
|
|
|
expect(counter).toEqual(1);
|
|
|
|
|
|
|
|
ctx.condition = false;
|
2019-09-23 14:08:51 -04:00
|
|
|
renderToHtml(Template, ctx, 3, 0, deps, null, null, false, attrs);
|
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
|
|
|
});
|