Skip to content

Commit

Permalink
Merge pull request #755 from Ekep-Obasi/search-bar-tests
Browse files Browse the repository at this point in the history
test: added comprehensive tests for searchBar
  • Loading branch information
Rassl authored Jan 11, 2024
2 parents 3cd2b90 + 28c27ee commit 82f9739
Showing 1 changed file with 49 additions and 2 deletions.
51 changes: 49 additions & 2 deletions src/components/SearchBar/__tests__/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as React from 'react'
import { render, screen } from '@testing-library/react'
import '@testing-library/jest-dom/extend-expect'
import { fireEvent, render, screen } from '@testing-library/react'
import * as React from 'react'
import { FormProvider, useForm } from 'react-hook-form'
import { SearchBar } from '../index'

Expand All @@ -18,4 +18,51 @@ describe('SearchBar', () => {

expect(searchInput).toBeInTheDocument()
})

it('should accept different types of input (text, numbers, and special characters)', () => {
const Wrapper = ({ children }: { children: React.ReactNode }) => {
const methods = useForm()

return <FormProvider {...methods}>{children}</FormProvider>
}

render(<SearchBar />, { wrapper: Wrapper })

const searchInput = screen.getByPlaceholderText('Search') as HTMLInputElement

// Test with text input
fireEvent.change(searchInput, { target: { value: 'test sample text' } })

expect(searchInput.value).toBe('test sample text')

// Test with number input
fireEvent.change(searchInput, { target: { value: '349395834' } })

expect(searchInput.value).toBe('349395834')

// Test with special characters
fireEvent.change(searchInput, { target: { value: '!@#$%' } })

expect(searchInput.value).toBe('!@#$%')
})

it('should execute a search on pressing the enter key', () => {
const handleSearch = jest.fn()

const Wrapper = ({ children }: { children: React.ReactNode }) => {
const methods = useForm()

return <FormProvider {...methods}>{children}</FormProvider>
}

render(<SearchBar onSubmit={handleSearch} />, { wrapper: Wrapper })

const searchInput = screen.getByPlaceholderText('Search') as HTMLInputElement

fireEvent.change(searchInput, { target: { value: 'search query' } })

fireEvent.keyPress(searchInput, { key: 'Enter', code: 'Enter', charCode: 13 })

expect(handleSearch).toHaveBeenCalledWith()
})
})

0 comments on commit 82f9739

Please sign in to comment.