|
| 1 | +import { meResponseFactory } from "@@/tests/test-utils/factory"; |
| 2 | +import { useAuthStore } from "@data/auth"; |
| 3 | +import { createTestingPinia } from "@pinia/testing"; |
| 4 | +import { setActivePinia } from "pinia"; |
| 5 | +import { RouteLocationNormalized } from "vue-router"; |
| 6 | +import { isAuthenticatedGuard } from "./is-authenticated.guard"; |
| 7 | + |
| 8 | +jest.mock("./login-redirect-url", () => ({ |
| 9 | + getLoginUrlWithRedirect: () => "login-url", |
| 10 | +})); |
| 11 | + |
| 12 | +describe("Authentication Guard", () => { |
| 13 | + beforeEach(() => { |
| 14 | + setActivePinia(createTestingPinia()); |
| 15 | + }); |
| 16 | + |
| 17 | + afterEach(() => { |
| 18 | + jest.clearAllMocks(); |
| 19 | + }); |
| 20 | + |
| 21 | + describe("isAuthenticatedGuard", () => { |
| 22 | + describe("when authenticated", () => { |
| 23 | + const setup = () => { |
| 24 | + const to: RouteLocationNormalized = { |
| 25 | + fullPath: "/test", |
| 26 | + } as RouteLocationNormalized; |
| 27 | + const from: RouteLocationNormalized = {} as RouteLocationNormalized; |
| 28 | + const next = jest.fn(); |
| 29 | + const authStore = useAuthStore(); |
| 30 | + |
| 31 | + authStore.me = meResponseFactory.build(); |
| 32 | + |
| 33 | + return { |
| 34 | + to, |
| 35 | + from, |
| 36 | + next, |
| 37 | + }; |
| 38 | + }; |
| 39 | + |
| 40 | + it("should pass", () => { |
| 41 | + const { to, from, next } = setup(); |
| 42 | + |
| 43 | + isAuthenticatedGuard(to, from, next); |
| 44 | + |
| 45 | + expect(next).toHaveBeenCalled(); |
| 46 | + }); |
| 47 | + }); |
| 48 | + |
| 49 | + describe("when the url is public", () => { |
| 50 | + const setup = () => { |
| 51 | + const to: RouteLocationNormalized = { |
| 52 | + fullPath: "/test", |
| 53 | + meta: { |
| 54 | + isPublic: true, |
| 55 | + }, |
| 56 | + } as unknown as RouteLocationNormalized; |
| 57 | + const from: RouteLocationNormalized = {} as RouteLocationNormalized; |
| 58 | + const next = jest.fn(); |
| 59 | + const authStore = useAuthStore(); |
| 60 | + |
| 61 | + authStore.me = null; |
| 62 | + |
| 63 | + return { |
| 64 | + to, |
| 65 | + from, |
| 66 | + next, |
| 67 | + }; |
| 68 | + }; |
| 69 | + |
| 70 | + it("should pass", () => { |
| 71 | + const { to, from, next } = setup(); |
| 72 | + |
| 73 | + isAuthenticatedGuard(to, from, next); |
| 74 | + |
| 75 | + expect(next).toHaveBeenCalled(); |
| 76 | + }); |
| 77 | + }); |
| 78 | + |
| 79 | + describe("when not authenticated and the url is not public", () => { |
| 80 | + const setup = () => { |
| 81 | + const to: RouteLocationNormalized = { |
| 82 | + fullPath: "/test", |
| 83 | + } as RouteLocationNormalized; |
| 84 | + const from: RouteLocationNormalized = {} as RouteLocationNormalized; |
| 85 | + const next = jest.fn(); |
| 86 | + const authStore = useAuthStore(); |
| 87 | + |
| 88 | + authStore.me = null; |
| 89 | + |
| 90 | + const assign = jest.fn(); |
| 91 | + Object.defineProperty(window, "location", { |
| 92 | + configurable: true, |
| 93 | + value: { assign }, |
| 94 | + }); |
| 95 | + |
| 96 | + return { |
| 97 | + to, |
| 98 | + from, |
| 99 | + next, |
| 100 | + assign, |
| 101 | + }; |
| 102 | + }; |
| 103 | + |
| 104 | + it("should redirect to login", () => { |
| 105 | + const { to, from, next, assign } = setup(); |
| 106 | + |
| 107 | + isAuthenticatedGuard(to, from, next); |
| 108 | + |
| 109 | + expect(assign).toHaveBeenCalledWith("login-url"); |
| 110 | + }); |
| 111 | + |
| 112 | + it("should not pass", () => { |
| 113 | + const { to, from, next } = setup(); |
| 114 | + |
| 115 | + isAuthenticatedGuard(to, from, next); |
| 116 | + |
| 117 | + expect(next).not.toHaveBeenCalled(); |
| 118 | + }); |
| 119 | + }); |
| 120 | + }); |
| 121 | +}); |
0 commit comments