Skip to content

Commit 6149753

Browse files
timdeschryverbrandonroberts
authored andcommitted
feat(schematics): update creators to the default
BREAKING CHANGE: BEFORE: The create functions weren't the default to create actions, reducers and effects AFTER: The create functions are the default to create actions (createAction, reducers (createReducer) and effects (createEffect) To fallback to the previous generators, use `sh ng generate reducer ReducerName --creators=false `
1 parent b146af5 commit 6149753

File tree

8 files changed

+102
-89
lines changed

8 files changed

+102
-89
lines changed

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

Lines changed: 72 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,12 @@ describe('Action Schematic', () => {
7676
});
7777

7878
describe('action classes', () => {
79+
const actionClassesDefaultOptions = { ...defaultOptions, creators: false };
80+
7981
it('should create an enum named "Foo"', () => {
8082
const tree = schematicRunner.runSchematic(
8183
'action',
82-
defaultOptions,
84+
actionClassesDefaultOptions,
8385
appTree
8486
);
8587
const fileContent = tree.readContent(
@@ -92,7 +94,7 @@ describe('Action Schematic', () => {
9294
it('should create a class based on the provided name', () => {
9395
const tree = schematicRunner.runSchematic(
9496
'action',
95-
defaultOptions,
97+
actionClassesDefaultOptions,
9698
appTree
9799
);
98100
const fileContent = tree.readContent(
@@ -105,7 +107,7 @@ describe('Action Schematic', () => {
105107
it('should create the union type based on the provided name', () => {
106108
const tree = schematicRunner.runSchematic(
107109
'action',
108-
defaultOptions,
110+
actionClassesDefaultOptions,
109111
appTree
110112
);
111113
const fileContent = tree.readContent(
@@ -116,7 +118,7 @@ describe('Action Schematic', () => {
116118
});
117119

118120
it('should create spec class with right imports', () => {
119-
const options = { ...defaultOptions, spec: true };
121+
const options = { ...actionClassesDefaultOptions, spec: true };
120122
const tree = schematicRunner.runSchematic('action', options, appTree);
121123
const fileContent = tree.readContent(
122124
`${projectPath}/src/app/foo.actions.spec.ts`
@@ -127,12 +129,12 @@ describe('Action Schematic', () => {
127129
});
128130

129131
describe('action creators', () => {
130-
const creatorOptions = { ...defaultOptions, creators: true };
132+
const creatorDefaultOptions = { ...defaultOptions };
131133

132134
it('should create a const for the action creator', () => {
133135
const tree = schematicRunner.runSchematic(
134136
'action',
135-
creatorOptions,
137+
creatorDefaultOptions,
136138
appTree
137139
);
138140
const fileContent = tree.readContent(
@@ -146,7 +148,7 @@ describe('Action Schematic', () => {
146148
it('should create success/error actions when the api flag is set', () => {
147149
const tree = schematicRunner.runSchematic(
148150
'action',
149-
{ ...creatorOptions, api: true },
151+
{ ...creatorDefaultOptions, api: true },
150152
appTree
151153
);
152154
const fileContent = tree.readContent(
@@ -161,71 +163,74 @@ describe('Action Schematic', () => {
161163
});
162164
});
163165

164-
it('should group within an "actions" folder if group is set', () => {
165-
const tree = schematicRunner.runSchematic(
166-
'action',
167-
{
168-
...defaultOptions,
169-
group: true,
170-
},
171-
appTree
172-
);
173-
expect(
174-
tree.files.indexOf(`${projectPath}/src/app/actions/foo.actions.ts`)
175-
).toBeGreaterThanOrEqual(0);
176-
});
166+
describe('api', () => {
167+
it('should group within an "actions" folder if group is set', () => {
168+
const tree = schematicRunner.runSchematic(
169+
'action',
170+
{
171+
...defaultOptions,
172+
group: true,
173+
},
174+
appTree
175+
);
176+
expect(
177+
tree.files.indexOf(`${projectPath}/src/app/actions/foo.actions.ts`)
178+
).toBeGreaterThanOrEqual(0);
179+
});
177180

178-
it('should create a success class based on the provided name, given api', () => {
179-
const tree = schematicRunner.runSchematic(
180-
'action',
181-
{
182-
...defaultOptions,
183-
api: true,
184-
},
185-
appTree
186-
);
187-
const fileContent = tree.readContent(
188-
`${projectPath}/src/app/foo.actions.ts`
189-
);
181+
it('should create a success class based on the provided name, given api', () => {
182+
const tree = schematicRunner.runSchematic(
183+
'action',
184+
{
185+
...defaultOptions,
186+
api: true,
187+
},
188+
appTree
189+
);
190+
const fileContent = tree.readContent(
191+
`${projectPath}/src/app/foo.actions.ts`
192+
);
190193

191-
expect(fileContent).toMatch(
192-
/export class LoadFoosSuccess implements Action/
193-
);
194-
});
194+
expect(fileContent).toMatch(
195+
/export const loadFoosSuccess = createAction\(\r?\n?\s*'\[Foo\] Load Foos Success'\r?\n?\s*,/
196+
);
197+
});
195198

196-
it('should create a failure class based on the provided name, given api', () => {
197-
const tree = schematicRunner.runSchematic(
198-
'action',
199-
{
200-
...defaultOptions,
201-
api: true,
202-
},
203-
appTree
204-
);
205-
const fileContent = tree.readContent(
206-
`${projectPath}/src/app/foo.actions.ts`
207-
);
199+
it('should create a failure class based on the provided name, given api', () => {
200+
const tree = schematicRunner.runSchematic(
201+
'action',
202+
{
203+
...defaultOptions,
204+
api: true,
205+
},
206+
appTree
207+
);
208+
const fileContent = tree.readContent(
209+
`${projectPath}/src/app/foo.actions.ts`
210+
);
208211

209-
expect(fileContent).toMatch(
210-
/export class LoadFoosFailure implements Action/
211-
);
212-
});
212+
expect(fileContent).toMatch(
213+
/export const loadFoosFailure = createAction\(\r?\n?\s*'\[Foo\] Load Foos Failure'\r?\n?\s*,/
214+
);
215+
});
213216

214-
it('should create the union type with success and failure based on the provided name, given api', () => {
215-
const tree = schematicRunner.runSchematic(
216-
'action',
217-
{
218-
...defaultOptions,
219-
api: true,
220-
},
221-
appTree
222-
);
223-
const fileContent = tree.readContent(
224-
`${projectPath}/src/app/foo.actions.ts`
225-
);
217+
it('should create the union type with success and failure based on the provided name, given api and creators false', () => {
218+
const tree = schematicRunner.runSchematic(
219+
'action',
220+
{
221+
...defaultOptions,
222+
api: true,
223+
creators: false,
224+
},
225+
appTree
226+
);
227+
const fileContent = tree.readContent(
228+
`${projectPath}/src/app/foo.actions.ts`
229+
);
226230

227-
expect(fileContent).toMatch(
228-
/export type FooActions = LoadFoos \| LoadFoosSuccess \| LoadFoosFailure/
229-
);
231+
expect(fileContent).toMatch(
232+
/export type FooActions = LoadFoos \| LoadFoosSuccess \| LoadFoosFailure/
233+
);
234+
});
230235
});
231236
});

modules/schematics/src/action/schema.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
},
5151
"creators": {
5252
"type": "boolean",
53-
"default": false,
53+
"default": true,
5454
"description":
5555
"Specifies whether to use creator functions for handling actions and reducers.",
5656
"aliases": ["c"],

modules/schematics/src/effect/schema.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969
},
7070
"creators": {
7171
"type": "boolean",
72-
"default": false,
72+
"default": true,
7373
"description":
7474
"Specifies whether to use creator functions for handling actions, reducers, and effects.",
7575
"aliases": ["c"],

modules/schematics/src/entity/schema.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
},
5454
"creators": {
5555
"type": "boolean",
56-
"default": false,
56+
"default": true,
5757
"description":
5858
"Specifies whether to use creator functions for handling actions and reducers.",
5959
"aliases": ["c"],

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

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -138,17 +138,18 @@ describe('Feature Schematic', () => {
138138
);
139139

140140
expect(moduleFileContent).toMatch(
141-
/import { FooEffects } from '\.\/foo\/effects\/foo.effects';/
141+
/import { FooEffects } from '.\/foo\/effects\/foo.effects';/
142142
);
143143
expect(moduleFileContent).toMatch(
144-
/import \* as fromFoo from '\.\/foo\/reducers\/foo.reducer';/
144+
/import \* as fromFoo from '.\/foo\/reducers\/foo.reducer';/
145145
);
146146
});
147147

148-
it('should have all three api actions in actions type union if api flag enabled', () => {
148+
it('should have all three api actions in actions type union if api flag enabled and creators=false', () => {
149149
const options = {
150150
...defaultOptions,
151151
api: true,
152+
creators: false,
152153
};
153154

154155
const tree = schematicRunner.runSchematic('feature', options, appTree);
@@ -173,26 +174,27 @@ describe('Feature Schematic', () => {
173174
);
174175

175176
expect(fileContent).toMatch(
176-
/import { Actions, Effect, ofType } from '@ngrx\/effects';/
177+
/import { Actions, createEffect, ofType } from '@ngrx\/effects';/
177178
);
178179
expect(fileContent).toMatch(
179180
/import { catchError, map, concatMap } from 'rxjs\/operators';/
180181
);
181182
expect(fileContent).toMatch(/import { EMPTY, of } from 'rxjs';/);
182183
expect(fileContent).toMatch(
183-
/import { LoadFoosFailure, LoadFoosSuccess, FooActionTypes, FooActions } from '\.\/foo.actions';/
184+
/import \* as FooActions from '.\/foo.actions';/
184185
);
185186

186187
expect(fileContent).toMatch(/export class FooEffects/);
187-
expect(fileContent).toMatch(/loadFoos\$ = this\.actions\$.pipe\(/);
188-
expect(fileContent).toMatch(/ofType\(FooActionTypes\.LoadFoos\),/);
188+
expect(fileContent).toMatch(/loadFoos\$ = createEffect\(\(\) => {/);
189+
expect(fileContent).toMatch(/return this.actions\$.pipe\(/);
190+
expect(fileContent).toMatch(/ofType\(FooActions.loadFoos\),/);
189191
expect(fileContent).toMatch(/concatMap\(\(\) =>/);
190-
expect(fileContent).toMatch(/EMPTY\.pipe\(/);
192+
expect(fileContent).toMatch(/EMPTY.pipe\(/);
191193
expect(fileContent).toMatch(
192-
/map\(data => new LoadFoosSuccess\({ data }\)\),/
194+
/map\(data => FooActions.loadFoosSuccess\({ data }\)\),/
193195
);
194196
expect(fileContent).toMatch(
195-
/catchError\(error => of\(new LoadFoosFailure\({ error }\)\)\)\)/
197+
/catchError\(error => of\(FooActions.loadFoosFailure\({ error }\)\)\)\)/
196198
);
197199
});
198200

@@ -207,7 +209,12 @@ describe('Feature Schematic', () => {
207209
`${projectPath}/src/app/foo.reducer.ts`
208210
);
209211

210-
expect(fileContent).toMatch(/case FooActionTypes\.LoadFoosSuccess/);
211-
expect(fileContent).toMatch(/case FooActionTypes\.LoadFoosFailure/);
212+
expect(fileContent).toMatch(/on\(FooActions.loadFoos, state => state\),/);
213+
expect(fileContent).toMatch(
214+
/on\(FooActions.loadFoosSuccess, \(state, action\) => state\),/
215+
);
216+
expect(fileContent).toMatch(
217+
/on\(FooActions.loadFoosFailure, \(state, action\) => state\),/
218+
);
212219
});
213220
});

modules/schematics/src/feature/schema.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
},
6262
"creators": {
6363
"type": "boolean",
64-
"default": false,
64+
"default": true,
6565
"description":
6666
"Specifies if the actions, reducers, and effects should be created using creator functions",
6767
"aliases": ["c"],

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -179,17 +179,18 @@ describe('Reducer Schematic', () => {
179179
);
180180

181181
expect(content).toMatch(
182-
/import\ \{\ FooActions,\ FooActionTypes\ }\ from\ \'\.\.\/\.\.\/actions\/foo\/foo\.actions';/
182+
/import \* as FooActions from '..\/..\/actions\/foo\/foo.actions';/
183183
);
184184
});
185185

186-
it('should create an reducer function with api success and failure, given feature and api', () => {
186+
it('should create an reducer function with api success and failure, given creators=false, feature and api', () => {
187187
const tree = schematicRunner.runSchematic(
188188
'reducer',
189189
{
190190
...defaultOptions,
191191
feature: true,
192192
api: true,
193+
creators: false,
193194
},
194195
appTree
195196
);
@@ -205,7 +206,7 @@ describe('Reducer Schematic', () => {
205206
it('should create an reducer function using createReducer', () => {
206207
const tree = schematicRunner.runSchematic(
207208
'reducer',
208-
{ ...defaultOptions, creators: true },
209+
defaultOptions,
209210
appTree
210211
);
211212
const fileContent = tree.readContent(
@@ -221,7 +222,7 @@ describe('Reducer Schematic', () => {
221222
it('should create an reducer function in a feature using createReducer', () => {
222223
const tree = schematicRunner.runSchematic(
223224
'reducer',
224-
{ ...defaultOptions, creators: true, feature: true },
225+
{ ...defaultOptions, feature: true },
225226
appTree
226227
);
227228
const fileContent = tree.readContent(
@@ -238,7 +239,7 @@ describe('Reducer Schematic', () => {
238239
it('should create an reducer function in an api feature using createReducer', () => {
239240
const tree = schematicRunner.runSchematic(
240241
'reducer',
241-
{ ...defaultOptions, creators: true, feature: true, api: true },
242+
{ ...defaultOptions, feature: true, api: true },
242243
appTree
243244
);
244245
const fileContent = tree.readContent(

modules/schematics/src/reducer/schema.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
},
6666
"creators": {
6767
"type": "boolean",
68-
"default": false,
68+
"default": true,
6969
"description":
7070
"Specifies whether to use creator functions for handling actions and reducers.",
7171
"aliases": ["c"],

0 commit comments

Comments
 (0)