Skip to content

Commit

Permalink
update btn add extra tests
Browse files Browse the repository at this point in the history
  • Loading branch information
qdraw committed Oct 17, 2024
1 parent d5ab8a6 commit b651369
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 4 deletions.
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);
});
});
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);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Expand All @@ -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(
Expand All @@ -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(
<UpdateButtonWrapper
handleExit={handleExit}
Expand All @@ -59,7 +59,7 @@ describe("UpdateButton", () => {
expect(button.disabled).toBe(true);
});

it("enables button when location is updated", () => {
it("[wrapper] enables button when location is updated", () => {
const { getByText } = render(
<UpdateButtonWrapper
handleExit={handleExit}
Expand Down

0 comments on commit b651369

Please sign in to comment.