From fc93e2d33cb037bed5c499ecb8625112fcad4681 Mon Sep 17 00:00:00 2001 From: Amir Raminfar Date: Fri, 5 Jan 2024 19:28:02 -0800 Subject: [PATCH] chore: adds more tests (#2660) --- assets/components/FuzzySearchModal.spec.ts | 25 +++++++++++++++++++--- assets/modules/router.ts | 13 ++++++----- 2 files changed, 28 insertions(+), 10 deletions(-) diff --git a/assets/components/FuzzySearchModal.spec.ts b/assets/components/FuzzySearchModal.spec.ts index 34244d11..8fe42062 100644 --- a/assets/components/FuzzySearchModal.spec.ts +++ b/assets/components/FuzzySearchModal.spec.ts @@ -4,12 +4,16 @@ import { mount } from "@vue/test-utils"; import FuzzySearchModal from "./FuzzySearchModal.vue"; import { Container } from "@/models/Container"; -import { describe, expect, test, vi } from "vitest"; +import { beforeEach, describe, expect, test, vi } from "vitest"; import { createI18n } from "vue-i18n"; +import { useRouter } from "vue-router"; +import { router } from "@/modules/router"; // @ts-ignore import EventSource, { sources } from "eventsourcemock"; +vi.mock("vue-router"); + vi.mock("@/stores/config", () => ({ __esModule: true, default: { base: "", hosts: [{ name: "localhost", id: "localhost" }] }, @@ -28,8 +32,8 @@ function createFuzzySearchModal() { container: { containers: [ new Container("123", new Date(), "image", "test", "command", "host", {}, "status", "running"), - new Container("123", new Date(), "image", "foo bar", "command", "host", {}, "status", "running"), - new Container("123", new Date(), "image", "baz", "command", "host", {}, "status", "exited"), + new Container("345", new Date(), "image", "foo bar", "command", "host", {}, "status", "running"), + new Container("567", new Date(), "image", "baz", "command", "host", {}, "status", "exited"), ], }, }, @@ -44,6 +48,14 @@ function createFuzzySearchModal() { * @vitest-environment jsdom */ describe("", () => { + vi.mocked(useRouter).mockReturnValue({ + ...router, + push: vi.fn(), + }); + + beforeEach(() => { + vi.mocked(useRouter().push).mockReset(); + }); test("shows all", async () => { const wrapper = createFuzzySearchModal(); expect(wrapper.findAll("li").length).toBe(3); @@ -57,4 +69,11 @@ describe("", () => { `"foo bar"`, ); }); + + test("choose baz", async () => { + const wrapper = createFuzzySearchModal(); + await wrapper.find("input").setValue("baz"); + await wrapper.find("input").trigger("keyup.enter"); + expect(useRouter().push).toHaveBeenCalledWith({ name: "container-id", params: { id: "567" } }); + }); }); diff --git a/assets/modules/router.ts b/assets/modules/router.ts index 852c5364..bbc826e9 100644 --- a/assets/modules/router.ts +++ b/assets/modules/router.ts @@ -3,13 +3,12 @@ import { createRouter, createWebHistory } from "vue-router"; import pages from "~pages"; import { setupLayouts } from "virtual:generated-layouts"; +const routes = setupLayouts(pages); +export const router = createRouter({ + history: createWebHistory(withBase("/")), + routes, +}); + export const install = (app: App) => { - const routes = setupLayouts(pages); - - const router = createRouter({ - history: createWebHistory(withBase("/")), - routes, - }); - app.use(router); };