Skip to content

Commit

Permalink
Remove directive instance from storage on destruction
Browse files Browse the repository at this point in the history
* Implement unit test for wrapper component remove
  • Loading branch information
farengeyt451 committed Jun 24, 2021
1 parent 61a1627 commit f3d683a
Show file tree
Hide file tree
Showing 15 changed files with 108 additions and 65 deletions.
59 changes: 37 additions & 22 deletions projects/demo/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion projects/demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"@angular/platform-browser": "^11.2.5",
"@angular/platform-browser-dynamic": "^11.2.5",
"@angular/router": "^11.2.5",
"ngx-tippy-wrapper": "^3.0.0",
"ngx-tippy-wrapper": "^3.0.1",
"rxjs": "~6.6.6",
"tslib": "^2.1.0",
"zone.js": "^0.11.4"
Expand Down
2 changes: 1 addition & 1 deletion projects/ngx-tippy-wrapper/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ngx-tippy-wrapper",
"version": "3.0.0",
"version": "3.0.1",
"description": "Angular 8+ wrapper for Tippy.js",
"keywords": [
"tooltip",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Component, ElementRef, Input, ViewChild, Inject, PLATFORM_ID, AfterViewInit } from '@angular/core';
import { isPlatformServer } from '@angular/common';
import { AfterViewInit, Component, ElementRef, Inject, Input, PLATFORM_ID, ViewChild } from '@angular/core';
import tippy from 'tippy.js';
import { NgxTippyProps } from './ngx-tippy.interfaces';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { Component, Input, Inject, PLATFORM_ID, AfterViewInit, ViewChild, ElementRef } from '@angular/core';
import { isPlatformServer } from '@angular/common';
import { AfterViewInit, Component, ElementRef, Inject, Input, PLATFORM_ID, ViewChild } from '@angular/core';
import { createSingleton } from 'tippy.js';
import { NgxTippyService } from './ngx-tippy.service';
import {
NgxSingletonProps,
NgxTippyInstance,
NgxTippySingletonInstance,
TippyHTMLElement,
NgxSingletonProps,
} from './ngx-tippy.interfaces';
import { NgxTippyService } from './ngx-tippy.service';

