Skip to content
New issue

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

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

Already on GitHub? # to your account

fix(tooltip): properly handle components using OnPush strategy #777

Closed
wants to merge 1 commit into from
Closed
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
26 changes: 24 additions & 2 deletions src/tooltip/tooltip.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {TestBed, ComponentFixture, inject} from '@angular/core/testing';
import {createGenericTestComponent} from '../test/common';

import {By} from '@angular/platform-browser';
import {Component, ViewChild} from '@angular/core';
import {Component, ViewChild, ChangeDetectionStrategy} from '@angular/core';

import {NgbTooltipModule} from './tooltip.module';
import {NgbTooltipWindow, NgbTooltip} from './tooltip';
Expand All @@ -11,6 +11,9 @@ import {NgbTooltipConfig} from './tooltip-config';
const createTestComponent =
(html: string) => <ComponentFixture<TestComponent>>createGenericTestComponent(html, TestComponent);

const createOnPushTestComponent =
(html: string) => <ComponentFixture<TestOnPushComponent>>createGenericTestComponent(html, TestOnPushComponent);

describe('ngb-tooltip-window', () => {
beforeEach(() => { TestBed.configureTestingModule({imports: [NgbTooltipModule]}); });

Expand All @@ -33,7 +36,9 @@ describe('ngb-tooltip-window', () => {

describe('ngb-tooltip', () => {

beforeEach(() => { TestBed.configureTestingModule({declarations: [TestComponent], imports: [NgbTooltipModule]}); });
beforeEach(() => {
TestBed.configureTestingModule({declarations: [TestComponent, TestOnPushComponent], imports: [NgbTooltipModule]});
});

function getWindow(fixture) { return fixture.nativeElement.querySelector('ngb-tooltip-window'); }

Expand Down Expand Up @@ -160,6 +165,19 @@ describe('ngb-tooltip', () => {
expect(windowEl).toHaveCssClass('tooltip-left');
expect(windowEl.textContent.trim()).toBe('Great tip!');
});

it('should properly position tooltips when a component is using the OnPush strategy', () => {
const fixture = createOnPushTestComponent(`<div ngbTooltip="Great tip!" placement="left"></div>`);
const directive = fixture.debugElement.query(By.directive(NgbTooltip));

directive.triggerEventHandler('mouseenter', {});
fixture.detectChanges();
const windowEl = getWindow(fixture);

expect(windowEl).toHaveCssClass('tooltip');
expect(windowEl).toHaveCssClass('tooltip-left');
expect(windowEl.textContent.trim()).toBe('Great tip!');
});
});

describe('triggers', () => {
Expand Down Expand Up @@ -325,3 +343,7 @@ export class TestComponent {

@ViewChild(NgbTooltip) tooltip: NgbTooltip;
}

@Component({selector: 'test-onpush-cmpt', changeDetection: ChangeDetectionStrategy.OnPush, template: ``})
export class TestOnPushComponent {
}
3 changes: 3 additions & 0 deletions src/tooltip/tooltip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ export class NgbTooltip implements OnInit, OnDestroy {
if (!this._windowRef && this._ngbTooltip) {
this._windowRef = this._popupService.open(this._ngbTooltip);
this._windowRef.instance.placement = this.placement;
// we need to manually invoke change detection since events registered via
// Renderer::listen() - to be determined if this is a bug in the Angular 2
this._windowRef.changeDetectorRef.markForCheck();
}
}

Expand Down