Skip to content

Commit

Permalink
feat(schematics): add support for create effect to schematics
Browse files Browse the repository at this point in the history
  • Loading branch information
itayod committed Apr 10, 2019
1 parent 00b550e commit 079517c
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Injectable } from '@angular/core';
import { Actions, Effect<% if (feature) { %>, ofType<% } %> } from '@ngrx/effects';
import { Actions, <% if (effectCreator) { %>createEffect<% } else { %>Effect<% } %><% if (feature) { %>, ofType<% } %> } from '@ngrx/effects';
<% if (feature && api) { %>import { catchError, map, concatMap } from 'rxjs/operators';
import { EMPTY, of } from 'rxjs';
import { Load<%= classify(name) %>sFailure, Load<%= classify(name) %>sSuccess, <%= classify(name) %>ActionTypes, <%= classify(name) %>Actions } from '<%= featurePath(group, flat, "actions", dasherize(name)) %><%= dasherize(name) %>.actions';
Expand All @@ -12,8 +12,8 @@ import { <%= classify(name) %>ActionTypes, <%= classify(name) %>Actions } from '
@Injectable()
export class <%= classify(name) %>Effects {
<% if (feature && api) { %>
@Effect()
load<%= classify(name) %>s$ = this.actions$.pipe(
<% if (!effectCreator) { %>@Effect()<% }%>
<% name %>
ofType(<%= classify(name) %>ActionTypes.Load<%= classify(name) %>s),
concatMap(() =>
/** An EMPTY observable only emits completion. Replace with your own observable API request */
Expand All @@ -23,8 +23,8 @@ export class <%= classify(name) %>Effects {
)
);<% } %>
<% if (feature && !api) { %>
@Effect()
load<%= classify(name) %>s$ = this.actions$.pipe(
<% if (!effectCreator) { %>@Effect()<% }%>
<%= effectMethod %>
ofType(<%= classify(name) %>ActionTypes.Load<%= classify(name) %>s),
/** An EMPTY observable only emits completion. Replace with your own observable API request */
concatMap(() => EMPTY)
Expand All @@ -35,4 +35,5 @@ export class <%= classify(name) %>Effects {
<% } else { %>
constructor(private actions$: Actions) {}
<% } %>

}
40 changes: 39 additions & 1 deletion modules/schematics/src/effect/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
defaultAppOptions,
} from '../../../schematics-core/testing';

describe('Effect Schematic', () => {
fdescribe('Effect Schematic', () => {
const schematicRunner = new SchematicTestRunner(
'@ngrx/schematics',
path.join(__dirname, '../../collection.json')
Expand All @@ -27,6 +27,7 @@ describe('Effect Schematic', () => {
feature: false,
root: false,
group: false,
effectCreator: false,
};

const projectPath = getTestProjectPath();
Expand Down Expand Up @@ -316,4 +317,41 @@ describe('Effect Schematic', () => {
/constructor\(private actions\$: Actions<FooActions>\) {}/
);
});

it('should create an effect using creator function', () => {
const options = { ...defaultOptions, effectCreator: true, feature: true };

const tree = schematicRunner.runSchematic('effect', options, appTree);
const content = tree.readContent(
`${projectPath}/src/app/foo/foo.effects.ts`
);
expect(content).toMatch(
/import { Actions, createEffect, ofType } from '@ngrx\/effects';/
);
expect(content).not.toMatch(/@Effect\(\)/);
expect(content).toMatch(
/loadFoos\$ = createEffect\(\(\) => this.actions\$.pipe\(/
);
});

it('should create an api effect using creator function', () => {
const options = {
...defaultOptions,
effectCreator: true,
api: true,
feature: true,
};

const tree = schematicRunner.runSchematic('effect', options, appTree);
const content = tree.readContent(
`${projectPath}/src/app/foo/foo.effects.ts`
);
expect(content).toMatch(
/import { Actions, createEffect, ofType } from '@ngrx\/effects';/
);
expect(content).not.toMatch(/@Effect\(\)/);
expect(content).toMatch(
/loadFoos\$ = createEffect\(\(\) => this.actions\$.pipe\(/
);
});
});
11 changes: 11 additions & 0 deletions modules/schematics/src/effect/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,16 @@ function addImportToNgModule(options: EffectOptions): Rule {
};
}

export function getEffectMethod(
name: string,
effectCreator: boolean | undefined
): string {
const effectName = stringUtils.classify(name);
return effectCreator
? `load${effectName}s$ = createEffect(() => this.actions$.pipe(`
: `load${effectName}s$ = this.actions$.pipe(`;
}

export default function(options: EffectOptions): Rule {
return (host: Tree, context: SchematicContext) => {
options.path = getProjectPath(host, options);
Expand All @@ -116,6 +126,7 @@ export default function(options: EffectOptions): Rule {
options.flat ? '' : s,
options.group ? 'effects' : ''
),
effectMethod: getEffectMethod(options.name, options.effectCreator),
...(options as object),
} as any),
move(parsedPath.path),
Expand Down
6 changes: 6 additions & 0 deletions modules/schematics/src/effect/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@
"description":
"Specifies if effect has api success and failure actions wired up",
"aliases": ["a"]
},
"effectCreator": {
"type": "boolean",
"default": false,
"description": "Specifies if the effect creation uses 'createEffect'",
"aliases": ["ec"]
}
},
"required": []
Expand Down
5 changes: 5 additions & 0 deletions modules/schematics/src/effect/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,9 @@ export interface Schema {
* Specifies if effect has api success and failure actions wired up
*/
api?: boolean;

/**
* Specifies if the effect creation uses 'createEffect'
*/
effectCreator?: boolean;
}
6 changes: 6 additions & 0 deletions modules/schematics/src/feature/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@
"description":
"Specifies if api success and failure actions, reducer, and effects should be generated as part of this feature.",
"aliases": ["a"]
},
"effectCreator": {
"type": "boolean",
"default": false,
"description": "Specifies if the effect creation uses 'createEffect'",
"aliases": ["ec"]
}
},
"required": []
Expand Down
5 changes: 5 additions & 0 deletions modules/schematics/src/feature/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,9 @@ export interface Schema {
* should be generated as part of this feature.
*/
api?: boolean;

/**
* Specifies if the effect creation uses 'createEffect'
*/
effectCreator?: boolean;
}

0 comments on commit 079517c

Please sign in to comment.