-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(react): fix text field prefix, suffix visible condition
- Loading branch information
1 parent
45c9323
commit b632c05
Showing
2 changed files
with
74 additions
and
4 deletions.
There are no files selected for viewing
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
72 changes: 72 additions & 0 deletions
72
packages/react/src/components/TextField/text-field.test.tsx
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,72 @@ | ||
import { render } from '@testing-library/react' | ||
import TextField from '.' | ||
|
||
import '@testing-library/jest-dom' | ||
|
||
describe('TextField component', () => { | ||
it('should not render prefix and suffix when not provided', () => { | ||
const { container } = render(<TextField />) | ||
|
||
// prefix and suffix elements should not be rendered | ||
const prefixElement = container.querySelector('.charcoal-text-field-prefix') | ||
const suffixElement = container.querySelector('.charcoal-text-field-suffix') | ||
|
||
expect(prefixElement).toBeNull() | ||
expect(suffixElement).toBeNull() | ||
}) | ||
|
||
test.each([ | ||
[null, 'null'], | ||
[undefined, 'undefined'], | ||
['', 'empty string'], | ||
[false, 'boolean false'], | ||
[0, 'zero'], | ||
])( | ||
'should not render prefix when value is falsy (%s: %s)', | ||
(prefixValue, _desc) => { | ||
const { container } = render(<TextField prefix={prefixValue} />) | ||
const prefixElement = container.querySelector('.charcoal-text-prefix') | ||
expect(prefixElement).toBeNull() | ||
} | ||
) | ||
|
||
test.each([ | ||
[null, 'null'], | ||
[undefined, 'undefined'], | ||
['', 'empty string'], | ||
[false, 'boolean false'], | ||
[0, 'zero'], | ||
])( | ||
'should not render suffix when value is falsy (%s: %s) and showCount is false', | ||
(suffixValue, _desc) => { | ||
const { container } = render( | ||
<TextField suffix={suffixValue} showCount={false} /> | ||
) | ||
const suffixElement = container.querySelector( | ||
'.charcoal-text-field-suffix' | ||
) | ||
expect(suffixElement).toBeNull() | ||
} | ||
) | ||
|
||
it('should render prefix and suffix when provided as truthy values', () => { | ||
const prefixContent = 'Test Prefix' | ||
const suffixContent = 'Test Suffix' | ||
const { container, getByText } = render( | ||
<TextField | ||
prefix={<span>{prefixContent}</span>} | ||
suffix={<span>{suffixContent}</span>} | ||
/> | ||
) | ||
|
||
const prefixElement = container.querySelector('.charcoal-text-field-prefix') | ||
const suffixElement = container.querySelector('.charcoal-text-field-suffix') | ||
|
||
expect(prefixElement).not.toBeNull() | ||
expect(suffixElement).not.toBeNull() | ||
|
||
// Verify text content | ||
expect(getByText(prefixContent)).toBeInTheDocument() | ||
expect(getByText(suffixContent)).toBeInTheDocument() | ||
}) | ||
}) |