|
| 1 | +import { Tree } from '@angular-devkit/schematics'; |
| 2 | +import { |
| 3 | + SchematicTestRunner, |
| 4 | + UnitTestTree, |
| 5 | +} from '@angular-devkit/schematics/testing'; |
| 6 | +import * as path from 'path'; |
| 7 | +import { createPackageJson } from '../../../schematics-core/testing/create-package'; |
| 8 | + |
| 9 | +describe('Effects Migration 9_0_0', () => { |
| 10 | + let appTree: UnitTestTree; |
| 11 | + const collectionPath = path.join(__dirname, '../migration.json'); |
| 12 | + const pkgName = 'effects'; |
| 13 | + |
| 14 | + beforeEach(() => { |
| 15 | + appTree = new UnitTestTree(Tree.empty()); |
| 16 | + appTree.create( |
| 17 | + '/tsconfig.json', |
| 18 | + ` |
| 19 | + { |
| 20 | + "include": [**./*.ts"] |
| 21 | + } |
| 22 | + ` |
| 23 | + ); |
| 24 | + createPackageJson('', pkgName, appTree); |
| 25 | + }); |
| 26 | + |
| 27 | + describe('Replaces resubscribeOnError with useEffectsErrorHandler in effect options', () => { |
| 28 | + describe('should replace resubscribeOnError configuration key with useEffectsErrorHandler', () => { |
| 29 | + it('in createEffect() effect creator', () => { |
| 30 | + const input = ` |
| 31 | + import { Injectable } from '@angular/core'; |
| 32 | + import { Actions, ofType, createEffect } from '@ngrx/effects'; |
| 33 | + import { tap } from 'rxjs/operators'; |
| 34 | + |
| 35 | + @Injectable() |
| 36 | + export class LogEffects { |
| 37 | + constructor(private actions$: Actions) {} |
| 38 | + |
| 39 | + logActions$ = createEffect(() => |
| 40 | + this.actions$.pipe( |
| 41 | + tap(action => console.log(action)) |
| 42 | + ), { resubscribeOnError: false }); |
| 43 | + } |
| 44 | + `; |
| 45 | + |
| 46 | + const expected = ` |
| 47 | + import { Injectable } from '@angular/core'; |
| 48 | + import { Actions, ofType, createEffect } from '@ngrx/effects'; |
| 49 | + import { tap } from 'rxjs/operators'; |
| 50 | + |
| 51 | + @Injectable() |
| 52 | + export class LogEffects { |
| 53 | + constructor(private actions$: Actions) {} |
| 54 | + |
| 55 | + logActions$ = createEffect(() => |
| 56 | + this.actions$.pipe( |
| 57 | + tap(action => console.log(action)) |
| 58 | + ), { useEffectsErrorHandler: false }); |
| 59 | + } |
| 60 | + `; |
| 61 | + |
| 62 | + test(input, expected); |
| 63 | + }); |
| 64 | + |
| 65 | + it('in @Effect() decorator', () => { |
| 66 | + const input = ` |
| 67 | + import { Injectable } from '@angular/core'; |
| 68 | + import { Actions, Effect, ofType } from '@ngrx/effects'; |
| 69 | + import { tap } from 'rxjs/operators'; |
| 70 | + |
| 71 | + @Injectable() |
| 72 | + export class LogEffects { |
| 73 | + constructor(private actions$: Actions) {} |
| 74 | + |
| 75 | + @Effect({ resubscribeOnError: false }) |
| 76 | + logActions$ = this.actions$.pipe( |
| 77 | + tap(action => console.log(action)) |
| 78 | + ) |
| 79 | + } |
| 80 | + `; |
| 81 | + |
| 82 | + const expected = ` |
| 83 | + import { Injectable } from '@angular/core'; |
| 84 | + import { Actions, Effect, ofType } from '@ngrx/effects'; |
| 85 | + import { tap } from 'rxjs/operators'; |
| 86 | + |
| 87 | + @Injectable() |
| 88 | + export class LogEffects { |
| 89 | + constructor(private actions$: Actions) {} |
| 90 | + |
| 91 | + @Effect({ useEffectsErrorHandler: false }) |
| 92 | + logActions$ = this.actions$.pipe( |
| 93 | + tap(action => console.log(action)) |
| 94 | + ) |
| 95 | + } |
| 96 | + `; |
| 97 | + |
| 98 | + test(input, expected); |
| 99 | + }); |
| 100 | + }); |
| 101 | + |
| 102 | + describe('should not replace non-ngrx identifiers', () => { |
| 103 | + it('in module scope', () => { |
| 104 | + const input = ` |
| 105 | +export const resubscribeOnError = null; |
| 106 | + `; |
| 107 | + |
| 108 | + test(input, input); |
| 109 | + }); |
| 110 | + |
| 111 | + it('within create effect callback', () => { |
| 112 | + const input = ` |
| 113 | +import { Injectable } from '@angular/core'; |
| 114 | +import { Actions, ofType, createEffect } from '@ngrx/effects'; |
| 115 | +import { tap } from 'rxjs/operators'; |
| 116 | + |
| 117 | +@Injectable() |
| 118 | +export class LogEffects { |
| 119 | + constructor(private actions$: Actions) {} |
| 120 | + |
| 121 | + logActions$ = createEffect(() => |
| 122 | + this.actions$.pipe( |
| 123 | + tap(resubscribeOnError => console.log(resubscribeOnError)) |
| 124 | + )); |
| 125 | +} |
| 126 | + `; |
| 127 | + |
| 128 | + test(input, input); |
| 129 | + }); |
| 130 | + }); |
| 131 | + |
| 132 | + function test(input: string, expected: string) { |
| 133 | + appTree.create('./app.module.ts', input); |
| 134 | + const runner = new SchematicTestRunner('schematics', collectionPath); |
| 135 | + |
| 136 | + const newTree = runner.runSchematic( |
| 137 | + `ngrx-${pkgName}-migration-02`, |
| 138 | + {}, |
| 139 | + appTree |
| 140 | + ); |
| 141 | + const file = newTree.readContent('app.module.ts'); |
| 142 | + |
| 143 | + expect(file).toBe(expected); |
| 144 | + } |
| 145 | + }); |
| 146 | +}); |
0 commit comments