Skip to content

fix: add providers to configureTestingModule #22

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"@angular/platform-browser": "^8.0.0",
"@angular/platform-browser-dynamic": "^8.0.0",
"@angular/router": "^8.0.0",
"@ngrx/store": "^8.0.0-rc.0",
"@phenomnomnominal/tsquery": "^3.0.0",
"@testing-library/dom": "^5.0.1",
"core-js": "^3.1.3",
Expand Down
11 changes: 7 additions & 4 deletions projects/testing-library/src/lib/testing-library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,18 @@ export async function render<T>(
TestBed.configureTestingModule({
declarations: [...declarations, ...componentDeclarations],
imports: [...imports],
providers: [...providers],
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);
});
providers
.reduce((acc, provider) => acc.concat(provider), [])
.forEach(p => {
const { provide, ...provider } = p;
TestBed.overrideProvider(provide, provider);
});
}

const fixture = isTemplate
Expand Down
4 changes: 4 additions & 0 deletions src/app/app.component.spec.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,30 @@
import { AppComponent } from './app.component';
import { render } from '@testing-library/angular';
import { configureJestSetup } from '@testing-library/angular/jest-utils';
import { provideMockStore } from '@ngrx/store/testing';

configureJestSetup();

test(`matches snapshot`, async () => {
const { container } = await render('<app-root></app-root>', {
declarations: [AppComponent],
providers: [provideMockStore()],
});
expect(container).toMatchSnapshot();
});

test(`should have a title`, async () => {
const { getByText } = await render('<app-root></app-root>', {
declarations: [AppComponent],
providers: [provideMockStore()],
});
expect(getByText('Welcome to app!')).toBeDefined();
});

test(`should render title in a h1 tag`, async () => {
const { container } = await render('<app-root></app-root>', {
declarations: [AppComponent],
providers: [provideMockStore()],
});
expect(container.querySelector('h1').textContent).toContain('Welcome to app!');
});
5 changes: 4 additions & 1 deletion src/app/app.component.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { Component } from '@angular/core';
import { Store } from '@ngrx/store';

@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
styleUrls: ['./app.component.css'],
})
export class AppComponent {
title = 'app';

constructor(private store: Store<any>) {}
}
13 changes: 5 additions & 8 deletions src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@ import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { AppComponent } from './app.component';
import { StoreModule } from '@ngrx/store';

@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule
],
declarations: [AppComponent],
imports: [BrowserModule, StoreModule.forRoot({})],
providers: [],
bootstrap: [AppComponent]
bootstrap: [AppComponent],
})
export class AppModule { }
export class AppModule {}
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,11 @@
call-me-maybe "^1.0.1"
glob-to-regexp "^0.3.0"

"@ngrx/store@^8.0.0-rc.0":
version "8.0.0-rc.0"
resolved "https://registry.yarnpkg.com/@ngrx/store/-/store-8.0.0-rc.0.tgz#3ec0ca8986fb3cb2b246cab0c851833a21f9a134"
integrity sha512-TL+2BERGH4DET8sIW1D02wkmKxlnVXWezm1OkQJd2Q/6h6MwZBd8SeDpuiEIUHr2/1BPUyPNe1bj+vemeiwWrw==

"@ngtools/json-schema@^1.1.0":
version "1.1.0"
resolved "https://registry.yarnpkg.com/@ngtools/json-schema/-/json-schema-1.1.0.tgz#c3a0c544d62392acc2813a42c8a0dc6f58f86922"
Expand Down