From 4166ca6477fbe69e51a8962f3a9dce2bf4e3c502 Mon Sep 17 00:00:00 2001 From: Georges-Antoine Assi Date: Fri, 4 Sep 2026 22:08:13 -0400 Subject: [PATCH] test(collections): pin the ?kind=virtual reset to the URL The fallback test only read the rendered list, so it passed whether the view cleared the query param or merely reset its local filter, and it seeded the preference before mount so the runtime flip went uncovered. Hold the setting in a live ref the tests can flip after mount, assert the router.replace that drops the param, and add the turn-it-off-while- filtered case. Co-Authored-By: Claude Opus 5 --- .../src/v2/views/CollectionsIndex.test.ts | 53 +++++++++++++------ 1 file changed, 36 insertions(+), 17 deletions(-) diff --git a/frontend/src/v2/views/CollectionsIndex.test.ts b/frontend/src/v2/views/CollectionsIndex.test.ts index 373dda820..d387f4103 100644 --- a/frontend/src/v2/views/CollectionsIndex.test.ts +++ b/frontend/src/v2/views/CollectionsIndex.test.ts @@ -1,8 +1,8 @@ /* eslint-disable vue/one-component-per-file */ -import { mount } from "@vue/test-utils"; +import { flushPromises, mount } from "@vue/test-utils"; import { createPinia, setActivePinia } from "pinia"; import { beforeEach, describe, expect, it, vi } from "vitest"; -import { defineComponent, nextTick, ref } from "vue"; +import { defineComponent, ref } from "vue"; import storeCollections, { type Collection, type SmartCollection, @@ -16,14 +16,11 @@ vi.mock("vue-i18n", () => ({ // Plain object rather than a reactive route: every test sets the query // before mounting, which is when the view reads it. -const { routeState, routerState, searchState, settingsState } = vi.hoisted( - () => ({ - routeState: { query: {} as Record }, - routerState: { replace: vi.fn() }, - searchState: { term: "" }, - settingsState: { showVirtualCollections: false }, - }), -); +const { routeState, routerState, searchState } = vi.hoisted(() => ({ + routeState: { query: {} as Record }, + routerState: { replace: vi.fn() }, + searchState: { term: "" }, +})); vi.mock("vue-router", async (importOriginal) => ({ ...(await importOriginal()), @@ -125,9 +122,12 @@ vi.mock("@/v2/composables/useWrapGridNav", () => ({ useWrapGridNav: vi.fn(), })); +// Live ref rather than a per-call snapshot, so a test can flip the +// preference after mount the way the settings toggle does. +const showVirtualCollections = ref(false); vi.mock("@/composables/useUISettings", () => ({ useUISettings: () => ({ - showVirtualCollections: ref(settingsState.showVirtualCollections), + showVirtualCollections, virtualCollectionType: ref("collection"), }), })); @@ -158,7 +158,7 @@ describe("CollectionsIndex", () => { routeState.query = {}; routerState.replace = vi.fn(); searchState.term = ""; - settingsState.showVirtualCollections = false; + showVirtualCollections.value = false; segmentFilterIds.value = []; }); @@ -174,7 +174,7 @@ describe("CollectionsIndex", () => { }); it("shows virtual collections when the setting is on", () => { - settingsState.showVirtualCollections = true; + showVirtualCollections.value = true; seed(); const wrapper = mount(CollectionsIndex); @@ -196,7 +196,7 @@ describe("CollectionsIndex", () => { }); it("fetches virtual collections on mount when the setting is on", () => { - settingsState.showVirtualCollections = true; + showVirtualCollections.value = true; const collections = seed(); collections.virtualCollections = []; const fetchVirtual = vi @@ -217,7 +217,7 @@ describe("CollectionsIndex", () => { }); it("keeps the virtual option in the kind filter when the setting is on", () => { - settingsState.showVirtualCollections = true; + showVirtualCollections.value = true; seed(); mount(CollectionsIndex); @@ -235,15 +235,34 @@ describe("CollectionsIndex", () => { seed(); const wrapper = mount(CollectionsIndex); - await nextTick(); + await flushPromises(); expect(wrapper.text()).toContain("Favourites"); expect(wrapper.text()).toContain("Recently Added"); expect(wrapper.text()).not.toContain("Nintendo"); + expect(routerState.replace).toHaveBeenCalledWith({ query: {} }); + }); + + it("drops ?kind=virtual when the setting is turned off after mount", async () => { + showVirtualCollections.value = true; + routeState.query = { kind: "virtual" }; + seed(); + + const wrapper = mount(CollectionsIndex); + await flushPromises(); + expect(wrapper.text()).toContain("Nintendo"); + expect(routerState.replace).not.toHaveBeenCalled(); + + showVirtualCollections.value = false; + await flushPromises(); + + expect(wrapper.text()).not.toContain("Nintendo"); + expect(wrapper.text()).toContain("Favourites"); + expect(routerState.replace).toHaveBeenCalledWith({ query: {} }); }); it("honors ?kind=virtual when the setting is on", () => { - settingsState.showVirtualCollections = true; + showVirtualCollections.value = true; routeState.query = { kind: "virtual" }; seed();