|
| 1 | +import { mount, config as VTUConfig } from "@vue/test-utils"; |
| 2 | +import { describe, it, expect, beforeEach } from "vitest"; |
| 3 | +import { nextTick } from "vue"; |
| 4 | +import PLightbox from "component/lightbox.vue"; |
| 5 | + |
| 6 | +const mountLightbox = () => |
| 7 | + mount(PLightbox, { |
| 8 | + global: { |
| 9 | + stubs: { |
| 10 | + "v-dialog": true, |
| 11 | + "v-icon": true, |
| 12 | + "v-slider": true, |
| 13 | + "p-lightbox-menu": true, |
| 14 | + "p-sidebar-info": true, |
| 15 | + }, |
| 16 | + }, |
| 17 | + }); |
| 18 | + |
| 19 | +describe("PLightbox (low-mock, jsdom-friendly)", () => { |
| 20 | + beforeEach(() => { |
| 21 | + localStorage.removeItem("lightbox.info"); |
| 22 | + sessionStorage.removeItem("lightbox.muted"); |
| 23 | + }); |
| 24 | + |
| 25 | + it("toggleInfo updates info and localStorage when visible", async () => { |
| 26 | + const wrapper = mountLightbox(); |
| 27 | + await wrapper.setData({ visible: true }); |
| 28 | + |
| 29 | + // Use exposed onShortCut to trigger info toggle (KeyI) |
| 30 | + await wrapper.vm.onShortCut({ code: "KeyI" }); |
| 31 | + await nextTick(); |
| 32 | + expect(localStorage.getItem("lightbox.info")).toBe("true"); |
| 33 | + |
| 34 | + await wrapper.vm.onShortCut({ code: "KeyI" }); |
| 35 | + await nextTick(); |
| 36 | + expect(localStorage.getItem("lightbox.info")).toBe("false"); |
| 37 | + }); |
| 38 | + |
| 39 | + it("toggleMute writes sessionStorage without requiring video or exposed state", async () => { |
| 40 | + const wrapper = mountLightbox(); |
| 41 | + expect(sessionStorage.getItem("lightbox.muted")).toBeNull(); |
| 42 | + await wrapper.vm.onShortCut({ code: "KeyM" }); |
| 43 | + expect(sessionStorage.getItem("lightbox.muted")).toBe("true"); |
| 44 | + await wrapper.vm.onShortCut({ code: "KeyM" }); |
| 45 | + expect(sessionStorage.getItem("lightbox.muted")).toBe("false"); |
| 46 | + }); |
| 47 | + |
| 48 | + it("getPadding returns expected structure for large and small screens", async () => { |
| 49 | + const wrapper = mountLightbox(); |
| 50 | + // Large viewport |
| 51 | + const large = wrapper.vm.$options.methods.getPadding.call( |
| 52 | + wrapper.vm, |
| 53 | + { x: 1200, y: 800 }, |
| 54 | + { width: 4000, height: 3000 } |
| 55 | + ); |
| 56 | + expect(large).toHaveProperty("top"); |
| 57 | + expect(large).toHaveProperty("bottom"); |
| 58 | + expect(large).toHaveProperty("left"); |
| 59 | + expect(large).toHaveProperty("right"); |
| 60 | + |
| 61 | + // Small viewport (<= mobileBreakpoint) should yield zeros |
| 62 | + const small = wrapper.vm.$options.methods.getPadding.call( |
| 63 | + wrapper.vm, |
| 64 | + { x: 360, y: 640 }, |
| 65 | + { width: 1200, height: 800 } |
| 66 | + ); |
| 67 | + expect(small).toEqual({ top: 0, bottom: 0, left: 0, right: 0 }); |
| 68 | + }); |
| 69 | + |
| 70 | + it("KeyI is ignored when dialog is not visible", async () => { |
| 71 | + const wrapper = mountLightbox(); |
| 72 | + expect(localStorage.getItem("lightbox.info")).toBeNull(); |
| 73 | + await wrapper.vm.onShortCut({ code: "KeyI" }); |
| 74 | + expect(localStorage.getItem("lightbox.info")).toBeNull(); |
| 75 | + }); |
| 76 | + |
| 77 | + it("getViewport falls back to window size without content ref", () => { |
| 78 | + const wrapper = mountLightbox(); |
| 79 | + const vp = wrapper.vm.$options.methods.getViewport.call(wrapper.vm); |
| 80 | + expect(vp.x).toBeGreaterThan(0); |
| 81 | + expect(vp.y).toBeGreaterThan(0); |
| 82 | + }); |
| 83 | + |
| 84 | + it("menuActions marks Download action visible when allowed", () => { |
| 85 | + const wrapper = mountLightbox(); |
| 86 | + const ctx = { |
| 87 | + $gettext: VTUConfig.global.mocks.$gettext, |
| 88 | + $pgettext: VTUConfig.global.mocks.$pgettext, |
| 89 | + // minimal state needed by menuActions visibility checks |
| 90 | + canManageAlbums: false, |
| 91 | + canArchive: false, |
| 92 | + canDownload: true, |
| 93 | + collection: null, |
| 94 | + context: "", |
| 95 | + model: {}, |
| 96 | + }; |
| 97 | + const actions = wrapper.vm.$options.methods.menuActions.call(ctx); |
| 98 | + const download = actions.find((a) => a?.name === "download"); |
| 99 | + expect(download).toBeTruthy(); |
| 100 | + expect(download.visible).toBe(true); |
| 101 | + }); |
| 102 | +}); |
0 commit comments