Fix JS unit tests

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel 2021-08-02 19:26:23 +02:00
parent 8c480af4bd
commit 0c749742ab
No known key found for this signature in database
GPG key ID: A061B9DDE0CA0773
6 changed files with 35 additions and 16 deletions

View file

@ -20,7 +20,6 @@ import {
newCommentForEventResponse, newCommentForEventResponse,
} from "../../mocks/event"; } from "../../mocks/event";
import flushPromises from "flush-promises"; import flushPromises from "flush-promises";
import { InMemoryCache } from "@apollo/client/core";
import { defaultResolvers } from "../../common"; import { defaultResolvers } from "../../common";
const localVue = createLocalVue(); const localVue = createLocalVue();
localVue.use(Buefy); localVue.use(Buefy);
@ -39,11 +38,9 @@ describe("CommentTree", () => {
let mockClient: MockApolloClient | null; let mockClient: MockApolloClient | null;
let apolloProvider; let apolloProvider;
let requestHandlers: Record<string, RequestHandler>; let requestHandlers: Record<string, RequestHandler>;
const cache = new InMemoryCache({ addTypename: false });
const generateWrapper = (handlers = {}, baseData = {}) => { const generateWrapper = (handlers = {}, baseData = {}) => {
mockClient = createMockClient({ mockClient = createMockClient({
cache,
resolvers: defaultResolvers, 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 () => { it("renders a comment tree with comments", async () => {
generateWrapper(); generateWrapper();
@ -144,12 +156,11 @@ describe("CommentTree", () => {
.fn() .fn()
.mockResolvedValue(eventNoCommentThreadsMock), .mockResolvedValue(eventNoCommentThreadsMock),
}); });
expect(requestHandlers.eventCommentThreadsQueryHandler).toHaveBeenCalled();
expect(wrapper.exists()).toBe(true);
expect(wrapper.find(".loading").text()).toBe("Loading comments…");
await flushPromises(); await flushPromises();
expect(
requestHandlers.eventCommentThreadsQueryHandler
).toHaveBeenCalledWith({ eventUUID: eventData.uuid });
expect(wrapper.find(".no-comments").text()).toBe("No comments yet"); expect(wrapper.find(".no-comments").text()).toBe("No comments yet");
expect(wrapper.html()).toMatchSnapshot(); expect(wrapper.html()).toMatchSnapshot();

View file

@ -33,6 +33,15 @@ exports[`CommentTree renders a comment tree with comments 1`] = `
</div> </div>
`; `;
exports[`CommentTree renders a loading comment tree 1`] = `
<div>
<!---->
<p class="loading has-text-centered">
Loading comments…
</p>
</div>
`;
exports[`CommentTree renders an empty comment tree 1`] = ` exports[`CommentTree renders an empty comment tree 1`] = `
<div> <div>
<form class="new-comment"> <form class="new-comment">

View file

@ -229,11 +229,7 @@ describe("ParticipationWithoutAccount", () => {
eventData.participantStats.notConfirmed + 1 eventData.participantStats.notConfirmed + 1
); );
} }
// lots of things to await await flushPromises();
await wrapper.vm.$nextTick();
await wrapper.vm.$nextTick();
await wrapper.vm.$nextTick();
await wrapper.vm.$nextTick();
expect(wrapper.find("form").exists()).toBeFalsy(); expect(wrapper.find("form").exists()).toBeFalsy();
expect(wrapper.find("h1.title").text()).toBe( expect(wrapper.find("h1.title").text()).toBe(
"Request for participation confirmation sent" "Request for participation confirmation sent"

View file

@ -76,8 +76,7 @@ describe("Reset page", () => {
token: "some-token", token: "some-token",
}); });
await wrapper.vm.$nextTick(); await flushPromises();
await wrapper.vm.$nextTick();
expect(wrapper.find("article.message.is-danger").text()).toContain( expect(wrapper.find("article.message.is-danger").text()).toContain(
"The token you provided is invalid" "The token you provided is invalid"

View file

@ -115,13 +115,14 @@ describe("Render login form", () => {
expect(requestHandlers.loginMutationHandler).toHaveBeenCalledWith({ expect(requestHandlers.loginMutationHandler).toHaveBeenCalledWith({
...loginMock, ...loginMock,
}); });
await wrapper.vm.$nextTick(); await flushPromises();
await wrapper.vm.$nextTick();
const currentUser = mockClient?.cache.readQuery<{ const currentUser = mockClient?.cache.readQuery<{
currentUser: ICurrentUser; currentUser: ICurrentUser;
}>({ }>({
query: CURRENT_USER_CLIENT, query: CURRENT_USER_CLIENT,
})?.currentUser; })?.currentUser;
await flushPromises();
expect(currentUser?.email).toBe("some@email.tld"); expect(currentUser?.email).toBe("some@email.tld");
expect(currentUser?.id).toBe("1"); expect(currentUser?.id).toBe("1");
expect(jest.isMockFunction(wrapper.vm.$router.push)).toBe(true); expect(jest.isMockFunction(wrapper.vm.$router.push)).toBe(true);

View file

@ -108,6 +108,7 @@ export const eventCommentThreadsMock = {
summary: "I am the senate", summary: "I am the senate",
}, },
deletedAt: null, deletedAt: null,
insertedAt: "2020-12-03T09:02:00Z",
isAnnouncement: false, isAnnouncement: false,
}, },
{ {
@ -137,6 +138,7 @@ export const eventCommentThreadsMock = {
summary: "I am the senate", summary: "I am the senate",
}, },
deletedAt: null, deletedAt: null,
insertedAt: "2020-12-03T11:02:00Z",
isAnnouncement: false, isAnnouncement: false,
}, },
], ],
@ -180,6 +182,7 @@ export const newCommentForEventResponse: DataMock = {
summary: "I am the senate", summary: "I am the senate",
}, },
deletedAt: null, deletedAt: null,
insertedAt: "2020-12-03T13:02:00Z",
isAnnouncement: false, isAnnouncement: false,
}, },
}, },