Skip to content

Commit

Permalink
add unit test for Dropdown field component
Browse files Browse the repository at this point in the history
  • Loading branch information
Temiakinsoto committed Nov 25, 2024
1 parent 6c49160 commit 53636d8
Showing 1 changed file with 73 additions and 0 deletions.
73 changes: 73 additions & 0 deletions src/app/components/cms/Feedback/Dropdownfield.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { render, screen } from '@/config/test-utils'

import DropdownField from './DropdownField'

describe('Dropdownfield Component', () => {
const mockProps = {
label: 'What would you like to see on the dashboard in the future?',
helpText: 'Pick one option from the list.',
cleanName: 'what_would_you_like_to_see_on_the_dashboard_in_the_future',
choices: 'Option 1, Option 2, Option 3',
}

it('should render the label correctly', () => {
render(<DropdownField {...mockProps} />)

const labelElement = screen.getByText('What would you like to see on the dashboard in the future?')
expect(labelElement).toBeInTheDocument()
})

it('should render help text if provided', () => {
render(<DropdownField {...mockProps} />)

const helpTextElement = screen.getByText('Pick one option from the list.')
expect(helpTextElement).toBeInTheDocument()
})

it('should not render help text if not provided', () => {
render(<DropdownField {...{ ...mockProps, helpText: '' }} />)

const helpTextElement = screen.queryByText('Pick one option from the list.')
expect(helpTextElement).toBeNull()
})

it('should render the default "Choose an option" option', () => {
render(<DropdownField {...mockProps} />)

const defaultOption = screen.getByText('Choose an option')
expect(defaultOption).toBeInTheDocument()
})

it('should render options based on choices string with commas', () => {
render(<DropdownField {...mockProps} />)

const options = screen.getAllByRole('option')
expect(options).toHaveLength(4) // 3 options + 1 default option
expect(screen.getByText('Option 1')).toBeInTheDocument()
expect(screen.getByText('Option 2')).toBeInTheDocument()
expect(screen.getByText('Option 3')).toBeInTheDocument()
})

it('should render options based on choices string with newline', () => {
const propsWithNewlineChoices = {
...mockProps,
choices: 'Option 1\r\nOption 2\r\nOption 3',
}
render(<DropdownField {...propsWithNewlineChoices} />)

const options = screen.getAllByRole('option')
expect(options).toHaveLength(4) // 3 options + 1 default option
expect(screen.getByText('Choose an option')).toBeInTheDocument()
expect(screen.getByText('Option 1')).toBeInTheDocument()
expect(screen.getByText('Option 2')).toBeInTheDocument()
expect(screen.getByText('Option 3')).toBeInTheDocument()
})

it('should correctly render the cleanName in the select input', () => {
render(<DropdownField {...mockProps} />)

const selectElement = screen.getByRole('combobox')
expect(selectElement).toHaveAttribute('name', 'what_would_you_like_to_see_on_the_dashboard_in_the_future')
expect(selectElement).toHaveAttribute('id', 'what_would_you_like_to_see_on_the_dashboard_in_the_future')
})
})

0 comments on commit 53636d8

Please sign in to comment.