From 3bbe4cd5357da404c9545474dbe246260b908abd Mon Sep 17 00:00:00 2001 From: mehmet-erim Date: Wed, 16 Oct 2019 09:49:29 +0300 Subject: [PATCH] test(core): add visibility.directive.spec --- .../lib/directives/visibility.directive.ts | 12 +- .../lib/tests/visibility.directive.spec.ts | 128 ++++++++++++++++++ 2 files changed, 136 insertions(+), 4 deletions(-) create mode 100644 npm/ng-packs/packages/core/src/lib/tests/visibility.directive.spec.ts diff --git a/npm/ng-packs/packages/core/src/lib/directives/visibility.directive.ts b/npm/ng-packs/packages/core/src/lib/directives/visibility.directive.ts index 2428b736a3..bbbc0417d5 100644 --- a/npm/ng-packs/packages/core/src/lib/directives/visibility.directive.ts +++ b/npm/ng-packs/packages/core/src/lib/directives/visibility.directive.ts @@ -3,7 +3,7 @@ import { Subject } from 'rxjs'; import snq from 'snq'; @Directive({ - selector: '[abpVisibility]' + selector: '[abpVisibility]', }) export class VisibilityDirective implements AfterViewInit { @Input('abpVisibility') @@ -17,6 +17,10 @@ export class VisibilityDirective implements AfterViewInit { constructor(@Optional() private elRef: ElementRef, private renderer: Renderer2) {} ngAfterViewInit() { + if (!this.focusedElement && this.elRef) { + this.focusedElement = this.elRef.nativeElement; + } + let observer: MutationObserver; if (this.mutationObserverEnabled) { observer = new MutationObserver(mutations => { @@ -25,7 +29,7 @@ export class VisibilityDirective implements AfterViewInit { const htmlNodes = snq( () => Array.from(mutation.target.childNodes).filter(node => node instanceof HTMLElement), - [] + [], ); if (!htmlNodes.length) { @@ -40,13 +44,13 @@ export class VisibilityDirective implements AfterViewInit { }); observer.observe(this.focusedElement, { - childList: true + childList: true, }); } else { setTimeout(() => { const htmlNodes = snq( () => Array.from(this.focusedElement.childNodes).filter(node => node instanceof HTMLElement), - [] + [], ); if (!htmlNodes.length) this.removeFromDOM(); diff --git a/npm/ng-packs/packages/core/src/lib/tests/visibility.directive.spec.ts b/npm/ng-packs/packages/core/src/lib/tests/visibility.directive.spec.ts new file mode 100644 index 0000000000..bbb669f70a --- /dev/null +++ b/npm/ng-packs/packages/core/src/lib/tests/visibility.directive.spec.ts @@ -0,0 +1,128 @@ +import { SpectatorDirective, createDirectiveFactory } from '@ngneat/spectator/jest'; +import { VisibilityDirective } from '../directives/visibility.directive'; + +describe('VisibilityDirective', () => { + let spectator: SpectatorDirective; + let directive: VisibilityDirective; + const createDirective = createDirectiveFactory({ + directive: VisibilityDirective, + }); + + describe('without mutation observer and without content', () => { + beforeEach(() => { + spectator = createDirective('
'); + directive = spectator.directive; + }); + + it('should be created', () => { + expect(directive).toBeTruthy(); + }); + + it('should be removed', done => { + setTimeout(() => { + expect(spectator.query('div')).toBeFalsy(); + done(); + }, 0); + }); + }); + + describe('without mutation observer and with content', () => { + beforeEach(() => { + spectator = createDirective( + '

Content

', + ); + directive = spectator.directive; + }); + + it('should not removed', done => { + setTimeout(() => { + expect(spectator.query('div')).toBeTruthy(); + done(); + }, 0); + }); + }); + + describe('without mutation observer and with focused element', () => { + beforeEach(() => { + spectator = createDirective( + '

Content

', + ); + directive = spectator.directive; + }); + + it('should not removed', done => { + setTimeout(() => { + expect(spectator.query('#main')).toBeTruthy(); + done(); + }, 0); + }); + }); + + describe('without content and with focused element', () => { + beforeEach(() => { + spectator = createDirective( + '
', + ); + directive = spectator.directive; + }); + + it('should be removed', done => { + setTimeout(() => { + expect(spectator.query('#main')).toBeFalsy(); + done(); + }, 0); + }); + }); + + describe('with mutation observer and with content', () => { + beforeEach(() => { + spectator = createDirective('
Content
'); + directive = spectator.directive; + }); + + it('should remove the main div element when content removed', done => { + spectator.query('#content').remove(); + + setTimeout(() => { + expect(spectator.query('div')).toBeFalsy(); + done(); + }, 0); + }); + + it('should not remove the main div element', done => { + spectator.query('div').appendChild(document.createElement('div')); + + setTimeout(() => { + expect(spectator.query('div')).toBeTruthy(); + done(); + }, 100); + }); + }); + + describe('with mutation observer and with focused element', () => { + beforeEach(() => { + spectator = createDirective( + '

Content

', + ); + directive = spectator.directive; + }); + + it('should remove the main div element when content removed', done => { + spectator.query('#content').remove(); + + setTimeout(() => { + expect(spectator.query('#main')).toBeFalsy(); + done(); + }, 0); + }); + + it('should not remove the main div element', done => { + spectator.query('#content').appendChild(document.createElement('div')); + + setTimeout(() => { + expect(spectator.query('#main')).toBeTruthy(); + done(); + }, 100); + }); + }); +});