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
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
2 changes: 2 additions & 0 deletions modules/schematics/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ npm_package(
"**/src/*/files/**/*",
"**/src/*/common-files/**/*",
"**/src/*/creator-files/**/*",
"**/src/*/integration-files/**/*",
"**/schema.json",
"**/migration.json",
]),
Expand All @@ -56,6 +57,7 @@ ts_test_library(
"**/src/*/files/**/*",
"**/src/*/common-files/**/*",
"**/src/*/creator-files/**/*",
"**/src/*/integration-files/**/*",
"**/*.json",
]),
deps = [
Expand Down
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 @@ -22,7 +22,6 @@ describe('<%= classify(name) %>Component', () => {
component = fixture.componentInstance;
store = TestBed.get<Store>(Store);

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

Expand Down
28 changes: 26 additions & 2 deletions modules/schematics/src/container/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,18 +110,29 @@ describe('Container Schematic', () => {
});

it('should update the component spec', async () => {
const options = { ...defaultOptions, spec: true };
const options = { ...defaultOptions, spec: true, testDepth: 'unit' };
const tree = await schematicRunner
.runSchematicAsync('container', options, appTree)
.toPromise();
const content = tree.readContent(
`${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 if unit', async () => {
const options = { ...defaultOptions, spec: true, testDepth: 'unit' };
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 @@ -132,4 +143,17 @@ describe('Container Schematic', () => {
);
expect(content).toMatch(/store = TestBed\.get<Store>\(Store\);/);
});

it('should use StoreModule if integration test', 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(
/import { Store, StoreModule } from '@ngrx\/store';/
);
});
});
25 changes: 14 additions & 11 deletions modules/schematics/src/container/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,17 +136,20 @@ export default function(options: ContainerOptions): Rule {
options
);

const templateSource = apply(url('./files'), [
options.spec
? noop()
: filter(path => !path.endsWith('.spec.ts.template')),
applyTemplates({
'if-flat': (s: string) => (options.flat ? '' : s),
...stringUtils,
...(options as object),
} as any),
move(parsedPath.path),
]);
const templateSource = apply(
url(options.testDepth === 'unit' ? './files' : './integration-files'),
[
options.spec
? noop()
: filter(path => !path.endsWith('.spec.ts.template')),
applyTemplates({
'if-flat': (s: string) => (options.flat ? '' : s),
...stringUtils,
...(options as object),
} as any),
move(parsedPath.path),
]
);

return chain([
externalSchematic('@schematics/angular', 'component', {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';

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

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

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

await TestBed.compileComponents();
});

beforeEach(() => {
fixture = TestBed.createComponent(<%= classify(name) %>Component);
component = fixture.componentInstance;
store = TestBed.get<Store>(Store);

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

it('should create', () => {
expect(component).toBeTruthy();
});
});
7 changes: 7 additions & 0 deletions modules/schematics/src/container/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,13 @@
"type": "string",
"default": "State",
"description": "Specifies the interface for the state."
},
"testDepth": {
"description":
"Specifies whether to create a unit test or an integration test.",
"enum": ["unit", "integration"],
"type": "string",
"default": "integration"
}
},
"required": []
Expand Down
5 changes: 5 additions & 0 deletions modules/schematics/src/container/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,9 @@ export interface Schema {
* Specifies the interface for the state
*/
stateInterface?: string;

/**
* Specifies whether to create a unit test or an integration test.
*/
testDepth?: string;
}
7 changes: 7 additions & 0 deletions projects/ngrx.io/content/guide/schematics/container.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ Provide the name of the interface exported for your state interface
- Type: `string`
- Default: `State`

Specifies whether to create a unit test or an integration test

- `--testDepth`
- Type: `string`
- Values: `unit|integration`
- Default: `integration`

## Examples

Generate a `UsersPage` container component with your reducers imported and the `Store` typed a custom interface named `MyState`.
Expand Down