Skip to content

Commit

Permalink
Add test for adminlink component
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Lerch <rlerch@redhat.com>
  • Loading branch information
ryanlerch committed Apr 13, 2023
1 parent ab03e61 commit df934b8
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 31 deletions.
55 changes: 55 additions & 0 deletions frontend/src/components/AdminLink.test.ts
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();
});
});
34 changes: 3 additions & 31 deletions frontend/src/components/LoginButton.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
// SPDX-License-Identifier: MIT

import { createTestingPinia } from "@pinia/testing";
import { cleanup, fireEvent, render as baseRender } from "@testing-library/vue";
import { getActivePinia, setActivePinia, type Pinia } from "pinia";
import { cleanup, fireEvent } from "@testing-library/vue";
import { setActivePinia } from "pinia";
import {
afterEach,
beforeEach,
Expand All @@ -14,42 +14,14 @@ import {
vi,
type Mock,
} from "vitest";
import type { Component } from "vue";
import { createI18n } from "vue-i18n";
import * as auth from "../auth";
import router from "../router";
import { useUserStore } from "../stores/user";
import LoginButton from "./LoginButton.vue";
import { loginUser, render } from "./test-utils";

vi.mock("../auth");

const loginUser = (userStore: ReturnType<typeof useUserStore>) => {
userStore.$patch({
accessToken: "testing",
username: "dummy-user",
fullName: "Dummy User",
email: "dummy@example.com",
});
};

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(),
},
},
});
};

describe("LoginButton", () => {
beforeEach(async () => {
await router.replace("/");
Expand Down
48 changes: 48 additions & 0 deletions frontend/src/components/test-utils.ts
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(),
},
},
});
};

0 comments on commit df934b8

Please sign in to comment.