Skip to content

Commit

Permalink
chore: update test tag input
Browse files Browse the repository at this point in the history
  • Loading branch information
urmauur committed Nov 25, 2024
1 parent e06399a commit 5980bc0
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions web/containers/TagInput/index.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import React from 'react'
import { render, fireEvent } from '@testing-library/react'
import TagInput from './index' // Adjust the import path as necessary
import '@testing-library/jest-dom'

describe('TagInput Component', () => {
let props: any

beforeEach(() => {
props = {
title: 'Tags',
name: 'tag-input',
description: 'Add your tags',
placeholder: 'Enter a tag',
value: ['tag1', 'tag2'],
onValueChanged: jest.fn(),
}
})

it('renders correctly', () => {
const { getByText, getByPlaceholderText } = render(<TagInput {...props} />)
expect(getByText('Tags')).toBeInTheDocument()
expect(getByText('tag1')).toBeInTheDocument()
expect(getByText('tag2')).toBeInTheDocument()
expect(getByPlaceholderText('Enter a tag')).toBeInTheDocument()
})

it('calls onValueChanged when a new tag is added', () => {
const { getByPlaceholderText } = render(<TagInput {...props} />)
const input = getByPlaceholderText('Enter a tag')

fireEvent.change(input, { target: { value: 'tag3' } })
fireEvent.keyDown(input, { key: 'Enter', code: 'Enter' })

expect(props.onValueChanged).toHaveBeenCalledWith(
expect.arrayContaining(['tag1', 'tag2', 'tag3'])
)
})

it('calls onValueChanged when a tag is removed', () => {
const { getAllByRole } = render(<TagInput {...props} />)
const removeButton = getAllByRole('button')[0] // Click on the first remove button

fireEvent.click(removeButton)

expect(props.onValueChanged).toHaveBeenCalledWith(
expect.arrayContaining(['tag2'])
)
})
})

0 comments on commit 5980bc0

Please sign in to comment.