-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Ryan Lerch <rlerch@redhat.com>
- Loading branch information
Showing
3 changed files
with
106 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// SPDX-FileCopyrightText: Contributors to the Fedora Project | ||
// | ||
// SPDX-License-Identifier: MIT | ||
|
||
import { createTestingPinia } from "@pinia/testing"; | ||
import { cleanup } from "@testing-library/vue"; | ||
import { setActivePinia } from "pinia"; | ||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; | ||
import router from "../router"; | ||
import { useUserStore } from "../stores/user"; | ||
import AdminLink from "./AdminLink.vue"; | ||
import { loginAdmin, loginUser, render } from "./test-utils"; | ||
|
||
vi.mock("../auth"); | ||
|
||
describe("AdminLink", () => { | ||
beforeEach(async () => { | ||
await router.replace("/"); | ||
// creates a fresh pinia and make it active so it's automatically picked | ||
// up by any useStore() call without having to pass it to it: | ||
// `useStore(pinia)` | ||
setActivePinia( | ||
createTestingPinia({ | ||
createSpy: vi.fn, | ||
stubActions: false, | ||
}) | ||
); | ||
}); | ||
// Unmount components after tests | ||
afterEach(() => { | ||
cleanup(); | ||
vi.restoreAllMocks(); | ||
}); | ||
|
||
it("doesnt render the admin link when not logged in", () => { | ||
const userStore = useUserStore(); | ||
expect(userStore.loggedIn).toBeFalsy(); | ||
const { queryAllByText } = render(AdminLink); | ||
expect(queryAllByText("Admin")).toStrictEqual([]); | ||
}); | ||
|
||
it("doesnt render the admin link when logged in as non-admin", () => { | ||
const userStore = useUserStore(); | ||
loginUser(userStore); | ||
const { queryAllByText } = render(AdminLink); | ||
expect(queryAllByText("Admin")).toStrictEqual([]); | ||
}); | ||
|
||
it("renders the admin link when not logged in as an admin", () => { | ||
const userStore = useUserStore(); | ||
loginAdmin(userStore); | ||
const { getByText } = render(AdminLink); | ||
expect(getByText("Admin")).toBeInTheDocument(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// SPDX-FileCopyrightText: Contributors to the Fedora Project | ||
// | ||
// SPDX-License-Identifier: MIT | ||
|
||
import type { useUserStore } from "../stores/user"; | ||
import { render as baseRender } from "@testing-library/vue"; | ||
import { getActivePinia, type Pinia } from "pinia"; | ||
import type { Component } from "vue"; | ||
import { createI18n } from "vue-i18n"; | ||
import router from "../router"; | ||
import { vi } from "vitest"; | ||
|
||
export const loginUser = (userStore: ReturnType<typeof useUserStore>) => { | ||
userStore.$patch({ | ||
accessToken: "testing", | ||
username: "dummy-user", | ||
fullName: "Dummy User", | ||
email: "dummy@example.com", | ||
}); | ||
}; | ||
|
||
export const loginAdmin = (userStore: ReturnType<typeof useUserStore>) => { | ||
userStore.$patch({ | ||
accessToken: "testing", | ||
username: "admin-user", | ||
fullName: "Admin User", | ||
email: "admin@example.com", | ||
isAdmin: true, | ||
}); | ||
}; | ||
|
||
export const render = (component: Component) => { | ||
const pinia = getActivePinia() as Pinia; | ||
const i18n = createI18n({ | ||
legacy: false, | ||
locale: navigator.language, | ||
fallbackLocale: "en-US", | ||
messages: {}, | ||
}); | ||
return baseRender(component, { | ||
global: { | ||
plugins: [router, pinia, i18n], | ||
provide: { | ||
auth: vi.fn(), | ||
}, | ||
}, | ||
}); | ||
}; |