-
-
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 #1441 from qdraw/feature/202403-code-smells-6
Feature/202403 code smells 6
- Loading branch information
Showing
5 changed files
with
215 additions
and
35 deletions.
There are no files selected for viewing
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
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
138 changes: 138 additions & 0 deletions
138
...src/components/organisms/modal-archive-synchronize-manually/internal/remove-cache.spec.ts
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,138 @@ | ||
import { waitFor } from "@testing-library/react"; | ||
import { PageType } from "../../../../interfaces/IDetailView"; | ||
import * as FetchGet from "../../../../shared/fetch/fetch-get"; | ||
import { FileListCache } from "../../../../shared/filelist-cache"; | ||
import { RemoveCache } from "./remove-cache"; | ||
|
||
describe("RemoveCache function", () => { | ||
beforeEach(() => { | ||
jest.useFakeTimers(); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.clearAllTimers(); | ||
}); | ||
|
||
it("should call setIsLoading with true", () => { | ||
const setIsLoading = jest.fn(); | ||
RemoveCache(setIsLoading, "/parent", "search", jest.fn(), jest.fn()); | ||
expect(setIsLoading).toHaveBeenCalledWith(true); | ||
}); | ||
|
||
it("should call CacheCleanEverything", () => { | ||
const cacheCleanEverything = jest.spyOn(FileListCache.prototype, "CacheCleanEverything"); | ||
RemoveCache(jest.fn(), "/parent", "search", jest.fn(), jest.fn()); | ||
expect(cacheCleanEverything).toHaveBeenCalled(); | ||
cacheCleanEverything.mockRestore(); | ||
}); | ||
|
||
it("should call FetchGet with the correct URL to remove cache", () => { | ||
const mockFetchGet = jest | ||
.spyOn(FetchGet, "default") | ||
.mockReset() | ||
.mockImplementationOnce(() => Promise.resolve({ statusCode: 200, data: null })); | ||
const parentFolder = "/parent"; | ||
RemoveCache(jest.fn(), parentFolder, "search", jest.fn(), jest.fn()); | ||
expect(mockFetchGet).toHaveBeenCalledWith("/starsky/api/remove-cache?json=true&f=/parent"); | ||
expect(mockFetchGet.mock.calls[0][0]).toContain("/remove-cache"); | ||
expect(mockFetchGet.mock.calls[0][0]).toContain( | ||
"/starsky/api/remove-cache?json=true&f=/parent" | ||
); | ||
mockFetchGet.mockRestore(); | ||
}); | ||
|
||
it("should call FetchGet with invalid url to remove cache", () => { | ||
const mockFetchGet = jest | ||
.spyOn(FetchGet, "default") | ||
.mockReset() | ||
.mockImplementationOnce(() => Promise.resolve({ statusCode: 200, data: null })); | ||
|
||
const parent: string | undefined = undefined as unknown as string; | ||
|
||
RemoveCache(jest.fn(), parent, "search", jest.fn(), jest.fn()); | ||
expect(mockFetchGet).toHaveBeenCalledWith("/starsky/api/remove-cache?json=true&f=/"); | ||
expect(mockFetchGet.mock.calls[0][0]).toContain("/remove-cache"); | ||
expect(mockFetchGet.mock.calls[0][0]).toContain("/starsky/api/remove-cache?json=true&f=/"); | ||
mockFetchGet.mockRestore(); | ||
}); | ||
|
||
it("should call FetchGet with the correct URL to index server API after setTimeout", async () => { | ||
const mockFetchGet = jest | ||
.spyOn(FetchGet, "default") | ||
.mockImplementationOnce(() => Promise.resolve({ statusCode: 200, data: null })); | ||
|
||
const parentFolder = "/parent"; | ||
const historyLocationSearch = "search"; | ||
RemoveCache(jest.fn(), parentFolder, historyLocationSearch, jest.fn(), jest.fn()); | ||
|
||
jest.runAllTimers(); | ||
|
||
expect(mockFetchGet).toHaveBeenCalledWith("/starsky/api/remove-cache?json=true&f=/parent"); | ||
|
||
mockFetchGet.mockRestore(); | ||
}); | ||
|
||
it("should dispatch force-reset action when payload has fileIndexItems", async () => { | ||
const dispatch = jest.fn(); | ||
const propsHandleExit = jest.fn(); | ||
|
||
const mediaArchiveData = { | ||
data: { | ||
pageType: PageType.Archive, | ||
fileIndexItems: [{}, {}] // Mocking fileIndexItems | ||
}, | ||
statusCode: 200 | ||
}; | ||
const mockFetchGet = jest | ||
.spyOn(FetchGet, "default") | ||
.mockImplementationOnce(() => Promise.resolve(mediaArchiveData)) | ||
.mockImplementationOnce(() => Promise.resolve(mediaArchiveData)); | ||
|
||
jest.useFakeTimers(); | ||
|
||
RemoveCache(jest.fn(), "/parent", "search", dispatch, propsHandleExit); | ||
|
||
jest.advanceTimersByTime(600); | ||
|
||
await waitFor(() => { | ||
expect(propsHandleExit).toHaveBeenCalled(); | ||
|
||
expect(dispatch).toHaveBeenCalledWith({ | ||
type: "force-reset", | ||
payload: mediaArchiveData.data | ||
}); | ||
}); | ||
|
||
mockFetchGet.mockRestore(); | ||
}); | ||
|
||
it("should not dispatch force-reset action when payload has no fileIndexItems", async () => { | ||
const dispatch = jest.fn(); | ||
const propsHandleExit = jest.fn(); | ||
|
||
const mediaArchiveData = { | ||
data: { | ||
pageType: PageType.Archive | ||
}, | ||
statusCode: 200 | ||
}; | ||
const mockFetchGet = jest | ||
.spyOn(FetchGet, "default") | ||
.mockImplementationOnce(() => Promise.resolve(mediaArchiveData)) | ||
.mockImplementationOnce(() => Promise.resolve(mediaArchiveData)); | ||
|
||
jest.useFakeTimers(); | ||
|
||
RemoveCache(jest.fn(), "/parent", "search", dispatch, propsHandleExit); | ||
|
||
jest.advanceTimersByTime(600); | ||
|
||
await waitFor(() => { | ||
expect(propsHandleExit).toHaveBeenCalled(); | ||
|
||
expect(dispatch).toHaveBeenCalledTimes(0); | ||
}); | ||
|
||
mockFetchGet.mockRestore(); | ||
}); | ||
}); |
47 changes: 47 additions & 0 deletions
47
...tapp/src/components/organisms/modal-archive-synchronize-manually/internal/remove-cache.ts
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,47 @@ | ||
import { Dispatch, SetStateAction } from "react"; | ||
import { ArchiveAction } from "../../../../contexts/archive-context.tsx"; | ||
import { IArchiveProps } from "../../../../interfaces/IArchiveProps.ts"; | ||
import { CastToInterface } from "../../../../shared/cast-to-interface.ts"; | ||
import FetchGet from "../../../../shared/fetch/fetch-get.ts"; | ||
import { FileListCache } from "../../../../shared/filelist-cache.ts"; | ||
import { URLPath } from "../../../../shared/url/url-path.ts"; | ||
import { UrlQuery } from "../../../../shared/url/url-query.ts"; | ||
|
||
/** | ||
* Remove Folder cache | ||
*/ | ||
export function RemoveCache( | ||
setIsLoading: Dispatch<SetStateAction<boolean>>, | ||
propsParentFolder: string, | ||
historyLocationSearch: string, | ||
dispatch: Dispatch<ArchiveAction>, | ||
propsHandleExit: () => void | ||
) { | ||
setIsLoading(true); | ||
new FileListCache().CacheCleanEverything(); | ||
const parentFolder = propsParentFolder ?? "/"; | ||
FetchGet(new UrlQuery().UrlRemoveCache(new URLPath().encodeURI(parentFolder))) | ||
.then(() => { | ||
return new Promise<string>((resolve) => { | ||
setTimeout(() => { | ||
resolve( | ||
new UrlQuery().UrlIndexServerApi(new URLPath().StringToIUrl(historyLocationSearch)) | ||
); | ||
}, 600); | ||
}); | ||
}) | ||
.then((url: string) => { | ||
return FetchGet(url, { "Cache-Control": "no-store, max-age=0" }); | ||
}) | ||
.then((connectionResult) => { | ||
const removeCacheResult = new CastToInterface().MediaArchive(connectionResult.data); | ||
const payload = removeCacheResult.data as IArchiveProps; | ||
if (payload.fileIndexItems) { | ||
dispatch({ type: "force-reset", payload }); | ||
} | ||
propsHandleExit(); | ||
}) | ||
.catch((error) => { | ||
console.error("Error:", error); | ||
}); | ||
} |
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
2ba54b6
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://65e8e637c55aad5e9bcab694--starskyapp.netlify.app