|
| 1 | +import * as path from 'path'; |
| 2 | +import { SchematicTestRunner, UnitTestTree } from '@angular-devkit/schematics/testing'; |
| 3 | +import { setupTestTree } from '../common/setup.spec'; |
| 4 | + |
| 5 | +describe('Migration 19.2.15 - Replace filteringOptions.filterable', () => { |
| 6 | + let appTree: UnitTestTree; |
| 7 | + const runner = new SchematicTestRunner( |
| 8 | + 'ig-migrate', |
| 9 | + path.join(__dirname, '../migration-collection.json') |
| 10 | + ); |
| 11 | + const migrationName = 'migration-46'; |
| 12 | + const makeTemplate = (name: string) => `/testSrc/appPrefix/component/${name}.component.html`; |
| 13 | + const makeScript = (name: string) => `/testSrc/appPrefix/component/${name}.component.ts`; |
| 14 | + const components = ['igx-simple-combo', 'igx-combo']; |
| 15 | + |
| 16 | + |
| 17 | + |
| 18 | + beforeEach(() => { |
| 19 | + appTree = setupTestTree(); |
| 20 | + }); |
| 21 | + |
| 22 | + it('should replace simple inline filteringOptions.filterable true with default behavior of the simple combo', async () => { |
| 23 | + components.forEach(async component =>{ |
| 24 | + const input = `<${component} [filteringOptions]="{ filterable: true }"></${component}>`; |
| 25 | + appTree.create(makeTemplate(`${component}-inline-true`), input); |
| 26 | + |
| 27 | + const tree = await runner.runSchematic(migrationName, {}, appTree); |
| 28 | + const output = tree.readContent(makeTemplate(`${component}-inline-true`)); |
| 29 | + |
| 30 | + expect(output).not.toContain('[disableFiltering]'); |
| 31 | + expect(output).not.toContain('filterable'); |
| 32 | + }); |
| 33 | + }); |
| 34 | + |
| 35 | + it('should handle mixed object literal correctly', async () => { |
| 36 | + components.forEach(async component =>{ |
| 37 | + const input = `<${component} [filteringOptions]="{ filterable: false, caseSensitive: true }"></${component}>`; |
| 38 | + appTree.create(makeTemplate(`${component}-inline2`), input); |
| 39 | + |
| 40 | + const tree = await runner.runSchematic(migrationName, {}, appTree); |
| 41 | + const output = tree.readContent(makeTemplate(`${component}-inline2`)); |
| 42 | + |
| 43 | + expect(output).toContain(`[disableFiltering]="true"`); |
| 44 | + expect(output).toContain(`[filteringOptions]="{ caseSensitive: true }"`); |
| 45 | + }); |
| 46 | + }); |
| 47 | + |
| 48 | + it('should warn on variable reference', async () => { |
| 49 | + components.forEach(async component =>{ |
| 50 | + const input = `<${component} [filteringOptions]="filterOpts""></${component}>`; |
| 51 | + const warnMsg = "Manual migration needed: please use 'disableFiltering' instead of filteringOptions.filterable." + |
| 52 | + "Since it has been deprecated.'"; |
| 53 | + |
| 54 | + appTree.create(makeTemplate(`${component}-referenceInTsFile`), input); |
| 55 | + |
| 56 | + const tree = await runner.runSchematic(migrationName, {}, appTree); |
| 57 | + const output = tree.readContent(makeTemplate(`${component}-referenceInTsFile`)); |
| 58 | + |
| 59 | + expect(output).toContain('[filteringOptions]'); |
| 60 | + expect(output).toContain(warnMsg); |
| 61 | + }); |
| 62 | + }); |
| 63 | + |
| 64 | + it('should skip adding new [disableFiltering] if already present on igx-combo', async () => { |
| 65 | + const input = `<igx-combo [disableFiltering]="true" [filteringOptions]="{ filterable: false }"></igx-combo>`; |
| 66 | + appTree.create(makeTemplate('combo-has-disableFiltering'), input); |
| 67 | + |
| 68 | + const tree = await runner.runSchematic(migrationName, {}, appTree); |
| 69 | + const output = tree.readContent(makeTemplate('combo-has-disableFiltering')); |
| 70 | + |
| 71 | + const occurrences = (output.match(/\[disableFiltering\]/g) || []).length; |
| 72 | + |
| 73 | + expect(occurrences).toBe(1); |
| 74 | + expect(output).not.toContain('filterable'); |
| 75 | + }); |
| 76 | + |
| 77 | + // TS file tests |
| 78 | + |
| 79 | + it('should remove line when filteringOptions.filterable is set to true', async () => { |
| 80 | + const input = `this.igxSimpleCombo.filteringOptions.filterable = true;`; |
| 81 | + appTree.create(makeScript('tsRemoveTrue'), input); |
| 82 | + |
| 83 | + const tree = await runner.runSchematic(migrationName, {}, appTree); |
| 84 | + const output = tree.readContent(makeScript('tsRemoveTrue')); |
| 85 | + |
| 86 | + expect(output).not.toContain('filteringOptions.filterable'); |
| 87 | + }); |
| 88 | + |
| 89 | + it('should replace filteringOptions.filterable = false with disableFiltering = true', async () => { |
| 90 | + const input = `this.igxSimpleCombo.filteringOptions.filterable = false;`; |
| 91 | + appTree.create(makeScript('tsReplaceFalse'), input); |
| 92 | + |
| 93 | + const tree = await runner.runSchematic(migrationName, {}, appTree); |
| 94 | + const output = tree.readContent(makeScript('tsReplaceFalse')); |
| 95 | + |
| 96 | + expect(output).toContain('this.igxSimpleCombo.disableFiltering = true;'); |
| 97 | + }); |
| 98 | + |
| 99 | + it('should handle the use of negative flag correctly', async () => { |
| 100 | + const input = `this.igxSimpleCombo.filteringOptions.filterable = !this.disableFilteringFlag;`; |
| 101 | + appTree.create(makeScript('tsNegativeFlag'), input); |
| 102 | + |
| 103 | + const tree = await runner.runSchematic(migrationName, {}, appTree); |
| 104 | + const output = tree.readContent(makeScript('tsNegativeFlag')); |
| 105 | + |
| 106 | + expect(output).toContain('this.igxSimpleCombo.disableFiltering = this.disableFilteringFlag;'); |
| 107 | + }); |
| 108 | + |
| 109 | + it('should handle the use of possitive flag correctly', async () => { |
| 110 | + const input = `this.igxSimpleCombo.filteringOptions.filterable = this.disableFilteringFlag;`; |
| 111 | + appTree.create(makeScript('tsNegativeFlag'), input); |
| 112 | + |
| 113 | + const tree = await runner.runSchematic(migrationName, {}, appTree); |
| 114 | + const output = tree.readContent(makeScript('tsNegativeFlag')); |
| 115 | + |
| 116 | + expect(output).toContain('this.igxSimpleCombo.disableFiltering = !this.disableFilteringFlag;'); |
| 117 | + }); |
| 118 | + |
| 119 | + it('should split filteringOptions object and move filterable out', async () => { |
| 120 | + const input = `this.igxSimpleCombo.filteringOptions = { filterable: false, caseSensitive: true };`; |
| 121 | + appTree.create(makeScript('tsSplitObj'), input); |
| 122 | + |
| 123 | + const tree = await runner.runSchematic(migrationName, {}, appTree); |
| 124 | + const output = tree.readContent(makeScript('tsSplitObj')); |
| 125 | + |
| 126 | + expect(output).toContain('this.igxSimpleCombo.disableFiltering = true;'); |
| 127 | + expect(output).toContain('this.igxSimpleCombo.filteringOptions = { caseSensitive: true };'); |
| 128 | + expect(output).not.toContain('filterable'); |
| 129 | + }); |
| 130 | + |
| 131 | + it('should not add disableFiltering again if already present when filterable is set to false', async () => { |
| 132 | + const input = ` |
| 133 | + this.igxCombo.filteringOptions.filterable = false; |
| 134 | + this.igxCombo.disableFiltering = true; |
| 135 | + `; |
| 136 | + appTree.create(makeScript('tsDirectFalseExistingDisable'), input); |
| 137 | + |
| 138 | + const tree = await runner.runSchematic(migrationName, {}, appTree); |
| 139 | + const output = tree.readContent(makeScript('tsDirectFalseExistingDisable')); |
| 140 | + |
| 141 | + const occurrences = (output.match(/\.disableFiltering/g) || []).length; |
| 142 | + |
| 143 | + expect(occurrences).toBe(1); |
| 144 | + expect(output).not.toContain('filterable'); |
| 145 | + }); |
| 146 | + |
| 147 | + it('should not add disableFiltering again if already present when using negative flag assignment', async () => { |
| 148 | + const input = ` |
| 149 | + this.igxSimpleCombo.filteringOptions.filterable = !this.flag; |
| 150 | + this.igxSimpleCombo.disableFiltering = this.flag; |
| 151 | + `; |
| 152 | + appTree.create(makeScript('tsNegativeFlagExistingDisable'), input); |
| 153 | + |
| 154 | + const tree = await runner.runSchematic(migrationName, {}, appTree); |
| 155 | + const output = tree.readContent(makeScript('tsNegativeFlagExistingDisable')); |
| 156 | + |
| 157 | + const occurrences = (output.match(/\.disableFiltering/g) || []).length; |
| 158 | + |
| 159 | + expect(occurrences).toBe(1); |
| 160 | + expect(output).not.toContain('filterable'); |
| 161 | + }); |
| 162 | + |
| 163 | + it('should not add disableFiltering again if already present when using positive flag assignment', async () => { |
| 164 | + const input = ` |
| 165 | + this.igxSimpleCombo.filteringOptions.filterable = this.flag; |
| 166 | + this.igxSimpleCombo.disableFiltering = !this.flag; |
| 167 | + `; |
| 168 | + appTree.create(makeScript('tsPositiveFlagExistingDisable'), input); |
| 169 | + |
| 170 | + const tree = await runner.runSchematic(migrationName, {}, appTree); |
| 171 | + const output = tree.readContent(makeScript('tsPositiveFlagExistingDisable')); |
| 172 | + |
| 173 | + const occurrences = (output.match(/\.disableFiltering/g) || []).length; |
| 174 | + |
| 175 | + expect(occurrences).toBe(1); |
| 176 | + expect(output).not.toContain('filterable'); |
| 177 | + }); |
| 178 | + |
| 179 | + it('should split filteringOptions object and remove filterable if disableFiltering is already present', async () => { |
| 180 | + const input = ` |
| 181 | + this.igxCombo.filteringOptions = { filterable: false, caseSensitive: true }; |
| 182 | + this.igxCombo.disableFiltering = true; |
| 183 | + `; |
| 184 | + appTree.create(makeScript('tsSplitObjAndDisabledFiltering'), input); |
| 185 | + |
| 186 | + const tree = await runner.runSchematic(migrationName, {}, appTree); |
| 187 | + const output = tree.readContent(makeScript('tsSplitObjAndDisabledFiltering')); |
| 188 | + |
| 189 | + const occurrences = (output.match(/\.disableFiltering/g) || []).length; |
| 190 | + |
| 191 | + expect(occurrences).toBe(1); |
| 192 | + expect(output).toContain('this.igxCombo.filteringOptions = { caseSensitive: true };'); |
| 193 | + expect(output).not.toContain('filterable'); |
| 194 | + }); |
| 195 | + |
| 196 | + it('should insert warning comment when filteringOptions is assigned from a variable', async () => { |
| 197 | + const input = `this.igxSimpleCombo.filteringOptions = filterOpts;`; |
| 198 | + const expectedComment = "// Manual migration needed: please use 'disableFiltering' instead of filteringOptions.filterable." + |
| 199 | + "Since it has been deprecated.'"; |
| 200 | + |
| 201 | + appTree.create(makeScript('tsVariableAssign'), input); |
| 202 | + |
| 203 | + const tree = await runner.runSchematic(migrationName, {}, appTree); |
| 204 | + const output = tree.readContent(makeScript('tsVariableAssign')); |
| 205 | + |
| 206 | + expect(output).toContain(input); |
| 207 | + expect(output).toContain(expectedComment); |
| 208 | + }); |
| 209 | +}); |
0 commit comments