/**
* Tippy singleton - single tippy element that takes the place of an array of regular tippy instances
Expand Down
23 changes: 16 additions & 7 deletions projects/ngx-tippy-wrapper/src/lib/ngx-tippy.directive.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
import { Directive, OnInit, ElementRef, Input, Renderer2, Inject, PLATFORM_ID } from '@angular/core';
import { isPlatformServer } from '@angular/common';
import { Directive, ElementRef, Inject, Input, OnDestroy, OnInit, PLATFORM_ID, Renderer2 } from '@angular/core';
import tippy from 'tippy.js';
import { NgxTippyContent, NgxTippyInstance, NgxTippyProps, TippyHTMLElement } from './ngx-tippy.interfaces';
import { NgxTippyService } from './ngx-tippy.service';
import { NgxTippyProps, NgxTippyInstance, NgxTippyContent, TippyHTMLElement } from './ngx-tippy.interfaces';
import { setTemplateVisible } from './ngx-tippy.utils';

@Directive({
selector: '[ngxTippy]',
})
export class NgxTippyDirective implements OnInit {
export class NgxTippyDirective implements OnInit, OnDestroy {
@Input() ngxTippy?: NgxTippyContent | null;
@Input() tippyProps?: NgxTippyProps;
@Input() tippyName?: string;
@Input() tippyClassName?: string;

private tippyInstance!: NgxTippyInstance;

constructor(
private tippyEl: ElementRef,
private ngxTippyService: NgxTippyService,
Expand All @@ -26,6 +28,10 @@ export class NgxTippyDirective implements OnInit {
this.initTippy();
}

ngOnDestroy() {
this.deleteEntryInStorage();
}

/**
* Tooltip initialize
* Content can be directly passed through `ngxTippy` selector
Expand All @@ -43,10 +49,9 @@ export class NgxTippyDirective implements OnInit {
}

private setTippyInstance(tippyTarget: TippyHTMLElement) {
const tippyInstance: NgxTippyInstance = tippyTarget._tippy;

this.setClassName(tippyInstance);
this.writeInstancesToStorage(tippyInstance);
this.tippyInstance = tippyTarget._tippy;
this.setClassName(this.tippyInstance);
this.writeInstancesToStorage(this.tippyInstance);
}

private setClassName(tippyInstance: NgxTippyInstance) {
Expand All @@ -69,4 +74,8 @@ export class NgxTippyDirective implements OnInit {
private writeInstancesToStorage(tippyInstance: NgxTippyInstance) {
this.ngxTippyService.setInstance(this.tippyName || `tippy-${tippyInstance.id}`, tippyInstance);
}

private deleteEntryInStorage() {
this.ngxTippyService.getInstances()?.delete(this.tippyName || `tippy-${this.tippyInstance.id}`);
}
}
2 changes: 1 addition & 1 deletion projects/ngx-tippy-wrapper/src/lib/ngx-tippy.interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Instance, Props, Content } from 'tippy.js';
import { Content, Instance, Props } from 'tippy.js';

export interface NgxTippyProps extends Partial<Props> {}

Expand Down
5 changes: 2 additions & 3 deletions projects/ngx-tippy-wrapper/src/lib/ngx-tippy.module.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';

import { NgxTippySingletonComponent } from './ngx-tippy-singleton.component';
import { NgModule } from '@angular/core';
import { NgxTippyGroupComponent } from './ngx-tippy-group.component';
import { NgxTippySingletonComponent } from './ngx-tippy-singleton.component';
import { NgxTippyDirective } from './ngx-tippy.directive';

@NgModule({
Expand Down
10 changes: 5 additions & 5 deletions projects/ngx-tippy-wrapper/src/lib/ngx-tippy.service.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { Injectable, Renderer2, RendererFactory2, isDevMode } from '@angular/core';
import { Injectable, isDevMode, Renderer2, RendererFactory2 } from '@angular/core';
import { Observable, Subject } from 'rxjs';
import tippy, { hideAll } from 'tippy.js';
import {
InstanceChangeReason,
InstancesChanges,
NgxHideAllOptions,
NgxTippyContent,
NgxTippyInstance,
NgxTippyProps,
NgxTippyContent,
NgxHideAllOptions,
InstancesChanges,
InstanceChangeReason,
NgxTippySingletonInstance,
} from './ngx-tippy.interfaces';
import { setTemplateVisible } from './ngx-tippy.utils';
Expand Down
6 changes: 3 additions & 3 deletions projects/ngx-tippy-wrapper/src/public-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
* Public API Surface of ngx-tippy-wrapper
*/

export * from './lib/ngx-tippy.interfaces';
export * from './lib/ngx-tippy.directive';
export * from './lib/ngx-tippy-group.component';
export * from './lib/ngx-tippy-singleton.component';
export * from './lib/ngx-tippy.service';
export * from './lib/ngx-tippy.directive';
export * from './lib/ngx-tippy.interfaces';
export * from './lib/ngx-tippy.module';
export * from './lib/ngx-tippy.service';
10 changes: 2 additions & 8 deletions projects/ngx-tippy-wrapper/src/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,12 @@
import 'zone.js/dist/zone';
import 'zone.js/dist/zone-testing';
import { getTestBed } from '@angular/core/testing';
import {
BrowserDynamicTestingModule,
platformBrowserDynamicTesting
} from '@angular/platform-browser-dynamic/testing';
import { BrowserDynamicTestingModule, platformBrowserDynamicTesting } from '@angular/platform-browser-dynamic/testing';

declare const require: any;

