Skip to content

Commit

Permalink
what's new: data-source test (#4876)
Browse files Browse the repository at this point in the history
Add test for notification center `_data_source/`
  • Loading branch information
japie1235813 committed Apr 16, 2021
1 parent 7479b24 commit 4ef27fd
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 0 deletions.
1 change: 1 addition & 0 deletions tensorboard/webapp/notification_center/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ tf_ng_web_test_suite(
name = "notification_center_test",
testonly = True,
deps = [
"//tensorboard/webapp/notification_center/_data_source:_data_source_test",
"//tensorboard/webapp/notification_center/_redux:_redux_test",
"//tensorboard/webapp/notification_center/_views:_views_test",
],
Expand Down
18 changes: 18 additions & 0 deletions tensorboard/webapp/notification_center/_data_source/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,21 @@ tf_ts_library(
"@npm//rxjs",
],
)

tf_ts_library(
name = "_data_source_test",
testonly = True,
srcs = [
"notification_center_data_source_test.ts",
],
deps = [
":_data_source",
":testing",
":types",
"//tensorboard/webapp/angular:expect_angular_core_testing",
"//tensorboard/webapp/webapp_data_source:http_client",
"//tensorboard/webapp/webapp_data_source:http_client_testing",
"@npm//@types/jasmine",
"@npm//rxjs",
],
)
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);
});
});

0 comments on commit 4ef27fd

Please sign in to comment.