|
| 1 | +import * as path from 'path'; |
| 2 | +import { |
| 3 | + SchematicTestRunner, |
| 4 | + UnitTestTree, |
| 5 | +} from '@angular-devkit/schematics/testing'; |
| 6 | +import { createWorkspace } from '@ngrx/schematics-core/testing'; |
| 7 | + |
| 8 | +describe('migrate tapResponse', () => { |
| 9 | + const collectionPath = path.join(__dirname, '../migration.json'); |
| 10 | + const schematicRunner = new SchematicTestRunner('schematics', collectionPath); |
| 11 | + let appTree: UnitTestTree; |
| 12 | + |
| 13 | + beforeEach(async () => { |
| 14 | + appTree = await createWorkspace(schematicRunner, appTree); |
| 15 | + }); |
| 16 | + |
| 17 | + const verifySchematic = async (input: string, output: string) => { |
| 18 | + appTree.create('main.ts', input); |
| 19 | + |
| 20 | + const tree = await schematicRunner.runSchematic( |
| 21 | + '20.0.0-rc_0-tap-response', |
| 22 | + {}, |
| 23 | + appTree |
| 24 | + ); |
| 25 | + |
| 26 | + const actual = tree.readContent('main.ts'); |
| 27 | + |
| 28 | + const normalize = (s: string) => s.replace(/\s+/g, '').replace(/;/g, ''); |
| 29 | + expect(normalize(actual)).toBe(normalize(output)); |
| 30 | + }; |
| 31 | + |
| 32 | + it('migrates basic tapResponse signature', async () => { |
| 33 | + const input = `import { tapResponse } from '@ngrx/operators'; |
| 34 | +tapResponse(() => {}, () => {}); |
| 35 | +`; |
| 36 | + |
| 37 | + const output = `import { tapResponse } from '@ngrx/operators'; |
| 38 | +tapResponse({ |
| 39 | + next: () => { }, |
| 40 | + error: () => { } |
| 41 | +}); |
| 42 | +`; |
| 43 | + |
| 44 | + await verifySchematic(input, output); |
| 45 | + }); |
| 46 | + |
| 47 | + it('migrates tapResponse with complete callback', async () => { |
| 48 | + const input = ` |
| 49 | + import { tapResponse } from '@ngrx/operators'; |
| 50 | + tapResponse( |
| 51 | + () => next, |
| 52 | + () => error, |
| 53 | + () => complete |
| 54 | + ); |
| 55 | + `; |
| 56 | + |
| 57 | + const output = ` |
| 58 | + import { tapResponse } from '@ngrx/operators'; |
| 59 | + tapResponse({ |
| 60 | + next: () => next, |
| 61 | + error: () => error, |
| 62 | + complete: () => complete |
| 63 | + }); |
| 64 | + `; |
| 65 | + |
| 66 | + await verifySchematic(input, output); |
| 67 | + }); |
| 68 | + |
| 69 | + it('migrates aliased tapResponse calls', async () => { |
| 70 | + const input = ` |
| 71 | + import { tapResponse } from '@ngrx/operators'; |
| 72 | + const myTapResponse = tapResponse; |
| 73 | + myTapResponse( |
| 74 | + () => next, |
| 75 | + () => error |
| 76 | + ); |
| 77 | + `; |
| 78 | + |
| 79 | + const output = ` |
| 80 | + import { tapResponse } from '@ngrx/operators'; |
| 81 | + const myTapResponse = tapResponse; |
| 82 | + myTapResponse({ |
| 83 | + next: () => next, |
| 84 | + error: () => error |
| 85 | + }); |
| 86 | + `; |
| 87 | + |
| 88 | + await verifySchematic(input, output); |
| 89 | + }); |
| 90 | + |
| 91 | + it('migrates namespaced tapResponse calls', async () => { |
| 92 | + const input = `import * as operators from '@ngrx/operators'; |
| 93 | +operators.tapResponse(() => next, () => error, () => complete); |
| 94 | +`; |
| 95 | + |
| 96 | + const output = `import * as operators from '@ngrx/operators'; |
| 97 | +operators.tapResponse({ |
| 98 | + next: () => next, |
| 99 | + error: () => error, |
| 100 | + complete: () => complete |
| 101 | +}); |
| 102 | +`; |
| 103 | + |
| 104 | + await verifySchematic(input, output); |
| 105 | + }); |
| 106 | + |
| 107 | + it('skips tapResponse if not imported from @ngrx/operators', async () => { |
| 108 | + const input = `import { tapResponse } from '@ngrx/component'; |
| 109 | +tapResponse(() => {}, () => {}); |
| 110 | +`; |
| 111 | + |
| 112 | + await verifySchematic(input, input); |
| 113 | + }); |
| 114 | + |
| 115 | + it('skips correct tapResponse signature', async () => { |
| 116 | + const input = `import { tapResponse } from '@ngrx/operators'; |
| 117 | +tapResponse({ |
| 118 | + next: () => { }, |
| 119 | + error: () => { } |
| 120 | +}); |
| 121 | +`; |
| 122 | + |
| 123 | + await verifySchematic(input, input); |
| 124 | + }); |
| 125 | + |
| 126 | + it('migrates tapResponse inside a full component-like body', async () => { |
| 127 | + const input = `import { tapResponse } from '@ngrx/operators'; |
| 128 | +function handle() { |
| 129 | + return tapResponse(() => next(), () => error(), () => complete()); |
| 130 | +} |
| 131 | +`; |
| 132 | + |
| 133 | + const output = `import { tapResponse } from '@ngrx/operators'; |
| 134 | +function handle() { |
| 135 | + return tapResponse({ |
| 136 | + next: () => next(), |
| 137 | + error: () => error(), |
| 138 | + complete: () => complete() |
| 139 | + }); |
| 140 | +} |
| 141 | +`; |
| 142 | + |
| 143 | + await verifySchematic(input, output); |
| 144 | + }); |
| 145 | + |
| 146 | + it('migrates tapResponse(onNext, onError) to object form', async () => { |
| 147 | + const input = ` |
| 148 | + import { tapResponse } from '@ngrx/operators'; |
| 149 | + const obs = tapResponse( |
| 150 | + (value) => console.log(value), |
| 151 | + (error) => console.error(error) |
| 152 | + ); |
| 153 | + `; |
| 154 | + |
| 155 | + const output = ` |
| 156 | + import { tapResponse } from '@ngrx/operators'; |
| 157 | + const obs = tapResponse({ |
| 158 | + next: (value) => console.log(value), |
| 159 | + error: (error) => console.error(error) |
| 160 | + }); |
| 161 | + `; |
| 162 | + await verifySchematic(input, output); |
| 163 | + }); |
| 164 | + |
| 165 | + it('skips migrating tapResponse imported from another module', async () => { |
| 166 | + const input = ` |
| 167 | + import { tapResponse } from 'some-other-lib'; |
| 168 | + const obs = tapResponse( |
| 169 | + (value) => console.log(value), |
| 170 | + (error) => console.error(error) |
| 171 | + ); |
| 172 | + `; |
| 173 | + |
| 174 | + await verifySchematic(input, input); |
| 175 | + }); |
| 176 | +}); |
0 commit comments