Skip to content

Commit

Permalink
feat(ngrx): add creator files
Browse files Browse the repository at this point in the history
  • Loading branch information
filip-va authored and va-stefanek committed Jul 2, 2021
1 parent b3d692c commit 4e340cc
Show file tree
Hide file tree
Showing 12 changed files with 162 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { Action } from '@ngrx/store';

export enum Types {}

export type CollectiveType = Action;
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { TestBed } from '@angular/core/testing';
import { Observable } from 'rxjs';
import { provideMockActions } from '@ngrx/effects/testing';
import { provideMockStore } from '@ngrx/store/testing';
import { NxModule } from '@nrwl/angular';
import { cold, hot } from 'jest-marbles';
import { <%= className %>Effects } from './<%= fileName %>.effects';

describe('<%= className %>Effects', () => {
let actions: Observable<any>;
let effects: <%= className %>Effects;

beforeEach(() => {
TestBed.configureTestingModule({
imports: [
NxModule.forRoot()
],
providers: [
<%= className %>Effects,
provideMockActions(() => actions),
provideMockStore({ initialState: {} }),
],
});

effects = TestBed.inject(<%= className %>Effects);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Injectable } from '@angular/core';
import { Effect } from '@ngrx/effects';
import * as from<%= className %>Actions from './<%= fileName %>.actions';
import { <%= className %>PartialState } from './<%= fileName %>.reducer';
import { Actions } from '@ngrx/store';

@Injectable()
export class <%= className %>Effects {
constructor(private actions$: Actions) {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { NgModule } from '@angular/core';
import { TestBed } from '@angular/core/testing';
import { provideMockActions } from '@ngrx/effects/testing';
import { Store } from '@ngrx/store';
import { MockStore, provideMockStore } from '@ngrx/store/testing';
import { NxModule } from '@nrwl/angular';
import { Observable } from 'rxjs';
import { <%= className %>Facade } from './<%= fileName %>.facade';

describe('<%= className %>Facade', () => {
let actions: Observable<any>;
let facade: <%= className %>Facade;
let store: MockStore;

describe('used in NgModule', () => {

beforeEach(() => {
@NgModule({
imports: [],
providers: [
<%= className %>Facade,
provideMockStore(),
provideMockActions(() => actions)
]
})
class CustomFeatureModule {}

@NgModule({
imports: [
NxModule.forRoot(),
CustomFeatureModule,
]
})
class RootModule {}

TestBed.configureTestingModule({ imports: [RootModule] });
facade = TestBed.inject(<%= className %>Facade);
store = TestBed.inject(MockStore);
jest.spyOn(store, 'dispatch');
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Injectable } from '@angular/core';
import { select, Store } from '@ngrx/store';
import { <%= className %>PartialState } from './<%= fileName %>.reducer';
import { <%= propertyName %>Query } from './<%= fileName %>.selectors';

@Injectable()
export class <%= className %>Facade {
constructor(private store: Store<<%= className %>PartialState>) {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import * as from<%= className %>Actions from './<%= fileName %>.actions';
import { <%= className %>State, initialState, reducer } from './<%= fileName %>.reducer';

describe('<%= className %> Reducer', () => {
let state: <%= className %>State;

beforeEach(() => {
state = { ...initialState };
});

describe('unknown action', () => {
test('returns the initial state', () => {
const action = {} as any;
const result = reducer(state, action);

expect(result).toBe(state);
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import * as from<%= className %>Actions from './<%= fileName %>.actions';
import { createReducer, ActionReducer } from '@ngrx/store';

export const <%= className.toUpperCase() %>_FEATURE_KEY = '<%= propertyName %>';

export interface <%= className %>State {
};

export interface <%= className %>PartialState {
readonly [<%= className.toUpperCase() %>_FEATURE_KEY]: <%= className %>State;
}

export const initialState: <%= className %>State = {};

const <%= propertyName %>Reducer = createReducer(initialState);

export function reducer(
state: <%= className %>State = initialState,
action: from<%= className %>Actions.CollectiveType
): ActionReducer<<%= className %>State, from<%= className %>Actions.CollectiveType> {
return <%= propertyName %>Reducer(state, action);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { initialState, <%= className.toUpperCase() %>_FEATURE_KEY, <%= className %>State } from './<%= fileName %>.reducer';
import { <%= propertyName %>Query } from './<%= fileName %>.selectors';

describe('<%= className %> Selectors', () => {
let storeState: { [<%= className.toUpperCase() %>_FEATURE_KEY]: <%= className %>State };

beforeEach(() => {
storeState = {
[<%= className.toUpperCase() %>_FEATURE_KEY]: initialState
};
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { createFeatureSelector, createSelector } from '@ngrx/store';
import { <%= className.toUpperCase() %>_FEATURE_KEY, <%= className %>State } from './<%= fileName %>.reducer';

// Lookup the '<%= className %>' feature state managed by NgRx
const get<%= className %>State = createFeatureSelector<<%= className %>State>(<%= propertyName.toUpperCase() %>_FEATURE_KEY);

export const <%= propertyName %>Query = {};
3 changes: 2 additions & 1 deletion src/collection/ngrx/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ function generateNgrxFilesFromTemplates(options: NgrxSchema): Rule {
const name = options.name;
const moduleDir = path.dirname(options.module);
const excludeFacade = (p: any) => p.match(/^((?!facade).)*$/);
const sourceUrl = options.creators ? './creator-files' : './files';

const templateSource = apply(url('./files'), [
const templateSource = apply(url(sourceUrl), [
!options.facade ? filter(excludeFacade) : noop(),
template({ ...options, tmpl: '', ...names(name) }),
move(moduleDir)
Expand Down
1 change: 1 addition & 0 deletions src/collection/ngrx/ngrx-schema.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ export interface NgrxSchema {
root?: boolean;
skipFormat?: boolean;
skipPackageJson?: boolean;
creators?: boolean;
}
6 changes: 6 additions & 0 deletions src/collection/ngrx/ngrx-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@
"type": "boolean",
"default": false,
"description": "Do not add NgRx dependencies to package.json (e.g., --skipPackageJson)"
},
"creator": {
"type": "boolean",
"default": true,
"description": "Generate store using Creators",
"x-prompt": "Would you like to use creator functions?"
}
},
"required": ["module"]
Expand Down

0 comments on commit 4e340cc

Please sign in to comment.