Skip to content

Commit

Permalink
add unit tests for composable file
Browse files Browse the repository at this point in the history
  • Loading branch information
muratmerdoglu-dp committed Dec 13, 2024
1 parent b865885 commit 4ae07d6
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 30 deletions.
77 changes: 56 additions & 21 deletions src/modules/data/room/roomMembers/roomMembers.composable.unit.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import {
roomMemberListFactory,
mockApiResponse,
roomMemberResponseFactory,
roomMemberFactory,
roomMemberSchoolResponseFactory,
schoolFactory,
meResponseFactory,
} from "@@/tests/test-utils";
import { createMock, DeepMocked } from "@golevelup/ts-jest";
import * as serverApi from "@/serverApi/v3/api";
Expand All @@ -18,8 +19,9 @@ import {
UserIdAndRoleRoleNameEnum,
} from "@/serverApi/v3/api";
import { useBoardNotifier } from "@util-board";
import { schoolsModule } from "@/store";
import { schoolsModule, authModule } from "@/store";
import SchoolsModule from "@/store/schools";
import AuthModule from "@/store/auth";
import setupStores from "@@/tests/test-utils/setupStores";

jest.mock("vue-i18n");
Expand Down Expand Up @@ -54,6 +56,7 @@ describe("useRoomMembers", () => {

setupStores({
schoolsModule: SchoolsModule,
authModule: AuthModule,
});

schoolsModule.setSchool(
Expand All @@ -62,6 +65,9 @@ describe("useRoomMembers", () => {
name: "Paul-Gerhardt-Gymnasium",
})
);

const mockMe = meResponseFactory.build();
authModule.setMe(mockMe);
});

afterEach(() => {
Expand All @@ -70,25 +76,52 @@ describe("useRoomMembers", () => {
});

describe("fetchMembers", () => {
it("should fetch members and map members with role names", async () => {
const { fetchMembers, roomMembers } = useRoomMembers(roomId);
const membersMock = roomMemberResponseFactory.buildList(3);
describe("when the user is not room owner", () => {
it("should fetch members and map members with role names", async () => {
const { fetchMembers, roomMembers } = useRoomMembers(roomId);
const membersMock = roomMemberFactory(RoleName.Roomeditor).buildList(3);

roomApiMock.roomControllerGetMembers.mockResolvedValue(
mockApiResponse({
data: { data: membersMock },
})
);

await fetchMembers();

expect(roomMembers.value).toEqual(
membersMock.map((member) => ({
...member,
displayRoleName: "common.labels.teacher",
isSelectable: true,
}))
);
});
});

roomApiMock.roomControllerGetMembers.mockResolvedValue(
mockApiResponse({
data: { data: membersMock },
})
);
describe("when the user is room owner", () => {
it("should fetch members and map members with role names", async () => {
const { fetchMembers, roomMembers } = useRoomMembers(roomId);
const membersMock = roomMemberFactory(RoleName.Roomowner).buildList(3);

await fetchMembers();

expect(roomMembers.value).toEqual(
membersMock.map((member) => ({
...member,
displayRoleName: "common.labels.teacher",
isSelectable: true,
}))
);
roomApiMock.roomControllerGetMembers.mockResolvedValue(
mockApiResponse({
data: {
data: membersMock,
},
})
);

await fetchMembers();

expect(roomMembers.value).toEqual(
membersMock.map((member) => ({
...member,
displayRoleName: "common.labels.teacher",
isSelectable: false,
}))
);
});
});

it("should throw an error if the API call fails", async () => {
Expand Down Expand Up @@ -152,7 +185,9 @@ describe("useRoomMembers", () => {
const { getPotentialMembers, potentialRoomMembers, roomMembers } =
useRoomMembers(roomId);

const membersMock: RoomMemberResponse = roomMemberResponseFactory.build();
const membersMock: RoomMemberResponse = roomMemberFactory(
RoleName.Roomeditor
).build();

roomMembers.value = [membersMock];

Expand Down Expand Up @@ -281,7 +316,7 @@ describe("useRoomMembers", () => {
mockApiResponse({})
);

const membersMock = roomMemberResponseFactory.buildList(3);
const membersMock = roomMemberFactory(RoleName.Roomeditor).buildList(3);
roomMembers.value = membersMock;

const firstMember = membersMock[0];
Expand Down
5 changes: 3 additions & 2 deletions src/modules/feature/room/RoomMembers/MembersTable.unit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import {
import MembersTable from "./MembersTable.vue";
import { ref } from "vue";
import { mdiMenuDown, mdiMenuUp, mdiMagnify } from "@icons/material";
import { roomMemberResponseFactory } from "@@/tests/test-utils";
import { roomMemberFactory } from "@@/tests/test-utils";
import { DOMWrapper, VueWrapper } from "@vue/test-utils";
import { VDataTable, VTextField } from "vuetify/lib/components/index.mjs";
import { useConfirmationDialog } from "@ui-confirmation-dialog";
import setupConfirmationComposableMock from "@@/tests/test-utils/composable-mocks/setupConfirmationComposableMock";
import { RoleName } from "@/serverApi/v3";

jest.mock("@ui-confirmation-dialog");
const mockedUseRemoveConfirmationDialog = jest.mocked(useConfirmationDialog);
Expand Down Expand Up @@ -37,7 +38,7 @@ describe("MembersTable", () => {
];

const setup = () => {
const mockMembers = roomMemberResponseFactory.buildList(3);
const mockMembers = roomMemberFactory(RoleName.Roomeditor).buildList(3);
const wrapper = mount(MembersTable, {
attachTo: document.body,
global: {
Expand Down
4 changes: 2 additions & 2 deletions src/modules/page/room/RoomMembers.page.unit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { createTestingPinia } from "@pinia/testing";
import {
roomMemberListFactory,
mockedPiniaStoreTyping,
roomMemberResponseFactory,
roomMemberFactory,
roomMemberSchoolResponseFactory,
} from "@@/tests/test-utils";
import { useRoomMembers, useRoomDetailsStore } from "@data-room";
Expand Down Expand Up @@ -76,7 +76,7 @@ describe("RoomMembersPage", () => {

const room = createRoom ? buildRoom() : undefined;

const members = roomMemberResponseFactory.buildList(3);
const members = roomMemberFactory(RoleName.Roomeditor).buildList(3);
mockRoomMemberCalls.roomMembers = ref(members);

const wrapper = mount(RoomMembersPage, {
Expand Down
9 changes: 4 additions & 5 deletions tests/test-utils/factory/room/roomMembersFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,15 @@ import {
} from "@/serverApi/v3";
import { RoomMember } from "@data-room";

export const roomMemberResponseFactory = Factory.define<RoomMemberResponse>(
({ sequence }) => ({
export const roomMemberFactory = (roleName = RoleName.Roomowner) =>
Factory.define<RoomMemberResponse>(({ sequence }) => ({
userId: `member${sequence}`,
firstName: `firstName${sequence}`,
lastName: `lastName${sequence}`,
roleName: RoleName.Roomeditor,
roleName,
displayRoleName: RoleName.Teacher,
schoolName: "Paul-Gerhardt-Gymnasium",
})
);
}));

export const roomMemberListFactory = Factory.define<RoomMember>(
({ sequence }) => ({
Expand Down

0 comments on commit 4ae07d6

Please sign in to comment.