-
Notifications
You must be signed in to change notification settings - Fork 600
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix overriding HttpClient in DxHttpModule (T1248609) (#27932)
- Loading branch information
1 parent
45256e0
commit 2b10a4c
Showing
4 changed files
with
125 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,23 @@ | ||
import { NgModule } from '@angular/core'; | ||
import { NgModule, Injector, createNgModuleRef } from '@angular/core'; | ||
import { HttpClient, HttpClientModule } from '@angular/common/http'; | ||
import devextremeAjax from 'devextreme/core/utils/ajax'; | ||
// eslint-disable-next-line import/named | ||
import { sendRequestFactory } from './ajax'; | ||
|
||
@NgModule({ | ||
exports: [], | ||
imports: [HttpClientModule], | ||
imports: [], | ||
providers: [], | ||
}) | ||
export class DxHttpModule { | ||
constructor(httpClient: HttpClient) { | ||
constructor(injector: Injector) { | ||
let httpClient: HttpClient = injector.get(HttpClient, null); | ||
|
||
if (!httpClient) { | ||
const moduleRef = createNgModuleRef(HttpClientModule, injector); | ||
|
||
httpClient = moduleRef.injector.get(HttpClient); | ||
} | ||
|
||
devextremeAjax.inject({ sendRequest: sendRequestFactory(httpClient) }); | ||
} | ||
} |
106 changes: 106 additions & 0 deletions
106
packages/devextreme-angular/tests/src/http/ajax-bootstrap-interceptors.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
import { TestBed } from '@angular/core/testing'; | ||
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing'; | ||
import { | ||
provideHttpClient, | ||
withInterceptors, | ||
HttpInterceptorFn, | ||
HttpClient, | ||
} from '@angular/common/http'; | ||
import { ApplicationRef, Component } from '@angular/core'; | ||
import { bootstrapApplication } from '@angular/platform-browser'; | ||
import { DxHttpModule } from 'devextreme-angular/http'; | ||
import DataSource from 'devextreme/data/data_source'; | ||
import ODataStore from 'devextreme/data/odata/store'; | ||
import { throwError } from 'rxjs'; | ||
|
||
const TEST_URL = ''; | ||
const interceptors: Record<string, () => void> = {}; | ||
|
||
interceptors.interceptorFn = () => {}; | ||
|
||
const testInterceptorFn: HttpInterceptorFn = () => { | ||
interceptors.interceptorFn(); | ||
return throwError(() => ({ | ||
status: 403, | ||
statusText: 'Request intercepted. Access Denied', | ||
})); | ||
}; | ||
|
||
@Component({ | ||
standalone: true, | ||
selector: 'test-app', | ||
imports: [DxHttpModule], | ||
template: '---', | ||
}) | ||
class TestAppComponent { | ||
constructor(private readonly httpClient: HttpClient) {} | ||
|
||
fetchData() { | ||
return this.httpClient.get(TEST_URL).toPromise(); | ||
} | ||
|
||
loadDataSource() { | ||
const dataSource = new DataSource({ | ||
store: new ODataStore({ | ||
version: 2, | ||
url: TEST_URL, | ||
}), | ||
}); | ||
|
||
return dataSource.load() as Promise<unknown>; | ||
} | ||
} | ||
|
||
describe('Using DxHttpModule in application with interceptors provided in bootstrapApplication() ', () => { | ||
let httpTestingControllerMock: HttpTestingController; | ||
let component: TestAppComponent; | ||
|
||
beforeEach(async () => { | ||
const testApp = document.createElement('test-app'); | ||
|
||
document.body.appendChild(testApp); | ||
|
||
TestBed.configureTestingModule({ | ||
imports: [HttpClientTestingModule], | ||
}); | ||
|
||
const appRef = await bootstrapApplication(TestAppComponent, { | ||
providers: [ | ||
provideHttpClient(withInterceptors([testInterceptorFn])), | ||
{ provide: HttpClientTestingModule }, | ||
], | ||
}); | ||
|
||
const applicationRef = appRef.injector.get(ApplicationRef); | ||
|
||
component = applicationRef.components[0].instance as TestAppComponent; | ||
|
||
httpTestingControllerMock = TestBed.inject(HttpTestingController); | ||
}); | ||
|
||
afterEach(() => { | ||
httpTestingControllerMock?.verify(); | ||
}); | ||
|
||
it('should call interceptors while calling httpClient directly ', (done) => { | ||
const interceptorFnSpy = spyOn(interceptors, 'interceptorFn'); | ||
|
||
component | ||
.fetchData() | ||
.catch(() => { | ||
expect(interceptorFnSpy).toHaveBeenCalledTimes(1); | ||
done(); | ||
}).finally(() => {}); | ||
}); | ||
|
||
it('dataSource load() should be intercepted', (done) => { | ||
const interceptorFnSpy = spyOn(interceptors, 'interceptorFn'); | ||
|
||
// eslint-disable-next-line no-void | ||
void component.loadDataSource() | ||
.catch(() => { | ||
expect(interceptorFnSpy).toHaveBeenCalledTimes(1); | ||
done(); | ||
}); | ||
}); | ||
}); |