|
| 1 | +import { render, screen } from '@testing-library/react' |
| 2 | +import { describe, expect, it } from 'vitest' |
| 3 | +import type { SomeCompanionInputField } from '@companion-app/shared/Model/Options.js' |
| 4 | +import { Grid } from '~/Components/Grid.js' |
| 5 | +import { MenuPortalContext } from '~/Components/MenuPortalContext.js' |
| 6 | +import { RootAppStoreContext, type RootAppStore } from '~/Stores/RootAppStore.js' |
| 7 | +import { OptionsInputField } from '../OptionsInputField.js' |
| 8 | + |
| 9 | +const mockStore: Partial<RootAppStore> = { |
| 10 | + variablesStore: { |
| 11 | + allVariableDefinitions: { get: () => [] }, |
| 12 | + } as unknown as RootAppStore['variablesStore'], |
| 13 | +} |
| 14 | + |
| 15 | +function renderField(option: SomeCompanionInputField, fieldSupportsExpression: boolean) { |
| 16 | + return render( |
| 17 | + <RootAppStoreContext.Provider value={mockStore as RootAppStore}> |
| 18 | + <MenuPortalContext.Provider value={document.body}> |
| 19 | + <Grid.Row> |
| 20 | + <OptionsInputField |
| 21 | + allowInternalFields={false} |
| 22 | + isLocatedInGrid={false} |
| 23 | + entityType={null} |
| 24 | + option={option} |
| 25 | + value={undefined} |
| 26 | + setValue={() => {}} |
| 27 | + visibility={true} |
| 28 | + localVariablesStore={null} |
| 29 | + fieldSupportsExpression={fieldSupportsExpression} |
| 30 | + /> |
| 31 | + </Grid.Row> |
| 32 | + </MenuPortalContext.Provider> |
| 33 | + </RootAppStoreContext.Provider> |
| 34 | + ) |
| 35 | +} |
| 36 | + |
| 37 | +const toggleName = /expression mode|value mode/i |
| 38 | + |
| 39 | +describe('OptionsInputField', () => { |
| 40 | + it('renders an expression toggle for a value field when expressions are supported', () => { |
| 41 | + renderField({ id: 'text', type: 'textinput', label: 'Text', default: '' }, true) |
| 42 | + expect(screen.getByRole('button', { name: toggleName })).toBeInTheDocument() |
| 43 | + }) |
| 44 | + |
| 45 | + it('does not render an expression toggle for a static-text field', () => { |
| 46 | + renderField({ id: 'note', type: 'static-text', label: 'Attention', value: 'Read me' }, true) |
| 47 | + expect(screen.getByText('Read me')).toBeInTheDocument() |
| 48 | + expect(screen.queryByRole('button', { name: toggleName })).toBeNull() |
| 49 | + }) |
| 50 | +}) |
0 commit comments