Skip to content

Commit 159cc01

Browse files
committed
Add tests for cases where subscription is made after http request
1 parent 80038be commit 159cc01

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

test/components/spinner/spinner.component.spec.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,4 +169,18 @@ describe('SpinnerComponent', () => {
169169
expect(() => fixture.detectChanges())
170170
.toThrow(new Error('`filteredUrlPatterns` must be an array.'));
171171
});
172+
173+
it('should show the spinner even if the component is created after the http request is performed', inject(
174+
[PendingInterceptorService, HttpClient, HttpTestingController],
175+
(service: PendingInterceptorService, http: HttpClient, httpMock: HttpTestingController) => {
176+
http.get('/fake').subscribe();
177+
178+
const newFixture = TestBed.createComponent(SpinnerComponent);
179+
const newComponent = newFixture.componentInstance;
180+
expect(newComponent.isSpinnerVisible).toBeTruthy();
181+
httpMock.expectOne('/fake').flush({});
182+
expect(newComponent.isSpinnerVisible).toBeFalsy();
183+
httpMock.verify();
184+
}
185+
));
172186
});

test/services/pending-interceptor.service.spec.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,22 @@ describe('PendingInterceptorService', () => {
7676
})
7777
));
7878

79+
it('should correctly notify the pendingRequestsStatus observable, even if subscribed after', async(
80+
inject(
81+
[PendingInterceptorService, HttpClient, HttpTestingController],
82+
(service: PendingInterceptorService, http: HttpClient, httpMock: HttpTestingController) => {
83+
http.get('/fake').subscribe();
84+
httpMock.expectOne('/fake');
85+
86+
const pendingRequestsStatus = service.pendingRequestsStatus;
87+
pendingRequestsStatus
88+
.subscribe(
89+
(next: boolean) => expect(next).toBeTruthy(),
90+
(error: HttpErrorResponse) => expect(1).toBe(2)
91+
);
92+
})
93+
));
94+
7995
it('should fail correctly',
8096
inject(
8197
[PendingInterceptorService, HttpClient, HttpTestingController],

0 commit comments

Comments
 (0)