Skip to content

Commit

Permalink
feat: adding api flag to schematics, cleaning up tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wesleygrimes committed Jan 25, 2019
1 parent 7cb5bb2 commit 9012cc3
Show file tree
Hide file tree
Showing 12 changed files with 94 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import { Action } from '@ngrx/store';

export enum <%= classify(name) %>ActionTypes {
Load<%= classify(name) %>s = '[<%= classify(name) %>] Load <%= classify(name) %>s',
Load<%= classify(name) %>sSuccess = '[<%= classify(name) %>] Load <%= classify(name) %>s Success',
Load<%= classify(name) %>sFailure = '[<%= classify(name) %>] Load <%= classify(name) %>s Failure',
<% if (api) { %>Load<%= classify(name) %>sSuccess = '[<%= classify(name) %>] Load <%= classify(name) %>s Success',<% } %>
<% if (api) { %>Load<%= classify(name) %>sFailure = '[<%= classify(name) %>] Load <%= classify(name) %>s Failure',<% } %>
}

export class Load<%= classify(name) %>s implements Action {
readonly type = <%= classify(name) %>ActionTypes.Load<%= classify(name) %>s;
}

<% if (api) { %>
export class Load<%= classify(name) %>sSuccess implements Action {
readonly type = <%= classify(name) %>ActionTypes.Load<%= classify(name) %>sSuccess;
constructor(public payload: { data: any[] }) { }
Expand All @@ -19,5 +19,6 @@ export class Load<%= classify(name) %>sFailure implements Action {
readonly type = <%= classify(name) %>ActionTypes.Load<%= classify(name) %>sFailure;
constructor(public payload: { error: any }) { }
}

export type <%= classify(name) %>Actions = Load<%= classify(name) %>s | Load<%= classify(name) %>sSuccess | Load<%= classify(name) %>sFailure;
<% } %>
<% if (api) { %>export type <%= classify(name) %>Actions = Load<%= classify(name) %>s | Load<%= classify(name) %>sSuccess | Load<%= classify(name) %>sFailure;<% } %>
<% if (!api) { %>export type <%= classify(name) %>Actions = Load<%= classify(name) %>s;<% } %>
30 changes: 0 additions & 30 deletions modules/schematics/src/action/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,36 +101,6 @@ describe('Action Schematic', () => {
expect(fileContent).toMatch(/export class LoadFoos implements Action/);
});

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

expect(fileContent).toMatch(
/export class LoadFoosSuccess implements Action/
);
});

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

expect(fileContent).toMatch(
/export class LoadFoosFailure implements Action/
);
});

