Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
297 changes: 297 additions & 0 deletions src/components/cameraAngle/CameraAngle.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,297 @@
import { render, screen, within } from '@testing-library/vue'
import userEvent from '@testing-library/user-event'
import { fromPartial } from '@total-typescript/shoehorn'
import { useElementSize } from '@vueuse/core'
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { defineComponent, nextTick, ref } from 'vue'
import { createI18n } from 'vue-i18n'

import type { SimplifiedWidget } from '@/types/simplifiedWidget'

const i18n = createI18n({
legacy: false,
locale: 'en',
messages: {
en: {
cameraAngle: {
cameraView: 'Camera',
objectView: 'Object',
cameraViewTooltip: 'Move the camera',
objectViewTooltip: 'Turn the subject',
horizontalLabel: 'Horizontal angle',
verticalLabel: 'Vertical angle',
zoomLabel: 'Shot size',
preview: 'Preview',
showPreview: 'Show shot preview',
hidePreview: 'Hide shot preview',
horizontal: { front: 'Front', back: 'Back' },
vertical: { eyeLevel: 'Eye level', highAngle: 'High angle' },
zoom: { medium: 'Medium shot', closeUp: 'Close-up' },
faces: {
back: 'BACK',
left: 'LEFT',
right: 'RIGHT',
top: 'TOP',
bottom: 'BOTTOM'
}
}
}
}
})

type ApiMocks = Record<string, ReturnType<typeof vi.fn>>

const holder = vi.hoisted(() => ({
state: null as { value: Record<string, number> } | null,
viewMode: null as { value: string } | null,
prompt: null as { value: string } | null,
previewVisible: null as { value: boolean } | null,
api: null as ApiMocks | null
}))

const toolbarWidth = ref(600)

vi.mock(import('@vueuse/core'), { spy: true })

vi.mock<unknown>(import('@/composables/useCameraAngle'), async () => {
const { ref } = await import('vue')
const state = ref({ horizontal: 0, vertical: 0, zoom: 5 })
const viewMode = ref('camera')
const prompt = ref('front view eye-level shot medium shot')
const previewVisible = ref(false)
const api = {
initialize: vi.fn(),
cleanup: vi.fn(),
handleMouseEnter: vi.fn(),
handleMouseLeave: vi.fn(),
setViewMode: vi.fn(),
setPreviewVisible: vi.fn(),
setField: vi.fn()
}
holder.state = state
holder.viewMode = viewMode
holder.prompt = prompt
holder.previewVisible = previewVisible
holder.api = api
return {
useCameraAngle: () => ({ ...api, state, viewMode, prompt, previewVisible })
}
})

const { getNodeByLocatorIdMock } = vi.hoisted(() => ({
getNodeByLocatorIdMock: vi.fn(() => ({ id: 7 }))
}))

vi.mock<unknown>(import('@/scripts/app'), () => ({
app: { rootGraphOrUndefined: {} }
}))
vi.mock<unknown>(import('@/utils/graphTraversalUtil'), () => ({
getNodeByLocatorId: getNodeByLocatorIdMock
}))

import { LGraph } from '@/lib/litegraph/src/LGraph'
import { toNodeId } from '@/types/nodeId'
import { createNodeLocatorId } from '@/types/nodeIdentification'
import { useCanvasStore } from '@/renderer/core/canvas/canvasStore'

import CameraAngle from './CameraAngle.vue'

const SelectStub = defineComponent({
name: 'Select',
props: {
modelValue: { type: String, default: '' },
open: { type: Boolean, default: false }
},
emits: ['update:modelValue', 'update:open'],
template: `
<div data-testid="preset-select" :data-value="modelValue" :data-open="open">
<button data-testid="open" @click="$emit('update:open', true)">open</button>
<button data-testid="close" @click="$emit('update:open', false)">close</button>
<button data-testid="pick-back" @click="$emit('update:modelValue', 'back')">back</button>
<button data-testid="pick-close-up" @click="$emit('update:modelValue', 'closeUp')">close-up</button>
<slot />
</div>
`
})
const Passthrough = defineComponent({
name: 'SelectPassthrough',
template: '<slot />'
})

function makeWidget(): SimplifiedWidget {
return {
name: 'view',
type: 'cameraAngle',
value: 'camera',
options: {},
nodeLocatorId: createNodeLocatorId(null, toNodeId(7))
}
}

function renderComponent() {
return render(CameraAngle, {
props: { widget: makeWidget() },
global: {
plugins: [i18n],
directives: { tooltip: {} },
stubs: {
Select: SelectStub,
SelectContent: Passthrough,
SelectTrigger: Passthrough,
SelectValue: Passthrough,
SelectItem: Passthrough
}
}
})
}

function api(): ApiMocks {
return holder.api!
}

