|
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' |
| 2 | +import { cleanup, fireEvent, render } from '@testing-library/react' |
| 3 | +import { QueryClient, QueryClientProvider } from '@tanstack/react-query' |
| 4 | +import { CommentButton, CommentHint, CommentsProvider, PageThreads } from '../components/Comments' |
| 5 | +import { PageImages } from '../pages/SnapshotPage' |
| 6 | +import type { PageRow } from '../api/types' |
| 7 | + |
| 8 | +// Both images are 800×1600 pixels, and jsdom lays every element out as a 400×800 box at the origin: a press at |
| 9 | +// (100, 200) is a quarter of the way across and down, so the comment's pixels are (200, 400) on whichever image it goes on. |
| 10 | +const page: PageRow = { |
| 11 | + position: 0, |
| 12 | + op: 'changed', |
| 13 | + state: 'changed', |
| 14 | + head_index: 0, |
| 15 | + base_index: 0, |
| 16 | + head_url: 'https://blobs.test/head.png', |
| 17 | + base_url: 'https://blobs.test/base.png', |
| 18 | + overlay_url: 'https://blobs.test/diff.png', |
| 19 | + expired: false, |
| 20 | + base_size: [800, 1600], |
| 21 | + head_size: [800, 1600], |
| 22 | + diff: null, |
| 23 | + text_diff: [], |
| 24 | +} |
| 25 | + |
| 26 | +beforeEach(() => { |
| 27 | + vi.spyOn(Element.prototype, 'getBoundingClientRect').mockReturnValue({ |
| 28 | + left: 0, top: 0, right: 400, bottom: 800, width: 400, height: 800, x: 0, y: 0, toJSON: () => ({}), |
| 29 | + } as DOMRect) |
| 30 | + // jsdom has no pointer capture. |
| 31 | + Element.prototype.setPointerCapture = () => {} |
| 32 | + Element.prototype.hasPointerCapture = () => false |
| 33 | +}) |
| 34 | +afterEach(() => { |
| 35 | + cleanup() |
| 36 | + vi.restoreAllMocks() |
| 37 | +}) |
| 38 | + |
| 39 | +function view(mode: 'side' | 'diff' | 'slider' | 'base' | 'head') { |
| 40 | + const client = new QueryClient({ defaultOptions: { queries: { enabled: false, retry: false } } }) |
| 41 | + return render( |
| 42 | + <QueryClientProvider client={client}> |
| 43 | + <CommentsProvider |
| 44 | + org="acme" |
| 45 | + project="web" |
| 46 | + build={3} |
| 47 | + snapshotId="s1" |
| 48 | + threads={[]} |
| 49 | + compared={{ kind: 'baseline' }} |
| 50 | + previousBuild={null} |
| 51 | + showPrevious={() => {}} |
| 52 | + canComment |
| 53 | + canResolve |
| 54 | + > |
| 55 | + <CommentButton page={0} /> |
| 56 | + <CommentHint page={0} extra="It goes on the image where you press." /> |
| 57 | + <PageImages page={page} mode={mode} highlight="off" onExpired={() => {}} name="dashboard/390-light" kind="image" /> |
| 58 | + <PageThreads page={0} /> |
| 59 | + </CommentsProvider> |
| 60 | + </QueryClientProvider>, |
| 61 | + ) |
| 62 | +} |
| 63 | + |
| 64 | +/** Turn comment mode on, press and release at (100, 200) on `layer`, and return where the draft pin landed. */ |
| 65 | +function comment(container: HTMLElement, layer: Element) { |
| 66 | + fireEvent.pointerDown(layer, { clientX: 100, clientY: 200, button: 0, pointerId: 1 }) |
| 67 | + fireEvent.pointerUp(layer, { clientX: 100, clientY: 200, pointerId: 1 }) |
| 68 | + const pin = container.querySelector('.pin[data-draft]') |
| 69 | + expect(pin, 'a draft pin').toBeTruthy() |
| 70 | + const point = pin!.closest('.pin-point') as HTMLElement |
| 71 | + return { pin: pin!, left: point.style.left, top: point.style.top, text: container.querySelector('.composer')?.textContent ?? '' } |
| 72 | +} |
| 73 | + |
| 74 | +const placing = (container: HTMLElement) => [...container.querySelectorAll('.pins[data-placing]')] |
| 75 | + |
| 76 | +describe('comment pins in each view mode', () => { |
| 77 | + it('side by side: on the image pressed', () => { |
| 78 | + const { container, getByRole } = view('side') |
| 79 | + fireEvent.click(getByRole('button', { name: /comment/i })) |
| 80 | + const layers = placing(container) |
| 81 | + expect(layers).toHaveLength(2) |
| 82 | + const at = comment(container, layers[0]) |
| 83 | + expect(at.text).toContain('A point on the old image') |
| 84 | + expect([at.left, at.top]).toEqual(['25%', '25%']) |
| 85 | + }) |
| 86 | + |
| 87 | + it('diff: on this build, in its pixels', () => { |
| 88 | + const { container, getByRole } = view('diff') |
| 89 | + fireEvent.click(getByRole('button', { name: /comment/i })) |
| 90 | + // The base side's layer is under the head side's, which takes the press. |
| 91 | + const layers = placing(container) |
| 92 | + const head = layers.find((l) => l.closest('[data-side]')?.getAttribute('data-side') === 'head')! |
| 93 | + expect(head).toBe(layers[layers.length - 1]) |
| 94 | + const at = comment(container, head) |
| 95 | + expect(at.text).toContain('A point on the new image') |
| 96 | + expect(at.pin.closest('[data-side]')?.getAttribute('data-side')).toBe('head') |
| 97 | + expect([at.left, at.top]).toEqual(['25%', '25%']) |
| 98 | + }) |
| 99 | + |
| 100 | + it("slider: each side's layer covers its side of the divider, and a press there doesn't move it", () => { |
| 101 | + const { container, getByRole } = view('slider') |
| 102 | + fireEvent.click(getByRole('button', { name: /comment/i })) |
| 103 | + const layer = (side: string) => container.querySelector(`.pins-layer[data-side="${side}"]`) as HTMLElement |
| 104 | + // Divider at 50%: the old image's pins left of it, the new image's from it on (a press on the line is new). |
| 105 | + expect(layer('base').style.clipPath).toBe('inset(0 50% 0 0)') |
| 106 | + expect(layer('head').style.clipPath).toBe('inset(0 0 0 50%)') |
| 107 | + |
| 108 | + const slider = getByRole('slider') |
| 109 | + const at = comment(container, layer('base').querySelector('.pins')!) |
| 110 | + expect(at.text).toContain('A point on the old image') |
| 111 | + expect([at.left, at.top]).toEqual(['25%', '25%']) |
| 112 | + expect(slider.getAttribute('aria-valuenow')).toBe('50') |
| 113 | + |
| 114 | + // The handle still drags the divider, and so do the arrow keys. |
| 115 | + fireEvent.pointerDown(container.querySelector('.slider-handle')!, { clientX: 300, button: 0, pointerId: 2 }) |
| 116 | + expect(slider.getAttribute('aria-valuenow')).toBe('75') |
| 117 | + fireEvent.keyDown(slider, { key: 'ArrowLeft' }) |
| 118 | + expect(slider.getAttribute('aria-valuenow')).toBe('73') |
| 119 | + expect(layer('head').style.clipPath).toBe('inset(0 0 0 73%)') |
| 120 | + }) |
| 121 | + |
| 122 | + it('slider without comment mode: a press anywhere moves the divider, and a drag follows', () => { |
| 123 | + const { container, getByRole } = view('slider') |
| 124 | + const slider = getByRole('slider') |
| 125 | + fireEvent.pointerDown(container.querySelector('.slider-img')!, { clientX: 40, button: 0, pointerId: 1 }) |
| 126 | + expect(slider.getAttribute('aria-valuenow')).toBe('10') |
| 127 | + fireEvent.pointerMove(slider, { clientX: 200, pointerId: 1 }) |
| 128 | + expect(slider.getAttribute('aria-valuenow')).toBe('50') |
| 129 | + fireEvent.pointerUp(slider, { pointerId: 1 }) |
| 130 | + fireEvent.pointerMove(slider, { clientX: 300, pointerId: 1 }) |
| 131 | + expect(slider.getAttribute('aria-valuenow')).toBe('50') |
| 132 | + expect(container.querySelector('input[type="range"]')).toBeNull() |
| 133 | + }) |
| 134 | + |
| 135 | + it.each(['base', 'head'] as const)('%s view: on the image shown', (mode) => { |
| 136 | + const { container, getByRole } = view(mode) |
| 137 | + fireEvent.click(getByRole('button', { name: /comment/i })) |
| 138 | + const layers = placing(container) |
| 139 | + expect(layers).toHaveLength(1) |
| 140 | + const at = comment(container, layers[0]) |
| 141 | + expect(at.text).toContain(mode === 'base' ? 'A point on the old image' : 'A point on the new image') |
| 142 | + expect([at.left, at.top]).toEqual(['25%', '25%']) |
| 143 | + }) |
| 144 | + |
| 145 | + it('shows the hint while commenting', () => { |
| 146 | + const { getByRole, queryByRole } = view('side') |
| 147 | + expect(queryByRole('status')).toBeNull() |
| 148 | + fireEvent.click(getByRole('button', { name: /comment/i })) |
| 149 | + expect(getByRole('status').textContent).toBe('Click for a point, drag for an area. It goes on the image where you press.') |
| 150 | + }) |
| 151 | +}) |
0 commit comments