Skip to content

Commit

Permalink
add tests for moveCard
Browse files Browse the repository at this point in the history
  • Loading branch information
NFriedo committed Apr 17, 2024
1 parent d0559a7 commit 55f49b6
Show file tree
Hide file tree
Showing 2 changed files with 176 additions and 5 deletions.
5 changes: 1 addition & 4 deletions src/modules/data/board/boardActions/restApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,7 @@ export const useBoardRestApi = () => {
targetColumnIndex: number,
fromColumnIndex: number
) => {
const { cardId, newIndex, oldIndex } = payload;

if (cardId === undefined) return false; // ensure values are set
const { newIndex, oldIndex } = payload;

const movedInsideColumn = fromColumnIndex === targetColumnIndex;
if (movedInsideColumn) {
Expand All @@ -194,7 +192,6 @@ export const useBoardRestApi = () => {
return true;
};

// TODO: investigate if move card with creating new column should be separated
const moveCardRequest = async (
action: ReturnType<typeof BoardActions.moveCardRequest>
): Promise<void> => {
Expand Down
176 changes: 175 additions & 1 deletion src/modules/data/board/boardActions/restApi.unit.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useErrorHandler } from "@/components/error-handling/ErrorHandler.composable";
import { useBoardStore } from "@data-board";
import { boardActions, useBoardStore } from "@data-board";
import { createMock, DeepMocked } from "@golevelup/ts-jest";
import { setActivePinia } from "pinia";
import { useBoardRestApi } from "./restApi";
Expand All @@ -15,6 +15,7 @@ import {
import { createTestingPinia } from "@pinia/testing";
import { cardResponseFactory } from "@@/tests/test-utils/factory/cardResponseFactory";
import { HttpStatusCode } from "@/store/types/http-status-code.enum";
import { CardMove } from "@/types/board/DragAndDrop";

jest.mock("@/components/error-handling/ErrorHandler.composable");
const mockedUseErrorHandler = jest.mocked(useErrorHandler);
Expand Down Expand Up @@ -154,4 +155,177 @@ describe("restApi", () => {
);
});
});

describe("createColumnRequest", () => {
it("should return if board is undefined", async () => {
const { boardStore } = setup(false);
const { createColumnRequest } = useBoardRestApi();

await createColumnRequest();

expect(boardStore.dispatch).not.toHaveBeenCalled();
});

it("should dispatch createColumnSuccess action if the API call is successful", async () => {
const { boardStore } = setup();
const { createColumnRequest } = useBoardRestApi();
const { setEditModeId } = useSharedEditMode();

const newColumn = columnResponseFactory.build();
mockedBoardApiCalls.createColumnCall.mockResolvedValue(newColumn);

const result = await createColumnRequest();

expect(setEditModeId).toHaveBeenCalledWith(newColumn.id);
expect(boardStore.dispatch).toHaveBeenCalledWith(
BoardActions.createColumnSuccess({ newColumn })
);
expect(result).toEqual(newColumn);
});

it("should dispatch notifyWithTemplateAndReload action if the API call fails", async () => {
const { boardStore } = setup();
const { createColumnRequest } = useBoardRestApi();

const expectedError = new Error("createColumnCall error");
mockedBoardApiCalls.createColumnCall.mockRejectedValue(expectedError);

await createColumnRequest();

expect(boardStore.dispatch).toHaveBeenCalledWith(
BoardActions.notifyWithTemplateAndReload({
error: expectedError,
errorType: "notCreated",
httpStatus: HttpStatusCode.NotFound,
boardObjectType: "boardColumn",
})
);
});
});

describe("deleteCardRequest", () => {
it("should return if board is undefined", async () => {
const { boardStore } = setup(false);
const { deleteCardRequest } = useBoardRestApi();

await deleteCardRequest(
boardActions.deleteCardRequest({ cardId: "cardId" })
);

expect(boardStore.dispatch).not.toHaveBeenCalled();
});

it("should dispatch deleteCardSuccess action if the API call is successful", async () => {
const { boardStore } = setup();
const { deleteCardRequest } = useBoardRestApi();
const cardId = boardStore.board!.columns[0].cards[0].cardId;

await deleteCardRequest(boardActions.deleteCardRequest({ cardId }));

expect(boardStore.dispatch).toHaveBeenCalledWith(
BoardActions.deleteCardSuccess({ cardId })
);
});

it("should dispatch notifyWithTemplateAndReload action if the API call fails", async () => {
const { boardStore } = setup();
const { deleteCardRequest } = useBoardRestApi();
const cardId = boardStore.board!.columns[0].cards[0].cardId;

const expectedError = new Error("deleteCardCall error");
mockedBoardApiCalls.deleteCardCall.mockRejectedValue(expectedError);

await deleteCardRequest(boardActions.deleteCardRequest({ cardId }));

expect(boardStore.dispatch).toHaveBeenCalledWith(
BoardActions.notifyWithTemplateAndReload({
error: expectedError,
errorType: "notDeleted",
httpStatus: HttpStatusCode.NotFound,
boardObjectType: "boardCard",
})
);
});
});

describe("deleteColumnRequest", () => {
it("should return if board is undefined", async () => {
const { boardStore } = setup(false);
const { deleteColumnRequest } = useBoardRestApi();

await deleteColumnRequest(
boardActions.deleteColumnRequest({ columnId: "columnId" })
);

expect(boardStore.dispatch).not.toHaveBeenCalled();
});

it("should dispatch deleteColumnSuccess action if the API call is successful", async () => {
const { boardStore } = setup();
const { deleteColumnRequest } = useBoardRestApi();
const columnId = boardStore.board!.columns[0].id;

await deleteColumnRequest(boardActions.deleteColumnRequest({ columnId }));

expect(boardStore.dispatch).toHaveBeenCalledWith(
BoardActions.deleteColumnSuccess({ columnId })
);
});

it("should dispatch notifyWithTemplateAndReload action if the API call fails", async () => {
const { boardStore } = setup();
const { deleteColumnRequest } = useBoardRestApi();
const columnId = boardStore.board!.columns[0].id;

const expectedError = new Error("deleteColumnCall error");
mockedBoardApiCalls.deleteColumnCall.mockRejectedValue(expectedError);

await deleteColumnRequest(boardActions.deleteColumnRequest({ columnId }));

expect(boardStore.dispatch).toHaveBeenCalledWith(
BoardActions.notifyWithTemplateAndReload({
error: expectedError,
errorType: "notDeleted",
httpStatus: HttpStatusCode.NotFound,
boardObjectType: "boardColumn",
})
);
});
});

describe("moveCardRequest", () => {
test.todo("should return if board is undefined");
test.todo(
"should dispatch moveCardSuccess action if the API call is successful"
);
test.todo(
"should dispatch notifyWithTemplateAndReload action if the API call fails"
);
describe("move is invalid", () => {
it("should not call moveCardCall if card is moved to the same position", async () => {
const { boardStore } = setup();
const { moveCardRequest } = useBoardRestApi();
const card = boardStore.board!.columns[0].cards[0];

const cardMove: CardMove = {
cardId: card.cardId,
oldIndex: 1,
newIndex: 1,
fromColumnId: boardStore.board!.columns[0].id,
toColumnId: boardStore.board!.columns[0].id,
};

await moveCardRequest(boardActions.moveCardRequest(cardMove));

expect(mockedBoardApiCalls.moveCardCall).not.toHaveBeenCalled();
});

test.todo(
"should not call moveCardCall if first card is moved to the first position"
);
test.todo(
"should not call moveCardCall if last card is moved to the last position"
);
});
});
});

0 comments on commit 55f49b6

Please sign in to comment.