Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[studio] router test coverage #1326 #1327

Merged
merged 1 commit into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions agdb_studio/.eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
/* eslint-env node */
require("@rushstack/eslint-patch/modern-module-resolution");

module.exports = {
root: true,
extends: [
Expand Down
2,038 changes: 978 additions & 1,060 deletions agdb_studio/package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions agdb_studio/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"@types/jsdom": "^21.1.7",
"@types/node": "^20.14.12",
"@vitejs/plugin-vue": "^5.1.0",
"@vitest/coverage-istanbul": "^2.1.3",
"@vitest/coverage-v8": "^2.0.4",
"@vue/eslint-config-prettier": "^9.0.0",
"@vue/eslint-config-typescript": "^13.0.0",
Expand Down
24 changes: 1 addition & 23 deletions agdb_studio/src/router/index.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,3 @@
import { createRouter, createWebHistory } from "vue-router";
import { createRoutes } from "./routes";
import { isLoggedIn, logout } from "@/services/auth.service";

const router = createRouter({
history: createWebHistory(import.meta.env.BASE_URL),
routes: createRoutes(),
});

router.beforeEach((to, from, next) => {
if (isLoggedIn()) {
if (to.name === "login") {
logout();
}
next();
} else {
if (to.name !== "login") {
next({ name: "login" });
} else {
next();
}
}
});
import router from "./router";

export default router;
42 changes: 42 additions & 0 deletions agdb_studio/src/router/router.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { describe, it, expect, beforeEach } from "vitest";
import router from "@/router/router";

const { isLoggedInMock, logoutMock } = vi.hoisted(() => {
return { isLoggedInMock: vi.fn(), logoutMock: vi.fn() };
});

vi.mock("@/services/auth.service", () => {
return {
isLoggedIn: isLoggedInMock,
logout: logoutMock,
};
});

describe("router", () => {
beforeEach(() => {
vi.clearAllMocks();
});

it("redirects to login if not logged in", async () => {
isLoggedInMock.mockReturnValue(false);

await router.push("/");

expect(router.currentRoute.value.name).toBe("login");
});
it("navigates to home if logged in", async () => {
isLoggedInMock.mockReturnValue(true);

await router.push("/");

expect(router.currentRoute.value.name).toBe("home");
});
it("logout if logged in and navigates to login", async () => {
isLoggedInMock.mockReturnValue(true);

await router.push("/login");

expect(router.currentRoute.value.name).toBe("login");
expect(logoutMock).toHaveBeenCalled();
});
});
36 changes: 36 additions & 0 deletions agdb_studio/src/router/router.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import {
createRouter,
createWebHistory,
type NavigationGuardNext,
type RouteLocationNormalizedGeneric,
} from "vue-router";
import { createRoutes } from "./routes";
import { isLoggedIn, logout } from "@/services/auth.service";

const router = createRouter({
history: createWebHistory(import.meta.env.BASE_URL),
routes: createRoutes(),
});

export const beforeEach = (
to: RouteLocationNormalizedGeneric,
from: RouteLocationNormalizedGeneric,
next: NavigationGuardNext,
) => {
if (isLoggedIn()) {
if (to.name === "login") {
logout();
}
next();
} else {
if (to.name !== "login") {
next({ name: "login" });
} else {
next();
}
}
};

router.beforeEach(beforeEach);

export default router;
49 changes: 49 additions & 0 deletions agdb_studio/src/router/routes.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { describe, it, expect, beforeEach } from "vitest";
import { createRouter, createWebHistory } from "vue-router";
import { mount } from "@vue/test-utils";
import { createRoutes } from "@/router/routes";
import HomeView from "@/views/HomeView.vue";
import AboutView from "@/views/HomeView.vue";
import LoginView from "@/views/LoginView.vue";
import App from "@/App.vue";

const setupTest = async (path: string) => {
const router = createRouter({
history: createWebHistory(),
routes: createRoutes(),
});
router.push(path);
await router.isReady();
const wrapper = mount(App, {
global: {
plugins: [router],
},
});
return { router, wrapper };
};

describe("router", () => {
beforeEach(() => {
vi.clearAllMocks();
});

it("renders a default route", async () => {
const { wrapper } = await setupTest("/");

expect(wrapper.findComponent(HomeView).exists()).toBe(true);
});

it("renders the about route", async () => {
const { wrapper, router } = await setupTest("/");
router.push("/about");
await router.isReady();

expect(wrapper.findComponent(AboutView).exists()).toBe(true);
});

it("renders the login route", async () => {
const { wrapper } = await setupTest("/login");

expect(wrapper.findComponent(LoginView).exists()).toBe(true);
});
});
56 changes: 0 additions & 56 deletions agdb_studio/tests/router/router.test.ts

This file was deleted.

2 changes: 1 addition & 1 deletion agdb_studio/tsconfig.vitest.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"exclude": [],
"compilerOptions": {
"lib": [],
"types": ["node", "jsdom"],
"types": ["node", "jsdom", "vitest/globals"],
"paths": {
"@/*": ["./src/*"]
}
Expand Down
File renamed without changes.
37 changes: 37 additions & 0 deletions agdb_studio/vitest.config.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// import { fileURLToPath } from "node:url";
import {
mergeConfig,
defineConfig,
configDefaults,
coverageConfigDefaults,
} from "vitest/config";
import viteConfig from "./vite.config.mjs";
import path from "path";

export default mergeConfig(
viteConfig,
defineConfig({
test: {
environment: "jsdom",
exclude: [...configDefaults.exclude, "e2e/*"],
root: path.resolve(__dirname, "."),
// root: fileURLToPath(new URL("./", import.meta.url)),
coverage: {
provider: "istanbul",
all: true,
exclude: [
...coverageConfigDefaults.exclude,
"e2e/*",
"*.config.*",
],
},
setupFiles: ["./vitest.setup.ts"],
globals: true,
},
resolve: {
alias: {
"@": path.resolve(__dirname, "./src"),
},
},
}),
);
29 changes: 0 additions & 29 deletions agdb_studio/vitest.config.ts

This file was deleted.

25 changes: 25 additions & 0 deletions agdb_studio/vitest.setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { vi } from "vitest";
import { AgdbApi } from "agdb_api";

const client = vi.fn().mockResolvedValue({
login: vi.fn().mockResolvedValue("token"),
logout: vi.fn().mockResolvedValue(undefined),
set_token: vi.fn(),
get_token: vi.fn(),
reset_token: vi.fn(),
interceptors: {
request: {
use: vi.fn(),
},
response: {
use: vi.fn(),
},
},
}) as unknown as AgdbApi.AgdbApiClient;
vi.mock("agdb_api", () => {
return {
AgdbApi: {
client,
},
};
});