From d80738f63812a00d631f9851eecfd3e32172ab28 Mon Sep 17 00:00:00 2001 From: Christian Darsow Date: Mon, 4 Sep 2023 16:00:28 +0200 Subject: [PATCH] Improve composable tests --- ...sionContentElementState.composable.unit.ts | 94 +++++++++++++------ 1 file changed, 63 insertions(+), 31 deletions(-) diff --git a/src/components/feature-board-submission-element/SubmissionContentElementState.composable.unit.ts b/src/components/feature-board-submission-element/SubmissionContentElementState.composable.unit.ts index 7fc5c0781a..a1589552fc 100644 --- a/src/components/feature-board-submission-element/SubmissionContentElementState.composable.unit.ts +++ b/src/components/feature-board-submission-element/SubmissionContentElementState.composable.unit.ts @@ -5,6 +5,7 @@ import { I18N_KEY, NOTIFIER_MODULE_KEY } from "@/utils/inject"; import NotifierModule from "@/store/notifier"; import { createModuleMocks } from "@/utils/mock-store-module"; import { SubmissionItemResponse } from "@/serverApi/v3"; +import { nextTick } from "vue"; const notifierModule = createModuleMocks(NotifierModule); @@ -20,31 +21,38 @@ const mockedSubmissionItems: Array = [ }, ]; jest.mock("./SubmissionItemApi.composable"); - -const mockedUseSubmissionItemApi = jest.mocked(useSubmissionItemApi); const mockedCreateSubmissionItemCall = jest.fn(); const mockedUpdateSubmissionItemCall = jest.fn(); const mockedFetchSubmissionItemsCall = jest.fn(); -mockedFetchSubmissionItemsCall.mockReturnValue(mockedSubmissionItems); - -const mocks = { - createSubmissionItemCall: mockedCreateSubmissionItemCall, - updateSubmissionItemCall: mockedUpdateSubmissionItemCall, - fetchSubmissionItemsCall: mockedFetchSubmissionItemsCall, -}; -mockedUseSubmissionItemApi.mockReturnValue(mocks); - -const setup = (contentElementId = "123123") => { - return mountComposable( - () => useSubmissionContentElementState(contentElementId), - { - [I18N_KEY.valueOf()]: { t: (key: string) => key }, - [NOTIFIER_MODULE_KEY.valueOf()]: notifierModule, - } - ); -}; - -describe("SubmissionContentElementState composable", () => { + +describe("SubmissionContentElementState.composable", () => { + const setup = ( + contentElementId = "123123", + submissionItems: Array = mockedSubmissionItems + ) => { + const mockedUseSubmissionItemApi = jest.mocked(useSubmissionItemApi); + + mockedFetchSubmissionItemsCall.mockReturnValue(submissionItems); + + mockedUseSubmissionItemApi.mockReturnValue({ + createSubmissionItemCall: mockedCreateSubmissionItemCall, + updateSubmissionItemCall: mockedUpdateSubmissionItemCall, + fetchSubmissionItemsCall: mockedFetchSubmissionItemsCall, + }); + + return mountComposable( + () => useSubmissionContentElementState(contentElementId), + { + [I18N_KEY.valueOf()]: { t: (key: string) => key }, + [NOTIFIER_MODULE_KEY.valueOf()]: notifierModule, + } + ); + }; + + afterEach(() => { + jest.resetAllMocks(); + }); + it("should fetch submission items on mount", async () => { const contentElementId = "123124"; @@ -71,15 +79,39 @@ describe("SubmissionContentElementState composable", () => { expect(submissionItems.value.length).toEqual(mockedSubmissionItems.length); }); - it("should return update function that updates completed state", async () => { - const contentElementId = "123124"; - const { updateSubmissionItem } = setup(contentElementId); + describe("if the student created a submission item before", () => { + it("should return update function that updates the completed state", async () => { + const contentElementId = "123124"; + const { fetchSubmissionItems, updateSubmissionItem } = + setup(contentElementId); - const completed = true; - await updateSubmissionItem(completed); - expect(mockedCreateSubmissionItemCall).toHaveBeenLastCalledWith( - contentElementId, - completed - ); + await fetchSubmissionItems(contentElementId); + + const completed = true; + await updateSubmissionItem(completed); + expect(mockedUpdateSubmissionItemCall).toHaveBeenLastCalledWith( + mockedSubmissionItems[0].id, + completed + ); + }); + }); + + describe("if the student did not create a submission item so far", () => { + it("should return update function that creates initial completed state", async () => { + const contentElementId = "123124"; + const { fetchSubmissionItems, updateSubmissionItem } = setup( + contentElementId, + [] + ); + + await fetchSubmissionItems(contentElementId); + + const completed = true; + await updateSubmissionItem(completed); + expect(mockedCreateSubmissionItemCall).toHaveBeenLastCalledWith( + contentElementId, + completed + ); + }); }); });