Skip to content

Commit

Permalink
fix(tooltip): properly handle components using OnPush strategy
Browse files Browse the repository at this point in the history
Fixes #772

Closes #777
  • Loading branch information
pkozlowski-opensource committed Sep 21, 2016
1 parent a9f0a5e commit 667833c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
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

0 comments on commit 667833c

Please sign in to comment.