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 69b6251
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 3 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 @@ -11,7 +11,7 @@ import { <%= classify(name) %>ActionTypes, <%= classify(name) %>Actions } from '

@Injectable()
export class <%= classify(name) %>Effects {
<% if (feature && api) { %>
<% if (feature && api && !effectCreator) { %>
@Effect()
load<%= classify(name) %>s$ = this.actions$.pipe(
ofType(<%= classify(name) %>ActionTypes.Load<%= classify(name) %>s),
Expand All @@ -22,7 +22,7 @@ export class <%= classify(name) %>Effects {
catchError(error => of(new Load<%= classify(name) %>sFailure({ error }))))
)
);<% } %>
<% if (feature && !api) { %>
<% if (feature && !api && !effectCreator) { %>
@Effect()
load<%= classify(name) %>s$ = this.actions$.pipe(
ofType(<%= classify(name) %>ActionTypes.Load<%= classify(name) %>s),
Expand All @@ -35,4 +35,22 @@ export class <%= classify(name) %>Effects {
<% } else { %>
constructor(private actions$: Actions) {}
<% } %>

<% if (feature && api && effectCreator) { %>
load<%= classify(name) %>s$ = createEffect(() => this.actions$.pipe(
ofType(<%= classify(name) %>ActionTypes.Load<%= classify(name) %>s),
concatMap(() =>
/** An EMPTY observable only emits completion. Replace with your own observable API request */
EMPTY.pipe(
map(data => new Load<%= classify(name) %>sSuccess({ data })),
catchError(error => of(new Load<%= classify(name) %>sFailure({ error }))))
)
));<% } %>

<% if (feature && !api && effectCreator) { %>
load<%= classify(name) %>s$ = createEffect(() => this.actions$.pipe(
ofType(<%= classify(name) %>ActionTypes.LoadMyTestFeatures),
concatMap(() => EMPTY)
));
<% } %>
}
38 changes: 38 additions & 0 deletions modules/schematics/src/effect/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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\(/
);
});
});
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 69b6251

Please sign in to comment.