Skip to content

Commit

Permalink
feat(directive): add getDirectiveInstance to host
Browse files Browse the repository at this point in the history
  • Loading branch information
NetanelBasal committed May 28, 2018
1 parent 9b8b256 commit 9b2ffc6
Showing 3 changed files with 32 additions and 14 deletions.
26 changes: 15 additions & 11 deletions src/app/auto-focus.directive.spec.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,39 @@
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<AutoFocusDirective, CustomHostComponent>;

const createHost = createHostComponentFactory({
component: AutoFocusDirective,
host: CustomHostComponent
});

it('should be focused', () => {
it("should be focused", () => {
host = createHost(`<input datoAutoFocus="true">`);
const instance = host.getDirectiveInstance<AutoFocusDirective>(
AutoFocusDirective
);
expect(host.element).toBeFocused();
});

it('should NOT be focused', () => {
it("should NOT be focused", () => {
host = createHost(`<input [datoAutoFocus]="false">`);
expect(host.element).not.toBeFocused();
});

it('should work with dynamic input', () => {
it("should work with dynamic input", () => {
host = createHost(`<input [datoAutoFocus]="isFocused">`);
expect(host.element).not.toBeFocused();
host.setHostInput({isFocused: true});
host.setHostInput({ isFocused: true });
expect(host.element).toBeFocused();
});
});
});
10 changes: 7 additions & 3 deletions src/app/auto-focus.directive.ts
Original file line number Diff line number Diff line change
@@ -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) {}
}

public method() {
console.log();
}
}
10 changes: 10 additions & 0 deletions src/lib/src/host.ts
Original file line number Diff line number Diff line change
@@ -51,6 +51,16 @@ export class SpectatorWithHost<C, H = HostComponent> extends Spectator<C> {
_setInput(input, inputValue, this.hostComponent);
this.hostFixture.detectChanges();
}

getDirectiveInstance<T>(directive: Type<any>, all?: false): T;
getDirectiveInstance<T>(directive: Type<any>, all?: true): T[];
getDirectiveInstance<T>(directive: Type<any>, 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<C, H = HostComponent>(component: Type<C>): (template: string, detectChanges?: boolean, complexInputs?: Partial<C>) => SpectatorWithHost<C, H>;

0 comments on commit 9b2ffc6

Please sign in to comment.