|
| 1 | +import { screen } from '@testing-library/react'; |
| 2 | +import userEvent from '@testing-library/user-event'; |
| 3 | +import React from 'react'; |
| 4 | +import { PipelineInput } from 'src/libs/ajax/teaspoons/teaspoons-models'; |
| 5 | +import { renderWithAppContexts as render } from 'src/testing/test-utils'; |
| 6 | + |
| 7 | +import { PipelineBooleanInput } from './PipelineBooleanInput'; |
| 8 | + |
| 9 | +describe('PipelineBooleanInput', () => { |
| 10 | + const mockOnChange = jest.fn(); |
| 11 | + |
| 12 | + const basePipelineInput: PipelineInput = { |
| 13 | + name: 'testInput', |
| 14 | + displayName: 'Test Input', |
| 15 | + description: 'This is a test input description', |
| 16 | + type: 'BOOLEAN', |
| 17 | + defaultValue: 'false', |
| 18 | + isRequired: false, |
| 19 | + }; |
| 20 | + |
| 21 | + describe('rendering', () => { |
| 22 | + it('renders with display name and description', () => { |
| 23 | + render(<PipelineBooleanInput input={basePipelineInput} value={false} onChange={mockOnChange} />); |
| 24 | + |
| 25 | + expect(screen.getByText('Test Input')).toBeInTheDocument(); |
| 26 | + expect(screen.getByText('This is a test input description')).toBeInTheDocument(); |
| 27 | + }); |
| 28 | + |
| 29 | + it('renders with input name when display name is not provided', () => { |
| 30 | + const inputWithoutDisplayName = { ...basePipelineInput, displayName: undefined }; |
| 31 | + render(<PipelineBooleanInput input={inputWithoutDisplayName} value={false} onChange={mockOnChange} />); |
| 32 | + |
| 33 | + expect(screen.getByText('testInput')).toBeInTheDocument(); |
| 34 | + }); |
| 35 | + |
| 36 | + it('does not render description when not provided', () => { |
| 37 | + const inputWithoutDescription = { ...basePipelineInput, description: undefined }; |
| 38 | + render(<PipelineBooleanInput input={inputWithoutDescription} value={false} onChange={mockOnChange} />); |
| 39 | + |
| 40 | + expect(screen.queryByText('This is a test input description')).not.toBeInTheDocument(); |
| 41 | + }); |
| 42 | + |
| 43 | + it('renders required indicator when input is required', () => { |
| 44 | + const requiredInput = { ...basePipelineInput, isRequired: true }; |
| 45 | + render(<PipelineBooleanInput input={requiredInput} value={false} onChange={mockOnChange} />); |
| 46 | + |
| 47 | + expect(screen.getByText('*')).toBeInTheDocument(); |
| 48 | + }); |
| 49 | + |
| 50 | + it('does not render required indicator when input is not required', () => { |
| 51 | + render(<PipelineBooleanInput input={basePipelineInput} value={false} onChange={mockOnChange} />); |
| 52 | + |
| 53 | + expect(screen.queryByText('*')).not.toBeInTheDocument(); |
| 54 | + }); |
| 55 | + |
| 56 | + it('renders checkbox as unchecked when value is false', () => { |
| 57 | + render(<PipelineBooleanInput input={basePipelineInput} value={false} onChange={mockOnChange} />); |
| 58 | + |
| 59 | + const checkbox = screen.getByRole('checkbox'); |
| 60 | + expect(checkbox).not.toBeChecked(); |
| 61 | + }); |
| 62 | + |
| 63 | + it('renders checkbox as checked when value is true', () => { |
| 64 | + render(<PipelineBooleanInput input={basePipelineInput} value onChange={mockOnChange} />); |
| 65 | + |
| 66 | + const checkbox = screen.getByRole('checkbox'); |
| 67 | + expect(checkbox).toBeChecked(); |
| 68 | + }); |
| 69 | + |
| 70 | + it('uses default value when value prop is undefined', () => { |
| 71 | + const inputWithDefaultTrue = { ...basePipelineInput, defaultValue: 'true' }; |
| 72 | + render(<PipelineBooleanInput input={inputWithDefaultTrue} value={undefined as any} onChange={mockOnChange} />); |
| 73 | + |
| 74 | + const checkbox = screen.getByRole('checkbox'); |
| 75 | + expect(checkbox).toBeChecked(); |
| 76 | + }); |
| 77 | + |
| 78 | + it('falls back to false when both value and defaultValue are undefined', () => { |
| 79 | + const inputWithoutDefault = { ...basePipelineInput, defaultValue: undefined }; |
| 80 | + render(<PipelineBooleanInput input={inputWithoutDefault} value={undefined as any} onChange={mockOnChange} />); |
| 81 | + |
| 82 | + const checkbox = screen.getByRole('checkbox'); |
| 83 | + expect(checkbox).not.toBeChecked(); |
| 84 | + }); |
| 85 | + |
| 86 | + it('calls onChange when checkbox is clicked', async () => { |
| 87 | + const user = userEvent.setup(); |
| 88 | + render(<PipelineBooleanInput input={basePipelineInput} value={false} onChange={mockOnChange} />); |
| 89 | + |
| 90 | + const checkbox = screen.getByRole('checkbox'); |
| 91 | + await user.click(checkbox); |
| 92 | + |
| 93 | + expect(mockOnChange).toHaveBeenCalledWith(true); |
| 94 | + }); |
| 95 | + |
| 96 | + it('calls onChange with correct value when toggling from true to false', async () => { |
| 97 | + const user = userEvent.setup(); |
| 98 | + render(<PipelineBooleanInput input={basePipelineInput} value onChange={mockOnChange} />); |
| 99 | + |
| 100 | + const checkbox = screen.getByRole('checkbox'); |
| 101 | + await user.click(checkbox); |
| 102 | + |
| 103 | + expect(mockOnChange).toHaveBeenCalledWith(false); |
| 104 | + }); |
| 105 | + |
| 106 | + it('calls onChange when clicking the label text', async () => { |
| 107 | + const user = userEvent.setup(); |
| 108 | + render(<PipelineBooleanInput input={basePipelineInput} value={false} onChange={mockOnChange} />); |
| 109 | + |
| 110 | + const label = screen.getByText('Test Input'); |
| 111 | + await user.click(label); |
| 112 | + |
| 113 | + expect(mockOnChange).toHaveBeenCalledWith(true); |
| 114 | + }); |
| 115 | + }); |
| 116 | +}); |
0 commit comments