|
| 1 | +/* eslint-disable testing-library/prefer-user-event -- crop dragging needs low-level pointer events */ |
| 2 | +import { fireEvent, render, screen } from '@testing-library/vue' |
| 3 | +import { describe, expect, it, vi } from 'vitest' |
| 4 | +import { ref } from 'vue' |
| 5 | +import { createI18n } from 'vue-i18n' |
| 6 | + |
| 7 | +import type { Bounds } from '@/renderer/core/layout/types' |
| 8 | + |
| 9 | +import VideoCropOverlay from './VideoCropOverlay.vue' |
| 10 | + |
| 11 | +const i18n = createI18n({ |
| 12 | + legacy: false, |
| 13 | + locale: 'en', |
| 14 | + messages: { |
| 15 | + en: { |
| 16 | + videoEdit: { |
| 17 | + adjustCrop: 'Adjust crop region' |
| 18 | + } |
| 19 | + } |
| 20 | + } |
| 21 | +}) |
| 22 | + |
| 23 | +function renderOverlay({ |
| 24 | + bounds = { x: 100, y: 100, width: 200, height: 200 }, |
| 25 | + disabled = false, |
| 26 | + lockedRatio = null as number | null |
| 27 | +} = {}) { |
| 28 | + const model = ref<Bounds>(bounds) |
| 29 | + render(VideoCropOverlay, { |
| 30 | + props: { |
| 31 | + modelValue: model.value, |
| 32 | + sourceWidth: 1000, |
| 33 | + sourceHeight: 1000, |
| 34 | + disabled, |
| 35 | + lockedRatio, |
| 36 | + 'onUpdate:modelValue': (value: Bounds) => { |
| 37 | + model.value = value |
| 38 | + } |
| 39 | + }, |
| 40 | + global: { |
| 41 | + plugins: [i18n] |
| 42 | + } |
| 43 | + }) |
| 44 | + |
| 45 | + const root = screen.getByTestId('video-crop-overlay') |
| 46 | + vi.spyOn(root, 'getBoundingClientRect').mockReturnValue({ |
| 47 | + left: 0, |
| 48 | + top: 0, |
| 49 | + width: 100, |
| 50 | + height: 100, |
| 51 | + right: 100, |
| 52 | + bottom: 100, |
| 53 | + x: 0, |
| 54 | + y: 0, |
| 55 | + toJSON: () => ({}) |
| 56 | + }) |
| 57 | + |
| 58 | + return { model } |
| 59 | +} |
| 60 | + |
| 61 | +describe('VideoCropOverlay', () => { |
| 62 | + it('positions the crop box by source-relative percentages', () => { |
| 63 | + renderOverlay({ bounds: { x: 100, y: 200, width: 500, height: 250 } }) |
| 64 | + |
| 65 | + const box = screen.getByTestId('crop-box') |
| 66 | + expect(box.style.left).toBe('10%') |
| 67 | + expect(box.style.top).toBe('20%') |
| 68 | + expect(box.style.width).toBe('50%') |
| 69 | + expect(box.style.height).toBe('25%') |
| 70 | + }) |
| 71 | + |
| 72 | + it('renders eight resize handles', () => { |
| 73 | + renderOverlay() |
| 74 | + |
| 75 | + for (const dir of ['n', 's', 'e', 'w', 'ne', 'nw', 'se', 'sw']) { |
| 76 | + expect(screen.getByTestId(`crop-handle-${dir}`)).toBeTruthy() |
| 77 | + } |
| 78 | + }) |
| 79 | + |
| 80 | + it('moves the crop box by dragging it', async () => { |
| 81 | + const { model } = renderOverlay() |
| 82 | + |
| 83 | + const box = screen.getByTestId('crop-box') |
| 84 | + box.setPointerCapture = vi.fn() |
| 85 | + |
| 86 | + await fireEvent.pointerDown(box, { |
| 87 | + clientX: 0, |
| 88 | + clientY: 0, |
| 89 | + button: 0, |
| 90 | + pointerId: 1 |
| 91 | + }) |
| 92 | + await fireEvent.pointerMove(box, { |
| 93 | + clientX: 5, |
| 94 | + clientY: 7, |
| 95 | + pointerId: 1 |
| 96 | + }) |
| 97 | + await fireEvent.pointerUp(box, { pointerId: 1 }) |
| 98 | + |
| 99 | + expect(model.value).toEqual({ x: 150, y: 170, width: 200, height: 200 }) |
| 100 | + }) |
| 101 | + |
| 102 | + it('resizes the crop box from a corner handle', async () => { |
| 103 | + const { model } = renderOverlay() |
| 104 | + |
| 105 | + const handle = screen.getByTestId('crop-handle-se') |
| 106 | + handle.setPointerCapture = vi.fn() |
| 107 | + |
| 108 | + await fireEvent.pointerDown(handle, { |
| 109 | + clientX: 0, |
| 110 | + clientY: 0, |
| 111 | + button: 0, |
| 112 | + pointerId: 1 |
| 113 | + }) |
| 114 | + await fireEvent.pointerMove(handle, { |
| 115 | + clientX: 10, |
| 116 | + clientY: 5, |
| 117 | + pointerId: 1 |
| 118 | + }) |
| 119 | + await fireEvent.pointerUp(handle, { pointerId: 1 }) |
| 120 | + |
| 121 | + expect(model.value).toEqual({ x: 100, y: 100, width: 300, height: 250 }) |
| 122 | + }) |
| 123 | + |
| 124 | + it('does not react to drags while disabled', async () => { |
| 125 | + const { model } = renderOverlay({ disabled: true }) |
| 126 | + |
| 127 | + const box = screen.getByTestId('crop-box') |
| 128 | + box.setPointerCapture = vi.fn() |
| 129 | + |
| 130 | + await fireEvent.pointerDown(box, { |
| 131 | + clientX: 0, |
| 132 | + clientY: 0, |
| 133 | + button: 0, |
| 134 | + pointerId: 1 |
| 135 | + }) |
| 136 | + await fireEvent.pointerMove(box, { clientX: 5, clientY: 5, pointerId: 1 }) |
| 137 | + |
| 138 | + expect(model.value).toEqual({ x: 100, y: 100, width: 200, height: 200 }) |
| 139 | + }) |
| 140 | +}) |
0 commit comments