From 4ef27fd041f7346c2f3306a71c75f4399b19b515 Mon Sep 17 00:00:00 2001 From: japie Date: Fri, 16 Apr 2021 09:02:18 -0700 Subject: [PATCH] what's new: data-source test (#4876) Add test for notification center `_data_source/` --- tensorboard/webapp/notification_center/BUILD | 1 + .../notification_center/_data_source/BUILD | 18 ++++ .../notification_center_data_source_test.ts | 88 +++++++++++++++++++ 3 files changed, 107 insertions(+) create mode 100644 tensorboard/webapp/notification_center/_data_source/notification_center_data_source_test.ts diff --git a/tensorboard/webapp/notification_center/BUILD b/tensorboard/webapp/notification_center/BUILD index 8fd51889fc2..d5eab0a0f5c 100644 --- a/tensorboard/webapp/notification_center/BUILD +++ b/tensorboard/webapp/notification_center/BUILD @@ -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", ], diff --git a/tensorboard/webapp/notification_center/_data_source/BUILD b/tensorboard/webapp/notification_center/_data_source/BUILD index 8437350b415..6d696bfc19e 100644 --- a/tensorboard/webapp/notification_center/_data_source/BUILD +++ b/tensorboard/webapp/notification_center/_data_source/BUILD @@ -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", + ], +) diff --git a/tensorboard/webapp/notification_center/_data_source/notification_center_data_source_test.ts b/tensorboard/webapp/notification_center/_data_source/notification_center_data_source_test.ts new file mode 100644 index 00000000000..d73b465f396 --- /dev/null +++ b/tensorboard/webapp/notification_center/_data_source/notification_center_data_source_test.ts @@ -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); + }); +});