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

fix(Schematics): Add store import to container blueprint #763

Merged
merged 1 commit into from
Jan 30, 2018
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
4 changes: 2 additions & 2 deletions docs/schematics/container.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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