Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
qdraw committed Feb 27, 2024
1 parent 9fdf5ce commit 3b710ab
Show file tree
Hide file tree
Showing 2 changed files with 150 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { render, screen } from "@testing-library/react";
import { fireEvent, render, screen, waitFor } from "@testing-library/react";
import { ChangeEvent } from "react";
import * as useFetch from "../../../hooks/use-fetch";
import { IAppSettings } from "../../../interfaces/IAppSettings";
import { RawJpegMode } from "../../../interfaces/ICollectionsOpenType";
import { IConnectionDefault } from "../../../interfaces/IConnectionDefault";
import { ImageFormat } from "../../../interfaces/IFileIndexItem";
import * as FetchPost from "../../../shared/fetch/fetch-post";
import { UrlQuery } from "../../../shared/url-query";
import * as FormControl from "../../atoms/form-control/form-control";
import * as SwitchButton from "../../atoms/switch-button/switch-button";
import PreferencesAppSettingsDesktop, {
ToggleCollections,
Expand Down Expand Up @@ -84,9 +86,145 @@ describe("PreferencesAppSettingsDesktop", () => {

component.unmount();
});

it("give message when done with SwitchButton", async () => {
const mockGetIConnectionDefaultAppSettings = {
statusCode: 200,
data: {
useLocalDesktop: true,
defaultDesktopEditor: []
}
} as IConnectionDefault;

const mockGetIConnectionDefaultPermissions = {
statusCode: 200,
data: null
} as IConnectionDefault;
const useFetchSpy = jest
.spyOn(useFetch, "default")
.mockReset()
.mockImplementationOnce(() => mockGetIConnectionDefaultAppSettings)
.mockImplementationOnce(() => mockGetIConnectionDefaultPermissions)
.mockImplementationOnce(() => mockGetIConnectionDefaultAppSettings)
.mockImplementationOnce(() => mockGetIConnectionDefaultPermissions)
.mockImplementationOnce(() => mockGetIConnectionDefaultAppSettings)
.mockImplementationOnce(() => mockGetIConnectionDefaultPermissions);

const mockIConnectionDefault: Promise<IConnectionDefault> = Promise.resolve({
data: null,
statusCode: 200
});

const spyFetchPost = jest
.spyOn(FetchPost, "default")
.mockReset()
.mockImplementationOnce(() => mockIConnectionDefault);

const switchButtonSpy = jest.spyOn(SwitchButton, "default").mockImplementationOnce((props) => {
return <button data-test="switch-button-spy" onClick={() => props.onToggle(true)}></button>;
});

const component = render(<PreferencesAppSettingsDesktop />);

expect(screen.queryByTestId("preference-app-settings-desktop-warning-box")).toBeFalsy();

fireEvent.click(screen.getByTestId("switch-button-spy"));

await waitFor(() => {
expect(spyFetchPost).toHaveBeenCalled();
expect(spyFetchPost).toHaveBeenCalledTimes(1);
expect(spyFetchPost).toHaveBeenCalledWith(
new UrlQuery().UrlApiAppSettings(),
"desktopCollectionsOpen=2"
);

expect(screen.getByTestId("preference-app-settings-desktop-warning-box")).toBeTruthy();
});

expect(switchButtonSpy).toHaveBeenCalled();

expect(useFetchSpy).toHaveBeenCalled();
expect(useFetchSpy).toHaveBeenCalledTimes(6);

component.unmount();
});

it("give message when done with FormControl", async () => {
const mockGetIConnectionDefaultAppSettings = {
statusCode: 200,
data: {
useLocalDesktop: true,
defaultDesktopEditor: []
}
} as IConnectionDefault;

const mockGetIConnectionDefaultPermissions = {
statusCode: 200,
data: null
} as IConnectionDefault;
const useFetchSpy = jest
.spyOn(useFetch, "default")
.mockReset()
.mockImplementationOnce(() => mockGetIConnectionDefaultAppSettings)
.mockImplementationOnce(() => mockGetIConnectionDefaultPermissions)
.mockImplementationOnce(() => mockGetIConnectionDefaultAppSettings)
.mockImplementationOnce(() => mockGetIConnectionDefaultPermissions);

const mockIConnectionDefault: Promise<IConnectionDefault> = Promise.resolve({
data: null,
statusCode: 200
});

const spyFetchPost = jest
.spyOn(FetchPost, "default")
.mockReset()
.mockImplementationOnce(() => mockIConnectionDefault);

const switchButtonSpy = jest.spyOn(FormControl, "default").mockImplementationOnce((props) => {
return (
<button
data-test="form-control-spy"
onClick={() => {
if (props.onBlur) {
props?.onBlur({
target: {
innerText: "test"
}
} as unknown as ChangeEvent<HTMLDivElement>);
}
}}
></button>
);
});

const component = render(<PreferencesAppSettingsDesktop />);

expect(screen.queryByTestId("preference-app-settings-desktop-warning-box")).toBeFalsy();

fireEvent.click(screen.getByTestId("form-control-spy"));

await waitFor(() => {
const query =
"DefaultDesktopEditor%5B0%5D.ImageFormats%5B0%5D=jpg&DefaultDesktopEditor%5B0%5D.ImageFormats%" +
"5B1%5D=png&DefaultDesktopEditor%5B0%5D.ImageFormats%5B2%5D=bmp&DefaultDesktopEditor%5B0%5D.ImageFormats%5B3%5D=tiff&" +
"DefaultDesktopEditor%5B0%5D.ApplicationPath=test";

expect(spyFetchPost).toHaveBeenCalledTimes(1);
expect(spyFetchPost).toHaveBeenCalledWith(new UrlQuery().UrlApiAppSettings(), query);

expect(screen.getByTestId("preference-app-settings-desktop-warning-box")).toBeTruthy();
});

expect(switchButtonSpy).toHaveBeenCalled();

expect(useFetchSpy).toHaveBeenCalled();
expect(useFetchSpy).toHaveBeenCalledTimes(4);

component.unmount();
});
});

