Skip to content

Commit

Permalink
fix: override component providers (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
timdeschryver authored May 31, 2019
1 parent f99e707 commit 474253f
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
9 changes: 8 additions & 1 deletion projects/testing-library/src/lib/testing-library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,18 @@ export async function render<T>(

TestBed.configureTestingModule({
declarations: [...declarations, ...componentDeclarations],
providers: [...providers],
imports: [...imports],
schemas: [...schemas],
});

if (providers) {
// override services this way to have the service overridden at the component level
providers.forEach(p => {
const { provide, ...provider } = p;
TestBed.overrideProvider(provide, provider);
});
}

const fixture = isTemplate
? createWrapperComponentFixture(templateOrComponent as string, { wrapper, componentProperties })
: createComponentFixture(templateOrComponent as Type<T>, { componentProperties });
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { Injectable } from '@angular/core';
import { Component, Input } from '@angular/core';
import { render } from '../../src/public_api';
import { TestBed } from '@angular/core/testing';

// tslint:disable: no-use-before-declare
// tslint:disable: no-use-before-declare
test('shows the service value', async () => {
const { getByText } = await render(FixtureComponent, {});
getByText('foo');
});

test('shows the provided service value', async () => {
const { getByText } = await render(FixtureComponent, {
providers: [
{
provide: Service,
useValue: {
foo() {
return 'bar';
},
},
},
],
});

getByText('bar');
});

@Injectable()
export class Service {
foo() {
return 'foo';
}
}

@Component({
template: '{{service.foo()}}',
providers: [Service],
})
export class FixtureComponent {
constructor(public service: Service) {}
}

0 comments on commit 474253f

Please sign in to comment.