Skip to content

Commit 00db79b

Browse files
committed
add selector tests and adding dependencies
1 parent 5406259 commit 00db79b

File tree

6 files changed

+81
-17
lines changed

6 files changed

+81
-17
lines changed

src/ngrx-entity/__files__/__name@dasherize@if-flat__/__name@dasherize__.effects.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ describe('<%= classify(name) %>Effects', () => {
6666
service.create.and.returnValue(cold('-e|', { e: entity }));
6767
const expected = cold('-s', { s: successAction });
6868

69-
expect(effects.insert).toBeObservable(expected);
69+
expect(effects.create).toBeObservable(expected);
7070
});
7171

7272
it('should return Create<%= classify(name) %>Fail with error object on failure', () => {
@@ -78,7 +78,7 @@ describe('<%= classify(name) %>Effects', () => {
7878
service.create.and.returnValue(cold('-#|', {}, { message: 'fail'}));
7979
const expected = cold('-f', { f: failAction });
8080

81-
expect(effects.insert).toBeObservable(expected);
81+
expect(effects.create).toBeObservable(expected);
8282
});
8383
});
8484

src/ngrx-entity/__files__/__name@dasherize@if-flat__/__name@dasherize__.reducer.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,13 @@ describe('<%= name %>Reducer', () => {
5858

5959
describe('upon Create<%= classify(name) %>Fail', () => {
6060
it('should set loading to true and echo the error', () => {
61-
const error = 'test insert error';
61+
const error = 'test create error';
6262
const action = new actions.Create<%= classify(name) %>Fail({ error });
6363

6464
expect(<%= name %>Reducer(initial<%= classify(name)%>State, action)).toEqual({
6565
...initial<%= classify(name)%>State,
6666
loading: false,
67-
error: `<%= classify(name) %> insert failed: ${error}`
67+
error: `<%= classify(name) %> create failed: ${error}`
6868
});
6969
});
7070
});
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import {
2+
current<%= classify(name) %>Id,
3+
current<%= classify(name) %>,
4+
<%= name %>Loading,
5+
<%= name %>Error,
6+
<%= name %>Query
7+
} from "./index";
8+
import { <%= classify(name) %> } from "./<%= name %>.model";
9+
10+
const create<%= classify(name) %> = ({ id = 0, name = "", description = "" } = {}): <%= classify(name) %> => ({
11+
id: id,
12+
name: name || "name",
13+
description: description || `description`
14+
});
15+
16+
// State Factory
17+
const create<%= classify(name) %>sState = ({
18+
entities = {
19+
"1": create<%= classify(name) %>({ id: 1, name: "Bob" }),
20+
"2": create<%= classify(name) %>({ id: 2, name: "Sue" }),
21+
"3": create<%= classify(name) %>({ id: 3, name: "Mary" })
22+
},
23+
ids = ["1", "2", "3"],
24+
selectedId = 1,
25+
loading = false,
26+
error = "",
27+
query = null
28+
} = {}) => ({
29+
<%= name %>: {
30+
ids,
31+
entities,
32+
selectedId,
33+
loading,
34+
error,
35+
query
36+
}
37+
});
38+
39+
let state;
40+
41+
describe("<%= name %>Selectors", () => {
42+
beforeEach(() => {
43+
state = create<%= classify(name) %>sState();
44+
});
45+
46+
it("current<%= classify(name) %>Id", () => {
47+
expect(current<%= classify(name) %>Id(state)).toEqual(1);
48+
});
49+
50+
it("current<%= classify(name) %>", () => {
51+
expect(current<%= classify(name) %>(state)).toEqual(state.<%= name %>.entities[1]);
52+
});
53+
54+
it("<%= name %>Loading", () => {
55+
state.<%= name %>.loading = true;
56+
expect(<%= name %>Loading(state)).toEqual(state.<%= name %>.loading);
57+
});
58+
59+
it("<%= name %>Error", () => {
60+
state.<%= name %>.error = 'error loading <%= name %>s';
61+
expect(<%= name %>Error(state)).toEqual(state.<%= name %>.error);
62+
});
63+
64+
it("<%= name %>Query", () => {
65+
state.<%= name %>.query = 'page;=2'
66+
expect(<%= name %>Query(state)).toEqual(state.<%= name %>.query);
67+
});
68+
});

src/ngrx-entity/__files__/state.module.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { CommonModule } from '@angular/common';
2+
import { HttpClientModule } from '@angular/common/http';
23
import { ModuleWithProviders, NgModule, Optional, SkipSelf } from '@angular/core';
34
import { RouterStateSerializer, StoreRouterConnectingModule } from '@ngrx/router-store';
45

@@ -14,6 +15,7 @@ import { <%= classify(name) %>Effects } from './<%= dasherize(name) %>/<%= dashe
1415
@NgModule({
1516
imports: [
1617
CommonModule,
18+
HttpClientModule,
1719
StoreRouterConnectingModule,
1820
StoreModule.forRoot(appReducer, { metaReducers: appMetaReducers }),
1921
EffectsModule.forRoot([<%= classify(name) %>Effects]),

src/ngrx-entity/index.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ import {
1111
template,
1212
filter,
1313
noop,
14-
SchematicsException
14+
SchematicsException,
15+
mergeWith
1516
} from "@angular-devkit/schematics";
1617
import {
1718
NgRxOptions,
@@ -29,7 +30,7 @@ import {
2930

3031
export default function(options: NgRxOptions): Rule {
3132
return (tree: Tree, context: SchematicContext) => {
32-
return chain([updateDependencies(), addNgRxFiles(options)])(tree, context);
33+
return chain([addNgRxFiles(options), updateDependencies()])(tree, context);
3334
};
3435
}
3536

@@ -126,6 +127,9 @@ function addNgRxFiles(options: NgRxOptions): Rule {
126127
move(options.path)
127128
]);
128129

129-
return templateSource(context);
130+
return chain([mergeWith(templateSource)])(
131+
tree,
132+
context
133+
);
130134
};
131135
}

src/ngrx-entity/utility/json-utils.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -102,16 +102,6 @@ export function insertPropertyInAstObjectInOrder(
102102
)}` +
103103
","
104104
);
105-
106-
console.log(
107-
insertIndex,
108-
`${indentStr}` +
109-
`"${propertyName}": ${JSON.stringify(value, null, 2).replace(
110-
/\n/g,
111-
indentStr
112-
)}` +
113-
","
114-
);
115105
}
116106

117107
export function appendValueInAstArray(

0 commit comments

Comments
 (0)