|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.io/license |
| 7 | + */ |
| 8 | + |
| 9 | +import {absoluteFrom} from '@angular/compiler-cli'; |
| 10 | +import {initMockFileSystem} from '@angular/compiler-cli/private/testing'; |
| 11 | +import {runTsurgeMigration} from '../../utils/tsurge/testing'; |
| 12 | +import {XhrBackendMigration} from './migration'; |
| 13 | + |
| 14 | +describe('http fetch backend migration', () => { |
| 15 | + beforeEach(() => { |
| 16 | + initMockFileSystem('Native'); |
| 17 | + }); |
| 18 | + |
| 19 | + it('should update an empty provideHttpClient', async () => { |
| 20 | + const {fs} = await runTsurgeMigration(new XhrBackendMigration(), [ |
| 21 | + { |
| 22 | + name: absoluteFrom('/index.ts'), |
| 23 | + isProgramRootFile: true, |
| 24 | + contents: ` |
| 25 | + import {AppConfig} from '@angular/core'; |
| 26 | + import {provideHttpClient} from '@angular/common/http'; |
| 27 | + |
| 28 | + const config: AppConfig = [ |
| 29 | + provideHttpClient(), |
| 30 | + ] |
| 31 | + `, |
| 32 | + }, |
| 33 | + ]); |
| 34 | + |
| 35 | + const actual = fs.readFile(absoluteFrom('/index.ts')); |
| 36 | + expect(actual).toContain('provideHttpClient(withXhr())'); |
| 37 | + }); |
| 38 | + |
| 39 | + it('should update provideHttpClient without withFetch', async () => { |
| 40 | + const {fs} = await runTsurgeMigration(new XhrBackendMigration(), [ |
| 41 | + { |
| 42 | + name: absoluteFrom('/index.ts'), |
| 43 | + isProgramRootFile: true, |
| 44 | + contents: ` |
| 45 | + import {AppConfig} from '@angular/core'; |
| 46 | + import {provideHttpClient, withInterceptorsFromDi, withXsrfConfiguration} from '@angular/common/http'; |
| 47 | +
|
| 48 | + const config: AppConfig = [ |
| 49 | + provideHttpClient(withInterceptorsFromDi(), withXsrfConfiguration({})), |
| 50 | + ] |
| 51 | + `, |
| 52 | + }, |
| 53 | + ]); |
| 54 | + |
| 55 | + const actual = fs.readFile(absoluteFrom('/index.ts')); |
| 56 | + expect(actual).toContain( |
| 57 | + 'provideHttpClient(withXhr(), withInterceptorsFromDi(), withXsrfConfiguration({}))', |
| 58 | + ); |
| 59 | + expect(actual).toMatch(/import \{.*withXhr.*\}/); |
| 60 | + }); |
| 61 | + |
| 62 | + it('should update provideHttpClient to remove withFetch', async () => { |
| 63 | + const {fs} = await runTsurgeMigration(new XhrBackendMigration(), [ |
| 64 | + { |
| 65 | + name: absoluteFrom('/index.ts'), |
| 66 | + isProgramRootFile: true, |
| 67 | + contents: ` |
| 68 | + import {AppConfig} from '@angular/core'; |
| 69 | + import {provideHttpClient, withFetch, withInterceptorsFromDi, withXsrfConfiguration} from '@angular/common/http'; |
| 70 | +
|
| 71 | + const config: AppConfig = [ |
| 72 | + provideHttpClient(withFetch(), withInterceptorsFromDi(), withXsrfConfiguration({})), |
| 73 | + ] |
| 74 | + `, |
| 75 | + }, |
| 76 | + ]); |
| 77 | + |
| 78 | + const actual = fs.readFile(absoluteFrom('/index.ts')); |
| 79 | + expect(actual).toContain( |
| 80 | + 'provideHttpClient(withInterceptorsFromDi(), withXsrfConfiguration({}))', |
| 81 | + ); |
| 82 | + expect(actual).not.toContain('withFetch'); |
| 83 | + }); |
| 84 | + |
| 85 | + it('should update provideHttpClient to remove withFetch as only arg', async () => { |
| 86 | + const {fs} = await runTsurgeMigration(new XhrBackendMigration(), [ |
| 87 | + { |
| 88 | + name: absoluteFrom('/index.ts'), |
| 89 | + isProgramRootFile: true, |
| 90 | + contents: ` |
| 91 | + import {AppConfig} from '@angular/core'; |
| 92 | + import {provideHttpClient, withFetch, withInterceptorsFromDi, withXsrfConfiguration} from '@angular/common/http'; |
| 93 | +
|
| 94 | + const config: AppConfig = [ |
| 95 | + provideHttpClient(withFetch()), |
| 96 | + ] |
| 97 | + `, |
| 98 | + }, |
| 99 | + ]); |
| 100 | + |
| 101 | + const actual = fs.readFile(absoluteFrom('/index.ts')); |
| 102 | + expect(actual).toContain('provideHttpClient()'); |
| 103 | + expect(actual).not.toContain('withFetch'); |
| 104 | + }); |
| 105 | + |
| 106 | + it('should not update provideHttpClient if withXhr is already present', async () => { |
| 107 | + const {fs} = await runTsurgeMigration(new XhrBackendMigration(), [ |
| 108 | + { |
| 109 | + name: absoluteFrom('/index.ts'), |
| 110 | + isProgramRootFile: true, |
| 111 | + contents: ` |
| 112 | + import {AppConfig} from '@angular/core'; |
| 113 | + import {provideHttpClient, withXhr, withInterceptorsFromDi, withXsrfConfiguration} from '@angular/common/http'; |
| 114 | +
|
| 115 | + const config: AppConfig = [ |
| 116 | + provideHttpClient(withXhr(), withInterceptorsFromDi(), withXsrfConfiguration({})), |
| 117 | + ] |
| 118 | + `, |
| 119 | + }, |
| 120 | + ]); |
| 121 | + |
| 122 | + const actual = fs.readFile(absoluteFrom('/index.ts')); |
| 123 | + expect(actual).toContain( |
| 124 | + 'provideHttpClient(withXhr(), withInterceptorsFromDi(), withXsrfConfiguration({})),', |
| 125 | + ); |
| 126 | + expect(actual).not.toContain('withFetch'); |
| 127 | + }); |
| 128 | +}); |
0 commit comments