-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
what's new: data-source test (#4876)
Add test for notification center `_data_source/`
- Loading branch information
1 parent
7479b24
commit 4ef27fd
Showing
3 changed files
with
107 additions
and
0 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
88 changes: 88 additions & 0 deletions
88
tensorboard/webapp/notification_center/_data_source/notification_center_data_source_test.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,88 @@ | ||
/* Copyright 2021 The TensorFlow Authors. All Rights Reserved. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
==============================================================================*/ | ||
import {TestBed} from '@angular/core/testing'; | ||
|
||
import {HttpErrorResponse} from '../../webapp_data_source/tb_http_client'; | ||
import { | ||
HttpTestingController, | ||
TBHttpClientTestingModule, | ||
} from '../../webapp_data_source/tb_http_client_testing'; | ||
import {NotificationCenterDataSource} from './backend_types'; | ||
import {TBNotificationCenterDataSource} from './notification_center_data_source'; | ||
import {buildNotificationResponse} from './testing'; | ||
import {throwError} from 'rxjs'; | ||
|
||
describe('TBNotificationCenterDataSource test', () => { | ||
let httpMock: HttpTestingController; | ||
let dataSource: NotificationCenterDataSource; | ||
|
||
beforeEach(async () => { | ||
await TestBed.configureTestingModule({ | ||
imports: [TBHttpClientTestingModule], | ||
providers: [ | ||
{ | ||
provide: NotificationCenterDataSource, | ||
useClass: TBNotificationCenterDataSource, | ||
}, | ||
], | ||
}).compileComponents(); | ||
|
||
httpMock = TestBed.inject(HttpTestingController); | ||
dataSource = TestBed.inject(NotificationCenterDataSource); | ||
}); | ||
|
||
afterEach(() => { | ||
httpMock.verify(); | ||
}); | ||
|
||
it('fetches empty notifications', () => { | ||
const resultSpy = jasmine.createSpy(); | ||
dataSource.fetchNotifications().subscribe(resultSpy); | ||
const req = httpMock.expectOne('data/notifications'); | ||
req.flush({notifications: [{}]}); | ||
|
||
expect(resultSpy).toHaveBeenCalledWith({notifications: [{}]}); | ||
}); | ||
|
||
it('fetches non-empty notifications', () => { | ||
const resultSpy = jasmine.createSpy(); | ||
dataSource.fetchNotifications().subscribe(resultSpy); | ||
const req = httpMock.expectOne('data/notifications'); | ||
req.flush(buildNotificationResponse()); | ||
|
||
expect(resultSpy).toHaveBeenCalledWith(buildNotificationResponse()); | ||
}); | ||
|
||
it('throws error when notification fetch failed', () => { | ||
const error = new ErrorEvent('Request failed'); | ||
const errorResponse = { | ||
url: 'data/notifications', | ||
status: 123, | ||
statusText: 'something went wrong', | ||
}; | ||
const resultSpy = jasmine.createSpy(); | ||
const errorSpy = jasmine.createSpy(); | ||
|
||
dataSource.fetchNotifications().subscribe(resultSpy, errorSpy); | ||
httpMock.expectOne('data/notifications').error(error, errorResponse); | ||
|
||
const httpErrorResponse = new HttpErrorResponse({ | ||
error, | ||
...errorResponse, | ||
}); | ||
expect(resultSpy).not.toHaveBeenCalled(); | ||
expect(errorSpy).toHaveBeenCalledWith(httpErrorResponse); | ||
}); | ||
}); |