diff --git a/tests/unit/specs/common.ts b/tests/unit/specs/common.ts index 4f172c0c4..a15b0ea7a 100644 --- a/tests/unit/specs/common.ts +++ b/tests/unit/specs/common.ts @@ -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 { + ApolloCache, + NormalizedCacheObject, + Resolver, + type Resolvers, +} from "@apollo/client"; -export const defaultResolvers = { - Query: { - currentUser: (): Record => ({ - email: "user@mail.com", - id: "2", - role: ICurrentUserRole.USER, - isLoggedIn: true, - __typename: "CurrentUser", - }), - currentActor: (): Record => ({ - id: "67", - preferredUsername: "someone", - name: "Personne", - avatar: null, - __typename: "CurrentActor", - }), - }, +export const fakeCurrentUserData: Resolver = (): Record => { + return { + __typename: "CurrentUser", + id: "2", + email: "user@mail.com", + role: ICurrentUserRole.USER, + isLoggedIn: true, + }; }; +export const fakeCurrentActorData: Resolver = (): Record => { + return { + __typename: "CurrentActor", + id: "67", + preferredUsername: "someone", + name: "Personne", + avatar: null, + }; +}; + +export function defaultResolvers( + cache: ApolloCache +): 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 = { data: {}, }; diff --git a/tests/unit/specs/components/Comment/CommentTree.spec.ts b/tests/unit/specs/components/Comment/CommentTree.spec.ts index d862043bc..e1ae6c517 100644 --- a/tests/unit/specs/components/Comment/CommentTree.spec.ts +++ b/tests/unit/specs/components/Comment/CommentTree.spec.ts @@ -12,7 +12,6 @@ import { import { CommentModeration } from "@/types/enums"; import { IEvent } from "@/types/event.model"; import { - currentActorClientMock, eventCommentThreadsMock, eventNoCommentThreadsMock, newCommentForEventMock, @@ -28,7 +27,8 @@ import { InMemoryCache } from "@apollo/client/cache"; import { createRouter, createWebHistory, Router } from "vue-router"; import { routes } from "@/router"; 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(notifierPlugin); @@ -53,7 +53,7 @@ describe("CommentTree", () => { mockClient = createMockClient({ cache, - resolvers: defaultResolvers, + resolvers: defaultResolvers(cache), }); requestHandlers = { @@ -63,9 +63,7 @@ describe("CommentTree", () => { createCommentForEventMutationHandler: vi .fn() .mockResolvedValue(newCommentForEventResponse), - getCurrentActorClientHandler: vi - .fn() - .mockResolvedValue(currentActorClientMock), + identityHandler: vi.fn().mockResolvedValue(defaultIdentityMock), ...handlers, }; @@ -77,10 +75,7 @@ describe("CommentTree", () => { CREATE_COMMENT_FROM_EVENT, requestHandlers.createCommentForEventMutationHandler ); - mockClient.setRequestHandler( - CURRENT_ACTOR_CLIENT, - requestHandlers.getCurrentActorClientHandler - ); + mockClient.setRequestHandler(IDENTITIES, requestHandlers.identityHandler); wrapper = shallowMount(CommentTree, { props: { event: { ...eventData }, diff --git a/tests/unit/specs/components/Comment/__snapshots__/CommentTree.spec.ts.snap b/tests/unit/specs/components/Comment/__snapshots__/CommentTree.spec.ts.snap index 53044db2f..901310f7d 100644 --- a/tests/unit/specs/components/Comment/__snapshots__/CommentTree.spec.ts.snap +++ b/tests/unit/specs/components/Comment/__snapshots__/CommentTree.spec.ts.snap @@ -1,5 +1,36 @@ // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html +exports[`CommentTree > renders a comment tree with comments 1`] = ` +"
+
+ +
+
+ +
+
+
+
+ + +
+ +
+
+
+ +
+
+
+ + + + + + +
" +`; + exports[`CommentTree > renders a loading comment tree 1`] = ` "
@@ -9,7 +40,26 @@ exports[`CommentTree > renders a loading comment tree 1`] = ` exports[`CommentTree > renders an empty comment tree 1`] = ` "
- +
+ +
+
+ +
+
+
+
+ + +
+ +
+
+
+ +
+
+
diff --git a/tests/unit/specs/components/Participation/ParticipationWithoutAccount.spec.ts b/tests/unit/specs/components/Participation/ParticipationWithoutAccount.spec.ts index 1fb725574..2fa4329d9 100644 --- a/tests/unit/specs/components/Participation/ParticipationWithoutAccount.spec.ts +++ b/tests/unit/specs/components/Participation/ParticipationWithoutAccount.spec.ts @@ -78,7 +78,7 @@ describe("ParticipationWithoutAccount", () => { ) => { mockClient = createMockClient({ cache, - resolvers: defaultResolvers, + resolvers: defaultResolvers(cache), }); requestHandlers = { anonymousActorIdQueryHandler: vi diff --git a/tests/unit/specs/mocks/auth.ts b/tests/unit/specs/mocks/auth.ts index 4f8f8d79f..91902b1a6 100644 --- a/tests/unit/specs/mocks/auth.ts +++ b/tests/unit/specs/mocks/auth.ts @@ -1,3 +1,5 @@ +import { fakeCurrentActorData } from "../common"; + export const loginMock = { email: "some@email.tld", password: "somepassword", @@ -42,3 +44,13 @@ export const nullIdentityMock = { }, }, }; + +export const defaultIdentityMock = { + data: { + loggedUser: { + __typename: "loggedUser", + id: 1, + actors: [fakeCurrentActorData()], + }, + }, +}; diff --git a/tests/unit/specs/mocks/event.ts b/tests/unit/specs/mocks/event.ts index 23aa6e427..206794746 100644 --- a/tests/unit/specs/mocks/event.ts +++ b/tests/unit/specs/mocks/event.ts @@ -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 = { eventId: "1", actorId: "1",