Skip to content

Commit

Permalink
feat(spectator): added directiveProviders to spectator-directive (#236)
Browse files Browse the repository at this point in the history
  • Loading branch information
tamasfoldi committed Dec 3, 2019
1 parent 2f18165 commit 4748c5f
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 3 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -866,6 +866,9 @@ To access the provider, get it from the component injector using the `fromCompon
spectator.get(FooService, true)
```
The same rules also apply to directives using the `directiveProviders` and `directiveMocks` parameters.
## Custom Matchers
```ts
expect('.zippy__content').not.toExist();
Expand Down
14 changes: 13 additions & 1 deletion projects/spectator/src/lib/spectator-directive/create-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export interface SpectatorDirectiveOverrides<D, H, HP> extends BaseSpectatorOver
detectChanges?: boolean;
props?: Partial<D>;
hostProps?: HostComponent extends H ? HP : Partial<H>;
directiveProviders?: Provider[];
}

/**
Expand All @@ -49,7 +50,12 @@ export function createDirectiveFactory<D, H = HostComponent>(
}));

return <HP>(template: string, overrides?: SpectatorDirectiveOverrides<D, H, HP>) => {
const defaults: SpectatorDirectiveOverrides<D, H, HP> = { props: {}, hostProps: {} as any, detectChanges: true, providers: [] };
const defaults: SpectatorDirectiveOverrides<D, H, HP> = {
props: {},
hostProps: {} as any,
detectChanges: true,
providers: []
};
const { detectChanges, props, hostProps, providers } = { ...defaults, ...overrides };

if (providers && providers.length) {
Expand All @@ -66,6 +72,12 @@ export function createDirectiveFactory<D, H = HostComponent>(
set: { template }
});

if (options.directiveProviders.length || options.directiveMocks.length) {
TestBed.overrideDirective(options.directive, {
set: { providers: [...options.directiveProviders, ...options.directiveMocks.map(p => options.mockProvider(p))] }
});
}

const spectator = createSpectatorDirective(options, props, hostProps);

if (options.detectChanges && detectChanges) {
Expand Down
8 changes: 6 additions & 2 deletions projects/spectator/src/lib/spectator-directive/options.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Type } from '@angular/core';
import { Type, Provider } from '@angular/core';

import { merge } from '../internals/merge';
import { OptionalsRequired } from '../types';
Expand All @@ -14,14 +14,18 @@ export interface SpectatorDirectiveOptions<D, H> extends BaseSpectatorOptions {
detectChanges?: boolean;
host?: Type<H>;
template?: string;
directiveProviders?: Provider[];
directiveMocks?: Type<any>[];
}

const defaultSpectatorRoutingOptions: OptionalsRequired<SpectatorDirectiveOptions<any, any>> = {
...getDefaultBaseOptions(),
host: HostComponent,
template: '',
shallow: false,
detectChanges: true
detectChanges: true,
directiveProviders: [],
directiveMocks: []
};

/**
Expand Down
20 changes: 20 additions & 0 deletions projects/spectator/test/directive-providers.directive.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { FormBuilder } from '@angular/forms';
import { createDirectiveFactory, SpectatorDirective } from '@ngneat/spectator';

import { DirectiveProviderDirective, directiveProviderToken } from './directive-providers.directive';

describe('DirectiveProviderDirective', () => {
let host: SpectatorDirective<DirectiveProviderDirective>;

const createHost = createDirectiveFactory({
directive: DirectiveProviderDirective,
directiveProviders: [{ provide: directiveProviderToken, useValue: 'notTest' }],
directiveMocks: [FormBuilder]
});

it('should inject the provided value', () => {
host = createHost(`<div directiveProvider>Testing Directive Providers</div>`);

expect(host.directive.provider).toEqual('notTest');
});
});
12 changes: 12 additions & 0 deletions projects/spectator/test/directive-providers.directive.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Directive, Inject, InjectionToken } from '@angular/core';
import { FormBuilder } from '@angular/forms';

export const directiveProviderToken = new InjectionToken('DirectiveProviderToken');

@Directive({
selector: '[directiveProvider]',
providers: [{ provide: directiveProviderToken, useValue: 'test' }]
})
export class DirectiveProviderDirective {
constructor(@Inject(directiveProviderToken) public provider: string, private fb: FormBuilder) {}
}

0 comments on commit 4748c5f

Please sign in to comment.