Skip to content

Commit a140fa9

Browse files
brandonrobertsMikeRyanDev
authored andcommitted
fix(Schematics): Add store import to container blueprint (#763)
Closes #760
1 parent afaabd7 commit a140fa9

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

docs/schematics/container.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ ng generate co ComponentName [options]
2525

2626
Provide the path to your file with an exported state interface
2727

28-
- `--reducers`
28+
- `--state`
2929
- Type: `string`
3030

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

4141
```sh
42-
ng generate container UsersPage --reducers reducers/index.ts --stateInterface MyState
42+
ng generate container UsersPage --state reducers/index.ts --stateInterface MyState
4343
```

modules/schematics/src/container/index.spec.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,15 @@ describe('Container Schematic', () => {
4646
expect(content).toMatch(/import \* as fromStore from '..\/reducers';/);
4747
});
4848

49+
it('should import Store into the component', () => {
50+
const options = { ...defaultOptions, state: 'reducers' };
51+
appTree.create('/src/app/reducers', '');
52+
const tree = schematicRunner.runSchematic('container', options, appTree);
53+
const content = getFileContent(tree, '/src/app/foo/foo.component.ts');
54+
console.log(content);
55+
expect(content).toMatch(/import\ {\ Store\ }\ from\ '@ngrx\/store';/);
56+
});
57+
4958
it('should update the component constructor if the state path if provided', () => {
5059
const options = { ...defaultOptions, state: 'reducers' };
5160
appTree.create('/src/app/reducers', '');

modules/schematics/src/container/index.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@ function addStateToComponent(options: FeatureOptions) {
6161
);
6262

6363
const stateImportPath = buildRelativePath(componentPath, statePath);
64+
const storeImport = insertImport(
65+
source,
66+
componentPath,
67+
'Store',
68+
'@ngrx/store'
69+
);
6470
const stateImport = options.state
6571
? insertImport(
6672
source,
@@ -94,7 +100,7 @@ function addStateToComponent(options: FeatureOptions) {
94100
`\n\n ${storeConstructor}`
95101
);
96102

97-
const changes = [stateImport, constructorUpdate];
103+
const changes = [storeImport, stateImport, constructorUpdate];
98104
const recorder = host.beginUpdate(componentPath);
99105

100106
for (const change of changes) {

0 commit comments

Comments
 (0)