Skip to content

Commit

Permalink
feat(schematics): add the ability to create actions with the prefix (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
mateuszbasinski authored May 26, 2021
1 parent 3b565b4 commit 15bc0df
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as from<%= classify(name) %> from './<%= dasherize(name) %>.actions';

describe('load<%= classify(name) %>s', () => {
describe('<%= prefix %><%= classify(name) %>s', () => {
it('should return an action', () => {
expect(from<%= classify(name) %>.load<%= classify(name) %>s().type).toBe('[<%= classify(name) %>] Load <%= classify(name) %>s');
expect(from<%= classify(name) %>.<%= prefix %><%= classify(name) %>s().type).toBe('[<%= classify(name) %>] <%= classify(prefix) %> <%= classify(name) %>s');
});
});
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { createAction, props } from '@ngrx/store';

export const load<%= classify(name) %>s = createAction(
'[<%= classify(name) %>] Load <%= classify(name) %>s'
export const <%= prefix %><%= classify(name) %>s = createAction(
'[<%= classify(name) %>] <%= classify(prefix) %> <%= classify(name) %>s'
);

<% if (api) { %>export const load<%= classify(name) %>sSuccess = createAction(
'[<%= classify(name) %>] Load <%= classify(name) %>s Success',
<% if (api) { %>export const <%= prefix %><%= classify(name) %>sSuccess = createAction(
'[<%= classify(name) %>] <%= classify(prefix) %> <%= classify(name) %>s Success',
props<{ data: any }>()
);<% } %>

<% if (api) { %>export const load<%= classify(name) %>sFailure = createAction(
'[<%= classify(name) %>] Load <%= classify(name) %>s Failure',
<% if (api) { %>export const <%= prefix %><%= classify(name) %>sFailure = createAction(
'[<%= classify(name) %>] <%= classify(prefix) %> <%= classify(name) %>s Failure',
props<{ error: any }>()
);<% } %>
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ import * as <%= classify(name) %>Actions from './<%= dasherize(name) %>.actions'

describe('<%= classify(name) %>', () => {
it('should create an instance', () => {
expect(new <%= classify(name)%>Actions.Load<%= classify(name) %>s()).toBeTruthy();
expect(new <%= classify(name)%>Actions.<%= prefix %><%= classify(name) %>s()).toBeTruthy();
});
});
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
import { Action } from '@ngrx/store';

export enum <%= classify(name) %>ActionTypes {
Load<%= classify(name) %>s = '[<%= classify(name) %>] Load <%= classify(name) %>s',
<% 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',<% } %>
<%= prefix %><%= classify(name) %>s = '[<%= classify(name) %>] <%= prefix %> <%= classify(name) %>s',
<% if (api) { %><%= prefix %><%= classify(name) %>sSuccess = '[<%= classify(name) %>] <%= prefix %> <%= classify(name) %>s Success',<% } %>
<% if (api) { %><%= prefix %><%= classify(name) %>sFailure = '[<%= classify(name) %>] <%= prefix %> <%= classify(name) %>s Failure',<% } %>
}

export class Load<%= classify(name) %>s implements Action {
readonly type = <%= classify(name) %>ActionTypes.Load<%= classify(name) %>s;
export class <%= prefix %><%= classify(name) %>s implements Action {
readonly type = <%= classify(name) %>ActionTypes.<%= prefix %><%= classify(name) %>s;
}
<% if (api) { %>
export class Load<%= classify(name) %>sSuccess implements Action {
readonly type = <%= classify(name) %>ActionTypes.Load<%= classify(name) %>sSuccess;
export class <%= prefix %><%= classify(name) %>sSuccess implements Action {
readonly type = <%= classify(name) %>ActionTypes.<%= prefix %><%= classify(name) %>sSuccess;
constructor(public payload: { data: any }) { }
}

export class Load<%= classify(name) %>sFailure implements Action {
readonly type = <%= classify(name) %>ActionTypes.Load<%= classify(name) %>sFailure;
export class <%= prefix %><%= classify(name) %>sFailure implements Action {
readonly type = <%= classify(name) %>ActionTypes.<%= prefix %><%= classify(name) %>sFailure;
constructor(public payload: { error: any }) { }
}
<% } %>
<% 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;<% } %>
<% if (api) { %>export type <%= classify(name) %>Actions = <%= prefix %><%= classify(name) %>s | <%= prefix %><%= classify(name) %>sSuccess | <%= prefix %><%= classify(name) %>sFailure;<% } %>
<% if (!api) { %>export type <%= classify(name) %>Actions = <%= prefix %><%= classify(name) %>s;<% } %>
24 changes: 24 additions & 0 deletions modules/schematics/src/action/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
defaultWorkspaceOptions,
defaultAppOptions,
} from '@ngrx/schematics-core/testing';
import { capitalize } from '../../schematics-core/utility/strings';

describe('Action Schematic', () => {
const schematicRunner = new SchematicTestRunner(
Expand All @@ -18,6 +19,7 @@ describe('Action Schematic', () => {
);
const defaultOptions: ActionOptions = {
name: 'foo',
prefix: 'load',
project: 'bar',
group: false,
flat: true,
Expand Down Expand Up @@ -173,6 +175,28 @@ describe('Action Schematic', () => {
expect(fileContent).toMatch(/\[Foo\] Load Foos Failure/);
expect(fileContent).toMatch(/props<{ error: any }>\(\)/);
});

it.each(['load', 'delete', 'update'])(
'should create a action with prefix',
async (prefix) => {
const tree = await schematicRunner
.runSchematicAsync(
'action',
{ ...creatorDefaultOptions, prefix: prefix },
appTree
)
.toPromise();
const fileContent = tree.readContent(
`${projectPath}/src/app/foo.actions.ts`
);
expect(fileContent).toMatch(
new RegExp(`export const ${prefix}Foos = createAction`)
);
expect(fileContent).toMatch(
new RegExp(`'\\[Foo] ${capitalize(prefix)} Foos'`)
);
}
);
});

describe('api', () => {
Expand Down
13 changes: 6 additions & 7 deletions modules/schematics/src/action/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {
Rule,
SchematicsException,
apply,
applyTemplates,
branchAndMerge,
Expand All @@ -9,22 +8,22 @@ import {
mergeWith,
move,
noop,
template,
url,
Tree,
SchematicContext,
} from '@angular-devkit/schematics';
import { Schema as ActionOptions } from './schema';
import {
getProjectPath,
stringUtils,
parseName,
} from '@ngrx/schematics/schematics-core';
import { getProjectPath, stringUtils, parseName } from '../../schematics-core';
import { capitalize, camelize } from '../../schematics-core/utility/strings';

export default function (options: ActionOptions): Rule {
return (host: Tree, context: SchematicContext) => {
options.path = getProjectPath(host, options);

options.prefix = options.creators
? camelize(options.prefix || 'load')
: capitalize(options.prefix || 'load');

const parsedPath = parseName(options.path, options.name);
options.name = parsedPath.name;
options.path = parsedPath.path;
Expand Down
6 changes: 6 additions & 0 deletions modules/schematics/src/action/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@
},
"x-prompt": "What should be the name of the action?"
},
"prefix": {
"description": "The prefix of the action.",
"type": "string",
"default": "load",
"x-prompt": "What should be the prefix of the action?"
},
"path": {
"type": "string",
"format": "path",
Expand Down
5 changes: 5 additions & 0 deletions modules/schematics/src/action/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ export interface Schema {
*/
name: string;

/**
* The prefix for the actions.
*/
prefix: string;

/**
* The path to create the component.
*/
Expand Down
6 changes: 6 additions & 0 deletions projects/ngrx.io/content/guide/schematics/action.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ Generate a spec file alongside the action file.
- Type: `boolean`
- Default: `false`

Specify the prefix for the actions.

- `--prefix`
- Type: `string`
- Default: `load`

## Examples

Generate a `User` actions file with an associated spec file.
Expand Down

0 comments on commit 15bc0df

Please sign in to comment.