-
Notifications
You must be signed in to change notification settings - Fork 362
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: [M3-8542] - Add unit tests for
CopyableTextField
component (#…
…11268) * test: [M3-8542] - Add unit tests for CopyableTextField component * Added changeset: unit test cases for `CopyableTextField` component * Add `renderWithTheme` directly in each test case for consistency
- Loading branch information
1 parent
654d24c
commit eb4b0e7
Showing
3 changed files
with
93 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@linode/manager": Added | ||
--- | ||
|
||
unit test cases for `CopyableTextField` component ([#11268](https://github.com/linode/manager/pull/11268)) |
87 changes: 87 additions & 0 deletions
87
packages/manager/src/components/CopyableTextField/CopyableTextField.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,87 @@ | ||
import userEvent from '@testing-library/user-event'; | ||
import React from 'react'; | ||
|
||
import { downloadFile } from 'src/utilities/downloadFile'; | ||
import { renderWithTheme } from 'src/utilities/testHelpers'; | ||
|
||
import { CopyableTextField } from './CopyableTextField'; | ||
|
||
import type { CopyableTextFieldProps } from './CopyableTextField'; | ||
|
||
vi.mock('src/utilities/downloadFile', () => ({ | ||
downloadFile: vi.fn(), | ||
})); | ||
|
||
const mockLabel = 'label'; | ||
const mockValue = 'Text to Copy'; | ||
const defaultProps: CopyableTextFieldProps = { | ||
label: mockLabel, | ||
value: mockValue, | ||
}; | ||
|
||
describe('CopyableTextField', () => { | ||
it('should render label and CopyableText', () => { | ||
const { getByRole } = renderWithTheme( | ||
<CopyableTextField {...defaultProps} /> | ||
); | ||
|
||
const textbox = getByRole('textbox', { name: mockLabel }); | ||
|
||
expect(textbox).toBeVisible(); | ||
expect(textbox).toHaveAttribute( | ||
'value', | ||
expect.stringContaining(mockValue) | ||
); | ||
}); | ||
|
||
it('should render the copy icon button and tooltip and allow user to copy the Text', async () => { | ||
const { findByRole, getByRole } = renderWithTheme( | ||
<CopyableTextField {...defaultProps} /> | ||
); | ||
|
||
const copyIcon = getByRole('button', { | ||
name: `Copy ${mockValue} to clipboard`, | ||
}); | ||
|
||
await userEvent.hover(copyIcon); | ||
const copyTooltip = await findByRole('tooltip'); | ||
expect(copyTooltip).toBeInTheDocument(); | ||
expect(copyTooltip).toHaveTextContent('Copy'); | ||
|
||
await userEvent.click(copyIcon); | ||
expect(copyIcon.getAttribute('data-qa-tooltip')).toBe('Copied!'); | ||
}); | ||
|
||
it('should render the download icon button and tooltip and allow user to download the Text File', async () => { | ||
const { findByRole, getByRole } = renderWithTheme( | ||
<CopyableTextField showDownloadIcon {...defaultProps} /> | ||
); | ||
|
||
const downloadIcon = getByRole('button', { name: `Download ${mockValue}` }); | ||
|
||
await userEvent.hover(downloadIcon); | ||
const copyTooltip = await findByRole('tooltip'); | ||
expect(copyTooltip).toBeInTheDocument(); | ||
expect(copyTooltip).toHaveTextContent('Download'); | ||
|
||
await userEvent.click(downloadIcon); | ||
expect(downloadFile).toHaveBeenCalledTimes(1); | ||
expect(downloadFile).toHaveBeenCalledWith(`${mockLabel}.txt`, mockValue); | ||
}); | ||
|
||
it('should not render any icon when hideIcons is true', () => { | ||
const { queryByRole } = renderWithTheme( | ||
<CopyableTextField hideIcons {...defaultProps} /> | ||
); | ||
|
||
const copyIcon = queryByRole('button', { | ||
name: `Copy ${mockValue} to clipboard`, | ||
}); | ||
const downloadIcon = queryByRole('button', { | ||
name: `Download ${mockValue}`, | ||
}); | ||
|
||
expect(copyIcon).not.toBeInTheDocument(); | ||
expect(downloadIcon).not.toBeInTheDocument(); | ||
}); | ||
}); |
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