forked from nrwl/nx
/
ngrx.spec.ts
723 lines (632 loc) · 22.4 KB
/
ngrx.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
import { UnitTestTree } from '@angular-devkit/schematics/testing';
import { Tree } from '@angular-devkit/schematics';
import { readJsonInTree } from '@nrwl/workspace';
import { findModuleParent } from '@nrwl/workspace';
import { getFileContent } from '@nrwl/workspace/testing';
import {
AppConfig,
createApp,
createLib,
getAppConfig,
getLibConfig,
runSchematic
} from '../../utils/testing';
import { createEmptyWorkspace } from '@nrwl/workspace/testing';
describe('ngrx', () => {
let appTree: Tree;
beforeEach(() => {
appTree = Tree.empty();
appTree = createEmptyWorkspace(appTree);
appTree = createApp(appTree, 'myapp');
});
it('should add empty root', async () => {
const tree = await runSchematic(
'ngrx',
{
name: 'state',
module: 'apps/myapp/src/app/app.module.ts',
onlyEmptyRoot: true,
minimal: false,
root: true
},
appTree
);
const appModule = getFileContent(tree, '/apps/myapp/src/app/app.module.ts');
expect(
tree.exists('apps/myapp/src/app/+state/state.actions.ts')
).toBeFalsy();
expect(appModule).toContain('StoreModule.forRoot(');
expect(appModule).toContain('runtimeChecks: {');
expect(appModule).toContain('strictActionImmutability: true');
expect(appModule).toContain('strictStateImmutability: true');
expect(appModule).toContain('EffectsModule.forRoot');
});
it('should add empty root with minimal option', async () => {
const tree = await runSchematic(
'ngrx',
{
name: 'state',
module: 'apps/myapp/src/app/app.module.ts',
root: true,
onlyEmptyRoot: false,
minimal: true
},
appTree
);
const appModule = getFileContent(tree, '/apps/myapp/src/app/app.module.ts');
expect(
tree.exists('apps/myapp/src/app/+state/state.actions.ts')
).toBeFalsy();
expect(appModule).toContain('StoreModule.forRoot(');
expect(appModule).toContain('runtimeChecks: {');
expect(appModule).toContain('strictActionImmutability: true');
expect(appModule).toContain('strictStateImmutability: true');
expect(appModule).toContain('EffectsModule.forRoot([])');
});
it('should add root', async () => {
const tree = await runSchematic(
'ngrx',
{
name: 'app',
module: 'apps/myapp/src/app/app.module.ts',
root: true,
minimal: false
},
appTree
);
[
'/apps/myapp/src/app/+state/app.actions.ts',
'/apps/myapp/src/app/+state/app.effects.ts',
'/apps/myapp/src/app/+state/app.effects.spec.ts',
'/apps/myapp/src/app/+state/app.reducer.ts',
'/apps/myapp/src/app/+state/app.reducer.spec.ts',
'/apps/myapp/src/app/+state/app.selectors.ts',
'/apps/myapp/src/app/+state/app.selectors.spec.ts'
].forEach(fileName => {
expect(tree.exists(fileName)).toBeTruthy();
});
// Since we did not include the `--facade` option
expect(tree.exists('/apps/myapp/src/app/+state/app.facade.ts')).toBeFalsy();
expect(
tree.exists('/apps/myapp/src/app/+state/app.facade.spec.ts')
).toBeFalsy();
const appModule = getFileContent(tree, '/apps/myapp/src/app/app.module.ts');
expect(appModule).toContain(`import { NxModule } from '@nrwl/angular';`);
expect(appModule).toContain(
`import * as fromApp from './+state/app.reducer';`
);
expect(appModule).toContain('NxModule.forRoot');
expect(appModule).toContain('StoreModule.forRoot');
expect(appModule).toContain(
`StoreModule.forFeature(fromApp.APP_FEATURE_KEY, fromApp.reducer)`
);
expect(appModule).toContain('EffectsModule.forRoot');
expect(appModule).toContain(
'metaReducers: !environment.production ? [] : []'
);
});
it('should add facade to root', async () => {
const tree = await runSchematic(
'ngrx',
{
name: 'app',
module: 'apps/myapp/src/app/app.module.ts',
root: true,
facade: true,
minimal: false
},
appTree
);
const appModule = getFileContent(tree, '/apps/myapp/src/app/app.module.ts');
expect(appModule).toContain(`import { NxModule } from '@nrwl/angular';`);
expect(appModule).toContain('NxModule.forRoot');
expect(appModule).toContain('StoreModule.forRoot');
expect(appModule).toContain('EffectsModule.forRoot');
expect(appModule).toContain(
'metaReducers: !environment.production ? [] : []'
);
// Do not add Effects file to providers; already registered in EffectsModule
expect(appModule).toContain('providers: [AppFacade]');
[
'/apps/myapp/src/app/+state/app.actions.ts',
'/apps/myapp/src/app/+state/app.effects.ts',
'/apps/myapp/src/app/+state/app.effects.spec.ts',
'/apps/myapp/src/app/+state/app.reducer.ts',
'/apps/myapp/src/app/+state/app.reducer.spec.ts',
'/apps/myapp/src/app/+state/app.facade.ts',
'/apps/myapp/src/app/+state/app.facade.spec.ts',
'/apps/myapp/src/app/+state/app.selectors.ts',
'/apps/myapp/src/app/+state/app.selectors.spec.ts'
].forEach(fileName => {
expect(tree.exists(fileName)).toBeTruthy();
});
});
it('should not add RouterStoreModule only if the module does not reference the router', async () => {
const newTree = createApp(appTree, 'myapp-norouter', false);
const tree = await runSchematic(
'ngrx',
{
name: 'app',
module: 'apps/myapp-norouter/src/app/app.module.ts',
root: true
},
newTree
);
const appModule = getFileContent(
tree,
'/apps/myapp-norouter/src/app/app.module.ts'
);
expect(appModule).not.toContain('StoreRouterConnectingModule.forRoot()');
});
it('should add feature', async () => {
const tree = await runSchematic(
'ngrx',
{
name: 'state',
module: 'apps/myapp/src/app/app.module.ts',
minimal: false
},
appTree
);
const appModule = getFileContent(tree, '/apps/myapp/src/app/app.module.ts');
expect(appModule).toContain('StoreModule.forFeature');
expect(appModule).toContain('EffectsModule.forFeature');
expect(appModule).not.toContain('!environment.production ? [] : []');
expect(
tree.exists(`/apps/myapp/src/app/+state/state.actions.ts`)
).toBeTruthy();
});
it('should add with custom directoryName', async () => {
const tree = await runSchematic(
'ngrx',
{
name: 'state',
module: 'apps/myapp/src/app/app.module.ts',
directory: 'myCustomState',
minimal: false
},
appTree
);
const appModule = getFileContent(tree, '/apps/myapp/src/app/app.module.ts');
expect(appModule).toContain('StoreModule.forFeature');
expect(appModule).toContain('EffectsModule.forFeature');
expect(appModule).not.toContain('!environment.production ? [] : []');
expect(
tree.exists(`/apps/myapp/src/app/my-custom-state/state.actions.ts`)
).toBeTruthy();
});
it('should only add files', async () => {
const tree = await runSchematic(
'ngrx',
{
name: 'state',
module: 'apps/myapp/src/app/app.module.ts',
onlyAddFiles: true,
facade: true,
minimal: false
},
appTree
);
const appModule = getFileContent(tree, '/apps/myapp/src/app/app.module.ts');
expect(appModule).not.toContain('StoreModule');
expect(appModule).not.toContain('!environment.production ? [] : []');
[
'/apps/myapp/src/app/+state/state.effects.ts',
'/apps/myapp/src/app/+state/state.facade.ts',
'/apps/myapp/src/app/+state/state.reducer.ts',
'/apps/myapp/src/app/+state/state.selectors.ts',
'/apps/myapp/src/app/+state/state.effects.spec.ts',
'/apps/myapp/src/app/+state/state.facade.spec.ts',
'/apps/myapp/src/app/+state/state.selectors.spec.ts'
].forEach(fileName => {
expect(tree.exists(fileName)).toBeTruthy();
});
});
it('should only add files with skipImport option', async () => {
const tree = await runSchematic(
'ngrx',
{
name: 'state',
module: 'apps/myapp/src/app/app.module.ts',
onlyAddFiles: false,
skipImport: true,
facade: true,
minimal: false
},
appTree
);
const appModule = getFileContent(tree, '/apps/myapp/src/app/app.module.ts');
expect(appModule).not.toContain('StoreModule');
expect(appModule).not.toContain('!environment.production ? [] : []');
[
'/apps/myapp/src/app/+state/state.effects.ts',
'/apps/myapp/src/app/+state/state.facade.ts',
'/apps/myapp/src/app/+state/state.reducer.ts',
'/apps/myapp/src/app/+state/state.selectors.ts',
'/apps/myapp/src/app/+state/state.effects.spec.ts',
'/apps/myapp/src/app/+state/state.facade.spec.ts',
'/apps/myapp/src/app/+state/state.selectors.spec.ts'
].forEach(fileName => {
expect(tree.exists(fileName)).toBeTruthy();
});
});
it('should update package.json', async () => {
const tree = await runSchematic(
'ngrx',
{
name: 'state',
module: 'apps/myapp/src/app/app.module.ts'
},
appTree
);
const packageJson = readJsonInTree(tree, 'package.json');
expect(packageJson.dependencies['@ngrx/store']).toBeDefined();
expect(packageJson.dependencies['@ngrx/router-store']).toBeDefined();
expect(packageJson.dependencies['@ngrx/effects']).toBeDefined();
expect(packageJson.devDependencies['@ngrx/store-devtools']).toBeDefined();
});
it('should error when no module is provided', async () => {
try {
await runSchematic(
'ngrx',
{
name: 'state',
module: ''
},
appTree
);
fail();
} catch (e) {
expect(e.message).toEqual('The required --module option must be passed');
}
});
it('should error the module could not be found', async () => {
try {
await runSchematic(
'ngrx',
{
name: 'state',
module: 'does-not-exist.ts'
},
appTree
);
} catch (e) {
expect(e.message).toEqual('Path does not exist: does-not-exist.ts');
}
});
describe('code generation', () => {
it('should scaffold the ngrx "user" files without a facade', async () => {
const appConfig = getAppConfig();
const hasFile = file => expect(tree.exists(file)).toBeTruthy();
const missingFile = file => expect(tree.exists(file)).not.toBeTruthy();
const statePath = `${findModuleParent(appConfig.appModule)}/+state`;
const tree = await buildNgrxTree(appConfig);
hasFile(`${statePath}/user.actions.ts`);
hasFile(`${statePath}/user.effects.ts`);
hasFile(`${statePath}/user.effects.spec.ts`);
missingFile(`${statePath}/user.facade.ts`);
missingFile(`${statePath}/user.facade.spec.ts`);
hasFile(`${statePath}/user.reducer.ts`);
hasFile(`${statePath}/user.reducer.spec.ts`);
hasFile(`${statePath}/user.selectors.ts`);
});
it('should scaffold the ngrx "user" files WITH a facade', async () => {
const appConfig = getAppConfig();
const hasFile = file => expect(tree.exists(file)).toBeTruthy();
const tree = await buildNgrxTree(appConfig, 'user', true);
const statePath = `${findModuleParent(appConfig.appModule)}/+state`;
hasFile(`${statePath}/user.actions.ts`);
hasFile(`${statePath}/user.effects.ts`);
hasFile(`${statePath}/user.facade.ts`);
hasFile(`${statePath}/user.reducer.ts`);
hasFile(`${statePath}/user.selectors.ts`);
hasFile(`${statePath}/user.reducer.spec.ts`);
hasFile(`${statePath}/user.effects.spec.ts`);
hasFile(`${statePath}/user.selectors.spec.ts`);
hasFile(`${statePath}/user.facade.spec.ts`);
});
it('should build the ngrx actions', async () => {
const appConfig = getAppConfig();
const tree = await buildNgrxTree(appConfig, 'users');
const statePath = `${findModuleParent(appConfig.appModule)}/+state`;
const content = getFileContent(tree, `${statePath}/users.actions.ts`);
expect(content).toContain('UsersActionTypes');
expect(content).toContain(`LoadUsers = '[Users] Load Users'`);
expect(content).toContain(`UsersLoaded = '[Users] Users Loaded'`);
expect(content).toContain(`UsersLoadError = '[Users] Users Load Error'`);
expect(content).toContain('class LoadUsers implements Action');
expect(content).toContain('class UsersLoaded implements Action');
expect(content).toContain(
'type UsersAction = LoadUsers | UsersLoaded | UsersLoadError'
);
expect(content).toContain('export const fromUsersActions');
});
it('should build the ngrx selectors', async () => {
const appConfig = getAppConfig();
const tree = await buildNgrxTree(appConfig, 'users');
const statePath = `${findModuleParent(appConfig.appModule)}/+state`;
const content = getFileContent(tree, `${statePath}/users.selectors.ts`);
[
`import { USERS_FEATURE_KEY, UsersState } from './users.reducer'`,
`export const usersQuery`
].forEach(text => {
expect(content).toContain(text);
});
});
it('should build the ngrx facade', async () => {
const appConfig = getAppConfig();
const includeFacade = true;
const tree = await buildNgrxTree(appConfig, 'users', includeFacade);
const statePath = `${findModuleParent(appConfig.appModule)}/+state`;
const content = getFileContent(tree, `${statePath}/users.facade.ts`);
[
`import { UsersPartialState } from './users.reducer'`,
`import { usersQuery } from './users.selectors'`,
`export class UsersFacade`
].forEach(text => {
expect(content).toContain(text);
});
});
it('should build the ngrx reducer', async () => {
const appConfig = getAppConfig();
const tree = await buildNgrxTree(appConfig, 'user');
const statePath = `${findModuleParent(appConfig.appModule)}/+state`;
const content = getFileContent(tree, `${statePath}/user.reducer.ts`);
[
`import { UserAction, UserActionTypes } from \'./user.actions\'`,
`export interface User`,
`export interface UserState`,
'export function reducer',
'state: UserState = initialState',
'action: UserAction',
'): UserState',
'case UserActionTypes.UserLoaded'
].forEach(text => {
expect(content).toContain(text);
});
});
it('should build the ngrx effects', async () => {
const appConfig = getAppConfig();
const tree = await buildNgrxTree(appConfig, 'users');
const statePath = `${findModuleParent(appConfig.appModule)}/+state`;
const content = getFileContent(tree, `${statePath}/users.effects.ts`);
[
`import { DataPersistence } from '@nrwl/angular'`,
`
import {
LoadUsers,
UsersLoaded,
UsersLoadError,
UsersActionTypes
} from './users.actions';`,
`loadUsers$`,
`run: (action: LoadUsers, state: UsersPartialState)`,
`return new UsersLoaded([])`,
`return new UsersLoadError(error)`,
'private actions$: Actions',
'private dataPersistence: DataPersistence<UsersPartialState>'
].forEach(text => {
expect(content).toContain(text);
});
});
});
describe('unit tests', () => {
it('should produce proper specs for the ngrx reducer', async () => {
const appConfig = getAppConfig();
const tree = await buildNgrxTree(appConfig);
const statePath = `${findModuleParent(appConfig.appModule)}/+state`;
const contents = tree.readContent(`${statePath}/user.reducer.spec.ts`);
expect(contents).toContain(`describe('User Reducer', () => {`);
expect(contents).toContain(
'const result = reducer(initialState, action);'
);
});
it('should update the barrel API with exports for ngrx facade, selector, and reducer', async () => {
appTree = createLib(appTree, 'flights');
let libConfig = getLibConfig();
let tree = await runSchematic(
'ngrx',
{
name: 'super-users',
module: libConfig.module,
facade: true
},
appTree
);
const barrel = tree.readContent(libConfig.barrel);
expect(barrel).toContain(
`export * from './lib/+state/super-users.facade';`
);
});
it('should not update the barrel API with a facade', async () => {
appTree = createLib(appTree, 'flights');
let libConfig = getLibConfig();
let tree = await runSchematic(
'ngrx',
{
name: 'super-users',
module: libConfig.module,
facade: false
},
appTree
);
const barrel = tree.readContent(libConfig.barrel);
expect(barrel).not.toContain(
`export * from './lib/+state/super-users.facade';`
);
});
it('should produce proper tests for the ngrx reducer for a name with a dash', async () => {
const appConfig = getAppConfig();
const tree = await runSchematic(
'ngrx',
{
name: 'super-users',
module: appConfig.appModule,
minimal: false
},
appTree
);
const statePath = `${findModuleParent(appConfig.appModule)}/+state`;
const contents = tree.readContent(
`${statePath}/super-users.reducer.spec.ts`
);
expect(contents).toContain(`describe('SuperUsers Reducer', () => {`);
expect(contents).toContain(
`const result = reducer(initialState, action);`
);
});
});
describe('creators syntax', () => {
let appConfig = getAppConfig();
let tree: UnitTestTree;
let statePath: string;
beforeEach(async () => {
appConfig = getAppConfig();
tree = await runSchematic(
'ngrx',
{
name: 'users',
module: appConfig.appModule,
syntax: 'creators',
minimal: false,
facade: true,
useDataPersistance: false
},
appTree
);
statePath = `${findModuleParent(appConfig.appModule)}/+state`;
});
it('should generate a set of actions for the feature', async () => {
const content = tree.readContent(`${statePath}/users.actions.ts`);
[
'[Users] Load Users',
'[Users] Load Users Success',
'props<{ users: UsersEntity[] }>()',
'[Users] Load Users Failure',
'props<{ error: any }>()'
].forEach(text => {
expect(content).toContain(text);
});
});
it('should generate a reducer for the feature', async () => {
const content = tree.readContent(`${statePath}/users.reducer.ts`);
[
`export const USERS_FEATURE_KEY = 'users';`,
`const usersReducer = createReducer`,
'export function reducer(state: State | undefined, action: Action) {'
].forEach(text => {
expect(content).toContain(text);
});
});
it('should generate effects for the feature', async () => {
const content = tree.readContent(`${statePath}/users.effects.ts`);
[
`import { createEffect, Actions, ofType } from '@ngrx/effects';`,
'fetch({'
].forEach(text => {
expect(content).toContain(text);
});
});
it('should generate selectors for the feature', async () => {
const content = tree.readContent(`${statePath}/users.selectors.ts`);
[
`
import {
USERS_FEATURE_KEY,
State,
UsersPartialState,
usersAdapter
} from './users.reducer';`,
`const { selectAll, selectEntities } = usersAdapter.getSelectors();`
].forEach(text => {
expect(content).toContain(text);
});
});
it('should generate a facade for the feature if enabled', async () => {
const content = tree.readContent(`${statePath}/users.facade.ts`);
[
`loaded$ = this.store.pipe(select(UsersSelectors.getUsersLoaded));`,
`allUsers$ = this.store.pipe(select(UsersSelectors.getAllUsers));`,
`selectedUsers$ = this.store.pipe(select(UsersSelectors.getSelected));`
].forEach(text => {
expect(content).toContain(text);
});
});
it('should generate a models file for the feature', async () => {
const content = tree.readContent(`${statePath}/users.models.ts`);
[
'export interface UsersEntity',
'id: string | number; // Primary ID'
].forEach(text => {
expect(content).toContain(text);
});
});
it('should use DataPersistence operators when useDataPersistence is set to false', async () => {
appTree = Tree.empty();
appTree = createEmptyWorkspace(appTree);
appTree = createApp(appTree, 'myapp');
const tree = await runSchematic(
'ngrx',
{
name: 'users',
module: appConfig.appModule,
syntax: 'creators',
facade: true,
minimal: false
},
appTree
);
const content = tree.readContent(`${statePath}/users.effects.ts`);
[`{ fetch }`, `, ofType`, `ofType(UsersActions.loadUsers),`].forEach(
text => {
expect(content).toContain(text);
}
);
expect(content).not.toContain('dataPersistence.fetch');
});
it('should re-export actions, state, and selectors using barrels if enabled', async () => {
appTree = Tree.empty();
appTree = createEmptyWorkspace(appTree);
appTree = createApp(appTree, 'myapp');
appTree.create('/apps/myapp/src/index.ts', '');
const tree = await runSchematic(
'ngrx',
{
name: 'users',
module: appConfig.appModule,
syntax: 'creators',
barrels: true
},
appTree
);
const content = tree.readContent('/apps/myapp/src/index.ts');
[
`import * as UsersActions from './lib/+state/users.actions';`,
`import * as UsersFeature from './lib/+state/users.reducer';`,
`import * as UsersSelectors from './lib/+state/users.selectors';`,
`export { UsersActions, UsersFeature, UsersSelectors };`,
`export * from './lib/+state/users.models';`
].forEach(text => {
expect(content).toContain(text);
});
});
});
async function buildNgrxTree(
appConfig: AppConfig,
featureName: string = 'user',
withFacade = false
): Promise<UnitTestTree> {
return await runSchematic(
'ngrx',
{
name: featureName,
module: appConfig.appModule,
facade: withFacade,
syntax: 'classes',
minimal: false,
useDataPersistance: true
},
appTree
);
}
});