Skip to content

Commit

Permalink
ng: functional settings dialog
Browse files Browse the repository at this point in the history
Note that this does not have the user experience we ultimately want.
This change only contains functional improvements.
  • Loading branch information
stephanwlee committed Oct 18, 2019
1 parent f675959 commit 4d0ae45
Show file tree
Hide file tree
Showing 13 changed files with 448 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
"@angular/cdk": "^8.1.3",
"@angular/common": "^8.1.2",
"@angular/core": "^8.1.2",
"@angular/forms": "^8.2.2",
"@angular/forms": "^8.2.11",
"@angular/http": "^7.2.15",
"@angular/material": "^8.1.3",
"@angular/platform-browser": "^8.1.3",
Expand Down
1 change: 1 addition & 0 deletions tensorboard/components/tf_ng_tensorboard/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -106,5 +106,6 @@ tf_ng_web_test_suite(
"//tensorboard/components/tf_ng_tensorboard/header:test_lib",
"//tensorboard/components/tf_ng_tensorboard/plugins:plugins_component_test_lib",
"//tensorboard/components/tf_ng_tensorboard/reloader:test_lib",
"//tensorboard/components/tf_ng_tensorboard/settings:test_lib",
],
)
2 changes: 2 additions & 0 deletions tensorboard/components/tf_ng_tensorboard/header/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ ng_module(
"//tensorboard/components/tf_ng_tensorboard/angular:expect_angular_material_tabs",
"//tensorboard/components/tf_ng_tensorboard/angular:expect_angular_material_toolbar",
"//tensorboard/components/tf_ng_tensorboard/core",
"//tensorboard/components/tf_ng_tensorboard/settings",
"//tensorboard/components/tf_ng_tensorboard/types",
"@npm//@angular/common",
"@npm//@angular/core",
Expand All @@ -45,6 +46,7 @@ tf_ts_library(
"//tensorboard/components/tf_ng_tensorboard/angular:expect_angular_platform_browser_animations",
"//tensorboard/components/tf_ng_tensorboard/core",
"//tensorboard/components/tf_ng_tensorboard/core/testing",
"//tensorboard/components/tf_ng_tensorboard/settings",
"//tensorboard/components/tf_ng_tensorboard/types",
"@npm//@angular/common",
"@npm//@angular/compiler",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,6 @@
</mat-form-field>
</span>
<span class="settings">
settings go here
<settings-button></settings-button>
</span>
</mat-toolbar>
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {MatSelectModule} from '@angular/material/select';
import {HeaderComponent} from './components/header.component';
import {HeaderContainer} from './containers/header.component';
import {CoreModule} from '../core/core.module';
import {SettingsModule} from '../settings/settings.module';

@NgModule({
declarations: [HeaderComponent, HeaderContainer],
Expand All @@ -34,6 +35,7 @@ import {CoreModule} from '../core/core.module';
MatSelectModule,
CommonModule,
CoreModule,
SettingsModule,
],
})
export class HeaderModule {}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ 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 {DebugElement} from '@angular/core';
import {DebugElement, NO_ERRORS_SCHEMA} from '@angular/core';
import {TestBed} from '@angular/core/testing';
import {MatTabsModule} from '@angular/material/tabs';
import {MatToolbarModule} from '@angular/material/toolbar';
Expand Down Expand Up @@ -62,6 +62,7 @@ describe('header test', () => {
HeaderContainer,
],
declarations: [HeaderComponent, HeaderContainer],
schemas: [NO_ERRORS_SCHEMA],
}).compileComponents();
store = TestBed.get(Store);
});
Expand Down
48 changes: 48 additions & 0 deletions tensorboard/components/tf_ng_tensorboard/settings/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package(default_visibility = ["//tensorboard:internal"])

load("@npm_angular_bazel//:index.bzl", "ng_module")
load("//tensorboard/defs:defs.bzl", "tf_ts_library")

ng_module(
name = "settings",
srcs = [
"dialog.component.ts",
"settings.component.ts",
"settings.module.ts",
],
assets = [
"dialog.component.css",
],
deps = [
"//tensorboard/components/tf_ng_tensorboard/core",
"@npm//@angular/common",
"@npm//@angular/core",
"@npm//@angular/forms",
"@npm//@angular/material",
"@npm//@ngrx/store",
"@npm//rxjs",
],
)