it('should create the union type based on the provided name', () => {
const tree = schematicRunner.runSchematic(
'action',
Expand Down
7 changes: 7 additions & 0 deletions modules/schematics/src/action/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@
"default": false,
"description": "Group actions file within 'actions' folder",
"aliases": ["g"]
},
"api": {
"type": "boolean",
"default": false,
"description":
"Specifies if api success and failure actions should be generated.",
"aliases": ["a"]
}
},
"required": []
Expand Down
14 changes: 11 additions & 3 deletions modules/schematics/src/action/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,37 @@ export interface Schema {
/**
* The name of the component.
*/

name: string;

/**
* The path to create the component.
*/

path?: string;

/**
* The name of the project.
*/
project?: string;

/**
* Specifies if a spec file is generated.
*/
spec?: boolean;

/**
* Flag to indicate if a dir is created.
*/

flat?: boolean;

/**
* Group actions file within 'actions' folder
*/

group?: boolean;

/**
* Specifies if api success and failure actions
* should be generated.
*/
api?: boolean;
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { Injectable } from '@angular/core';
import { Actions, Effect<% if (feature) { %>, ofType<% } %> } from '@ngrx/effects';
<% if (feature) { %>import { catchError, map, switchMap } from 'rxjs/operators';<% } %>
<% if (feature) { %>import { Load<%= classify(name) %>sFailure, Load<%= classify(name) %>sSuccess, <%= classify(name) %>ActionTypes } from '<%= featurePath(group, flat, "actions", dasherize(name)) %><%= dasherize(name) %>.actions';<% } %>
<% if (feature && !api) { %>import { <%= classify(name) %>ActionTypes } from '<%= featurePath(group, flat, "actions", dasherize(name)) %><%= dasherize(name) %>.actions';<% } %>
<% if (feature && api) { %>import { catchError, map, switchMap } from 'rxjs/operators';<% } %>
<% if (feature && api) { %>import { Load<%= classify(name) %>sFailure, Load<%= classify(name) %>sSuccess, <%= classify(name) %>ActionTypes } from '<%= featurePath(group, flat, "actions", dasherize(name)) %><%= dasherize(name) %>.actions';<% } %>

@Injectable()
export class <%= classify(name) %>Effects {
<% if (feature) { %>
<% if (feature && api) { %>

private _testObservable = of([
{ myDataProperty: 1000, myOtherDataProperty: 'some-text' },
Expand All @@ -26,6 +27,9 @@ export class <%= classify(name) %>Effects {
)
);
<% } %>

<% if (feature && !api) { %>
@Effect()
load<%= classify(name) %>s$ = this.actions$.pipe(ofType(<%= classify(name) %>ActionTypes.Load<%= classify(name) %>s));
<% } %>
constructor(private actions$: Actions) {}
}
11 changes: 3 additions & 8 deletions modules/schematics/src/effect/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ describe('Effect Schematic', () => {
);

expect(content).toMatch(
/import \{ LoadFoosFailure, LoadFoosSuccess, FooActionTypes } from \'\.\.\/\.\.\/actions\/foo\/foo\.actions';/
/import \{ FooActionTypes } from \'\.\.\/\.\.\/actions\/foo\/foo\.actions';/
);
});

Expand All @@ -251,16 +251,11 @@ describe('Effect Schematic', () => {
/import { Actions, Effect, ofType } from '@ngrx\/effects';/
);
expect(content).toMatch(
/import { LoadFoosFailure, LoadFoosSuccess, FooActionTypes } from '\.\/foo.actions';/
/import { FooActionTypes } from '\.\/foo.actions';/
);
expect(content).toMatch(/export class FooEffects/);
expect(content).toMatch(/loadFoos\$ = this\.actions\$.pipe\(/);
expect(content).toMatch(/ofType\(FooActionTypes\.LoadFoo\),/);
expect(content).toMatch(/switchMap\(\(\) =>/);
expect(content).toMatch(/this\._testObservable\.pipe\(/);
expect(content).toMatch(/map\(data => new LoadFoosSuccess\({ data }\)\),/);
expect(content).toMatch(
/catchError\(error => of\(new LoadFoosFailure\({ error }\)\)\)\)/
/loadFoos\$ = this\.actions\$.pipe\(ofType\(FooActionTypes\.LoadFoos\)\);/
);
});

Expand Down
7 changes: 7 additions & 0 deletions modules/schematics/src/effect/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@
"default": false,
"description": "Group effects file within 'effects' folder",
"aliases": ["g"]
},
"api": {
"type": "boolean",
"default": false,
"description":
"Specifies if effect has api success and failure actions wired up",
"aliases": ["a"]
}
},
"required": []
Expand Down
16 changes: 13 additions & 3 deletions modules/schematics/src/effect/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,50 @@ export interface Schema {
/**
* The name of the component.
*/

name: string;

/**
* The path to create the effect.
*/

path?: string;

/**
* The name of the project.
*/
project?: string;

/**
* Flag to indicate if a dir is created.
*/
flat?: boolean;

/**
* Specifies if a spec file is generated.
*/
spec?: boolean;

/**
* Allows specification of the declaring module.
*/
module?: string;

/**
* Specifies if this is a root-level effect
*/
root?: boolean;

/**
* Specifies if this is grouped within a feature
*/
feature?: boolean;

/**
* Specifies if this is grouped within an 'effects' folder
*/

group?: boolean;

/**
* Specifies if effect has api success and failure actions wired up
*/
api?: boolean;
}
7 changes: 7 additions & 0 deletions modules/schematics/src/feature/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@
"description":
"Group actions, reducers and effects within relative subfolders",
"aliases": ["g"]
},
"api": {
"type": "boolean",
"default": false,
"description":
"Specifies if api success and failure actions, reducer, and effects should be generated as part of this feature.",
"aliases": ["a"]
}
},
"required": []
Expand Down
15 changes: 11 additions & 4 deletions modules/schematics/src/feature/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,45 @@ export interface Schema {
* The name of the feature.
*/
name: string;

/**
* The path to create the feature.
*/
path?: string;

/**
* The name of the project.
*/
project?: string;

/**
* Flag to indicate if a dir is created.
*/
flat?: boolean;

/**
* Specifies if a spec file is generated.
*/
spec?: boolean;

/**
* Allows specification of the declaring module.
*/

module?: string;

/**
* Allows specification of the declaring reducers.
*/

reducers?: string;

/**
* Specifies if this is grouped within sub folders
*/

group?: boolean;

/**
* Specifies if this is grouped within a feature
* Specifies if api success and failure actions, reducer, and effects
* should be generated as part of this feature.
*/
api?: boolean;
}
7 changes: 7 additions & 0 deletions modules/schematics/src/reducer/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@
"default": false,
"description": "Group reducer file within 'reducers' folder",
"aliases": ["g"]
},
"api": {
"type": "boolean",
"default": false,
"description":
"Specifies if api success and failure actions should be added to the reducer",
"aliases": ["a"]
}
},
"required": []
Expand Down
19 changes: 14 additions & 5 deletions modules/schematics/src/reducer/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,50 @@ export interface Schema {
* The name of the component.
*/
name: string;

/**
* The path to create the effect.
*/

path?: string;

/**
* The name of the project.
*/
project?: string;

/**
* Flag to indicate if a dir is created.
*/
flat?: boolean;

/**
* Specifies if a spec file is generated.
*/
spec?: boolean;

/**
* Allows specification of the declaring module.
*/

module?: string;

/**
* Allows specification of the declaring reducers.
*/

reducers?: string;

/**
* Specifies if this is grouped within sub folders
*/

group?: boolean;

/**
* Specifies if this is grouped within a feature
*/

feature?: boolean;

/**
* Specifies if api success and failure actions
* should be added to the reducer.
*/
api?: boolean;
}

0 comments on commit 9012cc3

Please sign in to comment.