describe("FetchPost Function", () => {
describe("updateDefaultEditorPhotos", () => {
it("should call FetchPost with correct URL and bodyParams for updateDefaultEditorPhotos function", async () => {
const value = {
target: { innerText: "NewApplicationPath" }
Expand All @@ -109,11 +247,13 @@ describe("FetchPost Function", () => {
await UpdateDefaultEditorPhotos(value, jest.fn(), "", "", defaultDesktopEditor);

expect(spyFetchPost).toHaveBeenCalledWith(
expect.stringContaining("api/env"),
expect.stringContaining(new UrlQuery().UrlApiAppSettings()),
expect.any(String)
);
});
});

describe("ToggleCollections", () => {
it("should call FetchPost with correct URL and bodyParams for toggleCollections function", async () => {
const mockIConnectionDefault: Promise<IConnectionDefault> = Promise.resolve({
data: null,
Expand All @@ -131,7 +271,7 @@ describe("FetchPost Function", () => {
await ToggleCollections(true, jest.fn(), "", "", appSettings);

expect(spyFetchPost).toHaveBeenCalledWith(
expect.stringContaining("api/env"),
expect.stringContaining(new UrlQuery().UrlApiAppSettings()),
expect.any(String)
);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,12 @@ const PreferencesAppSettingsDesktop: React.FunctionComponent = () => {
</div>

{isMessage !== "" ? (
<div className="warning-box warning-box--optional">{isMessage}</div>
<div
data-test="preference-app-settings-desktop-warning-box"
className="warning-box warning-box--optional"
>
{isMessage}
</div>
) : null}
<SwitchButton
isOn={appSettings?.desktopCollectionsOpen === RawJpegMode.Raw}
Expand Down

0 comments on commit 3b710ab

Please sign in to comment.