Skip to content

Commit

Permalink
feat(schematics): Use createActionGroup in schematics (#3791)
Browse files Browse the repository at this point in the history
  • Loading branch information
faragos authored Apr 21, 2023
1 parent cdaeeb6 commit f6ce20f
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 90 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
import { createAction, props } from '@ngrx/store';
import { createActionGroup, emptyProps, props } from '@ngrx/store';

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

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

<% if (api) { %>export const <%= prefix %><%= classify(name) %>sFailure = createAction(
'[<%= classify(name) %>] <%= classify(prefix) %> <%= classify(name) %>s Failure',
props<{ error: unknown }>()
);<% } %>
export const <%= classify(name) %>Actions = createActionGroup({
source: '<%= classify(name) %>',
events: {
'<%= classify(prefix) %> <%= classify(name) %>s': emptyProps(),
<% if (api) { %>'<%= classify(prefix) %> <%= classify(name) %>s Success': props<{ data: unknown }>(),<% } %>
<% if (api) { %>'<%= classify(prefix) %> <%= classify(name) %>s Failure': props<{ error: unknown }>(),<% } %>
}
});
25 changes: 16 additions & 9 deletions modules/schematics/src/action/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,11 @@ describe('Action Schematic', () => {
`${projectPath}/src/app/foo.actions.ts`
);

expect(fileContent).toMatch(/export const loadFoos = createAction\(/);
expect(fileContent).toMatch(/\[Foo\] Load Foos'/);
expect(fileContent).toMatch(
/export const FooActions = createActionGroup\(/
);
expect(fileContent).toMatch(new RegExp(`source: 'Foo'`));
expect(fileContent).toMatch(/Load Foos'/);
});

it('should create success/error actions when the api flag is set', async () => {
Expand All @@ -97,10 +100,13 @@ describe('Action Schematic', () => {
`${projectPath}/src/app/foo.actions.ts`
);

expect(fileContent).toMatch(/export const loadFoos = createAction\(/);
expect(fileContent).toMatch(/\[Foo\] Load Foos Success/);
expect(fileContent).toMatch(
/export const FooActions = createActionGroup\(/
);
expect(fileContent).toMatch(new RegExp(`source: 'Foo'`));
expect(fileContent).toMatch(/Load Foos Success/);
expect(fileContent).toMatch(/props<{ data: unknown }>\(\)/);
expect(fileContent).toMatch(/\[Foo\] Load Foos Failure/);
expect(fileContent).toMatch(/Load Foos Failure/);
expect(fileContent).toMatch(/props<{ error: unknown }>\(\)/);
});

Expand All @@ -121,10 +127,11 @@ describe('Action Schematic', () => {
`${projectPath}/src/app/foo.actions.ts`
);
expect(fileContent).toMatch(
new RegExp(`export const ${prefix}Foos = createAction`)
new RegExp(`export const FooActions = createActionGroup`)
);
expect(fileContent).toMatch(new RegExp(`source: 'Foo'`));
expect(fileContent).toMatch(
new RegExp(`'\\[Foo] ${capitalize(prefix)} Foos'`)
new RegExp(`'${capitalize(prefix)} Foos': emptyProps\\(\\),`)
);
}
);
Expand Down Expand Up @@ -158,7 +165,7 @@ describe('Action Schematic', () => {
);

expect(fileContent).toMatch(
/export const loadFoosSuccess = createAction\(\r?\n?\s*'\[Foo\] Load Foos Success'\r?\n?\s*,/
/'Load Foos Success': props<\{ data: unknown }>\(\),/
);
});

Expand All @@ -176,7 +183,7 @@ describe('Action Schematic', () => {
);

expect(fileContent).toMatch(
/export const loadFoosFailure = createAction\(\r?\n?\s*'\[Foo\] Load Foos Failure'\r?\n?\s*,/
/'Load Foos Failure': props<\{ error: unknown }>\(\),/
);
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { Injectable } from '@angular/core';
import { Actions, <%= effectMethod %><% if (feature) { %>, ofType<% } %> } from '@ngrx/effects';
<% if (feature && api) { %>import { catchError, map, concatMap } from 'rxjs/operators';
import { Observable, EMPTY, of } from 'rxjs';
import * as <%= classify(name) %>Actions from '<%= featurePath(group, flat, "actions", dasherize(name)) %><%= dasherize(name) %>.actions';<% } %>
import { <%= classify(name) %>Actions } from '<%= featurePath(group, flat, "actions", dasherize(name)) %><%= dasherize(name) %>.actions';<% } %>
<% if (feature && !api) { %>import { concatMap } from 'rxjs/operators';
import { Observable, EMPTY } from 'rxjs';
import * as <%= classify(name) %>Actions from '<%= featurePath(group, flat, "actions", dasherize(name)) %><%= dasherize(name) %>.actions';<% } %>
import { <%= classify(name) %>Actions } from '<%= featurePath(group, flat, "actions", dasherize(name)) %><%= dasherize(name) %>.actions';<% } %>

@Injectable()
export class <%= classify(name) %>Effects {
Expand Down
14 changes: 7 additions & 7 deletions modules/schematics/src/effect/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ describe('Effect Schematic', () => {
import { concatMap } from 'rxjs/operators';
import { Observable, EMPTY } from 'rxjs';
import * as FooActions from '../../actions/foo/foo.actions';
import { FooActions } from '../../actions/foo/foo.actions';
@Injectable()
export class FooEffects {
Expand Down Expand Up @@ -313,7 +313,7 @@ describe('Effect Schematic', () => {
import { concatMap } from 'rxjs/operators';
import { Observable, EMPTY } from 'rxjs';
import * as FooActions from './foo.actions';
import { FooActions } from './foo.actions';
@Injectable()
export class FooEffects {
Expand Down Expand Up @@ -370,7 +370,7 @@ describe('Effect Schematic', () => {
import { Actions, createEffect, ofType } from '@ngrx/effects';
import { catchError, map, concatMap } from 'rxjs/operators';
import { Observable, EMPTY, of } from 'rxjs';
import * as FooActions from './foo.actions';
import { FooActions } from './foo.actions';
@Injectable()
Expand Down Expand Up @@ -409,7 +409,7 @@ describe('Effect Schematic', () => {
import { concatMap } from 'rxjs/operators';
import { Observable, EMPTY } from 'rxjs';
import * as FooActions from './foo.actions';
import { FooActions } from './foo.actions';
@Injectable()
export class FooEffects {
Expand Down Expand Up @@ -447,7 +447,7 @@ describe('Effect Schematic', () => {
import { Actions, createEffect, ofType } from '@ngrx/effects';
import { catchError, map, concatMap } from 'rxjs/operators';
import { Observable, EMPTY, of } from 'rxjs';
import * as FooActions from './foo.actions';
import { FooActions } from './foo.actions';
@Injectable()
Expand All @@ -470,7 +470,7 @@ describe('Effect Schematic', () => {
constructor(private actions$: Actions) {}
}
"
`);
`);
});

it('should inject the effect service correctly', async () => {
Expand Down Expand Up @@ -501,7 +501,7 @@ describe('Effect Schematic', () => {
import { Actions, createEffect, ofType } from '@ngrx/effects';
import { catchError, map, concatMap } from 'rxjs/operators';
import { Observable, EMPTY, of } from 'rxjs';
import * as FooActions from './foo.actions';
import { FooActions } from './foo.actions';
@Injectable()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,53 +1,20 @@
import { createAction, props } from '@ngrx/store';
import { createActionGroup, emptyProps, props } from '@ngrx/store';
import { Update } from '@ngrx/entity';

import { <%= classify(name) %> } from '<%= featurePath(group, flat, "models", dasherize(name)) %><%= dasherize(name) %>.model';

export const load<%= classify(name) %>s = createAction(
'[<%= classify(name) %>/API] Load <%= classify(name) %>s',
props<{ <%= camelize(name) %>s: <%= classify(name) %>[] }>()
);

export const add<%= classify(name) %> = createAction(
'[<%= classify(name) %>/API] Add <%= classify(name) %>',
props<{ <%= camelize(name) %>: <%= classify(name) %> }>()
);

export const upsert<%= classify(name) %> = createAction(
'[<%= classify(name) %>/API] Upsert <%= classify(name) %>',
props<{ <%= camelize(name) %>: <%= classify(name) %> }>()
);

export const add<%= classify(name) %>s = createAction(
'[<%= classify(name) %>/API] Add <%= classify(name) %>s',
props<{ <%= camelize(name) %>s: <%= classify(name) %>[] }>()
);

export const upsert<%= classify(name) %>s = createAction(
'[<%= classify(name) %>/API] Upsert <%= classify(name) %>s',
props<{ <%= camelize(name) %>s: <%= classify(name) %>[] }>()
);

export const update<%= classify(name) %> = createAction(
'[<%= classify(name) %>/API] Update <%= classify(name) %>',
props<{ <%= camelize(name) %>: Update<<%= classify(name) %>> }>()
);

export const update<%= classify(name) %>s = createAction(
'[<%= classify(name) %>/API] Update <%= classify(name) %>s',
props<{ <%= camelize(name) %>s: Update<<%= classify(name) %>>[] }>()
);

export const delete<%= classify(name) %> = createAction(
'[<%= classify(name) %>/API] Delete <%= classify(name) %>',
props<{ id: string }>()
);

export const delete<%= classify(name) %>s = createAction(
'[<%= classify(name) %>/API] Delete <%= classify(name) %>s',
props<{ ids: string[] }>()
);

export const clear<%= classify(name) %>s = createAction(
'[<%= classify(name) %>/API] Clear <%= classify(name) %>s'
);
export const <%= classify(name) %>Actions = createActionGroup({
source: '<%= classify(name) %>/API',
events: {
'Load <%= classify(name) %>s': props<{ <%= camelize(name) %>s: <%= classify(name) %>[] }>(),
'Add <%= classify(name) %>': props<{ <%= camelize(name) %>: <%= classify(name) %> }>(),
'Upsert <%= classify(name) %>': props<{ <%= camelize(name) %>: <%= classify(name) %> }>(),
'Add <%= classify(name) %>s': props<{ <%= camelize(name) %>: <%= classify(name) %>[] }>(),
'Upsert <%= classify(name) %>s': props<{ <%= camelize(name) %>: <%= classify(name) %>[] }>(),
'Update <%= classify(name) %>': props<{ <%= camelize(name) %>: Update<<%= classify(name) %>> }>(),
'Update <%= classify(name) %>s': props<{ <%= camelize(name) %>s: Update<<%= classify(name) %>>[] }>(),
'Delete <%= classify(name) %>': props<{ id: string }>(),
'Delete <%= classify(name) %>s': props<{ ids: string[] }>(),
'Clear <%= classify(name) %>s': emptyProps(),
}
});
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createFeature, createReducer, on } from '@ngrx/store';
import { EntityState, EntityAdapter, createEntityAdapter } from '@ngrx/entity';
import { <%= classify(name) %> } from '<%= featurePath(group, flat, "models", dasherize(name)) %><%= dasherize(name) %>.model';
import * as <%= classify(name) %>Actions from '<%= featurePath(group, flat, "actions", dasherize(name)) %><%= dasherize(name) %>.actions';
import { <%= classify(name) %>Actions } from '<%= featurePath(group, flat, "actions", dasherize(name)) %><%= dasherize(name) %>.actions';

export const <%= pluralize(name) %>FeatureKey = '<%= pluralize(name) %>';

Expand Down
8 changes: 5 additions & 3 deletions modules/schematics/src/entity/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,10 @@ describe('Entity Schematic', () => {
const fileContent = tree.readContent(
`${projectPath}/src/app/foo.actions.ts`
);
expect(fileContent).toMatch(/export const loadFoos = createAction\(/);
expect(fileContent).toMatch(/\[Foo\/API\] Load Foos'/);
expect(fileContent).toMatch(
/export const FooActions = createActionGroup\(/
);
expect(fileContent).toMatch(/'Load Foos'/);
expect(fileContent).toMatch(/props<\{ foos: Foo\[\] }>\(\)/);
});

Expand All @@ -230,7 +232,7 @@ describe('Entity Schematic', () => {
`${projectPath}/src/app/foo.reducer.ts`
);
expect(fileContent).toMatch(
/import \* as FooActions from '\.\/foo.actions';/
/import { FooActions } from '\.\/foo.actions';/
);
expect(fileContent).toMatch(/on\(FooActions.addFoo,/);
expect(fileContent).toMatch(
Expand Down
4 changes: 1 addition & 3 deletions modules/schematics/src/feature/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,9 +225,7 @@ describe('Feature Schematic', () => {
expect(fileContent).toMatch(
/import { Observable, EMPTY, of } from 'rxjs';/
);
expect(fileContent).toMatch(
/import \* as FooActions from '.\/foo.actions';/
);
expect(fileContent).toMatch(/import { FooActions } from '.\/foo.actions';/);

expect(fileContent).toMatch(/export class FooEffects/);
expect(fileContent).toMatch(/loadFoos\$ = createEffect\(\(\) => {/);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {<% if(feature) { %> createFeature,<% } %> createReducer, on } from '@ngrx/store';
import * as <%= classify(name) %>Actions from '<%= featurePath(group, flat, "actions", dasherize(name)) %><%= dasherize(name) %>.actions';
import { <%= classify(name) %>Actions } from '<%= featurePath(group, flat, "actions", dasherize(name)) %><%= dasherize(name) %>.actions';

export const <%= camelize(name) %>FeatureKey = '<%= camelize(name) %>';

Expand Down
2 changes: 1 addition & 1 deletion modules/schematics/src/reducer/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ describe('Reducer Schematic', () => {
);

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

Expand Down

0 comments on commit f6ce20f

Please sign in to comment.