Skip to content

Commit

Permalink
feat(schematics): Add option to customize test depth between Unit or …
Browse files Browse the repository at this point in the history
…Integration
  • Loading branch information
ijz953 committed Jul 24, 2019
1 parent 689fc93 commit 896899c
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 11 deletions.
13 changes: 13 additions & 0 deletions modules/schematics/src/container/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,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, testDepth: 'Integration' };
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';/
);
});
});
27 changes: 16 additions & 11 deletions modules/schematics/src/container/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,17 +136,22 @@ 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 === 'Integration' ? './integration-files' : './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": "Unit"
}
},
"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;
}

0 comments on commit 896899c

Please sign in to comment.