// First, initialize the Angular testing environment.
getTestBed().initTestEnvironment(
BrowserDynamicTestingModule,
platformBrowserDynamicTesting()
);
getTestBed().initTestEnvironment(BrowserDynamicTestingModule, platformBrowserDynamicTesting());
// Then we find all the tests.
const context = require.context('./', true, /\.spec\.ts$/);
// And load the modules.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Component, DebugElement, PLATFORM_ID } from '@angular/core';
import { TestBed, ComponentFixture, getTestBed, fakeAsync, tick } from '@angular/core/testing';
import { ComponentFixture, fakeAsync, getTestBed, TestBed, tick } from '@angular/core/testing';
import { By } from '@angular/platform-browser';
import { NgxTippyGroupComponent } from '../lib/ngx-tippy-group.component';
import { NgxTippyProps } from '../lib/ngx-tippy.interfaces';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Component, DebugElement, PLATFORM_ID } from '@angular/core';
import { ComponentFixture, fakeAsync, getTestBed, TestBed, tick } from '@angular/core/testing';
import { By } from '@angular/platform-browser';
import { TestBed, ComponentFixture, getTestBed, fakeAsync, tick } from '@angular/core/testing';
import { NgxSingletonProps, NgxTippyProps } from '../lib/ngx-tippy.interfaces';
import { NgxTippySingletonComponent } from '../lib/ngx-tippy-singleton.component';
import { NgxTippyService } from '../lib/ngx-tippy.service';
import { NgxTippyDirective } from '../lib/ngx-tippy.directive';
import { NgxSingletonProps, NgxTippyProps } from '../lib/ngx-tippy.interfaces';
import { NgxTippyService } from '../lib/ngx-tippy.service';

const template = `
<div class="singleton">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { TestBed, ComponentFixture, getTestBed, fakeAsync, tick } from '@angular/core/testing';
import { DebugElement, Component, ViewChild, ViewContainerRef, Compiler, NgModule, PLATFORM_ID } from '@angular/core';
import { Compiler, Component, DebugElement, NgModule, PLATFORM_ID, ViewChild, ViewContainerRef } from '@angular/core';
import { ComponentFixture, fakeAsync, getTestBed, TestBed, tick } from '@angular/core/testing';
import { BrowserModule, By } from '@angular/platform-browser';
import { fakeInstance } from '../fixtures/tippy-instance.fixture';
import { NgxTippyDirective } from '../lib/ngx-tippy.directive';
import { NgxTippyService } from '../lib/ngx-tippy.service';
import { fakeInstance } from '../fixtures/tippy-instance.fixture';

const styles = [
`
Expand Down Expand Up @@ -132,10 +132,36 @@ describe('Directive: NgxTippyDirective', () => {
tooltipDebugEl.nativeElement.dispatchEvent(new MouseEvent('click'));
fixture.detectChanges();
const tooltip = fixture.debugElement.query(By.css('.tippy-content'));

expect(tooltip).toBeNull('Tooltip initialized');
});

it('Should destroy tooltip in case component destruction', () => {
component.addComponent(
`
<div class="test">
<button
class="test__btn"
[ngxTippy]="'Value'"
[tippyProps]="{
appendTo: 'parent',
trigger: 'click'
}"
>
Element with tooltip
</button>
</div>
`,
styles
);

fixture.destroy();
fixture.detectChanges();
tooltipDebugEl = fixture.debugElement.query(By.directive(NgxTippyDirective));
tooltipDebugEl.nativeElement.dispatchEvent(new MouseEvent('click'));
const tooltip = fixture.debugElement.query(By.css('.tippy-content'));
expect(tooltip).toBeNull('Tooltip did not destroy');
});

it('Should show tooltip on hover', () => {
component.addComponent(
`
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { TestBed, getTestBed, fakeAsync } from '@angular/core/testing';
import { DevModeService, NgxTippyService } from '../lib/ngx-tippy.service';
import { fakeAsync, getTestBed, TestBed } from '@angular/core/testing';
import { fakeInstance } from '../fixtures/tippy-instance.fixture';
import { NgxTippyProps } from '../lib/ngx-tippy.interfaces';
import { DevModeService, NgxTippyService } from '../lib/ngx-tippy.service';

describe('Service: NgxTippyWrapperService - Instance exist ', () => {
let injector: TestBed;
Expand Down

0 comments on commit f3d683a

Please sign in to comment.