|
| 1 | +import { createTestingPinia } from '@pinia/testing' |
| 2 | +import { render, screen } from '@testing-library/vue' |
| 3 | +import { setActivePinia } from 'pinia' |
| 4 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' |
| 5 | +import { reactive } from 'vue' |
| 6 | + |
| 7 | +import NodeBoxOverlay from '@/renderer/extensions/vueNodes/components/NodeBoxOverlay.vue' |
| 8 | + |
| 9 | +const callbacks = vi.hoisted(() => ({ |
| 10 | + frame: vi.fn<() => void>(), |
| 11 | + resize: vi.fn<() => void>() |
| 12 | +})) |
| 13 | + |
| 14 | +vi.mock('@vueuse/core', () => ({ |
| 15 | + useRafFn: (callback: () => void) => |
| 16 | + callbacks.frame.mockImplementation(callback), |
| 17 | + useResizeObserver: (_target: unknown, callback: () => void) => |
| 18 | + callbacks.resize.mockImplementation(callback) |
| 19 | +})) |
| 20 | + |
| 21 | +const camera = reactive({ x: -100, y: -50, z: 2 }) |
| 22 | + |
| 23 | +vi.mock('@/renderer/core/layout/transform/useTransformState', () => ({ |
| 24 | + useTransformState: () => ({ camera }) |
| 25 | +})) |
| 26 | + |
| 27 | +const originalDpr = window.devicePixelRatio |
| 28 | + |
| 29 | +function setDpr(value: number): void { |
| 30 | + Object.defineProperty(window, 'devicePixelRatio', { |
| 31 | + configurable: true, |
| 32 | + value |
| 33 | + }) |
| 34 | +} |
| 35 | + |
| 36 | +beforeEach(() => { |
| 37 | + setActivePinia(createTestingPinia({ stubActions: false })) |
| 38 | + Object.assign(camera, { x: -100, y: -50, z: 2 }) |
| 39 | + setDpr(2) |
| 40 | +}) |
| 41 | + |
| 42 | +afterEach(() => setDpr(originalDpr)) |
| 43 | + |
| 44 | +describe('NodeBoxOverlay', () => { |
| 45 | + it('draws the visible graph region once per render signature', async () => { |
| 46 | + const getBoxes = vi.fn(() => []) |
| 47 | + const context = { |
| 48 | + clearRect: vi.fn(), |
| 49 | + fillRect: vi.fn(), |
| 50 | + restore: vi.fn(), |
| 51 | + save: vi.fn(), |
| 52 | + scale: vi.fn(), |
| 53 | + setTransform: vi.fn(), |
| 54 | + translate: vi.fn() |
| 55 | + } |
| 56 | + const { rerender } = render(NodeBoxOverlay, { props: { getBoxes } }) |
| 57 | + const canvas = screen.getByTestId<HTMLCanvasElement>('node-box-overlay') |
| 58 | + Object.defineProperties(canvas, { |
| 59 | + clientHeight: { configurable: true, value: 200 }, |
| 60 | + clientWidth: { configurable: true, value: 400 }, |
| 61 | + getContext: { configurable: true, value: () => context } |
| 62 | + }) |
| 63 | + |
| 64 | + callbacks.resize() |
| 65 | + callbacks.frame() |
| 66 | + |
| 67 | + expect(canvas.width).toBe(800) |
| 68 | + expect(canvas.height).toBe(400) |
| 69 | + expect(context.setTransform).toHaveBeenCalledWith(2, 0, 0, 2, 0, 0) |
| 70 | + expect(context.clearRect).toHaveBeenCalledWith(0, 0, 400, 200) |
| 71 | + expect(getBoxes).toHaveBeenCalledWith({ |
| 72 | + x: 100, |
| 73 | + y: 50, |
| 74 | + width: 200, |
| 75 | + height: 100 |
| 76 | + }) |
| 77 | + |
| 78 | + callbacks.frame() |
| 79 | + expect(getBoxes).toHaveBeenCalledOnce() |
| 80 | + |
| 81 | + await rerender({ contentVersion: 1, getBoxes }) |
| 82 | + callbacks.frame() |
| 83 | + expect(getBoxes).toHaveBeenCalledTimes(2) |
| 84 | + }) |
| 85 | +}) |
0 commit comments