From 73705c6035b9ac5d3d24f47215adb6ca22008fea Mon Sep 17 00:00:00 2001 From: Dion Date: Thu, 17 Oct 2024 08:53:27 +0200 Subject: [PATCH 1/3] refactor update btn --- .../internal/update-button-active.tsx | 51 +++++++++ .../internal/update-button-disabled.tsx | 23 ++++ .../modal-geo/internal/update-button.spec.tsx | 57 +++++----- .../modal-geo/internal/update-button.tsx | 105 ++++++++---------- .../organisms/modal-geo/modal-geo.spec.tsx | 24 ++-- .../organisms/modal-geo/modal-geo.tsx | 22 ++-- 6 files changed, 179 insertions(+), 103 deletions(-) create mode 100644 starsky/starsky/clientapp/src/components/organisms/modal-geo/internal/update-button-active.tsx create mode 100644 starsky/starsky/clientapp/src/components/organisms/modal-geo/internal/update-button-disabled.tsx diff --git a/starsky/starsky/clientapp/src/components/organisms/modal-geo/internal/update-button-active.tsx b/starsky/starsky/clientapp/src/components/organisms/modal-geo/internal/update-button-active.tsx new file mode 100644 index 0000000000..41f024bcfd --- /dev/null +++ b/starsky/starsky/clientapp/src/components/organisms/modal-geo/internal/update-button-active.tsx @@ -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>; + setError: React.Dispatch>; + propsCollections: boolean | undefined; +} + +export const UpdateButtonActive: React.FunctionComponent = ({ + handleExit, + parentDirectory, + latitude, + longitude, + language, + ...props +}) => { + return ( + + ); +}; diff --git a/starsky/starsky/clientapp/src/components/organisms/modal-geo/internal/update-button-disabled.tsx b/starsky/starsky/clientapp/src/components/organisms/modal-geo/internal/update-button-disabled.tsx new file mode 100644 index 0000000000..582298bbf3 --- /dev/null +++ b/starsky/starsky/clientapp/src/components/organisms/modal-geo/internal/update-button-disabled.tsx @@ -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 = ({ + latitude, + longitude, + language +}) => { + return ( + + ); +}; diff --git a/starsky/starsky/clientapp/src/components/organisms/modal-geo/internal/update-button.spec.tsx b/starsky/starsky/clientapp/src/components/organisms/modal-geo/internal/update-button.spec.tsx index 04c332727a..4b7563689b 100644 --- a/starsky/starsky/clientapp/src/components/organisms/modal-geo/internal/update-button.spec.tsx +++ b/starsky/starsky/clientapp/src/components/organisms/modal-geo/internal/update-button.spec.tsx @@ -15,8 +15,6 @@ describe("UpdateButton", () => { const setIsLoading = jest.fn(); const propsCollections = true; const handleExit = jest.fn(); - const latitude = 0; - const longitude = 0; const language = new Language(SupportedLanguages.en); beforeEach(() => { @@ -25,15 +23,18 @@ describe("UpdateButton", () => { it("calls UpdateGeoLocation when clicked and updates location", async () => { (UpdateGeoLocation as jest.Mock).mockResolvedValueOnce({} as any); + const { getByTestId } = render( - new UpdateButton( - parentDirectory, - selectedSubPath, - location, - setError, - setIsLoading, - propsCollections - ).updateButton(true, handleExit, latitude, longitude, language) + ); const button = getByTestId("update-geo-location"); fireEvent.click(button); @@ -43,14 +44,16 @@ describe("UpdateButton", () => { it("disables button when location is not updated", () => { const { getByText } = render( - new UpdateButton( - parentDirectory, - selectedSubPath, - location, - setError, - setIsLoading, - propsCollections - ).updateButton(false, handleExit, latitude, longitude, language) + ); const button = getByText(language.key(localization.MessageAddLocation)) as HTMLButtonElement; expect(button.disabled).toBe(true); @@ -58,14 +61,16 @@ describe("UpdateButton", () => { it("enables button when location is updated", () => { const { getByText } = render( - new UpdateButton( - parentDirectory, - selectedSubPath, - location, - setError, - setIsLoading, - propsCollections - ).updateButton(true, handleExit, latitude, longitude, language) + ); const button = getByText(language.key(localization.MessageAddLocation)) as HTMLButtonElement; diff --git a/starsky/starsky/clientapp/src/components/organisms/modal-geo/internal/update-button.tsx b/starsky/starsky/clientapp/src/components/organisms/modal-geo/internal/update-button.tsx index 0d2b967c25..a11654cb17 100644 --- a/starsky/starsky/clientapp/src/components/organisms/modal-geo/internal/update-button.tsx +++ b/starsky/starsky/clientapp/src/components/organisms/modal-geo/internal/update-button.tsx @@ -1,68 +1,53 @@ +import React from "react"; +import useGlobalSettings from "../../../../hooks/use-global-settings"; 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"; +import { UpdateButtonActive } from "./update-button-active"; +import { UpdateButtonDisabled } from "./update-button-disabled"; -export class UpdateButton { +interface UpdateButtonProps { parentDirectory: string; selectedSubPath: string; location: ILatLong; - setError: any; - setIsLoading: any; - propsCollections: boolean | undefined; - - constructor( - parentDirectory: string, - selectedSubPath: string, - location: ILatLong, - setError: React.Dispatch>, - setIsLoading: React.Dispatch>, - propsCollections?: boolean | undefined - ) { - this.parentDirectory = parentDirectory; - this.selectedSubPath = selectedSubPath; - this.location = location; - this.setError = setError; - this.setIsLoading = setIsLoading; - this.propsCollections = propsCollections; - } - updateButton( - isLocationUpdated: boolean, - handleExit: (result: IGeoLocationModel | null) => void, - latitude: number | undefined, - longitude: number | undefined, - language: Language - ): React.JSX.Element { - return isLocationUpdated ? ( - - ) : ( - - ); - } + setError: React.Dispatch>; + setIsLoading: React.Dispatch>; + propsCollections?: boolean; + isLocationUpdated: boolean; + handleExit: (result: IGeoLocationModel | null) => void; } + +export const UpdateButton: React.FC = ({ + parentDirectory, + selectedSubPath, + location, + setError, + setIsLoading, + propsCollections, + isLocationUpdated, + handleExit +}) => { + const settings = useGlobalSettings(); + const language = new Language(settings.language); + + return isLocationUpdated ? ( + + ) : ( + + ); +}; diff --git a/starsky/starsky/clientapp/src/components/organisms/modal-geo/modal-geo.spec.tsx b/starsky/starsky/clientapp/src/components/organisms/modal-geo/modal-geo.spec.tsx index 83fed3bc11..2036351a0d 100644 --- a/starsky/starsky/clientapp/src/components/organisms/modal-geo/modal-geo.spec.tsx +++ b/starsky/starsky/clientapp/src/components/organisms/modal-geo/modal-geo.spec.tsx @@ -2,14 +2,14 @@ import { fireEvent, render, screen } from "@testing-library/react"; import L, { LatLng } from "leaflet"; import * as Modal from "../../atoms/modal/modal"; -import ModalGeo, { ILatLong } from "./modal-geo"; import { AddDefaultClickSetMarker } from "./internal/add-default-click-set-marker"; import * as AddDefaultMarker from "./internal/add-default-marker"; import { GetZoom } from "./internal/get-zoom"; import { OnDrag } from "./internal/on-drag"; import { RealtimeMapUpdate } from "./internal/realtime-map-update"; -import { UpdateButton } from "./internal/update-button"; +import * as UpdateButton from "./internal/update-button"; import * as updateGeoLocation from "./internal/update-geo-location"; +import ModalGeo, { ILatLong } from "./modal-geo"; describe("ModalGeo", () => { beforeEach(() => { @@ -194,7 +194,7 @@ describe("ModalGeo", () => { it("check if form is disabled it hides the button", async () => { const updateButtonSpy = jest - .spyOn(UpdateButton.prototype, "updateButton") + .spyOn(UpdateButton, "UpdateButton") .mockImplementationOnce(() => { return
test
; }); @@ -219,7 +219,7 @@ describe("ModalGeo", () => { it("check if form is enabled it shows the button", () => { const updateButtonSpy = jest - .spyOn(UpdateButton.prototype, "updateButton") + .spyOn(UpdateButton, "UpdateButton") .mockImplementationOnce(() => { return
test
; }); @@ -239,9 +239,19 @@ describe("ModalGeo", () => { expect(updateButtonSpy).toHaveBeenCalledTimes(2); - expect(updateButtonSpy).toHaveBeenLastCalledWith(false, expect.any(Function), 51, 3, { - selectedLanguage: "en" - }); + expect(updateButtonSpy).toHaveBeenLastCalledWith( + { + handleExit: expect.any(Function), + isLocationUpdated: false, + location: { latitude: 51, longitude: 3 }, + parentDirectory: "/", + propsCollections: undefined, + selectedSubPath: "/test.jpg", + setError: expect.any(Function), + setIsLoading: expect.any(Function) + }, + {} + ); modal.unmount(); }); diff --git a/starsky/starsky/clientapp/src/components/organisms/modal-geo/modal-geo.tsx b/starsky/starsky/clientapp/src/components/organisms/modal-geo/modal-geo.tsx index 180ab8f2ed..f564f9b670 100644 --- a/starsky/starsky/clientapp/src/components/organisms/modal-geo/modal-geo.tsx +++ b/starsky/starsky/clientapp/src/components/organisms/modal-geo/modal-geo.tsx @@ -121,16 +121,18 @@ const ModalGeo: React.FunctionComponent = ({ > {language.key(localization.MessageCancel)} - {isFormEnabled - ? new UpdateButton( - parentDirectory, - selectedSubPath, - location, - setError, - setIsLoading, - props.collections - ).updateButton(isLocationUpdated, props.handleExit, latitude, longitude, language) - : null} + {isFormEnabled ? ( + + ) : null}
Latitude:{" "} Date: Thu, 17 Oct 2024 09:02:33 +0200 Subject: [PATCH 2/3] rename method --- ...ate-button.spec.tsx => update-button-wrapper.spec.tsx} | 8 ++++---- .../{update-button.tsx => update-button-wrapper.tsx} | 4 ++-- .../src/components/organisms/modal-geo/modal-geo.spec.tsx | 6 +++--- .../src/components/organisms/modal-geo/modal-geo.tsx | 4 ++-- 4 files changed, 11 insertions(+), 11 deletions(-) rename starsky/starsky/clientapp/src/components/organisms/modal-geo/internal/{update-button.spec.tsx => update-button-wrapper.spec.tsx} (94%) rename starsky/starsky/clientapp/src/components/organisms/modal-geo/internal/{update-button.tsx => update-button-wrapper.tsx} (92%) diff --git a/starsky/starsky/clientapp/src/components/organisms/modal-geo/internal/update-button.spec.tsx b/starsky/starsky/clientapp/src/components/organisms/modal-geo/internal/update-button-wrapper.spec.tsx similarity index 94% rename from starsky/starsky/clientapp/src/components/organisms/modal-geo/internal/update-button.spec.tsx rename to starsky/starsky/clientapp/src/components/organisms/modal-geo/internal/update-button-wrapper.spec.tsx index 4b7563689b..881d059c1f 100644 --- a/starsky/starsky/clientapp/src/components/organisms/modal-geo/internal/update-button.spec.tsx +++ b/starsky/starsky/clientapp/src/components/organisms/modal-geo/internal/update-button-wrapper.spec.tsx @@ -2,7 +2,7 @@ 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 { UpdateButton } from "./update-button"; +import { UpdateButtonWrapper } from "./update-button-wrapper"; import { UpdateGeoLocation } from "./update-geo-location"; jest.mock("./update-geo-location"); @@ -25,7 +25,7 @@ describe("UpdateButton", () => { (UpdateGeoLocation as jest.Mock).mockResolvedValueOnce({} as any); const { getByTestId } = render( - { it("disables button when location is not updated", () => { const { getByText } = render( - { it("enables button when location is updated", () => { const { getByText } = render( - void; } -export const UpdateButton: React.FC = ({ +export const UpdateButtonWrapper: React.FC = ({ parentDirectory, selectedSubPath, location, diff --git a/starsky/starsky/clientapp/src/components/organisms/modal-geo/modal-geo.spec.tsx b/starsky/starsky/clientapp/src/components/organisms/modal-geo/modal-geo.spec.tsx index 2036351a0d..8379c16124 100644 --- a/starsky/starsky/clientapp/src/components/organisms/modal-geo/modal-geo.spec.tsx +++ b/starsky/starsky/clientapp/src/components/organisms/modal-geo/modal-geo.spec.tsx @@ -7,7 +7,7 @@ import * as AddDefaultMarker from "./internal/add-default-marker"; import { GetZoom } from "./internal/get-zoom"; import { OnDrag } from "./internal/on-drag"; import { RealtimeMapUpdate } from "./internal/realtime-map-update"; -import * as UpdateButton from "./internal/update-button"; +import * as UpdateButtonWrapper from "./internal/update-button-wrapper"; import * as updateGeoLocation from "./internal/update-geo-location"; import ModalGeo, { ILatLong } from "./modal-geo"; @@ -194,7 +194,7 @@ describe("ModalGeo", () => { it("check if form is disabled it hides the button", async () => { const updateButtonSpy = jest - .spyOn(UpdateButton, "UpdateButton") + .spyOn(UpdateButtonWrapper, "UpdateButtonWrapper") .mockImplementationOnce(() => { return
test
; }); @@ -219,7 +219,7 @@ describe("ModalGeo", () => { it("check if form is enabled it shows the button", () => { const updateButtonSpy = jest - .spyOn(UpdateButton, "UpdateButton") + .spyOn(UpdateButtonWrapper, "UpdateButtonWrapper") .mockImplementationOnce(() => { return
test
; }); diff --git a/starsky/starsky/clientapp/src/components/organisms/modal-geo/modal-geo.tsx b/starsky/starsky/clientapp/src/components/organisms/modal-geo/modal-geo.tsx index f564f9b670..01ee124e68 100644 --- a/starsky/starsky/clientapp/src/components/organisms/modal-geo/modal-geo.tsx +++ b/starsky/starsky/clientapp/src/components/organisms/modal-geo/modal-geo.tsx @@ -9,7 +9,7 @@ import Modal from "../../atoms/modal/modal"; import Preloader from "../../atoms/preloader/preloader"; import { LatLongRound } from "./internal/lat-long-round"; import { RealtimeMapUpdate } from "./internal/realtime-map-update"; -import { UpdateButton } from "./internal/update-button"; +import { UpdateButtonWrapper } from "./internal/update-button-wrapper"; import { UpdateMap } from "./internal/update-map"; interface IModalMoveFileProps { @@ -122,7 +122,7 @@ const ModalGeo: React.FunctionComponent = ({ {language.key(localization.MessageCancel)} {isFormEnabled ? ( - Date: Thu, 17 Oct 2024 09:05:31 +0200 Subject: [PATCH 3/3] update btn add extra tests --- .../internal/update-button-active.spec.tsx | 63 +++++++++++++++++++ .../internal/update-button-disabled.spec.tsx | 22 +++++++ .../internal/update-button-wrapper.spec.tsx | 8 +-- 3 files changed, 89 insertions(+), 4 deletions(-) create mode 100644 starsky/starsky/clientapp/src/components/organisms/modal-geo/internal/update-button-active.spec.tsx create mode 100644 starsky/starsky/clientapp/src/components/organisms/modal-geo/internal/update-button-disabled.spec.tsx diff --git a/starsky/starsky/clientapp/src/components/organisms/modal-geo/internal/update-button-active.spec.tsx b/starsky/starsky/clientapp/src/components/organisms/modal-geo/internal/update-button-active.spec.tsx new file mode 100644 index 0000000000..2d1559fd88 --- /dev/null +++ b/starsky/starsky/clientapp/src/components/organisms/modal-geo/internal/update-button-active.spec.tsx @@ -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( + + ); + 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( + + ); + const button = getByText(language.key(localization.MessageAddLocation)) as HTMLButtonElement; + + expect(button.disabled).toBe(false); + }); +}); diff --git a/starsky/starsky/clientapp/src/components/organisms/modal-geo/internal/update-button-disabled.spec.tsx b/starsky/starsky/clientapp/src/components/organisms/modal-geo/internal/update-button-disabled.spec.tsx new file mode 100644 index 0000000000..acff9be609 --- /dev/null +++ b/starsky/starsky/clientapp/src/components/organisms/modal-geo/internal/update-button-disabled.spec.tsx @@ -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( + + ); + const button = getByText(language.key(localization.MessageAddLocation)) as HTMLButtonElement; + expect(button.disabled).toBe(true); + }); +}); diff --git a/starsky/starsky/clientapp/src/components/organisms/modal-geo/internal/update-button-wrapper.spec.tsx b/starsky/starsky/clientapp/src/components/organisms/modal-geo/internal/update-button-wrapper.spec.tsx index 881d059c1f..9cb7b7f594 100644 --- a/starsky/starsky/clientapp/src/components/organisms/modal-geo/internal/update-button-wrapper.spec.tsx +++ b/starsky/starsky/clientapp/src/components/organisms/modal-geo/internal/update-button-wrapper.spec.tsx @@ -7,7 +7,7 @@ import { UpdateGeoLocation } from "./update-geo-location"; jest.mock("./update-geo-location"); -describe("UpdateButton", () => { +describe("UpdateButtonWrapper", () => { const parentDirectory = "parentDirectory"; const selectedSubPath = "selectedSubPath"; const location: ILatLong = { latitude: 0, longitude: 0 }; @@ -21,7 +21,7 @@ describe("UpdateButton", () => { jest.clearAllMocks(); }); - it("calls UpdateGeoLocation when clicked and updates location", async () => { + it("[wrapper] calls UpdateGeoLocation when clicked and updates location", async () => { (UpdateGeoLocation as jest.Mock).mockResolvedValueOnce({} as any); const { getByTestId } = render( @@ -42,7 +42,7 @@ describe("UpdateButton", () => { expect(handleExit).toHaveBeenCalledTimes(1); }); - it("disables button when location is not updated", () => { + it("[wrapper] disables button when location is not updated", () => { const { getByText } = render( { expect(button.disabled).toBe(true); }); - it("enables button when location is updated", () => { + it("[wrapper] enables button when location is updated", () => { const { getByText } = render(