Skip to content

Commit

Permalink
add fix for code smells
Browse files Browse the repository at this point in the history
  • Loading branch information
qdraw committed Dec 13, 2023
1 parent a89e84a commit 0ef5f3c
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 13 deletions.
62 changes: 59 additions & 3 deletions starsky/starsky/clientapp/src/hooks/use-filelist.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { act } from "react-dom/test-utils";
import { newIArchive } from "../interfaces/IArchive";
import { PageType } from "../interfaces/IDetailView";
import { IArchive, newIArchive } from "../interfaces/IArchive";
import { IRelativeObjects, PageType } from "../interfaces/IDetailView";
import {
newIFileIndexItem,
newIFileIndexItemArray
Expand All @@ -11,6 +11,8 @@ import useFileList, {
IFileList,
fetchContentUseFileList
} from "./use-filelist";
import * as fetchUseFileListContentCache from "./use-filelist";
import { render } from "@testing-library/react";

describe("UseFileList", () => {
describe("Archive", () => {
Expand Down Expand Up @@ -76,6 +78,57 @@ describe("UseFileList", () => {
expect(hook2).toBeCalledTimes(1);
});

const TestComponent = () => {
useFileList("mockLocationSearch", true);

// You can use the result object to interact with the state or render UI components

return null;
};

xit("should call setArchive when pageType is Archive", async () => {
// Mock fetchUseFileListContentCache to resolve immediately
const fetchSpy = jest
.spyOn(fetchUseFileListContentCache, "fetchUseFileListContentCache")
.mockResolvedValue();

// Render the component
let result: any;
await act(async () => {
result = render(<TestComponent />);
});

// Trigger the setPageTypeHelper with a response object having pageType: Archive
const responseObject = {
pageType: "Archive",
data: {
fileIndexItems: [],
pageType: "Archive",
subPath: "/test",
breadcrumb: [],
colorClassUsage: [],
collections: true,
lastEdited: "",
lastEditedUtc: "",
parentDirectory: "",
relativeObjects: {} as IRelativeObjects,
colorClassActiveList: [],
collectionsCount: 0,
isReadOnly: false,
dateCache: Date.now()
} as IArchive
};
await act(async () => {
result.current.setPageTypeHelper(responseObject);
});

// Assert that setArchive has been called with the expected data
expect(result.current).toBe(responseObject.data as unknown as IArchive);

// Clean up the spies
fetchSpy.mockRestore();
});

it("with detailview content 200", async () => {
const hook = jest.fn();
const hook2 = jest.fn();
Expand Down Expand Up @@ -179,6 +232,7 @@ describe("UseFileList", () => {
const { hook } = mounter();
const cacheGetSpy = jest
.spyOn(FileListCache.prototype, "CacheGet")
.mockReset()
.mockImplementationOnce(() => {
return { ...newIArchive(), dateCache: Date.now() };
});
Expand All @@ -195,13 +249,15 @@ describe("UseFileList", () => {
});

it("check cache first and then query", async () => {
const { hook } = mounter();
const cacheSetSpy = jest
.spyOn(FileListCache.prototype, "CacheGet")
.mockReset()
.mockImplementationOnce(() => {
return null;
});

const { hook } = mounter();

setFetchSpy(200, PageType.Archive);

hook.fetchUseFileListContentCache(
Expand Down
20 changes: 10 additions & 10 deletions starsky/starsky/clientapp/src/hooks/use-filelist.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect, useState } from "react";
import { SetStateAction, useEffect, useState } from "react";
import { IArchive, newIArchive } from "../interfaces/IArchive";
import {
IDetailView,
Expand All @@ -21,7 +21,7 @@ export interface IFileList {
abortController: AbortController,
setPageTypeHelper: (responseObject: any) => void,
resetPageTypeBeforeLoading: boolean,
setPageType: (value: React.SetStateAction<PageType>) => void
setPageType: (value: SetStateAction<PageType>) => void
) => Promise<void>;
}

Expand All @@ -31,7 +31,7 @@ export const fetchContentUseFileList = async (
abortController: AbortController,
setPageTypeHelper: (responseObject: any) => void,
resetPageTypeBeforeLoading: boolean,
setPageType: (value: React.SetStateAction<PageType>) => void
setPageType: (value: SetStateAction<PageType>) => void
): Promise<void> => {
try {
// force start with a loading icon
Expand Down Expand Up @@ -66,7 +66,7 @@ export const fetchContentUseFileList = async (
}
};

const fetchUseFileListContentCache = async (
export const fetchUseFileListContentCache = async (
locationLocal: string,
locationSearch: string,
abortController: AbortController,
Expand Down Expand Up @@ -124,14 +124,12 @@ const useFileList = (
setPageType(responseObject.pageType);
switch (responseObject.pageType) {
case PageType.Archive:
const archiveMedia = new CastToInterface().MediaArchive(responseObject);
setArchive(archiveMedia.data);
setArchive(new CastToInterface().MediaArchive(responseObject).data);

Check warning on line 127 in starsky/starsky/clientapp/src/hooks/use-filelist.ts

View check run for this annotation

Codecov / codecov/patch

starsky/starsky/clientapp/src/hooks/use-filelist.ts#L127

Added line #L127 was not covered by tests
break;
case PageType.DetailView:
const detailViewMedia = new CastToInterface().MediaDetailView(
responseObject
setDetailView(
new CastToInterface().MediaDetailView(responseObject).data
);
setDetailView(detailViewMedia.data);
break;
default:
break;
Expand All @@ -147,7 +145,9 @@ const useFileList = (
setPageTypeHelper,
resetPageTypeBeforeLoading,
setPageType
);
).then(() => {
// do nothing
});

return () => {
abortController.abort();
Expand Down

0 comments on commit 0ef5f3c

Please sign in to comment.