-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: migrate reset-settings-button-component to Angular (#2774)
* refactor: migrate reset-settings-button-component to Angular ref #2318
- Loading branch information
1 parent
afc2204
commit 7bf5b55
Showing
18 changed files
with
150 additions
and
183 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
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
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
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
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
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
35 changes: 35 additions & 0 deletions
35
visualization/app/codeCharta/ui/resetSettingsButton/getPartialDefaultState.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,35 @@ | ||
import { RecursivePartial, Settings } from "../../codeCharta.model" | ||
import { defaultState } from "../../state/store/state.actions" | ||
import { convertToVectors } from "../../util/settingsHelper" | ||
|
||
export const getPartialDefaultState = (settingKeys: string[]) => { | ||
const updatedSettings: RecursivePartial<Settings> = {} | ||
let settingsCounter = 0 | ||
|
||
for (const token of settingKeys) { | ||
const steps = token.split(".") | ||
let defaultSettingsPointer = defaultState | ||
let updatedSettingsPointer = updatedSettings | ||
|
||
for (const [index, step] of steps.entries()) { | ||
if (defaultSettingsPointer[step] !== undefined) { | ||
if (!updatedSettingsPointer[step]) { | ||
updatedSettingsPointer[step] = {} | ||
settingsCounter++ | ||
} | ||
if (index === steps.length - 1) { | ||
updatedSettingsPointer[step] = defaultSettingsPointer[step] | ||
} else { | ||
defaultSettingsPointer = defaultSettingsPointer[step] | ||
updatedSettingsPointer = updatedSettingsPointer[step] | ||
} | ||
} | ||
} | ||
} | ||
|
||
if (settingsCounter !== 0) { | ||
convertToVectors(updatedSettings) | ||
} | ||
|
||
return updatedSettings | ||
} |
6 changes: 3 additions & 3 deletions
6
visualization/app/codeCharta/ui/resetSettingsButton/resetSettingsButton.component.html
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
<md-button class="md-primary" ng-click="$ctrl.applyDefaultSettings()" ng-attr-title="{{ $ctrl.tooltip }}"> | ||
<button (click)="applyDefaultSettings()" [title]="tooltip"> | ||
<i class="fa fa-undo"></i> | ||
<label class="md-button-label" ng-if="$ctrl.text">{{ $ctrl.text }}</label> | ||
</md-button> | ||
<span *ngIf="label" class="reset-settings-button-label"> {{ label }}</span> | ||
</button> |
18 changes: 8 additions & 10 deletions
18
visualization/app/codeCharta/ui/resetSettingsButton/resetSettingsButton.component.scss
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 |
---|---|---|
@@ -1,13 +1,11 @@ | ||
reset-settings-button-component { | ||
display: inline-block; | ||
height: 48px; | ||
.md-button { | ||
border-radius: 3%; | ||
margin-left: 0; | ||
min-width: 36px; | ||
} | ||
cc-reset-settings-button { | ||
button { | ||
color: rgb(0, 150, 136); | ||
background: none; | ||
padding: 8px; | ||
|
||
.md-button-label { | ||
text-transform: none; | ||
&:hover { | ||
background-color: rgba(158, 158, 158, 0.2); | ||
} | ||
} | ||
} |
116 changes: 31 additions & 85 deletions
116
visualization/app/codeCharta/ui/resetSettingsButton/resetSettingsButton.component.spec.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 |
---|---|---|
@@ -1,99 +1,45 @@ | ||
import "./resetSettingsButton.module" | ||
|
||
import { TestBed } from "@angular/core/testing" | ||
import { render, screen } from "@testing-library/angular" | ||
import userEvent from "@testing-library/user-event" | ||
import { Vector3 } from "three" | ||
import { ResetSettingsButtonController } from "./resetSettingsButton.component" | ||
import { getService, instantiateModule } from "../../../../mocks/ng.mockhelper" | ||
import { StoreService } from "../../state/store.service" | ||
import { setScaling } from "../../state/store/appSettings/scaling/scaling.actions" | ||
import { defaultMapColors, setMapColors } from "../../state/store/appSettings/mapColors/mapColors.actions" | ||
import { setColorRange } from "../../state/store/dynamicSettings/colorRange/colorRange.actions" | ||
|
||
describe("resetSettingsButtonController", () => { | ||
let resetSettingsButtonController: ResetSettingsButtonController | ||
let storeService: StoreService | ||
import { Store } from "../../state/angular-redux/store" | ||
import { setState } from "../../state/store/state.actions" | ||
import { ResetSettingsButtonComponent } from "./resetSettingsButton.component" | ||
|
||
describe("resetSettingsButtonComponent", () => { | ||
const mockedStore = { dispatch: jest.fn() } | ||
beforeEach(() => { | ||
restartSystem() | ||
rebuildController() | ||
}) | ||
|
||
function restartSystem() { | ||
instantiateModule("app.codeCharta.ui.resetSettingsButton") | ||
|
||
storeService = getService<StoreService>("storeService") | ||
} | ||
|
||
function rebuildController() { | ||
resetSettingsButtonController = new ResetSettingsButtonController(storeService) | ||
} | ||
|
||
describe("applyDefaultSettings", () => { | ||
it("should update store with available default settings objects", () => { | ||
resetSettingsButtonController["settingsNames"] = | ||
"appSettings.hideFlatBuildings, appSettings.notInAppSettings, notInSettings.something" | ||
|
||
resetSettingsButtonController.applyDefaultSettings() | ||
|
||
expect(storeService.getState().appSettings.hideFlatBuildings).toBeFalsy() | ||
mockedStore.dispatch = jest.fn() | ||
TestBed.configureTestingModule({ | ||
providers: [{ provide: Store, useValue: mockedStore }] | ||
}) | ||
}) | ||
|
||
it("should only reset the color options when specified", () => { | ||
resetSettingsButtonController["settingsNames"] = | ||
"appSettings.mapColors.positive, appSettings.mapColors.negative, appSettings.mapColors.neutral, " + | ||
"appSettings.mapColors.selected" | ||
const mapColors = storeService.getState().appSettings.mapColors | ||
const colorRange = storeService.getState().dynamicSettings.colorRange | ||
|
||
storeService.dispatch(setMapColors({ ...mapColors, positive: "#176666" })) | ||
storeService.dispatch(setColorRange({ ...colorRange, from: 50 })) | ||
resetSettingsButtonController.applyDefaultSettings() | ||
|
||
expect(storeService.getState().appSettings.mapColors).toEqual(defaultMapColors) | ||
expect(storeService.getState().dynamicSettings.colorRange.from).toEqual(50) | ||
}) | ||
|
||
it("settingsNames should allow newline", () => { | ||
resetSettingsButtonController["settingsNames"] = "appSettings.mapColors.neutral,\nappSettings.hideFlatBuildings" | ||
|
||
resetSettingsButtonController.applyDefaultSettings() | ||
|
||
expect(storeService.getState().appSettings.hideFlatBuildings).toBeFalsy() | ||
}) | ||
|
||
it("should do nothing if settingsNames only contains comma characters", () => { | ||
storeService.dispatch = jest.fn() | ||
resetSettingsButtonController["settingsNames"] = ",," | ||
|
||
resetSettingsButtonController.applyDefaultSettings() | ||
|
||
expect(storeService.dispatch).not.toHaveBeenCalled() | ||
}) | ||
|
||
it("should do nothing if settingsNames only contains space characters", () => { | ||
storeService.dispatch = jest.fn() | ||
resetSettingsButtonController["settingsNames"] = " " | ||
|
||
resetSettingsButtonController.applyDefaultSettings() | ||
|
||
expect(storeService.dispatch).not.toHaveBeenCalled() | ||
it("should reset given appSetting", async () => { | ||
await render(ResetSettingsButtonComponent, { | ||
componentProperties: { | ||
settingsKeys: ["appSettings.mapColors.selected", "appSettings.scaling.x, appSettings.scaling.y, appSettings.scaling.z"] | ||
} | ||
}) | ||
|
||
it("should do nothing if settingsName not in defaultSettings", () => { | ||
storeService.dispatch = jest.fn() | ||
resetSettingsButtonController["settingsNames"] = "deltas.something.bla" | ||
userEvent.click(screen.getByRole("button")) | ||
|
||
resetSettingsButtonController.applyDefaultSettings() | ||
expect(mockedStore.dispatch).toHaveBeenCalledWith( | ||
setState({ appSettings: { mapColors: { selected: "#EB8319" }, scaling: new Vector3(1, 1, 1) } }) | ||
) | ||
}) | ||
|
||
expect(storeService.dispatch).not.toHaveBeenCalled() | ||
it("should execute callback", async () => { | ||
const callback = jest.fn() | ||
await render(ResetSettingsButtonComponent, { | ||
componentProperties: { | ||
settingsKeys: ["appSettings.mapColors.selected"], | ||
callback | ||
} | ||
}) | ||
|
||
it("should update nested settings in service", () => { | ||
storeService.dispatch(setScaling(new Vector3(2, 2, 2))) | ||
resetSettingsButtonController["settingsNames"] = "appSettings.scaling.x, appSettings.scaling.y, appSettings.scaling.z" | ||
|
||
resetSettingsButtonController.applyDefaultSettings() | ||
userEvent.click(screen.getByRole("button")) | ||
|
||
expect(storeService.getState().appSettings.scaling).toEqual(new Vector3(1, 1, 1)) | ||
}) | ||
expect(callback).toHaveBeenCalledTimes(1) | ||
}) | ||
}) |
Oops, something went wrong.