describe('CameraAngle', () => {
beforeEach(() => {
useCanvasStore().currentGraph = new LGraph()
holder.state!.value = { horizontal: 0, vertical: 0, zoom: 5 }
holder.viewMode!.value = 'camera'
holder.previewVisible!.value = false
toolbarWidth.value = 600
vi.mocked(useElementSize).mockImplementation(() =>
fromPartial({ width: toolbarWidth, height: ref(400) })
)
Object.values(api()).forEach((fn) => fn.mockClear())
})

it('initializes the viewport on mount and cleans up on unmount', () => {
const { unmount } = renderComponent()
expect(getNodeByLocatorIdMock).toHaveBeenCalledWith(
expect.anything(),
createNodeLocatorId(null, toNodeId(7))
)
expect(api().initialize).toHaveBeenCalledOnce()

unmount()
expect(api().cleanup).toHaveBeenCalledOnce()
})

it('switches to object view from the toolbar', async () => {
renderComponent()
const user = userEvent.setup()

expect(screen.getByRole('button', { name: 'Camera' })).toHaveAttribute(
'aria-pressed',
'true'
)
await user.click(screen.getByRole('button', { name: 'Object' }))

expect(api().setViewMode).toHaveBeenCalledWith('object')
})

it('toggles the shot preview from the toolbar and disables it in object view', async () => {
renderComponent()
const user = userEvent.setup()

const button = screen.getByRole('button', { name: 'Show shot preview' })
expect(button).toHaveAttribute('aria-pressed', 'false')
await user.click(button)
expect(api().setPreviewVisible).toHaveBeenCalledWith(true)

holder.viewMode!.value = 'object'
await nextTick()
expect(
screen.getByRole('button', { name: 'Show shot preview' })
).toBeDisabled()
})

it('collapses the toolbar to icons when it gets narrow', async () => {
renderComponent()
expect(screen.getByRole('button', { name: 'Object' })).toHaveTextContent(
'Object'
)

toolbarWidth.value = 300
await nextTick()

const button = screen.getByRole('button', { name: 'Object' })
expect(button).not.toHaveTextContent('Object')
expect(
screen.getByRole('button', { name: 'Show shot preview' })
).not.toHaveTextContent('Preview')
})

it('shows the prompt that will be output', () => {
renderComponent()
expect(screen.getByTestId('camera-angle-prompt')).toHaveTextContent(
'front view eye-level shot medium shot'
)
})

it('reflects the current buckets in the preset selects', () => {
holder.state!.value = { horizontal: 180, vertical: 50, zoom: 9 }
renderComponent()

const values = screen
.getAllByTestId('preset-select')
.map((el) => el.dataset.value)
expect(values).toEqual(['back', 'highAngle', 'closeUp'])
})

it('applies a preset value when a bucket is chosen', async () => {
renderComponent()
const user = userEvent.setup()

const [horizontal, , zoom] = screen.getAllByTestId('preset-select')
await user.click(within(horizontal).getByTestId('pick-back'))
await user.click(within(zoom).getByTestId('pick-close-up'))

expect(api().setField).toHaveBeenCalledWith('horizontal', 180)
expect(api().setField).toHaveBeenCalledWith('zoom', 8)
})

it('keeps only one preset select open at a time', async () => {
renderComponent()
const user = userEvent.setup()
const [horizontal, vertical, zoom] = screen.getAllByTestId('preset-select')
const openStates = () =>
[horizontal, vertical, zoom].map((el) => el.dataset.open)

await user.click(within(horizontal).getByTestId('open'))
expect(openStates()).toEqual(['true', 'false', 'false'])

await user.click(within(zoom).getByTestId('open'))
expect(openStates()).toEqual(['false', 'false', 'true'])

within(horizontal).getByTestId('close').focus()
await user.keyboard('{Enter}')
expect(openStates()).toEqual(['false', 'false', 'true'])

within(zoom).getByTestId('close').focus()
await user.keyboard('{Enter}')
expect(openStates()).toEqual(['false', 'false', 'false'])
})

it('closes the open preset select when the viewport is pressed', async () => {
renderComponent()
const user = userEvent.setup()
const [, vertical] = screen.getAllByTestId('preset-select')
const [container] = api().initialize.mock.calls[0]

await user.click(within(vertical).getByTestId('open'))
expect(vertical.dataset.open).toBe('true')

await user.pointer({ keys: '[MouseLeft>]', target: container })
expect(vertical.dataset.open).toBe('false')
})

it('closes the open preset select when a toolbar control is pressed', async () => {
renderComponent()
const user = userEvent.setup()
const [horizontal] = screen.getAllByTestId('preset-select')

await user.click(within(horizontal).getByTestId('open'))
expect(horizontal.dataset.open).toBe('true')

await user.click(screen.getByRole('button', { name: 'Object' }))
expect(horizontal.dataset.open).toBe('false')
})
})
Loading
Loading