Correct commentTree failure unit-test - issue #1776

This commit is contained in:
Laurent GAY
2025-07-07 16:14:13 +02:00
parent fc2ee69505
commit 4fd9e3bc86
6 changed files with 135 additions and 42 deletions

View File

@@ -1,24 +1,73 @@
import { CURRENT_ACTOR_CLIENT } from "@/graphql/actor";
import { CURRENT_USER_LOCATION_CLIENT } from "@/graphql/location";
import { CURRENT_USER_CLIENT } from "@/graphql/user";
import { ICurrentUserRole } from "@/types/enums"; import { ICurrentUserRole } from "@/types/enums";
import {
ApolloCache,
NormalizedCacheObject,
Resolver,
type Resolvers,
} from "@apollo/client";
export const defaultResolvers = { export const fakeCurrentUserData: Resolver = (): Record<string, any> => {
Query: { return {
currentUser: (): Record<string, any> => ({ __typename: "CurrentUser",
email: "user@mail.com", id: "2",
id: "2", email: "user@mail.com",
role: ICurrentUserRole.USER, role: ICurrentUserRole.USER,
isLoggedIn: true, isLoggedIn: true,
__typename: "CurrentUser", };
}),
currentActor: (): Record<string, any> => ({
id: "67",
preferredUsername: "someone",
name: "Personne",
avatar: null,
__typename: "CurrentActor",
}),
},
}; };
export const fakeCurrentActorData: Resolver = (): Record<string, any> => {
return {
__typename: "CurrentActor",
id: "67",
preferredUsername: "someone",
name: "Personne",
avatar: null,
};
};
export function defaultResolvers(
cache: ApolloCache<NormalizedCacheObject>
): Resolvers {
cache?.writeQuery({
query: CURRENT_USER_CLIENT,
data: {
currentUser: fakeCurrentUserData(),
},
});
cache?.writeQuery({
query: CURRENT_ACTOR_CLIENT,
data: {
currentActor: fakeCurrentActorData(),
},
});
cache?.writeQuery({
query: CURRENT_USER_LOCATION_CLIENT,
data: {
currentUserLocation: {
lat: null,
lon: null,
accuracy: null,
isIPLocation: null,
name: null,
picture: null,
},
},
});
return {
Query: {
currentUser: fakeCurrentUserData,
currentActor: fakeCurrentActorData,
},
} satisfies Resolvers;
}
export const nullMock = { export const nullMock = {
data: {}, data: {},
}; };

View File

