-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added unit tests for HighlightSearchTerm component
- Loading branch information
Showing
1 changed file
with
57 additions
and
0 deletions.
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
client/src/components/_common/HighlightSearchTerm/HighlightSearchTerm.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import React from 'react'; | ||
import { render } from '@testing-library/react'; | ||
import { | ||
toBeInTheDocument, | ||
toHaveClass, | ||
toBeNull, | ||
} from '@testing-library/jest-dom'; | ||
|
||
import HighlightSearchTerm from './HighlightSearchTerm'; | ||
|
||
describe('HighlightSearchTerm Component', () => { | ||
it('renders content when searchTerm is not provided', () => { | ||
const { getByText } = render(<HighlightSearchTerm content="Lorem ipsum" />); | ||
expect(getByText('Lorem ipsum')).toBeInTheDocument(); | ||
}); | ||
|
||
it('renders without highlighting when searchTerm in content do not match', () => { | ||
const { getByText } = render( | ||
<HighlightSearchTerm | ||
searchTerm="minim" | ||
content="Lorem ipsum dolor sit amet" | ||
/> | ||
); | ||
expect(getByText('Lorem ipsum dolor sit amet')).toBeInTheDocument(); | ||
expect(document.querySelector('.highlight')).toBeNull(); | ||
}); | ||
|
||
it('renders content when searchTerm is not provided', () => { | ||
const { getByText } = render(<HighlightSearchTerm content="Lorem ipsum" />); | ||
expect(getByText('Lorem ipsum')).toBeInTheDocument(); | ||
}); | ||
|
||
it('renders content with searchTerm highlighted', () => { | ||
const { getByText } = render( | ||
<HighlightSearchTerm | ||
searchTerm="ipsum" | ||
content="Lorem ipsum dolor sit amet" | ||
/> | ||
); | ||
const highlightedText = getByText('ipsum'); | ||
expect(highlightedText).toHaveClass('highlight'); | ||
}); | ||
|
||
it('renders content with multiple searchTerm occurrences highlighted', () => { | ||
const { getAllByText } = render( | ||
<HighlightSearchTerm | ||
searchTerm="ipsum" | ||
content="Lorem ipsum ipsum Loremipsum ipsumipsum dolor sit amet" | ||
/> | ||
); | ||
const highlightedText = getAllByText('ipsum'); | ||
expect(highlightedText.length).toBe(5); | ||
highlightedText.forEach((element) => { | ||
expect(element).toHaveClass('highlight'); | ||
}); | ||
}); | ||
}); |