Skip to content

Commit

Permalink
feat(schematics): update creators to the default
Browse files Browse the repository at this point in the history
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
`
  • Loading branch information
timdeschryver authored and brandonroberts committed Jan 7, 2020
1 parent b146af5 commit 6149753
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 89 deletions.
139 changes: 72 additions & 67 deletions modules/schematics/src/action/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,12 @@ describe('Action Schematic', () => {
});

describe('action classes', () => {
const actionClassesDefaultOptions = { ...defaultOptions, creators: false };

it('should create an enum named "Foo"', () => {
const tree = schematicRunner.runSchematic(
'action',
defaultOptions,
actionClassesDefaultOptions,
appTree
);
const fileContent = tree.readContent(
Expand All @@ -92,7 +94,7 @@ describe('Action Schematic', () => {
it('should create a class based on the provided name', () => {
const tree = schematicRunner.runSchematic(
'action',
defaultOptions,
actionClassesDefaultOptions,
appTree
);
const fileContent = tree.readContent(
Expand All @@ -105,7 +107,7 @@ describe('Action Schematic', () => {
it('should create the union type based on the provided name', () => {
const tree = schematicRunner.runSchematic(
'action',
defaultOptions,
actionClassesDefaultOptions,
appTree
);
const fileContent = tree.readContent(
Expand All @@ -116,7 +118,7 @@ describe('Action Schematic', () => {
});

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

describe('action creators', () => {
const creatorOptions = { ...defaultOptions, creators: true };
const creatorDefaultOptions = { ...defaultOptions };

it('should create a const for the action creator', () => {
const tree = schematicRunner.runSchematic(
'action',
creatorOptions,
creatorDefaultOptions,
appTree
);
const fileContent = tree.readContent(
Expand All @@ -146,7 +148,7 @@ describe('Action Schematic', () => {
it('should create success/error actions when the api flag is set', () => {
const tree = schematicRunner.runSchematic(
'action',
{ ...creatorOptions, api: true },
{ ...creatorDefaultOptions, api: true },
appTree
);
const fileContent = tree.readContent(
Expand All @@ -161,71 +163,74 @@ describe('Action Schematic', () => {
});
});

it('should group within an "actions" folder if group is set', () => {
const tree = schematicRunner.runSchematic(
'action',
{
...defaultOptions,
group: true,
},
appTree
);
expect(
tree.files.indexOf(`${projectPath}/src/app/actions/foo.actions.ts`)
).toBeGreaterThanOrEqual(0);
});
describe('api', () => {
it('should group within an "actions" folder if group is set', () => {
const tree = schematicRunner.runSchematic(
'action',
{
...defaultOptions,
group: true,
},
appTree
);
expect(
tree.files.indexOf(`${projectPath}/src/app/actions/foo.actions.ts`)
).toBeGreaterThanOrEqual(0);
});

it('should create a success class based on the provided name, given api', () => {
const tree = schematicRunner.runSchematic(
'action',
{
...defaultOptions,
api: true,
},
appTree
);
const fileContent = tree.readContent(
`${projectPath}/src/app/foo.actions.ts`
);
it('should create a success class based on the provided name, given api', () => {
const tree = schematicRunner.runSchematic(
'action',
{
...defaultOptions,
api: true,
},
appTree
);
const fileContent = tree.readContent(
`${projectPath}/src/app/foo.actions.ts`
);

expect(fileContent).toMatch(
/export class LoadFoosSuccess implements Action/
);
});
expect(fileContent).toMatch(
/export const loadFoosSuccess = createAction\(\r?\n?\s*'\[Foo\] Load Foos Success'\r?\n?\s*,/
);
});

it('should create a failure class based on the provided name, given api', () => {
const tree = schematicRunner.runSchematic(
'action',
{
...defaultOptions,
api: true,
},
appTree
);
const fileContent = tree.readContent(
`${projectPath}/src/app/foo.actions.ts`
);
it('should create a failure class based on the provided name, given api', () => {
const tree = schematicRunner.runSchematic(
'action',
{
...defaultOptions,
api: true,
},
appTree
);
const fileContent = tree.readContent(
`${projectPath}/src/app/foo.actions.ts`
);

expect(fileContent).toMatch(
/export class LoadFoosFailure implements Action/
);
});
expect(fileContent).toMatch(
/export const loadFoosFailure = createAction\(\r?\n?\s*'\[Foo\] Load Foos Failure'\r?\n?\s*,/
);
});

it('should create the union type with success and failure based on the provided name, given api', () => {
const tree = schematicRunner.runSchematic(
'action',
{
...defaultOptions,
api: true,
},
appTree
);
const fileContent = tree.readContent(
`${projectPath}/src/app/foo.actions.ts`
);
it('should create the union type with success and failure based on the provided name, given api and creators false', () => {
const tree = schematicRunner.runSchematic(
'action',
{
...defaultOptions,
api: true,
creators: false,
},
appTree
);
const fileContent = tree.readContent(
`${projectPath}/src/app/foo.actions.ts`
);

expect(fileContent).toMatch(
/export type FooActions = LoadFoos \| LoadFoosSuccess \| LoadFoosFailure/
);
expect(fileContent).toMatch(
/export type FooActions = LoadFoos \| LoadFoosSuccess \| LoadFoosFailure/
);
});
});
});
2 changes: 1 addition & 1 deletion modules/schematics/src/action/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
},
"creators": {
"type": "boolean",
"default": false,
"default": true,
"description":
"Specifies whether to use creator functions for handling actions and reducers.",
"aliases": ["c"],
Expand Down
2 changes: 1 addition & 1 deletion modules/schematics/src/effect/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
},
"creators": {
"type": "boolean",
"default": false,
"default": true,
"description":
"Specifies whether to use creator functions for handling actions, reducers, and effects.",
"aliases": ["c"],
Expand Down
2 changes: 1 addition & 1 deletion modules/schematics/src/entity/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
},
"creators": {
"type": "boolean",
"default": false,
"default": true,
"description":
"Specifies whether to use creator functions for handling actions and reducers.",
"aliases": ["c"],
Expand Down
31 changes: 19 additions & 12 deletions modules/schematics/src/feature/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,17 +138,18 @@ describe('Feature Schematic', () => {
);

expect(moduleFileContent).toMatch(
/import { FooEffects } from '\.\/foo\/effects\/foo.effects';/
/import { FooEffects } from '.\/foo\/effects\/foo.effects';/
);
expect(moduleFileContent).toMatch(
/import \* as fromFoo from '\.\/foo\/reducers\/foo.reducer';/
/import \* as fromFoo from '.\/foo\/reducers\/foo.reducer';/
);
});

it('should have all three api actions in actions type union if api flag enabled', () => {
it('should have all three api actions in actions type union if api flag enabled and creators=false', () => {
const options = {
...defaultOptions,
api: true,
creators: false,
};

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

expect(fileContent).toMatch(
/import { Actions, Effect, ofType } from '@ngrx\/effects';/
/import { Actions, createEffect, ofType } from '@ngrx\/effects';/
);
expect(fileContent).toMatch(
/import { catchError, map, concatMap } from 'rxjs\/operators';/
);
expect(fileContent).toMatch(/import { EMPTY, of } from 'rxjs';/);
expect(fileContent).toMatch(
/import { LoadFoosFailure, LoadFoosSuccess, FooActionTypes, FooActions } from '\.\/foo.actions';/
/import \* as FooActions from '.\/foo.actions';/
);

expect(fileContent).toMatch(/export class FooEffects/);
expect(fileContent).toMatch(/loadFoos\$ = this\.actions\$.pipe\(/);
expect(fileContent).toMatch(/ofType\(FooActionTypes\.LoadFoos\),/);
expect(fileContent).toMatch(/loadFoos\$ = createEffect\(\(\) => {/);
expect(fileContent).toMatch(/return this.actions\$.pipe\(/);
expect(fileContent).toMatch(/ofType\(FooActions.loadFoos\),/);
expect(fileContent).toMatch(/concatMap\(\(\) =>/);
expect(fileContent).toMatch(/EMPTY\.pipe\(/);
expect(fileContent).toMatch(/EMPTY.pipe\(/);
expect(fileContent).toMatch(
/map\(data => new LoadFoosSuccess\({ data }\)\),/
/map\(data => FooActions.loadFoosSuccess\({ data }\)\),/
);
expect(fileContent).toMatch(
/catchError\(error => of\(new LoadFoosFailure\({ error }\)\)\)\)/
/catchError\(error => of\(FooActions.loadFoosFailure\({ error }\)\)\)\)/
);
});

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

expect(fileContent).toMatch(/case FooActionTypes\.LoadFoosSuccess/);
expect(fileContent).toMatch(/case FooActionTypes\.LoadFoosFailure/);
expect(fileContent).toMatch(/on\(FooActions.loadFoos, state => state\),/);
expect(fileContent).toMatch(
/on\(FooActions.loadFoosSuccess, \(state, action\) => state\),/
);
expect(fileContent).toMatch(
/on\(FooActions.loadFoosFailure, \(state, action\) => state\),/
);
});
});
2 changes: 1 addition & 1 deletion modules/schematics/src/feature/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
},
"creators": {
"type": "boolean",
"default": false,
"default": true,
"description":
"Specifies if the actions, reducers, and effects should be created using creator functions",
"aliases": ["c"],
Expand Down
11 changes: 6 additions & 5 deletions modules/schematics/src/reducer/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,17 +179,18 @@ describe('Reducer Schematic', () => {
);

expect(content).toMatch(
/import\ \{\ FooActions,\ FooActionTypes\ }\ from\ \'\.\.\/\.\.\/actions\/foo\/foo\.actions';/
/import \* as FooActions from '..\/..\/actions\/foo\/foo.actions';/
);
});

it('should create an reducer function with api success and failure, given feature and api', () => {
it('should create an reducer function with api success and failure, given creators=false, feature and api', () => {
const tree = schematicRunner.runSchematic(
'reducer',
{
...defaultOptions,
feature: true,
api: true,
creators: false,
},
appTree
);
Expand All @@ -205,7 +206,7 @@ describe('Reducer Schematic', () => {
it('should create an reducer function using createReducer', () => {
const tree = schematicRunner.runSchematic(
'reducer',
{ ...defaultOptions, creators: true },
defaultOptions,
appTree
);
const fileContent = tree.readContent(
Expand All @@ -221,7 +222,7 @@ describe('Reducer Schematic', () => {
it('should create an reducer function in a feature using createReducer', () => {
const tree = schematicRunner.runSchematic(
'reducer',
{ ...defaultOptions, creators: true, feature: true },
{ ...defaultOptions, feature: true },
appTree
);
const fileContent = tree.readContent(
Expand All @@ -238,7 +239,7 @@ describe('Reducer Schematic', () => {
it('should create an reducer function in an api feature using createReducer', () => {
const tree = schematicRunner.runSchematic(
'reducer',
{ ...defaultOptions, creators: true, feature: true, api: true },
{ ...defaultOptions, feature: true, api: true },
appTree
);
const fileContent = tree.readContent(
Expand Down
2 changes: 1 addition & 1 deletion modules/schematics/src/reducer/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
},
"creators": {
"type": "boolean",
"default": false,
"default": true,
"description":
"Specifies whether to use creator functions for handling actions and reducers.",
"aliases": ["c"],
Expand Down

0 comments on commit 6149753

Please sign in to comment.