|
| 1 | +/** |
| 2 | + * @vitest-environment jsdom |
| 3 | + */ |
| 4 | +import { act } from 'react' |
| 5 | +import { Lightbox } from '@sim/emcn' |
| 6 | +import { createRoot, type Root } from 'react-dom/client' |
| 7 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' |
| 8 | + |
| 9 | +vi.mock('next/navigation', () => ({ usePathname: () => '/introduction' })) |
| 10 | + |
| 11 | +let root: Root |
| 12 | +let container: HTMLDivElement |
| 13 | + |
| 14 | +function button(label: string): HTMLButtonElement { |
| 15 | + const element = document.querySelector<HTMLButtonElement>(`button[aria-label="${label}"]`) |
| 16 | + if (!element) throw new Error(`Missing button: ${label}`) |
| 17 | + return element |
| 18 | +} |
| 19 | + |
| 20 | +function media(): HTMLImageElement { |
| 21 | + const element = document.querySelector<HTMLImageElement>('[role="dialog"] img') |
| 22 | + if (!element) throw new Error('Missing lightbox image') |
| 23 | + return element |
| 24 | +} |
| 25 | + |
| 26 | +function wheel(init: WheelEventInit): WheelEvent { |
| 27 | + const event = new WheelEvent('wheel', { bubbles: true, cancelable: true, ...init }) |
| 28 | + act(() => media().dispatchEvent(event)) |
| 29 | + return event |
| 30 | +} |
| 31 | + |
| 32 | +async function click(label: string) { |
| 33 | + await act(async () => button(label).click()) |
| 34 | +} |
| 35 | + |
| 36 | +describe('Lightbox interactions', () => { |
| 37 | + beforeEach(async () => { |
| 38 | + ;(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true |
| 39 | + container = document.createElement('div') |
| 40 | + document.body.appendChild(container) |
| 41 | + root = createRoot(container) |
| 42 | + await act(async () => |
| 43 | + root.render( |
| 44 | + <Lightbox src='/image.png' alt='Example image'> |
| 45 | + <button type='button' aria-label='Open image'> |
| 46 | + Preview |
| 47 | + </button> |
| 48 | + </Lightbox> |
| 49 | + ) |
| 50 | + ) |
| 51 | + await click('Open image') |
| 52 | + }) |
| 53 | + |
| 54 | + afterEach(async () => { |
| 55 | + await act(async () => root.unmount()) |
| 56 | + container.remove() |
| 57 | + document.body.replaceChildren() |
| 58 | + document.body.removeAttribute('style') |
| 59 | + vi.restoreAllMocks() |
| 60 | + }) |
| 61 | + |
| 62 | + it.each([{ ctrlKey: true }, { metaKey: true }])( |
| 63 | + 'zooms the media in both directions and cancels page zoom for %j', |
| 64 | + (modifier) => { |
| 65 | + const zoomIn = wheel({ ...modifier, deltaY: -40 }) |
| 66 | + expect(zoomIn.defaultPrevented).toBe(true) |
| 67 | + expect(Number(media().style.zoom)).toBeCloseTo(Math.exp(0.2)) |
| 68 | + |
| 69 | + const zoomOut = wheel({ ...modifier, deltaY: 40 }) |
| 70 | + expect(zoomOut.defaultPrevented).toBe(true) |
| 71 | + expect(Number(media().style.zoom)).toBeCloseTo(1) |
| 72 | + } |
| 73 | + ) |
| 74 | + |
| 75 | + it('leaves ordinary vertical scrolling available without changing zoom', () => { |
| 76 | + const event = wheel({ deltaY: 40 }) |
| 77 | + expect(event.defaultPrevented).toBe(false) |
| 78 | + expect(media().style.zoom).toBe('1') |
| 79 | + }) |
| 80 | + |
| 81 | + it('keeps toolbar controls open, clamps zoom, and resets to fit', async () => { |
| 82 | + await click('Zoom in') |
| 83 | + expect(media().style.zoom).toBe('1.25') |
| 84 | + await click('Zoom out') |
| 85 | + expect(media().style.zoom).toBe('1') |
| 86 | + |
| 87 | + wheel({ ctrlKey: true, deltaY: -10000 }) |
| 88 | + expect(media().style.zoom).toBe('4') |
| 89 | + expect(button('Zoom in').disabled).toBe(true) |
| 90 | + wheel({ ctrlKey: true, deltaY: 10000 }) |
| 91 | + expect(media().style.zoom).toBe('0.25') |
| 92 | + expect(button('Zoom out').disabled).toBe(true) |
| 93 | + await click('Reset zoom (25%)') |
| 94 | + expect(media().style.zoom).toBe('1') |
| 95 | + }) |
| 96 | + |
| 97 | + it('preserves the point beneath the gesture when zoom changes', () => { |
| 98 | + const frame = button('Close media viewer') |
| 99 | + const viewport = frame.parentElement?.parentElement |
| 100 | + if (!viewport) throw new Error('Missing viewport') |
| 101 | + const getBounds = vi.spyOn(frame, 'getBoundingClientRect') |
| 102 | + getBounds.mockReturnValueOnce(new DOMRect(100, 100, 400, 300)) |
| 103 | + getBounds.mockReturnValueOnce(new DOMRect(100, 100, 800, 600)) |
| 104 | + |
| 105 | + wheel({ ctrlKey: true, deltaY: -Math.log(2) / 0.005, clientX: 200, clientY: 175 }) |
| 106 | + |
| 107 | + expect(viewport.scrollLeft).toBeCloseTo(100) |
| 108 | + expect(viewport.scrollTop).toBeCloseTo(75) |
| 109 | + }) |
| 110 | + |
| 111 | + it('closes on the image, restores focus, and binds gestures again after reopening', async () => { |
| 112 | + const oldMedia = media() |
| 113 | + wheel({ ctrlKey: true, deltaY: -40 }) |
| 114 | + await click('Close media viewer') |
| 115 | + expect(document.querySelector('[role="dialog"]')).toBeNull() |
| 116 | + await vi.waitFor(() => expect(document.activeElement).toBe(button('Open image'))) |
| 117 | + const detachedWheel = new WheelEvent('wheel', { |
| 118 | + bubbles: true, |
| 119 | + cancelable: true, |
| 120 | + ctrlKey: true, |
| 121 | + deltaY: -40, |
| 122 | + }) |
| 123 | + oldMedia.dispatchEvent(detachedWheel) |
| 124 | + expect(detachedWheel.defaultPrevented).toBe(false) |
| 125 | + |
| 126 | + await click('Open image') |
| 127 | + expect(media().style.zoom).toBe('1') |
| 128 | + expect(wheel({ ctrlKey: true, deltaY: -40 }).defaultPrevented).toBe(true) |
| 129 | + expect(Number(media().style.zoom)).toBeGreaterThan(1) |
| 130 | + }) |
| 131 | + |
| 132 | + it('dismisses on the viewport background and Escape', async () => { |
| 133 | + const dialog = document.querySelector<HTMLElement>('[role="dialog"]') |
| 134 | + await act(async () => dialog?.click()) |
| 135 | + expect(document.querySelector('[role="dialog"]')).toBeNull() |
| 136 | + await click('Open image') |
| 137 | + await act(async () => { |
| 138 | + document.dispatchEvent(new KeyboardEvent('keydown', { key: 'Escape', bubbles: true })) |
| 139 | + }) |
| 140 | + expect(document.querySelector('[role="dialog"]')).toBeNull() |
| 141 | + }) |
| 142 | +}) |
0 commit comments