2022-07-12 10:55:28 +02:00
|
|
|
import { config, mount, VueWrapper } from "@vue/test-utils";
|
|
|
|
import Login from "@/views/User/LoginView.vue";
|
2021-04-16 14:49:43 +02:00
|
|
|
import {
|
|
|
|
createMockClient,
|
|
|
|
MockApolloClient,
|
|
|
|
RequestHandler,
|
|
|
|
} from "mock-apollo-client";
|
|
|
|
import buildCurrentUserResolver from "@/apollo/user";
|
2022-07-12 10:55:28 +02:00
|
|
|
import { loginMock as loginConfigMock } from "../../mocks/config";
|
|
|
|
import { LOGIN_CONFIG } from "@/graphql/config";
|
2021-04-16 14:49:43 +02:00
|
|
|
import { loginMock, loginResponseMock } from "../../mocks/auth";
|
|
|
|
import { LOGIN } from "@/graphql/auth";
|
|
|
|
import { CURRENT_USER_CLIENT } from "@/graphql/user";
|
|
|
|
import { ICurrentUser } from "@/types/current-user.model";
|
|
|
|
import flushPromises from "flush-promises";
|
|
|
|
import RouteName from "@/router/name";
|
2023-11-15 12:29:39 +01:00
|
|
|
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
2022-07-12 10:55:28 +02:00
|
|
|
import { DefaultApolloClient } from "@vue/apollo-composable";
|
|
|
|
import Oruga from "@oruga-ui/oruga-next";
|
|
|
|
import { cache } from "@/apollo/memory";
|
2023-11-15 12:29:39 +01:00
|
|
|
import {
|
|
|
|
VueRouterMock,
|
|
|
|
createRouterMock,
|
|
|
|
injectRouterMock,
|
|
|
|
getRouter,
|
|
|
|
} from "vue-router-mock";
|
2022-07-12 10:55:28 +02:00
|
|
|
|
|
|
|
config.global.plugins.push(Oruga);
|
2023-11-15 12:29:39 +01:00
|
|
|
config.plugins.VueWrapper.install(VueRouterMock);
|
2021-04-16 14:49:43 +02:00
|
|
|
|
|
|
|
describe("Render login form", () => {
|
2022-07-12 10:55:28 +02:00
|
|
|
let wrapper: VueWrapper;
|
2021-04-16 14:49:43 +02:00
|
|
|
let mockClient: MockApolloClient | null;
|
|
|
|
let requestHandlers: Record<string, RequestHandler>;
|
|
|
|
|
2023-11-15 12:29:39 +01:00
|
|
|
const router = createRouterMock({
|
|
|
|
spy: {
|
|
|
|
create: (fn) => vi.fn(fn),
|
|
|
|
reset: (spy) => spy.mockReset(),
|
|
|
|
},
|
|
|
|
});
|
|
|
|
beforeEach(() => {
|
|
|
|
// inject it globally to ensure `useRoute()`, `$route`, etc work
|
|
|
|
// properly and give you access to test specific functions
|
|
|
|
injectRouterMock(router);
|
|
|
|
});
|
|
|
|
|
2021-04-16 14:49:43 +02:00
|
|
|
const generateWrapper = (
|
|
|
|
handlers: Record<string, unknown> = {},
|
|
|
|
customProps: Record<string, unknown> = {},
|
|
|
|
customMocks: Record<string, unknown> = {}
|
|
|
|
) => {
|
|
|
|
mockClient = createMockClient({
|
|
|
|
cache,
|
|
|
|
resolvers: buildCurrentUserResolver(cache),
|
|
|
|
});
|
|
|
|
|
|
|
|
requestHandlers = {
|
2022-07-12 10:55:28 +02:00
|
|
|
configQueryHandler: vi.fn().mockResolvedValue(loginConfigMock),
|
|
|
|
loginMutationHandler: vi.fn().mockResolvedValue(loginResponseMock),
|
2021-04-16 14:49:43 +02:00
|
|
|
...handlers,
|
|
|
|
};
|
2022-07-12 10:55:28 +02:00
|
|
|
mockClient.setRequestHandler(
|
|
|
|
LOGIN_CONFIG,
|
|
|
|
requestHandlers.configQueryHandler
|
|
|
|
);
|
2021-04-16 14:49:43 +02:00
|
|
|
mockClient.setRequestHandler(LOGIN, requestHandlers.loginMutationHandler);
|
|
|
|
|
|
|
|
wrapper = mount(Login, {
|
2022-07-12 10:55:28 +02:00
|
|
|
props: {
|
2021-04-16 14:49:43 +02:00
|
|
|
...customProps,
|
|
|
|
},
|
|
|
|
mocks: {
|
|
|
|
...customMocks,
|
|
|
|
},
|
|
|
|
stubs: ["router-link", "router-view"],
|
2022-07-12 10:55:28 +02:00
|
|
|
global: {
|
|
|
|
provide: {
|
|
|
|
[DefaultApolloClient]: mockClient,
|
|
|
|
},
|
2021-04-16 14:49:43 +02:00
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
afterEach(() => {
|
2022-07-12 10:55:28 +02:00
|
|
|
wrapper?.unmount();
|
|
|
|
cache.reset();
|
2021-04-16 14:49:43 +02:00
|
|
|
mockClient = null;
|
|
|
|
});
|
|
|
|
|
|
|
|
it("requires email and password to be filled", async () => {
|
|
|
|
generateWrapper();
|
|
|
|
await wrapper.vm.$nextTick();
|
|
|
|
await wrapper.vm.$nextTick();
|
2023-11-15 12:29:39 +01:00
|
|
|
await flushPromises();
|
2021-04-16 14:49:43 +02:00
|
|
|
|
|
|
|
expect(wrapper.exists()).toBe(true);
|
|
|
|
expect(requestHandlers.configQueryHandler).toHaveBeenCalled();
|
|
|
|
wrapper.find('form input[type="email"]').setValue("");
|
|
|
|
wrapper.find('form input[type="password"]').setValue("");
|
2022-07-12 10:55:28 +02:00
|
|
|
wrapper.find('form button[type="submit"]').trigger("click");
|
2021-04-16 14:49:43 +02:00
|
|
|
const form = wrapper.find("form");
|
|
|
|
expect(form.exists()).toBe(true);
|
|
|
|
const formElement = form.element as HTMLFormElement;
|
|
|
|
expect(formElement.checkValidity()).toBe(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("renders and submits the login form", async () => {
|
|
|
|
generateWrapper();
|
|
|
|
await wrapper.vm.$nextTick();
|
|
|
|
await wrapper.vm.$nextTick();
|
2023-11-15 12:29:39 +01:00
|
|
|
await flushPromises();
|
2021-04-16 14:49:43 +02:00
|
|
|
|
|
|
|
expect(wrapper.exists()).toBe(true);
|
|
|
|
expect(requestHandlers.configQueryHandler).toHaveBeenCalled();
|
|
|
|
wrapper.find('form input[type="email"]').setValue("some@email.tld");
|
|
|
|
wrapper.find('form input[type="password"]').setValue("somepassword");
|
|
|
|
wrapper.find("form").trigger("submit");
|
|
|
|
await wrapper.vm.$nextTick();
|
|
|
|
expect(requestHandlers.loginMutationHandler).toHaveBeenCalledWith({
|
|
|
|
...loginMock,
|
|
|
|
});
|
2021-08-02 19:26:23 +02:00
|
|
|
await flushPromises();
|
2021-04-16 14:49:43 +02:00
|
|
|
const currentUser = mockClient?.cache.readQuery<{
|
|
|
|
currentUser: ICurrentUser;
|
|
|
|
}>({
|
|
|
|
query: CURRENT_USER_CLIENT,
|
|
|
|
})?.currentUser;
|
2021-08-02 19:26:23 +02:00
|
|
|
|
|
|
|
await flushPromises();
|
2021-04-16 14:49:43 +02:00
|
|
|
expect(currentUser?.email).toBe("some@email.tld");
|
|
|
|
expect(currentUser?.id).toBe("1");
|
|
|
|
await flushPromises();
|
2023-11-15 12:29:39 +01:00
|
|
|
expect(wrapper.router.replace).toHaveBeenCalledWith({
|
|
|
|
name: RouteName.HOME,
|
|
|
|
});
|
2021-04-16 14:49:43 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
it("handles a login error", async () => {
|
|
|
|
generateWrapper({
|
2022-07-12 10:55:28 +02:00
|
|
|
loginMutationHandler: vi.fn().mockResolvedValue({
|
2021-04-16 14:49:43 +02:00
|
|
|
errors: [
|
|
|
|
{
|
|
|
|
message:
|
|
|
|
'"Impossible to authenticate, either your email or password are invalid."',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
}),
|
|
|
|
});
|
|
|
|
|
|
|
|
await wrapper.vm.$nextTick();
|
|
|
|
await wrapper.vm.$nextTick();
|
2023-11-15 12:29:39 +01:00
|
|
|
await flushPromises();
|
2021-04-16 14:49:43 +02:00
|
|
|
|
|
|
|
expect(wrapper.exists()).toBe(true);
|
|
|
|
expect(requestHandlers.configQueryHandler).toHaveBeenCalled();
|
|
|
|
wrapper.find('form input[type="email"]').setValue("some@email.tld");
|
|
|
|
wrapper.find('form input[type="password"]').setValue("somepassword");
|
|
|
|
wrapper.find("form").trigger("submit");
|
|
|
|
await wrapper.vm.$nextTick();
|
|
|
|
expect(requestHandlers.loginMutationHandler).toHaveBeenCalledWith({
|
|
|
|
...loginMock,
|
|
|
|
});
|
|
|
|
await flushPromises();
|
2022-07-12 10:55:28 +02:00
|
|
|
expect(wrapper.find(".o-notification--danger").text()).toContain(
|
2021-04-16 14:49:43 +02:00
|
|
|
"Impossible to authenticate, either your email or password are invalid."
|
|
|
|
);
|
2023-11-15 12:29:39 +01:00
|
|
|
expect(wrapper.router.push).not.toHaveBeenCalled();
|
2021-04-16 14:49:43 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
it("handles redirection after login", async () => {
|
2022-07-12 10:55:28 +02:00
|
|
|
generateWrapper();
|
2023-11-15 12:29:39 +01:00
|
|
|
getRouter().setQuery({ redirect: "/about/instance" });
|
2021-04-16 14:49:43 +02:00
|
|
|
|
|
|
|
await wrapper.vm.$nextTick();
|
|
|
|
await wrapper.vm.$nextTick();
|
2023-11-15 12:29:39 +01:00
|
|
|
await flushPromises();
|
2021-04-16 14:49:43 +02:00
|
|
|
|
|
|
|
wrapper.find('form input[type="email"]').setValue("some@email.tld");
|
|
|
|
wrapper.find('form input[type="password"]').setValue("somepassword");
|
|
|
|
wrapper.find("form").trigger("submit");
|
|
|
|
await flushPromises();
|
2023-11-15 12:29:39 +01:00
|
|
|
expect(wrapper.router.push).toHaveBeenCalledWith("/about/instance");
|
2021-04-16 14:49:43 +02:00
|
|
|
});
|
|
|
|
});
|