-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1774 from qdraw/feature/202410_update_btn_code_sm…
…ell_17 refactor update btn
- Loading branch information
Showing
9 changed files
with
278 additions
and
117 deletions.
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
...arsky/clientapp/src/components/organisms/modal-geo/internal/update-button-active.spec.tsx
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,63 @@ | ||
import { fireEvent, render, waitFor } from "@testing-library/react"; | ||
import localization from "../../../../localization/localization.json"; | ||
import { Language, SupportedLanguages } from "../../../../shared/language"; | ||
import { ILatLong } from "../modal-geo"; | ||
import { UpdateButtonActive } from "./update-button-active"; | ||
import { UpdateButtonWrapper } from "./update-button-wrapper"; | ||
import { UpdateGeoLocation } from "./update-geo-location"; | ||
|
||
jest.mock("./update-geo-location"); | ||
|
||
describe("UpdateButtonActive", () => { | ||
const parentDirectory = "parentDirectory"; | ||
const selectedSubPath = "selectedSubPath"; | ||
const location: ILatLong = { latitude: 0, longitude: 0 }; | ||
const setError = jest.fn(); | ||
const setIsLoading = jest.fn(); | ||
const propsCollections = true; | ||
const handleExit = jest.fn(); | ||
const language = new Language(SupportedLanguages.en); | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it("[UpdateButtonActive] calls UpdateGeoLocation when clicked and updates location", async () => { | ||
(UpdateGeoLocation as jest.Mock).mockResolvedValueOnce({} as any); | ||
|
||
const { getByTestId } = render( | ||
<UpdateButtonActive | ||
handleExit={handleExit} | ||
parentDirectory={parentDirectory} | ||
location={location} | ||
language={language} | ||
selectedSubPath={selectedSubPath} | ||
setError={setError} | ||
setIsLoading={setIsLoading} | ||
propsCollections={propsCollections} | ||
/> | ||
); | ||
const button = getByTestId("update-geo-location"); | ||
fireEvent.click(button); | ||
await waitFor(() => expect(UpdateGeoLocation).toHaveBeenCalledTimes(1)); | ||
expect(handleExit).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it("[UpdateButtonActive] enables button when location is updated", () => { | ||
const { getByText } = render( | ||
<UpdateButtonWrapper | ||
handleExit={handleExit} | ||
isLocationUpdated={true} | ||
parentDirectory={parentDirectory} | ||
location={location} | ||
selectedSubPath={selectedSubPath} | ||
setError={setError} | ||
setIsLoading={setIsLoading} | ||
propsCollections={propsCollections} | ||
/> | ||
); | ||
const button = getByText(language.key(localization.MessageAddLocation)) as HTMLButtonElement; | ||
|
||
expect(button.disabled).toBe(false); | ||
}); | ||
}); |
51 changes: 51 additions & 0 deletions
51
...ky/starsky/clientapp/src/components/organisms/modal-geo/internal/update-button-active.tsx
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,51 @@ | ||
import { IGeoLocationModel } from "../../../../interfaces/IGeoLocationModel"; | ||
import localization from "../../../../localization/localization.json"; | ||
import { Language } from "../../../../shared/language"; | ||
import { ILatLong } from "../modal-geo"; | ||
import { UpdateGeoLocation } from "./update-geo-location"; | ||
|
||
interface IUpdateButtonActiveProps { | ||
parentDirectory: string; | ||
handleExit: (result: IGeoLocationModel | null) => void; | ||
selectedSubPath: string; | ||
latitude?: number; | ||
longitude?: number; | ||
location: ILatLong; | ||
language: Language; | ||
setIsLoading: React.Dispatch<React.SetStateAction<boolean>>; | ||
setError: React.Dispatch<React.SetStateAction<boolean>>; | ||
propsCollections: boolean | undefined; | ||
} | ||
|
||
export const UpdateButtonActive: React.FunctionComponent<IUpdateButtonActiveProps> = ({ | ||
handleExit, | ||
parentDirectory, | ||
latitude, | ||
longitude, | ||
language, | ||
...props | ||
}) => { | ||
return ( | ||
<button | ||
onClick={async () => { | ||
const model = await UpdateGeoLocation( | ||
parentDirectory, | ||
props.selectedSubPath, | ||
props.location, | ||
props.setError, | ||
props.setIsLoading, | ||
props.propsCollections | ||
); | ||
if (model) { | ||
handleExit(model); | ||
} | ||
}} | ||
data-test="update-geo-location" | ||
className="btn btn--default" | ||
> | ||
{!latitude && !longitude | ||
? language.key(localization.MessageAddLocation) | ||
: language.key(localization.MessageUpdateLocation)} | ||
</button> | ||
); | ||
}; |
22 changes: 22 additions & 0 deletions
22
...sky/clientapp/src/components/organisms/modal-geo/internal/update-button-disabled.spec.tsx
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,22 @@ | ||
import { render } from "@testing-library/react"; | ||
import localization from "../../../../localization/localization.json"; | ||
import { Language, SupportedLanguages } from "../../../../shared/language"; | ||
import { ILatLong } from "../modal-geo"; | ||
import { UpdateButtonDisabled } from "./update-button-disabled"; | ||
|
||
describe("UpdateButtonActive", () => { | ||
const location: ILatLong = { latitude: 0, longitude: 0 }; | ||
const language = new Language(SupportedLanguages.en); | ||
|
||
it("[disabledBtn] disables button when location is not updated", () => { | ||
const { getByText } = render( | ||
<UpdateButtonDisabled | ||
language={language} | ||
latitude={location.latitude} | ||
longitude={location.longitude} | ||
/> | ||
); | ||
const button = getByText(language.key(localization.MessageAddLocation)) as HTMLButtonElement; | ||
expect(button.disabled).toBe(true); | ||
}); | ||
}); |
23 changes: 23 additions & 0 deletions
23
.../starsky/clientapp/src/components/organisms/modal-geo/internal/update-button-disabled.tsx
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,23 @@ | ||
import localization from "../../../../localization/localization.json"; | ||
import { Language } from "../../../../shared/language"; | ||
|
||
interface IUpdateButtonActiveProps { | ||
latitude?: number; | ||
longitude?: number; | ||
language: Language; | ||
} | ||
|
||
export const UpdateButtonDisabled: React.FunctionComponent<IUpdateButtonActiveProps> = ({ | ||
latitude, | ||
longitude, | ||
language | ||
}) => { | ||
return ( | ||
<button className="btn btn--default" disabled={true}> | ||
{/* disabled */} | ||
{!latitude && !longitude | ||
? language.key(localization.MessageAddLocation) | ||
: language.key(localization.MessageUpdateLocation)} | ||
</button> | ||
); | ||
}; |
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
53 changes: 53 additions & 0 deletions
53
...y/starsky/clientapp/src/components/organisms/modal-geo/internal/update-button-wrapper.tsx
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,53 @@ | ||
import React from "react"; | ||
import useGlobalSettings from "../../../../hooks/use-global-settings"; | ||
import { IGeoLocationModel } from "../../../../interfaces/IGeoLocationModel"; | ||
import { Language } from "../../../../shared/language"; | ||
import { ILatLong } from "../modal-geo"; | ||
import { UpdateButtonActive } from "./update-button-active"; | ||
import { UpdateButtonDisabled } from "./update-button-disabled"; | ||
|
||
interface IUpdateButtonWrapperProps { | ||
parentDirectory: string; | ||
selectedSubPath: string; | ||
location: ILatLong; | ||
setError: React.Dispatch<React.SetStateAction<boolean>>; | ||
setIsLoading: React.Dispatch<React.SetStateAction<boolean>>; | ||
propsCollections?: boolean; | ||
isLocationUpdated: boolean; | ||
handleExit: (result: IGeoLocationModel | null) => void; | ||
} | ||
|
||
export const UpdateButtonWrapper: React.FC<IUpdateButtonWrapperProps> = ({ | ||
parentDirectory, | ||
selectedSubPath, | ||
location, | ||
setError, | ||
setIsLoading, | ||
propsCollections, | ||
isLocationUpdated, | ||
handleExit | ||
}) => { | ||
const settings = useGlobalSettings(); | ||
const language = new Language(settings.language); | ||
|
||
return isLocationUpdated ? ( | ||
<UpdateButtonActive | ||
parentDirectory={parentDirectory} | ||
selectedSubPath={selectedSubPath} | ||
location={location} | ||
handleExit={handleExit} | ||
latitude={location.latitude} | ||
longitude={location.longitude} | ||
language={language} | ||
setIsLoading={setIsLoading} | ||
setError={setError} | ||
propsCollections={propsCollections} | ||
/> | ||
) : ( | ||
<UpdateButtonDisabled | ||
language={language} | ||
latitude={location.latitude} | ||
longitude={location.longitude} | ||
/> | ||
); | ||
}; |
68 changes: 0 additions & 68 deletions
68
starsky/starsky/clientapp/src/components/organisms/modal-geo/internal/update-button.tsx
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
a9df46a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎉 Published on https://starskyapp.netlify.app as production
🚀 Deployed on https://6710bce160fb34b533946a86--starskyapp.netlify.app