Skip to content
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

Schematics: Use MockStore in container generated tests #2029

Merged
merged 7 commits into from
Jul 31, 2019
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';

import { <%= classify(name) %>Component } from './<%= dasherize(name) %>.component';
import { Store, StoreModule } from '@ngrx/store';
import { provideMockStore, MockStore } from '@ngrx/store/testing';

describe('<%= classify(name) %>Component', () => {
let component: <%= classify(name) %>Component;
let fixture: ComponentFixture<<%= classify(name) %>Component>;
let store: Store<any>;
let store: MockStore<any>;

beforeEach(async() => {
TestBed.configureTestingModule({
imports: [ StoreModule.forRoot({}) ],
providers: [ provideMockStore() ],
declarations: [ <%= classify(name) %>Component ]
});

Expand All @@ -20,9 +20,8 @@ describe('<%= classify(name) %>Component', () => {
beforeEach(() => {
fixture = TestBed.createComponent(<%= classify(name) %>Component);
component = fixture.componentInstance;
store = TestBed.get<Store>(Store);
store = TestBed.get(Store);
jtcrowson marked this conversation as resolved.
Show resolved Hide resolved

spyOn(store, 'dispatch').and.callThrough();
fixture.detectChanges();
});

Expand Down
15 changes: 13 additions & 2 deletions modules/schematics/src/container/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,21 @@ describe('Container Schematic', () => {
`${projectPath}/src/app/foo/foo.component.spec.ts`
);
expect(content).toMatch(
/import { Store, StoreModule } from '@ngrx\/store';/
/import { provideMockStore, MockStore } from '@ngrx\/store\/testing';/
);
});

it('should use the provideMockStore helper', async () => {
const options = { ...defaultOptions, spec: true };
const tree = await schematicRunner
.runSchematicAsync('container', options, appTree)
.toPromise();
const content = tree.readContent(
`${projectPath}/src/app/foo/foo.component.spec.ts`
);
expect(content).toMatch(/providers: \[ provideMockStore\(\) \]/);
});

it('should inject Store correctly', async () => {
const options = { ...defaultOptions, spec: true };
const tree = await schematicRunner
Expand All @@ -130,6 +141,6 @@ describe('Container Schematic', () => {
const content = tree.readContent(
`${projectPath}/src/app/foo/foo.component.spec.ts`
);
expect(content).toMatch(/store = TestBed\.get<Store>\(Store\);/);
expect(content).toMatch(/store = TestBed\.get\(Store\);/);
});
});