Skip to content

Commit

Permalink
fix(Schematics): Add store import to container blueprint (#763)
Browse files Browse the repository at this point in the history
Closes #760
  • Loading branch information
brandonroberts authored and MikeRyanDev committed Jan 30, 2018
1 parent afaabd7 commit a140fa9
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
4 changes: 2 additions & 2 deletions docs/schematics/container.md
Expand Up @@ -25,7 +25,7 @@ ng generate co ComponentName [options]

Provide the path to your file with an exported state interface

- `--reducers`
- `--state`
- Type: `string`

Provide the name of the interface exported for your state interface
Expand All @@ -39,5 +39,5 @@ Provide the name of the interface exported for your state interface
Generate a `UsersPage` container component with your reducers imported and the `Store` typed a custom interface named `MyState`.

```sh
ng generate container UsersPage --reducers reducers/index.ts --stateInterface MyState
ng generate container UsersPage --state reducers/index.ts --stateInterface MyState
```
9 changes: 9 additions & 0 deletions modules/schematics/src/container/index.spec.ts
Expand Up @@ -46,6 +46,15 @@ describe('Container Schematic', () => {
expect(content).toMatch(/import \* as fromStore from '..\/reducers';/);
});

it('should import Store into the component', () => {
const options = { ...defaultOptions, state: 'reducers' };
appTree.create('/src/app/reducers', '');
const tree = schematicRunner.runSchematic('container', options, appTree);
const content = getFileContent(tree, '/src/app/foo/foo.component.ts');
console.log(content);
expect(content).toMatch(/import\ {\ Store\ }\ from\ '@ngrx\/store';/);
});

it('should update the component constructor if the state path if provided', () => {
const options = { ...defaultOptions, state: 'reducers' };
appTree.create('/src/app/reducers', '');
Expand Down
8 changes: 7 additions & 1 deletion modules/schematics/src/container/index.ts
Expand Up @@ -61,6 +61,12 @@ function addStateToComponent(options: FeatureOptions) {
);

const stateImportPath = buildRelativePath(componentPath, statePath);
const storeImport = insertImport(
source,
componentPath,
'Store',
'@ngrx/store'
);
const stateImport = options.state
? insertImport(
source,
Expand Down Expand Up @@ -94,7 +100,7 @@ function addStateToComponent(options: FeatureOptions) {
`\n\n ${storeConstructor}`
);

const changes = [stateImport, constructorUpdate];
const changes = [storeImport, stateImport, constructorUpdate];
const recorder = host.beginUpdate(componentPath);

for (const change of changes) {
Expand Down

0 comments on commit a140fa9

Please sign in to comment.