Skip to content

Commit b057391

Browse files
crisbetommalerba
authored andcommitted
fix(directionality): complete change event on destroy (#10826)
Fixes the `change` event on the top-level `Directionality` service not being completed.
1 parent 2af9c35 commit b057391

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

src/cdk/bidi/directionality.spec.ts

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ describe('Directionality', () => {
2020
it('should read dir from the html element if not specified on the body', () => {
2121
fakeDocument.documentElement.dir = 'rtl';
2222

23-
let fixture = TestBed.createComponent(InjectsDirectionality);
24-
let testComponent = fixture.debugElement.componentInstance;
23+
const fixture = TestBed.createComponent(InjectsDirectionality);
24+
const testComponent = fixture.debugElement.componentInstance;
2525

2626
expect(testComponent.dir.value).toBe('rtl');
2727
});
@@ -30,23 +30,36 @@ describe('Directionality', () => {
3030
fakeDocument.documentElement.dir = 'ltr';
3131
fakeDocument.body.dir = 'rtl';
3232

33-
let fixture = TestBed.createComponent(InjectsDirectionality);
34-
let testComponent = fixture.debugElement.componentInstance;
33+
const fixture = TestBed.createComponent(InjectsDirectionality);
34+
const testComponent = fixture.debugElement.componentInstance;
3535

3636
expect(testComponent.dir.value).toBe('rtl');
3737
});
3838

3939
it('should default to ltr if nothing is specified on either body or the html element', () => {
40-
let fixture = TestBed.createComponent(InjectsDirectionality);
41-
let testComponent = fixture.debugElement.componentInstance;
40+
const fixture = TestBed.createComponent(InjectsDirectionality);
41+
const testComponent = fixture.debugElement.componentInstance;
4242

4343
expect(testComponent.dir.value).toBe('ltr');
4444
});
45+
46+
it('should complete the `change` stream on destroy', () => {
47+
const fixture = TestBed.createComponent(InjectsDirectionality);
48+
const spy = jasmine.createSpy('complete spy');
49+
const subscription =
50+
fixture.componentInstance.dir.change.subscribe(undefined, undefined, spy);
51+
52+
fixture.componentInstance.dir.ngOnDestroy();
53+
expect(spy).toHaveBeenCalled();
54+
55+
subscription.unsubscribe();
56+
});
57+
4558
});
4659

4760
describe('Dir directive', () => {
4861
it('should provide itself as Directionality', () => {
49-
let fixture = TestBed.createComponent(ElementWithDir);
62+
const fixture = TestBed.createComponent(ElementWithDir);
5063
const injectedDirectionality =
5164
fixture.debugElement.query(By.directive(InjectsDirectionality)).componentInstance.dir;
5265

@@ -56,7 +69,7 @@ describe('Directionality', () => {
5669
});
5770

5871
it('should emit a change event when the value changes', fakeAsync(() => {
59-
let fixture = TestBed.createComponent(ElementWithDir);
72+
const fixture = TestBed.createComponent(ElementWithDir);
6073
const injectedDirectionality =
6174
fixture.debugElement.query(By.directive(InjectsDirectionality)).componentInstance.dir;
6275

src/cdk/bidi/directionality.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {EventEmitter, Inject, Injectable, Optional} from '@angular/core';
9+
import {EventEmitter, Inject, Injectable, Optional, OnDestroy} from '@angular/core';
1010
import {DIR_DOCUMENT} from './dir-document-token';
1111

1212

@@ -18,7 +18,7 @@ export type Direction = 'ltr' | 'rtl';
1818
* Exposes the current direction and a stream of direction changes.
1919
*/
2020
@Injectable({providedIn: 'root'})
21-
export class Directionality {
21+
export class Directionality implements OnDestroy {
2222
/** The current 'ltr' or 'rtl' value. */
2323
readonly value: Direction = 'ltr';
2424

@@ -36,4 +36,8 @@ export class Directionality {
3636
this.value = (bodyDir || htmlDir || 'ltr') as Direction;
3737
}
3838
}
39+
40+
ngOnDestroy() {
41+
this.change.complete();
42+
}
3943
}

0 commit comments

Comments
 (0)