Skip to content

fix(directionality): complete change event on destroy #10826

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 21 additions & 8 deletions src/cdk/bidi/directionality.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ describe('Directionality', () => {
it('should read dir from the html element if not specified on the body', () => {
fakeDocument.documentElement.dir = 'rtl';

let fixture = TestBed.createComponent(InjectsDirectionality);
let testComponent = fixture.debugElement.componentInstance;
const fixture = TestBed.createComponent(InjectsDirectionality);
const testComponent = fixture.debugElement.componentInstance;

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

let fixture = TestBed.createComponent(InjectsDirectionality);
let testComponent = fixture.debugElement.componentInstance;
const fixture = TestBed.createComponent(InjectsDirectionality);
const testComponent = fixture.debugElement.componentInstance;

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

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

expect(testComponent.dir.value).toBe('ltr');
});

it('should complete the `change` stream on destroy', () => {
const fixture = TestBed.createComponent(InjectsDirectionality);
const spy = jasmine.createSpy('complete spy');
const subscription =
fixture.componentInstance.dir.change.subscribe(undefined, undefined, spy);

fixture.componentInstance.dir.ngOnDestroy();
expect(spy).toHaveBeenCalled();

subscription.unsubscribe();
});

});

describe('Dir directive', () => {
it('should provide itself as Directionality', () => {
let fixture = TestBed.createComponent(ElementWithDir);
const fixture = TestBed.createComponent(ElementWithDir);
const injectedDirectionality =
fixture.debugElement.query(By.directive(InjectsDirectionality)).componentInstance.dir;

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

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

Expand Down
8 changes: 6 additions & 2 deletions src/cdk/bidi/directionality.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/

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


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

Expand All @@ -36,4 +36,8 @@ export class Directionality {
this.value = (bodyDir || htmlDir || 'ltr') as Direction;
}
}

ngOnDestroy() {
this.change.complete();
}
}