|
8 | 8 |
|
9 | 9 |
|
10 | 10 | import {CommonModule, NgIfContext} from '@angular/common';
|
11 |
| -import {Component, DebugNode, Directive, ElementRef, EmbeddedViewRef, EventEmitter, HostBinding, Injectable, Input, NO_ERRORS_SCHEMA, Renderer2, TemplateRef, ViewChild, ViewContainerRef} from '@angular/core'; |
| 11 | +import {Component, DebugNode, Directive, ElementRef, EmbeddedViewRef, EventEmitter, HostBinding, Injectable, Input, NO_ERRORS_SCHEMA, Output, Renderer2, TemplateRef, ViewChild, ViewContainerRef} from '@angular/core'; |
12 | 12 | import {ComponentFixture, TestBed, async} from '@angular/core/testing';
|
13 | 13 | import {By} from '@angular/platform-browser/src/dom/debug/by';
|
14 | 14 | import {getDOM} from '@angular/platform-browser/src/dom/dom_adapter';
|
@@ -927,5 +927,40 @@ class TestCmptWithPropBindings {
|
927 | 927 | expect(div.attributes.foo).toBe('bar');
|
928 | 928 | });
|
929 | 929 |
|
| 930 | + it('should clear event listeners when node is destroyed', () => { |
| 931 | + let calls = 0; |
| 932 | + @Component({ |
| 933 | + selector: 'cancel-button', |
| 934 | + template: '', |
| 935 | + }) |
| 936 | + class CancelButton { |
| 937 | + @Output() cancel = new EventEmitter<void>(); |
| 938 | + } |
| 939 | + |
| 940 | + @Component({ |
| 941 | + template: '<cancel-button *ngIf="visible" (cancel)="cancel()"></cancel-button>', |
| 942 | + }) |
| 943 | + class App { |
| 944 | + visible = true; |
| 945 | + cancel() { calls++; } |
| 946 | + } |
| 947 | + |
| 948 | + TestBed.configureTestingModule({declarations: [App, CancelButton]}); |
| 949 | + const fixture = TestBed.createComponent(App); |
| 950 | + fixture.detectChanges(); |
| 951 | + |
| 952 | + const button = fixture.debugElement.query(By.directive(CancelButton)); |
| 953 | + button.triggerEventHandler('cancel', {}); |
| 954 | + |
| 955 | + expect(calls).toBe(1, 'Expected calls to be 1 after one event.'); |
| 956 | + |
| 957 | + fixture.componentInstance.visible = false; |
| 958 | + fixture.detectChanges(); |
| 959 | + |
| 960 | + button.triggerEventHandler('cancel', {}); |
| 961 | + |
| 962 | + expect(calls).toBe(1, 'Expected calls to stay 1 after destroying the node.'); |
| 963 | + }); |
| 964 | + |
930 | 965 | });
|
931 | 966 | }
|
0 commit comments