|
| 1 | +/** |
| 2 | + * @vitest-environment jsdom |
| 3 | + */ |
| 4 | +import { act } from 'react' |
| 5 | +import { createRoot, type Root } from 'react-dom/client' |
| 6 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' |
| 7 | +import { CoreFeatureCard } from '@/app/(landing)/components/features/components/core-feature-card' |
| 8 | +import { LogsRunGraph } from '@/app/(landing)/components/shared/logs-run-graph' |
| 9 | +import { RunTraceGraphic } from '@/app/(landing)/logs/components/feature-graphics' |
| 10 | + |
| 11 | +let root: Root |
| 12 | +let host: HTMLDivElement |
| 13 | + |
| 14 | +beforeEach(() => { |
| 15 | + vi.useFakeTimers() |
| 16 | + ;(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true |
| 17 | + host = document.createElement('div') |
| 18 | + document.body.append(host) |
| 19 | + root = createRoot(host) |
| 20 | +}) |
| 21 | + |
| 22 | +afterEach(() => { |
| 23 | + act(() => root.unmount()) |
| 24 | + host.remove() |
| 25 | + vi.restoreAllMocks() |
| 26 | + vi.useRealTimers() |
| 27 | +}) |
| 28 | + |
| 29 | +function mount() { |
| 30 | + act(() => root.render(<LogsRunGraph layout='card' />)) |
| 31 | + const graph = host.querySelector<HTMLButtonElement>('button') |
| 32 | + if (!graph) throw new Error('Graph not rendered') |
| 33 | + return graph |
| 34 | +} |
| 35 | + |
| 36 | +function pointer(element: Element, type: string, pointerType = 'touch', x = 40, y = 300) { |
| 37 | + const event = new MouseEvent(type, { bubbles: true, clientX: x, clientY: y }) |
| 38 | + Object.defineProperty(event, 'pointerType', { value: pointerType }) |
| 39 | + act(() => element.dispatchEvent(event)) |
| 40 | +} |
| 41 | + |
| 42 | +describe('LogsRunGraph', () => { |
| 43 | + it('keeps tapped details visible and dismisses them on outside interaction', () => { |
| 44 | + const graph = mount() |
| 45 | + const bar = graph.querySelectorAll('[data-run-count]')[1] |
| 46 | + pointer(bar, 'pointerdown') |
| 47 | + pointer(bar, 'pointerup') |
| 48 | + pointer(graph, 'pointerout') |
| 49 | + expect(document.querySelector('[role="tooltip"]')?.textContent).toContain('5 succeeded') |
| 50 | + expect(graph.getAttribute('aria-label')).toContain('23–22 hours ago') |
| 51 | + pointer(document.body, 'pointerdown') |
| 52 | + expect(document.querySelector('[role="tooltip"]')).toBeNull() |
| 53 | + }) |
| 54 | + |
| 55 | + it.each(['pointercancel', 'swipe'])('does not open details after a %s gesture', (gesture) => { |
| 56 | + const graph = mount() |
| 57 | + pointer(graph, 'pointerdown') |
| 58 | + if (gesture === 'pointercancel') pointer(graph, 'pointercancel') |
| 59 | + pointer(graph, 'pointerup', 'touch', gesture === 'swipe' ? 120 : 40) |
| 60 | + expect(document.querySelector('[role="tooltip"]')).toBeNull() |
| 61 | + }) |
| 62 | + |
| 63 | + it.each(['scroll', 'resize'])( |
| 64 | + 'dismisses details when the viewport changes through %s', |
| 65 | + (event) => { |
| 66 | + const graph = mount() |
| 67 | + pointer(graph, 'pointerdown') |
| 68 | + pointer(graph, 'pointerup') |
| 69 | + expect(document.querySelector('[role="tooltip"]')).not.toBeNull() |
| 70 | + act(() => (event === 'resize' ? window : host).dispatchEvent(new Event(event))) |
| 71 | + expect(document.querySelector('[role="tooltip"]')).toBeNull() |
| 72 | + } |
| 73 | + ) |
| 74 | + |
| 75 | + it('supports keyboard exploration and Escape dismissal', () => { |
| 76 | + const graph = mount() |
| 77 | + act(() => graph.focus()) |
| 78 | + act(() => graph.dispatchEvent(new KeyboardEvent('keydown', { key: 'End', bubbles: true }))) |
| 79 | + expect(graph.getAttribute('aria-label')).toContain('Last hour: 7 succeeded') |
| 80 | + act(() => |
| 81 | + graph.dispatchEvent(new KeyboardEvent('keydown', { key: 'ArrowLeft', bubbles: true })) |
| 82 | + ) |
| 83 | + expect(graph.getAttribute('aria-label')).toContain('2–1 hours ago: 6 succeeded') |
| 84 | + act(() => graph.dispatchEvent(new KeyboardEvent('keydown', { key: 'Escape', bubbles: true }))) |
| 85 | + expect(document.querySelector('[role="tooltip"]')).toBeNull() |
| 86 | + }) |
| 87 | + |
| 88 | + it('keeps first-hour details inside a narrow viewport', () => { |
| 89 | + const graph = mount() |
| 90 | + vi.spyOn(window, 'innerWidth', 'get').mockReturnValue(320) |
| 91 | + pointer(graph, 'pointerdown') |
| 92 | + pointer(graph, 'pointerup') |
| 93 | + const tooltip = document.querySelector<HTMLElement>('[role="tooltip"]') |
| 94 | + expect(tooltip?.style.translate).toContain('calc(192px - 100%)') |
| 95 | + expect(tooltip?.className).toContain('w-[176px]') |
| 96 | + }) |
| 97 | + |
| 98 | + it('keeps the interactive chart outside the route link and decorative wrappers', () => { |
| 99 | + act(() => |
| 100 | + root.render( |
| 101 | + <CoreFeatureCard |
| 102 | + title='Logs' |
| 103 | + description='Inspect sample runs' |
| 104 | + href='/logs' |
| 105 | + visual={<RunTraceGraphic />} |
| 106 | + interactiveVisual |
| 107 | + /> |
| 108 | + ) |
| 109 | + ) |
| 110 | + const graph = host.querySelector('[data-run-overview-graph]') |
| 111 | + expect(graph).not.toBeNull() |
| 112 | + expect(graph?.closest('a, [aria-hidden="true"]')).toBeNull() |
| 113 | + expect(host.querySelector('a')?.getAttribute('href')).toBe('/logs') |
| 114 | + }) |
| 115 | +}) |
0 commit comments