tf_ts_library(
name = "test_lib",
testonly = True,
srcs = [
"dialog.component.test.ts",
],
tsconfig = "//:tsconfig-test",
deps = [
":settings",
"//tensorboard/components/tf_ng_tensorboard/angular:expect_angular_core_testing",
"//tensorboard/components/tf_ng_tensorboard/core",
"//tensorboard/components/tf_ng_tensorboard/core/testing",
"@npm//@angular/common",
"@npm//@angular/compiler",
"@npm//@angular/core",
"@npm//@angular/forms",
"@npm//@angular/material",
"@npm//@angular/platform-browser",
"@npm//@ngrx/store",
"@npm//@types/jasmine",
],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/* Copyright 2019 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.
==============================================================================*/
.label {
margin: 10px 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
/* Copyright 2019 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 {DebugElement} from '@angular/core';
import {TestBed, tick, fakeAsync} from '@angular/core/testing';
import {FormsModule, ReactiveFormsModule} from '@angular/forms';
import {MatButtonModule} from '@angular/material/button';
import {MatCheckboxModule} from '@angular/material/checkbox';
import {MatInputModule} from '@angular/material/input';
import {By} from '@angular/platform-browser';
import {NoopAnimationsModule} from '@angular/platform-browser/animations';
import {Store} from '@ngrx/store';
import {provideMockStore, MockStore} from '@ngrx/store/testing';

import {SettingsDialogComponent} from './dialog.component';

import {
State,
getReloadEnabled,
getReloadPeriodInMs,
} from '../core/core.reducers';
import {toggleReloadEnabled, changeReloadPeriod} from '../core/core.actions';
import {createCoreState, createState} from '../core/testing';

/** @typehack */ import * as _typeHackStore from '@ngrx/store';

describe('settings dialog test', () => {
let store: MockStore<State>;
let dispatchSpy: jasmine.Spy;

beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [
FormsModule,
MatButtonModule,
MatCheckboxModule,
MatInputModule,
NoopAnimationsModule,
ReactiveFormsModule,
],
providers: [
provideMockStore({
initialState: createState(
createCoreState({
reloadPeriodInMs: 30000,
reloadEnabled: true,
})
),
}),
SettingsDialogComponent,
],
declarations: [SettingsDialogComponent],
}).compileComponents();
store = TestBed.get(Store);
dispatchSpy = spyOn(store, 'dispatch');
});

it('renders settings', () => {
const fixture = TestBed.createComponent(SettingsDialogComponent);
fixture.detectChanges();

const checkbox = fixture.debugElement.query(By.css('mat-checkbox'));
expect(checkbox.classes['mat-checkbox-checked']).toBeTruthy();

const reloadPeriod = fixture.debugElement.query(By.css('#reload-period'));
expect(reloadPeriod.nativeElement.value).toBe('30');
});

it('updates the UI according to store changes.', () => {
const fixture = TestBed.createComponent(SettingsDialogComponent);
fixture.detectChanges();

// sanity to make sure it really did change.
const checkbox = fixture.debugElement.query(By.css('mat-checkbox'));
expect(checkbox.classes['mat-checkbox-checked']).toBeTruthy();

store.setState(
createState(
createCoreState({
reloadPeriodInMs: 60000,
reloadEnabled: false,
})
)
);
fixture.detectChanges();

expect(checkbox.classes['mat-checkbox-checked']).toBeFalsy();

const reloadPeriod = fixture.debugElement.query(By.css('#reload-period'));
expect(reloadPeriod.nativeElement.value).toBe('60');
});

describe('toggleReloadEnabled', () => {
it('dispatches when clicking on checkbox', fakeAsync(() => {
const fixture = TestBed.createComponent(SettingsDialogComponent);
fixture.detectChanges();

const checkbox = fixture.debugElement.query(By.css('mat-checkbox input'));
checkbox.nativeElement.click();

expect(dispatchSpy).toHaveBeenCalledWith(toggleReloadEnabled());
}));
});

describe('changeReloadPeriod', () => {
it('dispatches changing the value', fakeAsync(async () => {
const fixture = TestBed.createComponent(SettingsDialogComponent);
fixture.detectChanges();

const reloadPeriod = fixture.debugElement.query(By.css('#reload-period'));
reloadPeriod.nativeElement.value = 20;
reloadPeriod.nativeElement.dispatchEvent(new Event('input'));

expect(dispatchSpy).not.toHaveBeenCalled();

// We debounce it so it does not spam other components on very keystroke.
tick(500);

expect(dispatchSpy).toHaveBeenCalledWith(
changeReloadPeriod({periodInMs: 20000})
);
}));

it('does not dispatch when input is invalid', fakeAsync(async () => {
const fixture = TestBed.createComponent(SettingsDialogComponent);
fixture.detectChanges();

const reloadPeriod = fixture.debugElement.query(By.css('#reload-period'));
reloadPeriod.nativeElement.value = 5;
reloadPeriod.nativeElement.dispatchEvent(new Event('input'));

tick(1e10);

expect(dispatchSpy).not.toHaveBeenCalled();
}));

it('does not set state when reload is disabled', fakeAsync(() => {
store.setState(
createState(
createCoreState({
reloadPeriodInMs: 30000,
reloadEnabled: false,
})
)
);
const fixture = TestBed.createComponent(SettingsDialogComponent);
fixture.detectChanges();

const reloadPeriod = fixture.debugElement.query(By.css('#reload-period'));
reloadPeriod.nativeElement.value = 30;
reloadPeriod.nativeElement.dispatchEvent(new Event('input'));

tick(1e10);

expect(dispatchSpy).not.toHaveBeenCalled();
}));
});
});
Loading

0 comments on commit 4d0ae45

Please sign in to comment.