@@ -12,7 +12,6 @@ import {
import { CommentModeration } from "@/types/enums"; import { CommentModeration } from "@/types/enums";
import { IEvent } from "@/types/event.model"; import { IEvent } from "@/types/event.model";
import { import {
currentActorClientMock,
eventCommentThreadsMock, eventCommentThreadsMock,
eventNoCommentThreadsMock, eventNoCommentThreadsMock,
newCommentForEventMock, newCommentForEventMock,
@@ -28,7 +27,8 @@ import { InMemoryCache } from "@apollo/client/cache";
import { createRouter, createWebHistory, Router } from "vue-router"; import { createRouter, createWebHistory, Router } from "vue-router";
import { routes } from "@/router"; import { routes } from "@/router";
import { dialogPlugin } from "@/plugins/dialog"; import { dialogPlugin } from "@/plugins/dialog";
import { CURRENT_ACTOR_CLIENT } from "@/graphql/actor"; import { IDENTITIES } from "@/graphql/actor";
import { defaultIdentityMock } from "../../mocks/auth";
config.global.plugins.push(Oruga); config.global.plugins.push(Oruga);
config.global.plugins.push(notifierPlugin); config.global.plugins.push(notifierPlugin);
@@ -53,7 +53,7 @@ describe("CommentTree", () => {
mockClient = createMockClient({ mockClient = createMockClient({
cache, cache,
resolvers: defaultResolvers, resolvers: defaultResolvers(cache),
}); });
requestHandlers = { requestHandlers = {
@@ -63,9 +63,7 @@ describe("CommentTree", () => {
createCommentForEventMutationHandler: vi createCommentForEventMutationHandler: vi
.fn() .fn()
.mockResolvedValue(newCommentForEventResponse), .mockResolvedValue(newCommentForEventResponse),
getCurrentActorClientHandler: vi identityHandler: vi.fn().mockResolvedValue(defaultIdentityMock),
.fn()
.mockResolvedValue(currentActorClientMock),
...handlers, ...handlers,
}; };
@@ -77,10 +75,7 @@ describe("CommentTree", () => {
CREATE_COMMENT_FROM_EVENT, CREATE_COMMENT_FROM_EVENT,
requestHandlers.createCommentForEventMutationHandler requestHandlers.createCommentForEventMutationHandler
); );
mockClient.setRequestHandler( mockClient.setRequestHandler(IDENTITIES, requestHandlers.identityHandler);
CURRENT_ACTOR_CLIENT,
requestHandlers.getCurrentActorClientHandler
);
wrapper = shallowMount(CommentTree, { wrapper = shallowMount(CommentTree, {
props: { props: {
event: { ...eventData }, event: { ...eventData },

View File

@@ -1,5 +1,36 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
exports[`CommentTree > renders a comment tree with comments 1`] = `
"<div data-v-5d0380ab="">
<form data-v-5d0380ab="" class="mt-2">
<!--v-if-->
<article data-v-5d0380ab="" class="flex flex-wrap items-start gap-2">
<figure data-v-5d0380ab="" class="">
<identity-picker-wrapper-stub data-v-5d0380ab="" modelvalue="[object Object]" inline="false" masked="false"></identity-picker-wrapper-stub>
</figure>
<div data-v-5d0380ab="" class="flex-1">
<div data-v-5d0380ab="" class="flex flex-col gap-2">
<div data-v-5d0380ab="" class="editor-wrapper">
<editor-stub data-v-5d0380ab="" currentactor="[object Object]" mode="comment" modelvalue="" aria-label="Comment body" placeholder="Write a new comment"></editor-stub>
<!--v-if-->
</div>
<!--v-if-->
</div>
</div>
<div data-v-5d0380ab="" class="">
<o-button-stub data-v-5d0380ab="" tag="button" variant="primary" iconleft="send" rounded="false" expanded="false" disabled="false" outlined="false" loading="false" inverted="false" nativetype="submit" role="button" iconboth="false"></o-button-stub>
</div>
</article>
</form>
<transition-group-stub data-v-5d0380ab="" tag="div" name="comment-empty-list" appear="false" persisted="false" css="true" class="mt-2">
<transition-group-stub data-v-5d0380ab="" name="comment-list" tag="ul" appear="false" persisted="false" css="true" class="comment-list">
<event-comment-stub data-v-5d0380ab="" comment="[object Object]" event="[object Object]" currentactor="[object Object]" rootcomment="true" readonly="false" class="root-comment my-2"></event-comment-stub>
<event-comment-stub data-v-5d0380ab="" comment="[object Object]" event="[object Object]" currentactor="[object Object]" rootcomment="true" readonly="false" class="root-comment my-2"></event-comment-stub>
</transition-group-stub>
</transition-group-stub>
</div>"
`;
exports[`CommentTree > renders a loading comment tree 1`] = ` exports[`CommentTree > renders a loading comment tree 1`] = `
"<div data-v-5d0380ab=""> "<div data-v-5d0380ab="">
<!--v-if--> <!--v-if-->
@@ -9,7 +40,26 @@ exports[`CommentTree > renders a loading comment tree 1`] = `
exports[`CommentTree > renders an empty comment tree 1`] = ` exports[`CommentTree > renders an empty comment tree 1`] = `
"<div data-v-5d0380ab=""> "<div data-v-5d0380ab="">
<!--v-if--> <form data-v-5d0380ab="" class="mt-2">
<!--v-if-->
<article data-v-5d0380ab="" class="flex flex-wrap items-start gap-2">
<figure data-v-5d0380ab="" class="">
<identity-picker-wrapper-stub data-v-5d0380ab="" modelvalue="[object Object]" inline="false" masked="false"></identity-picker-wrapper-stub>
</figure>
<div data-v-5d0380ab="" class="flex-1">
<div data-v-5d0380ab="" class="flex flex-col gap-2">
<div data-v-5d0380ab="" class="editor-wrapper">
<editor-stub data-v-5d0380ab="" currentactor="[object Object]" mode="comment" modelvalue="" aria-label="Comment body" placeholder="Write a new comment"></editor-stub>
<!--v-if-->
</div>
<!--v-if-->
</div>
</div>
<div data-v-5d0380ab="" class="">
<o-button-stub data-v-5d0380ab="" tag="button" variant="primary" iconleft="send" rounded="false" expanded="false" disabled="false" outlined="false" loading="false" inverted="false" nativetype="submit" role="button" iconboth="false"></o-button-stub>
</div>
</article>
</form>
<transition-group-stub data-v-5d0380ab="" tag="div" name="comment-empty-list" appear="false" persisted="false" css="true" class="mt-2"> <transition-group-stub data-v-5d0380ab="" tag="div" name="comment-empty-list" appear="false" persisted="false" css="true" class="mt-2">
<empty-content-stub data-v-5d0380ab="" icon="comment" descriptionclasses="" inline="true" center="false"></empty-content-stub> <empty-content-stub data-v-5d0380ab="" icon="comment" descriptionclasses="" inline="true" center="false"></empty-content-stub>
</transition-group-stub> </transition-group-stub>

View File

@@ -78,7 +78,7 @@ describe("ParticipationWithoutAccount", () => {
) => { ) => {
mockClient = createMockClient({ mockClient = createMockClient({
cache, cache,
resolvers: defaultResolvers, resolvers: defaultResolvers(cache),
}); });
requestHandlers = { requestHandlers = {
anonymousActorIdQueryHandler: vi anonymousActorIdQueryHandler: vi

View File

@@ -1,3 +1,5 @@
import { fakeCurrentActorData } from "../common";
export const loginMock = { export const loginMock = {
email: "some@email.tld", email: "some@email.tld",
password: "somepassword", password: "somepassword",
@@ -42,3 +44,13 @@ export const nullIdentityMock = {
}, },
}, },
}; };
export const defaultIdentityMock = {
data: {
loggedUser: {
__typename: "loggedUser",
id: 1,
actors: [fakeCurrentActorData()],
},
},
};

View File

@@ -51,19 +51,6 @@ export const joinEventResponseMock = {
}, },
}; };
export const currentActorClientMock = {
data: {
actor: {
__typename: "Person",
preferredUsername: "some_actor",
name: "Some actor",
avatar: null,
domain: null,
id: "1",
},
},
};
export const joinEventMock = { export const joinEventMock = {
eventId: "1", eventId: "1",
actorId: "1", actorId: "1",