From 3eae315cbbab7a8b74da75ee660da8f5e728f898 Mon Sep 17 00:00:00 2001 From: davelopez <46503462+davelopez@users.noreply.github.com> Date: Fri, 8 Dec 2023 16:42:49 +0100 Subject: [PATCH] Add more tests for keyedCache --- client/src/composables/keyedCache.test.ts | 100 +++++++++++++++++++++- 1 file changed, 99 insertions(+), 1 deletion(-) diff --git a/client/src/composables/keyedCache.test.ts b/client/src/composables/keyedCache.test.ts index 513e5642d500..9b05b308a5c0 100644 --- a/client/src/composables/keyedCache.test.ts +++ b/client/src/composables/keyedCache.test.ts @@ -1,4 +1,5 @@ import flushPromises from "flush-promises"; +import { computed, ref } from "vue"; import { useKeyedCache } from "./keyedCache"; @@ -64,7 +65,7 @@ describe("useSimpleKeyStore", () => { const apiResponse = { data: item }; fetchItem.mockResolvedValue(apiResponse); - shouldFetch.mockReturnValue(true); + shouldFetch.mockReturnValue(() => true); const { storedItems, getItemById, isLoadingItem } = useKeyedCache(fetchItem, shouldFetch); @@ -79,5 +80,102 @@ describe("useSimpleKeyStore", () => { expect(isLoadingItem.value(id)).toBeFalsy(); expect(storedItems.value[id]).toEqual(item); expect(fetchItem).toHaveBeenCalledWith(fetchParams); + expect(shouldFetch).toHaveBeenCalled(); + }); + + it("should not fetch the item if it is already being fetched", async () => { + const id = "1"; + const item = { id: id, name: "Item 1" }; + const fetchParams = { id: id }; + const apiResponse = { data: item }; + + fetchItem.mockResolvedValue(apiResponse); + + const { storedItems, getItemById, isLoadingItem } = useKeyedCache(fetchItem); + + expect(isLoadingItem.value(id)).toBeFalsy(); + + getItemById.value(id); + getItemById.value(id); + + expect(isLoadingItem.value(id)).toBeTruthy(); + await flushPromises(); + expect(isLoadingItem.value(id)).toBeFalsy(); + expect(storedItems.value[id]).toEqual(item); + expect(fetchItem).toHaveBeenCalledTimes(1); + expect(fetchItem).toHaveBeenCalledWith(fetchParams); + }); + + it("should not fetch the item if it is already being fetched, even if shouldFetch returns true", async () => { + const id = "1"; + const item = { id: id, name: "Item 1" }; + const fetchParams = { id: id }; + const apiResponse = { data: item }; + + fetchItem.mockResolvedValue(apiResponse); + shouldFetch.mockReturnValue(() => true); + + const { storedItems, getItemById, isLoadingItem } = useKeyedCache(fetchItem, shouldFetch); + + expect(isLoadingItem.value(id)).toBeFalsy(); + + getItemById.value(id); + getItemById.value(id); + + expect(isLoadingItem.value(id)).toBeTruthy(); + await flushPromises(); + expect(isLoadingItem.value(id)).toBeFalsy(); + expect(storedItems.value[id]).toEqual(item); + expect(fetchItem).toHaveBeenCalledTimes(1); + expect(fetchItem).toHaveBeenCalledWith(fetchParams); + expect(shouldFetch).toHaveBeenCalled(); + }); + + it("should accept a ref for fetchItem", async () => { + const id = "1"; + const item = { id: id, name: "Item 1" }; + const fetchParams = { id: id }; + const apiResponse = { data: item }; + + fetchItem.mockResolvedValue(apiResponse); + + const fetchItemRef = ref(fetchItem); + + const { storedItems, getItemById, isLoadingItem } = useKeyedCache(fetchItemRef); + + expect(isLoadingItem.value(id)).toBeFalsy(); + + getItemById.value(id); + + expect(isLoadingItem.value(id)).toBeTruthy(); + await flushPromises(); + expect(isLoadingItem.value(id)).toBeFalsy(); + expect(storedItems.value[id]).toEqual(item); + expect(fetchItem).toHaveBeenCalledWith(fetchParams); + }); + + it("should accept a computed for shouldFetch", async () => { + const id = "1"; + const item = { id: id, name: "Item 1" }; + const fetchParams = { id: id }; + const apiResponse = { data: item }; + + fetchItem.mockResolvedValue(apiResponse); + shouldFetch.mockReturnValue(true); + + const shouldFetchComputed = computed(() => shouldFetch); + + const { storedItems, getItemById, isLoadingItem } = useKeyedCache(fetchItem, shouldFetchComputed); + + expect(isLoadingItem.value(id)).toBeFalsy(); + + getItemById.value(id); + + expect(isLoadingItem.value(id)).toBeTruthy(); + await flushPromises(); + expect(isLoadingItem.value(id)).toBeFalsy(); + expect(storedItems.value[id]).toEqual(item); + expect(fetchItem).toHaveBeenCalledWith(fetchParams); + expect(shouldFetch).toHaveBeenCalled(); }); });