|
| 1 | +import { |
| 2 | + SchematicTestRunner, |
| 3 | + UnitTestTree, |
| 4 | +} from '@angular-devkit/schematics/testing'; |
| 5 | +import { createWorkspace } from '@ngrx/schematics-core/testing'; |
| 6 | +import * as path from 'path'; |
| 7 | +import { tags } from '@angular-devkit/core'; |
| 8 | + |
| 9 | +describe('Store Migration to 21.0.0', () => { |
| 10 | + const collectionPath = path.join( |
| 11 | + process.cwd(), |
| 12 | + 'dist/modules/store/migrations/migration.json' |
| 13 | + ); |
| 14 | + const schematicRunner = new SchematicTestRunner('schematics', collectionPath); |
| 15 | + |
| 16 | + let appTree: UnitTestTree; |
| 17 | + |
| 18 | + beforeEach(async () => { |
| 19 | + appTree = await createWorkspace(schematicRunner, appTree); |
| 20 | + }); |
| 21 | + |
| 22 | + describe('removing SelectorWithProps and MemoizedSelectorWithProps', () => { |
| 23 | + it('should remove SelectorWithProps from import', async () => { |
| 24 | + const input = tags.stripIndent` |
| 25 | +import { createSelector, SelectorWithProps } from '@ngrx/store'; |
| 26 | +
|
| 27 | +const selector = createSelector( |
| 28 | + (state: AppState) => state.items, |
| 29 | + (items) => items.length |
| 30 | +); |
| 31 | + `; |
| 32 | + const output = tags.stripIndent` |
| 33 | +import { createSelector } from '@ngrx/store'; |
| 34 | +
|
| 35 | +const selector = createSelector( |
| 36 | + (state: AppState) => state.items, |
| 37 | + (items) => items.length |
| 38 | +); |
| 39 | + `; |
| 40 | + |
| 41 | + appTree.create('main.ts', input); |
| 42 | + const tree = await schematicRunner.runSchematic( |
| 43 | + 'ngrx-store-migration-21', |
| 44 | + {}, |
| 45 | + appTree |
| 46 | + ); |
| 47 | + const actual = tree.readContent('main.ts'); |
| 48 | + expect(actual).toBe(output); |
| 49 | + }); |
| 50 | + |
| 51 | + it('should remove MemoizedSelectorWithProps from import', async () => { |
| 52 | + const input = tags.stripIndent` |
| 53 | +import { Store, MemoizedSelectorWithProps } from '@ngrx/store'; |
| 54 | +
|
| 55 | +class MyComponent { |
| 56 | + constructor(private store: Store) {} |
| 57 | +} |
| 58 | + `; |
| 59 | + const output = tags.stripIndent` |
| 60 | +import { Store } from '@ngrx/store'; |
| 61 | +
|
| 62 | +class MyComponent { |
| 63 | + constructor(private store: Store) {} |
| 64 | +} |
| 65 | + `; |
| 66 | + |
| 67 | + appTree.create('main.ts', input); |
| 68 | + const tree = await schematicRunner.runSchematic( |
| 69 | + 'ngrx-store-migration-21', |
| 70 | + {}, |
| 71 | + appTree |
| 72 | + ); |
| 73 | + const actual = tree.readContent('main.ts'); |
| 74 | + expect(actual).toBe(output); |
| 75 | + }); |
| 76 | + |
| 77 | + it('should remove both SelectorWithProps and MemoizedSelectorWithProps from import', async () => { |
| 78 | + const input = tags.stripIndent` |
| 79 | +import { createSelector, SelectorWithProps, MemoizedSelectorWithProps } from '@ngrx/store'; |
| 80 | +
|
| 81 | +const selector = createSelector( |
| 82 | + (state: AppState) => state.items, |
| 83 | + (items) => items.length |
| 84 | +); |
| 85 | + `; |
| 86 | + const output = tags.stripIndent` |
| 87 | +import { createSelector } from '@ngrx/store'; |
| 88 | +
|
| 89 | +const selector = createSelector( |
| 90 | + (state: AppState) => state.items, |
| 91 | + (items) => items.length |
| 92 | +); |
| 93 | + `; |
| 94 | + |
| 95 | + appTree.create('main.ts', input); |
| 96 | + const tree = await schematicRunner.runSchematic( |
| 97 | + 'ngrx-store-migration-21', |
| 98 | + {}, |
| 99 | + appTree |
| 100 | + ); |
| 101 | + const actual = tree.readContent('main.ts'); |
| 102 | + expect(actual).toBe(output); |
| 103 | + }); |
| 104 | + |
| 105 | + it('should remove entire import if only removed types are imported', async () => { |
| 106 | + const input = tags.stripIndent` |
| 107 | +import { SelectorWithProps } from '@ngrx/store'; |
| 108 | +import { Component } from '@angular/core'; |
| 109 | + `; |
| 110 | + const output = tags.stripIndent` |
| 111 | +import { Component } from '@angular/core'; |
| 112 | + `; |
| 113 | + |
| 114 | + appTree.create('main.ts', input); |
| 115 | + const tree = await schematicRunner.runSchematic( |
| 116 | + 'ngrx-store-migration-21', |
| 117 | + {}, |
| 118 | + appTree |
| 119 | + ); |
| 120 | + const actual = tree.readContent('main.ts'); |
| 121 | + expect(actual).toBe(output); |
| 122 | + }); |
| 123 | + |
| 124 | + it('should not modify files without removed types', async () => { |
| 125 | + const input = tags.stripIndent` |
| 126 | +import { createSelector, Store } from '@ngrx/store'; |
| 127 | +
|
| 128 | +const selector = createSelector( |
| 129 | + (state: AppState) => state.items, |
| 130 | + (items) => items.length |
| 131 | +); |
| 132 | + `; |
| 133 | + |
| 134 | + appTree.create('main.ts', input); |
| 135 | + const tree = await schematicRunner.runSchematic( |
| 136 | + 'ngrx-store-migration-21', |
| 137 | + {}, |
| 138 | + appTree |
| 139 | + ); |
| 140 | + const actual = tree.readContent('main.ts'); |
| 141 | + expect(actual).toBe(input); |
| 142 | + }); |
| 143 | + |
| 144 | + it('should handle double-quote imports', async () => { |
| 145 | + const input = tags.stripIndent` |
| 146 | +import { createSelector, SelectorWithProps } from "@ngrx/store"; |
| 147 | +
|
| 148 | +const selector = createSelector( |
| 149 | + (state: AppState) => state.items, |
| 150 | + (items) => items.length |
| 151 | +); |
| 152 | + `; |
| 153 | + const output = tags.stripIndent` |
| 154 | +import { createSelector } from "@ngrx/store"; |
| 155 | +
|
| 156 | +const selector = createSelector( |
| 157 | + (state: AppState) => state.items, |
| 158 | + (items) => items.length |
| 159 | +); |
| 160 | + `; |
| 161 | + |
| 162 | + appTree.create('main.ts', input); |
| 163 | + const tree = await schematicRunner.runSchematic( |
| 164 | + 'ngrx-store-migration-21', |
| 165 | + {}, |
| 166 | + appTree |
| 167 | + ); |
| 168 | + const actual = tree.readContent('main.ts'); |
| 169 | + expect(actual).toBe(output); |
| 170 | + }); |
| 171 | + }); |
| 172 | +}); |
0 commit comments