-
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.
- Loading branch information
Showing
9 changed files
with
112 additions
and
5 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
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 @@ | ||
export const DATA_TESTID = 'data-testid'; |
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,9 @@ | ||
import generateTestId from './generateTestId'; | ||
import { DATA_TESTID } from './generateTestId.constants'; | ||
|
||
test('generateTestId tests', () => { | ||
expect(generateTestId()).toMatchObject({}); | ||
expect(generateTestId('')).toMatchObject({}); | ||
expect(generateTestId(1)).toMatchObject({ [DATA_TESTID]: '1' }); | ||
expect(generateTestId('name')).toMatchObject({ [DATA_TESTID]: 'name' }); | ||
}); |
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,14 @@ | ||
import { DATA_TESTID } from './generateTestId.constants'; | ||
|
||
interface IGenerateTEstIdReturn { | ||
'data-testid': string; | ||
} | ||
|
||
export default function generateTestId( | ||
id?: string | number, | ||
): IGenerateTEstIdReturn | {} { | ||
if (!id) return {}; | ||
return { | ||
[DATA_TESTID]: `${id}`, | ||
}; | ||
} |
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 @@ | ||
export { default } from './generateTestId'; |
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,4 @@ | ||
export const TAB_TESTID = 'tab-'; | ||
export const TAB_BADGE_TESTID = 'tab-badge-'; | ||
export const TAB_ARIA_LABEL = 'Tab '; | ||
export const TAB_BADGE_ARIA_LABEL = 'Tab Badge '; |
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,62 @@ | ||
import React from 'react'; | ||
import { render, screen } from '@testing-library/react'; | ||
import userEvent from '@testing-library/user-event'; | ||
import Tab from './Tab'; | ||
import { | ||
TAB_ARIA_LABEL, | ||
TAB_BADGE_ARIA_LABEL, | ||
TAB_BADGE_TESTID, | ||
TAB_TESTID, | ||
} from './Tab.constants'; | ||
|
||
const LABEL = 'Tab 1'; | ||
const VALUE = '1'; | ||
const BADGE = '20'; | ||
// https://github.com/testing-library/eslint-plugin-testing-library/blob/main/docs/rules/no-render-in-setup.md | ||
const setup = () => { | ||
render(<Tab label={LABEL} value={VALUE} />); | ||
}; | ||
|
||
test('Tab Component should render', async () => { | ||
setup(); | ||
expect(await screen.findByRole('tab')).toBeInTheDocument(); | ||
}); | ||
|
||
test('Tab Component should render label and value', async () => { | ||
setup(); | ||
expect(await screen.findByText(LABEL)).toBeInTheDocument(); | ||
}); | ||
|
||
test('Tab Component should render tab attributes', async () => { | ||
setup(); | ||
expect( | ||
await screen.findByTestId(`${TAB_TESTID}${VALUE}`), | ||
).toBeInTheDocument(); | ||
expect( | ||
await screen.findByLabelText(`${TAB_ARIA_LABEL}${LABEL}`), | ||
).toBeInTheDocument(); | ||
}); | ||
|
||
test('Tab Component should handle onClick', async () => { | ||
const handleClick = jest.fn(); | ||
render(<Tab label={LABEL} value={VALUE} onClick={handleClick} />); | ||
userEvent.click(await screen.findByRole('tab')); | ||
expect(handleClick).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
test('Tab Component should handle disable mode', async () => { | ||
const handleClick = jest.fn(); | ||
render(<Tab label={LABEL} value={VALUE} onClick={handleClick} disable />); | ||
userEvent.click(await screen.findByRole('tab')); | ||
expect(handleClick).toHaveBeenCalledTimes(0); | ||
}); | ||
|
||
test('Tab Component should render badge', async () => { | ||
render(<Tab label={LABEL} value={VALUE} badge={BADGE} />); | ||
expect( | ||
await screen.findByTestId(`${TAB_BADGE_TESTID}${BADGE}`), | ||
).toBeInTheDocument(); | ||
expect( | ||
await screen.findByLabelText(`${TAB_BADGE_ARIA_LABEL}${BADGE}`), | ||
).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
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