2022-07-12 10:55:28 +02:00
|
|
|
import { config, shallowMount, VueWrapper } from "@vue/test-utils";
|
2020-12-03 17:22:05 +01:00
|
|
|
import CommentTree from "@/components/Comment/CommentTree.vue";
|
|
|
|
import {
|
|
|
|
createMockClient,
|
|
|
|
MockApolloClient,
|
|
|
|
RequestHandler,
|
|
|
|
} from "mock-apollo-client";
|
2021-06-04 20:24:43 +02:00
|
|
|
import {
|
|
|
|
COMMENTS_THREADS_WITH_REPLIES,
|
|
|
|
CREATE_COMMENT_FROM_EVENT,
|
|
|
|
} from "@/graphql/comment";
|
2020-12-03 17:22:05 +01:00
|
|
|
import { CommentModeration } from "@/types/enums";
|
|
|
|
import { IEvent } from "@/types/event.model";
|
|
|
|
import {
|
|
|
|
eventCommentThreadsMock,
|
2021-06-10 14:44:56 +02:00
|
|
|
eventNoCommentThreadsMock,
|
2020-12-03 17:22:05 +01:00
|
|
|
newCommentForEventMock,
|
|
|
|
newCommentForEventResponse,
|
|
|
|
} from "../../mocks/event";
|
2021-06-04 20:24:43 +02:00
|
|
|
import flushPromises from "flush-promises";
|
|
|
|
import { defaultResolvers } from "../../common";
|
2022-07-12 10:55:28 +02:00
|
|
|
import { afterEach, describe, vi, it, expect, beforeEach } from "vitest";
|
|
|
|
import { DefaultApolloClient } from "@vue/apollo-composable";
|
|
|
|
import Oruga from "@oruga-ui/oruga-next";
|
|
|
|
import { notifierPlugin } from "@/plugins/notifier";
|
|
|
|
import { InMemoryCache } from "@apollo/client/cache";
|
|
|
|
import { createRouter, createWebHistory, Router } from "vue-router";
|
|
|
|
import { routes } from "@/router";
|
|
|
|
import { dialogPlugin } from "@/plugins/dialog";
|
|
|
|
|
|
|
|
config.global.plugins.push(Oruga);
|
|
|
|
config.global.plugins.push(notifierPlugin);
|
|
|
|
config.global.plugins.push(dialogPlugin);
|
|
|
|
|
|
|
|
let router: Router;
|
2020-12-03 17:22:05 +01:00
|
|
|
|
|
|
|
const eventData = {
|
|
|
|
id: "1",
|
|
|
|
uuid: "e37910ea-fd5a-4756-7634-00971f3f4107",
|
|
|
|
options: {
|
|
|
|
commentModeration: CommentModeration.ALLOW_ALL,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
describe("CommentTree", () => {
|
2022-07-12 10:55:28 +02:00
|
|
|
let wrapper: VueWrapper;
|
2021-06-10 14:44:56 +02:00
|
|
|
let mockClient: MockApolloClient | null;
|
2020-12-03 17:22:05 +01:00
|
|
|
let requestHandlers: Record<string, RequestHandler>;
|
|
|
|
|
2022-07-12 10:55:28 +02:00
|
|
|
const generateWrapper = (handlers = {}, extraProps = {}) => {
|
|
|
|
const cache = new InMemoryCache({ addTypename: true });
|
|
|
|
|
2020-12-03 17:22:05 +01:00
|
|
|
mockClient = createMockClient({
|
2022-07-12 10:55:28 +02:00
|
|
|
cache,
|
2021-06-04 20:24:43 +02:00
|
|
|
resolvers: defaultResolvers,
|
2020-12-03 17:22:05 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
requestHandlers = {
|
2022-07-12 10:55:28 +02:00
|
|
|
eventCommentThreadsQueryHandler: vi
|
2020-12-03 17:22:05 +01:00
|
|
|
.fn()
|
|
|
|
.mockResolvedValue(eventCommentThreadsMock),
|
2022-07-12 10:55:28 +02:00
|
|
|
createCommentForEventMutationHandler: vi
|
2020-12-03 17:22:05 +01:00
|
|
|
.fn()
|
|
|
|
.mockResolvedValue(newCommentForEventResponse),
|
|
|
|
...handlers,
|
|
|
|
};
|
|
|
|
|
|
|
|
mockClient.setRequestHandler(
|
2021-06-04 20:24:43 +02:00
|
|
|
COMMENTS_THREADS_WITH_REPLIES,
|
2020-12-03 17:22:05 +01:00
|
|
|
requestHandlers.eventCommentThreadsQueryHandler
|
|
|
|
);
|
|
|
|
mockClient.setRequestHandler(
|
|
|
|
CREATE_COMMENT_FROM_EVENT,
|
|
|
|
requestHandlers.createCommentForEventMutationHandler
|
|
|
|
);
|
|
|
|
wrapper = shallowMount(CommentTree, {
|
2022-07-12 10:55:28 +02:00
|
|
|
props: {
|
2020-12-03 17:22:05 +01:00
|
|
|
event: { ...eventData },
|
2022-07-12 10:55:28 +02:00
|
|
|
...extraProps,
|
2020-12-03 17:22:05 +01:00
|
|
|
},
|
2022-07-12 10:55:28 +02:00
|
|
|
global: {
|
|
|
|
provide: {
|
|
|
|
[DefaultApolloClient]: mockClient,
|
|
|
|
},
|
|
|
|
plugins: [router],
|
2020-12-03 17:22:05 +01:00
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2022-07-12 10:55:28 +02:00
|
|
|
beforeEach(async () => {
|
|
|
|
router = createRouter({
|
|
|
|
history: createWebHistory(),
|
|
|
|
routes: routes,
|
|
|
|
});
|
|
|
|
|
|
|
|
// await router.isReady();
|
|
|
|
});
|
|
|
|
|
2021-08-02 19:26:23 +02:00
|
|
|
afterEach(() => {
|
|
|
|
mockClient = null;
|
|
|
|
requestHandlers = {};
|
2022-07-12 10:55:28 +02:00
|
|
|
wrapper.unmount();
|
2021-08-02 19:26:23 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
it("renders a loading comment tree", async () => {
|
|
|
|
generateWrapper();
|
|
|
|
|
2022-07-12 10:55:28 +02:00
|
|
|
expect(wrapper.find("p.text-center").text()).toBe("Loading comments…");
|
2021-08-02 19:26:23 +02:00
|
|
|
|
|
|
|
expect(wrapper.html()).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
|
2021-06-04 20:24:43 +02:00
|
|
|
it("renders a comment tree with comments", async () => {
|
2020-12-03 17:22:05 +01:00
|
|
|
generateWrapper();
|
|
|
|
|
|
|
|
expect(wrapper.exists()).toBe(true);
|
2020-12-07 14:48:48 +01:00
|
|
|
expect(
|
|
|
|
requestHandlers.eventCommentThreadsQueryHandler
|
|
|
|
).toHaveBeenCalledWith({ eventUUID: eventData.uuid });
|
2022-07-12 10:55:28 +02:00
|
|
|
await flushPromises();
|
|
|
|
expect(wrapper.find("p.text-center").exists()).toBe(false);
|
|
|
|
|
2023-11-15 12:29:39 +01:00
|
|
|
expect(wrapper.findAllComponents("event-comment-stub").length).toBe(2);
|
2020-12-03 17:22:05 +01:00
|
|
|
expect(wrapper.html()).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("renders the form if we can comment", async () => {
|
|
|
|
generateWrapper(
|
|
|
|
{},
|
|
|
|
{
|
|
|
|
newComment: {
|
|
|
|
text: newCommentForEventMock.text,
|
2022-07-12 10:55:28 +02:00
|
|
|
isAnnouncement: false,
|
2023-11-15 12:29:39 +01:00
|
|
|
insertedAt: "2021-12-03T13:02:00Z",
|
|
|
|
updatedAt: "2021-12-03T13:02:00Z",
|
|
|
|
publishedAt: "2021-12-03T13:02:00Z",
|
2020-12-03 17:22:05 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2021-06-04 20:24:43 +02:00
|
|
|
await flushPromises();
|
2020-12-03 17:22:05 +01:00
|
|
|
|
2022-07-12 10:55:28 +02:00
|
|
|
expect(wrapper.find("form").isVisible()).toBe(true);
|
2023-11-15 12:29:39 +01:00
|
|
|
expect(wrapper.findAllComponents("event-comment-stub").length).toBe(2);
|
2022-07-12 10:55:28 +02:00
|
|
|
wrapper.getComponent({ ref: "commenteditor" });
|
2020-12-03 17:22:05 +01:00
|
|
|
|
2022-07-12 10:55:28 +02:00
|
|
|
wrapper.find("form").trigger("submit");
|
2020-12-03 17:22:05 +01:00
|
|
|
|
2022-07-12 10:55:28 +02:00
|
|
|
await flushPromises();
|
2020-12-03 17:22:05 +01:00
|
|
|
expect(
|
|
|
|
requestHandlers.createCommentForEventMutationHandler
|
|
|
|
).toHaveBeenCalledWith({
|
|
|
|
...newCommentForEventMock,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (mockClient) {
|
|
|
|
const cachedData = mockClient.cache.readQuery<{ event: IEvent }>({
|
2021-06-04 20:24:43 +02:00
|
|
|
query: COMMENTS_THREADS_WITH_REPLIES,
|
2020-12-03 17:22:05 +01:00
|
|
|
variables: {
|
|
|
|
eventUUID: eventData.uuid,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
if (cachedData) {
|
|
|
|
const { event } = cachedData;
|
|
|
|
|
|
|
|
expect(event.comments).toHaveLength(3);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2021-06-10 14:44:56 +02:00
|
|
|
|
|
|
|
it("renders an empty comment tree", async () => {
|
|
|
|
generateWrapper({
|
2022-07-12 10:55:28 +02:00
|
|
|
eventCommentThreadsQueryHandler: vi
|
2021-06-10 14:44:56 +02:00
|
|
|
.fn()
|
|
|
|
.mockResolvedValue(eventNoCommentThreadsMock),
|
|
|
|
});
|
|
|
|
|
|
|
|
await flushPromises();
|
2021-08-02 19:26:23 +02:00
|
|
|
expect(
|
|
|
|
requestHandlers.eventCommentThreadsQueryHandler
|
|
|
|
).toHaveBeenCalledWith({ eventUUID: eventData.uuid });
|
2021-06-10 14:44:56 +02:00
|
|
|
|
2022-07-12 10:55:28 +02:00
|
|
|
expect(wrapper.findComponent({ name: "EmptyContent" }).exists());
|
2021-06-10 14:44:56 +02:00
|
|
|
expect(wrapper.html()).toMatchSnapshot();
|
|
|
|
});
|
2020-12-03 17:22:05 +01:00
|
|
|
});
|