From 9b2ffc6150d4a14d76dedf0cf5d4adf9f7fc85d3 Mon Sep 17 00:00:00 2001 From: Netanel Basal Date: Mon, 28 May 2018 18:02:53 +0300 Subject: [PATCH] feat(directive): add getDirectiveInstance to host --- src/app/auto-focus.directive.spec.ts | 26 +++++++++++++++----------- src/app/auto-focus.directive.ts | 10 +++++++--- src/lib/src/host.ts | 10 ++++++++++ 3 files changed, 32 insertions(+), 14 deletions(-) diff --git a/src/app/auto-focus.directive.spec.ts b/src/app/auto-focus.directive.spec.ts index 5052c5ab..329b0d94 100644 --- a/src/app/auto-focus.directive.spec.ts +++ b/src/app/auto-focus.directive.spec.ts @@ -1,14 +1,15 @@ -import { AutoFocusDirective } from './auto-focus.directive'; -import { Component } from '@angular/core'; -import { SpectatorWithHost } from '../lib/src'; -import { createHostComponentFactory } from '../lib/src/host'; +import { AutoFocusDirective } from "./auto-focus.directive"; +import { Component, Directive } from "@angular/core"; +import { SpectatorWithHost } from "../lib/src"; +import { createHostComponentFactory } from "../lib/src/host"; +import { By } from "@angular/platform-browser"; -@Component({ selector: 'custom-host', template: '' }) +@Component({ selector: "custom-host", template: "" }) class CustomHostComponent { isFocused = false; } -describe('DatoAutoFocusDirective', function() { +describe("DatoAutoFocusDirective", function() { let host: SpectatorWithHost; const createHost = createHostComponentFactory({ @@ -16,20 +17,23 @@ describe('DatoAutoFocusDirective', function() { host: CustomHostComponent }); - it('should be focused', () => { + it("should be focused", () => { host = createHost(``); + const instance = host.getDirectiveInstance( + AutoFocusDirective + ); expect(host.element).toBeFocused(); }); - it('should NOT be focused', () => { + it("should NOT be focused", () => { host = createHost(``); expect(host.element).not.toBeFocused(); }); - it('should work with dynamic input', () => { + it("should work with dynamic input", () => { host = createHost(``); expect(host.element).not.toBeFocused(); - host.setHostInput({isFocused: true}); + host.setHostInput({ isFocused: true }); expect(host.element).toBeFocused(); }); -}); \ No newline at end of file +}); diff --git a/src/app/auto-focus.directive.ts b/src/app/auto-focus.directive.ts index 36aab0e6..0c917e10 100644 --- a/src/app/auto-focus.directive.ts +++ b/src/app/auto-focus.directive.ts @@ -1,7 +1,7 @@ -import { Directive, ElementRef, Input } from '@angular/core'; +import { Directive, ElementRef, Input } from "@angular/core"; @Directive({ - selector: '[datoAutoFocus]' + selector: "[datoAutoFocus]" }) export class AutoFocusDirective { @Input() @@ -12,4 +12,8 @@ export class AutoFocusDirective { } public constructor(private host: ElementRef) {} -} \ No newline at end of file + + public method() { + console.log(); + } +} diff --git a/src/lib/src/host.ts b/src/lib/src/host.ts index d293dba4..e49e41fb 100644 --- a/src/lib/src/host.ts +++ b/src/lib/src/host.ts @@ -51,6 +51,16 @@ export class SpectatorWithHost extends Spectator { _setInput(input, inputValue, this.hostComponent); this.hostFixture.detectChanges(); } + + getDirectiveInstance(directive: Type, all?: false): T; + getDirectiveInstance(directive: Type, all?: true): T[]; + getDirectiveInstance(directive: Type, all = false): T | T[] { + if (all) { + return this.hostFixture.debugElement.queryAll(By.directive(directive)).map(d => d.injector.get(directive)); + } + + return this.hostFixture.debugElement.query(By.directive(directive)).injector.get(directive); + } } export function createHostComponentFactory(component: Type): (template: string, detectChanges?: boolean, complexInputs?: Partial) => SpectatorWithHost;