-
-
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.
- Loading branch information
Showing
4 changed files
with
144 additions
and
33 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
74 changes: 74 additions & 0 deletions
74
...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,74 @@ | ||
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 fetchGet = jest.spyOn(global, "FetchGet").mockResolvedValue(); | ||
const parentFolder = "/parent"; | ||
RemoveCache(jest.fn(), parentFolder, "search", jest.fn(), jest.fn()); | ||
expect(fetchGet).toHaveBeenCalledWith(expect.any(String)); | ||
expect(fetchGet.mock.calls[0][0]).toContain("/remove-cache"); | ||
expect(fetchGet.mock.calls[0][0]).toContain(encodeURIComponent(parentFolder)); | ||
fetchGet.mockRestore(); | ||
}); | ||
|
||
it("should call FetchGet with the correct URL to index server API after setTimeout", () => { | ||
const fetchGet = jest.spyOn(global, "FetchGet").mockResolvedValue(); | ||
const setTimeoutSpy = jest.spyOn(global, "setTimeout"); | ||
const parentFolder = "/parent"; | ||
const historyLocationSearch = "search"; | ||
RemoveCache(jest.fn(), parentFolder, historyLocationSearch, jest.fn(), jest.fn()); | ||
|
||
expect(setTimeoutSpy).toHaveBeenCalledWith(expect.any(Function), 600); | ||
jest.runAllTimers(); | ||
|
||
expect(fetchGet).toHaveBeenCalledWith(expect.any(String)); | ||
expect(fetchGet.mock.calls[1][0]).toContain("/index-server-api"); | ||
expect(fetchGet.mock.calls[1][0]).toContain(historyLocationSearch); | ||
|
||
fetchGet.mockRestore(); | ||
setTimeoutSpy.mockRestore(); | ||
}); | ||
|
||
it("should dispatch force-reset action when payload has fileIndexItems", async () => { | ||
const dispatch = jest.fn(); | ||
const mediaArchiveData = { | ||
data: { | ||
fileIndexItems: [{}, {}] // Mocking fileIndexItems | ||
} | ||
}; | ||
const fetchGet = jest.spyOn(global, "FetchGet").mockResolvedValue(mediaArchiveData); | ||
await RemoveCache(jest.fn(), "/parent", "search", dispatch, jest.fn()); | ||
|
||
expect(dispatch).toHaveBeenCalledWith({ type: "force-reset", payload: mediaArchiveData.data }); | ||
fetchGet.mockRestore(); | ||
}); | ||
|
||
it("should call propsHandleExit after completing", async () => { | ||
const propsHandleExit = jest.fn(); | ||
const dispatch = jest.fn(); | ||
await RemoveCache(jest.fn(), "/parent", "search", dispatch, propsHandleExit); | ||
expect(propsHandleExit).toHaveBeenCalled(); | ||
}); | ||
}); |
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 { ArchiveAction } from "../../../../contexts/archive-context.tsx"; | ||
import { FileListCache } from "../../../../shared/filelist-cache.ts"; | ||
import FetchGet from "../../../../shared/fetch/fetch-get.ts"; | ||
import { UrlQuery } from "../../../../shared/url/url-query.ts"; | ||
import { URLPath } from "../../../../shared/url/url-path.ts"; | ||
import { CastToInterface } from "../../../../shared/cast-to-interface.ts"; | ||
import { IArchiveProps } from "../../../../interfaces/IArchiveProps.ts"; | ||
import { Dispatch, SetStateAction } from "react"; | ||
|
||
/** | ||
* Remove Folder cache | ||
*/ | ||
export function RemoveCache( | ||
setIsLoading: Dispatch<SetStateAction<boolean>>, | ||
propsParentFolder: string, | ||
historyLocationSearch: string, | ||
dispatch: Dispatch<ArchiveAction>, | ||
propsHandleExit: () => {} | ||
) { | ||
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