From 0c749742ab7c503c37a0370c816cab969082437d Mon Sep 17 00:00:00 2001 From: Thomas Citharel Date: Mon, 2 Aug 2021 19:26:23 +0200 Subject: [PATCH] Fix JS unit tests Signed-off-by: Thomas Citharel --- .../components/Comment/CommentTree.spec.ts | 25 +++++++++++++------ .../__snapshots__/CommentTree.spec.ts.snap | 9 +++++++ .../ParticipationWithoutAccount.spec.ts | 6 +---- .../components/User/PasswordReset.spec.ts | 3 +-- .../unit/specs/components/User/login.spec.ts | 5 ++-- js/tests/unit/specs/mocks/event.ts | 3 +++ 6 files changed, 35 insertions(+), 16 deletions(-) diff --git a/js/tests/unit/specs/components/Comment/CommentTree.spec.ts b/js/tests/unit/specs/components/Comment/CommentTree.spec.ts index 6bdad5ef4..b7e28b2bb 100644 --- a/js/tests/unit/specs/components/Comment/CommentTree.spec.ts +++ b/js/tests/unit/specs/components/Comment/CommentTree.spec.ts @@ -20,7 +20,6 @@ import { newCommentForEventResponse, } from "../../mocks/event"; import flushPromises from "flush-promises"; -import { InMemoryCache } from "@apollo/client/core"; import { defaultResolvers } from "../../common"; const localVue = createLocalVue(); localVue.use(Buefy); @@ -39,11 +38,9 @@ describe("CommentTree", () => { let mockClient: MockApolloClient | null; let apolloProvider; let requestHandlers: Record; - const cache = new InMemoryCache({ addTypename: false }); const generateWrapper = (handlers = {}, baseData = {}) => { mockClient = createMockClient({ - cache, resolvers: defaultResolvers, }); @@ -84,6 +81,21 @@ describe("CommentTree", () => { }); }; + afterEach(() => { + mockClient = null; + requestHandlers = {}; + apolloProvider = null; + wrapper.destroy(); + }); + + it("renders a loading comment tree", async () => { + generateWrapper(); + + expect(wrapper.find(".loading").text()).toBe("Loading comments…"); + + expect(wrapper.html()).toMatchSnapshot(); + }); + it("renders a comment tree with comments", async () => { generateWrapper(); @@ -144,12 +156,11 @@ describe("CommentTree", () => { .fn() .mockResolvedValue(eventNoCommentThreadsMock), }); - expect(requestHandlers.eventCommentThreadsQueryHandler).toHaveBeenCalled(); - - expect(wrapper.exists()).toBe(true); - expect(wrapper.find(".loading").text()).toBe("Loading comments…"); await flushPromises(); + expect( + requestHandlers.eventCommentThreadsQueryHandler + ).toHaveBeenCalledWith({ eventUUID: eventData.uuid }); expect(wrapper.find(".no-comments").text()).toBe("No comments yet"); expect(wrapper.html()).toMatchSnapshot(); diff --git a/js/tests/unit/specs/components/Comment/__snapshots__/CommentTree.spec.ts.snap b/js/tests/unit/specs/components/Comment/__snapshots__/CommentTree.spec.ts.snap index 63520cf4c..c2d67bc89 100644 --- a/js/tests/unit/specs/components/Comment/__snapshots__/CommentTree.spec.ts.snap +++ b/js/tests/unit/specs/components/Comment/__snapshots__/CommentTree.spec.ts.snap @@ -33,6 +33,15 @@ exports[`CommentTree renders a comment tree with comments 1`] = ` `; +exports[`CommentTree renders a loading comment tree 1`] = ` +
+ +

+ Loading comments… +

+
+`; + exports[`CommentTree renders an empty comment tree 1`] = `
diff --git a/js/tests/unit/specs/components/Participation/ParticipationWithoutAccount.spec.ts b/js/tests/unit/specs/components/Participation/ParticipationWithoutAccount.spec.ts index 4f36f5357..377965ebd 100644 --- a/js/tests/unit/specs/components/Participation/ParticipationWithoutAccount.spec.ts +++ b/js/tests/unit/specs/components/Participation/ParticipationWithoutAccount.spec.ts @@ -229,11 +229,7 @@ describe("ParticipationWithoutAccount", () => { eventData.participantStats.notConfirmed + 1 ); } - // lots of things to await - await wrapper.vm.$nextTick(); - await wrapper.vm.$nextTick(); - await wrapper.vm.$nextTick(); - await wrapper.vm.$nextTick(); + await flushPromises(); expect(wrapper.find("form").exists()).toBeFalsy(); expect(wrapper.find("h1.title").text()).toBe( "Request for participation confirmation sent" diff --git a/js/tests/unit/specs/components/User/PasswordReset.spec.ts b/js/tests/unit/specs/components/User/PasswordReset.spec.ts index 7fc4fb630..752b62190 100644 --- a/js/tests/unit/specs/components/User/PasswordReset.spec.ts +++ b/js/tests/unit/specs/components/User/PasswordReset.spec.ts @@ -76,8 +76,7 @@ describe("Reset page", () => { token: "some-token", }); - await wrapper.vm.$nextTick(); - await wrapper.vm.$nextTick(); + await flushPromises(); expect(wrapper.find("article.message.is-danger").text()).toContain( "The token you provided is invalid" diff --git a/js/tests/unit/specs/components/User/login.spec.ts b/js/tests/unit/specs/components/User/login.spec.ts index 1769be71c..79ee76602 100644 --- a/js/tests/unit/specs/components/User/login.spec.ts +++ b/js/tests/unit/specs/components/User/login.spec.ts @@ -115,13 +115,14 @@ describe("Render login form", () => { expect(requestHandlers.loginMutationHandler).toHaveBeenCalledWith({ ...loginMock, }); - await wrapper.vm.$nextTick(); - await wrapper.vm.$nextTick(); + await flushPromises(); const currentUser = mockClient?.cache.readQuery<{ currentUser: ICurrentUser; }>({ query: CURRENT_USER_CLIENT, })?.currentUser; + + await flushPromises(); expect(currentUser?.email).toBe("some@email.tld"); expect(currentUser?.id).toBe("1"); expect(jest.isMockFunction(wrapper.vm.$router.push)).toBe(true); diff --git a/js/tests/unit/specs/mocks/event.ts b/js/tests/unit/specs/mocks/event.ts index 39f37708b..e1bcd0be9 100644 --- a/js/tests/unit/specs/mocks/event.ts +++ b/js/tests/unit/specs/mocks/event.ts @@ -108,6 +108,7 @@ export const eventCommentThreadsMock = { summary: "I am the senate", }, deletedAt: null, + insertedAt: "2020-12-03T09:02:00Z", isAnnouncement: false, }, { @@ -137,6 +138,7 @@ export const eventCommentThreadsMock = { summary: "I am the senate", }, deletedAt: null, + insertedAt: "2020-12-03T11:02:00Z", isAnnouncement: false, }, ], @@ -180,6 +182,7 @@ export const newCommentForEventResponse: DataMock = { summary: "I am the senate", }, deletedAt: null, + insertedAt: "2020-12-03T13:02:00Z", isAnnouncement: false, }, },