-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add button testing and jest dependencies. (#3532)
- Loading branch information
Showing
4 changed files
with
109 additions
and
2 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,82 @@ | ||
import { render } from '@testing-library/react'; | ||
import { Button } from './Button'; | ||
import '@testing-library/jest-dom'; | ||
|
||
describe('Button', () => { | ||
test('when visible is false Button return null', () => { | ||
const { container } = render(<Button label={'test'} visible={false} />); | ||
|
||
expect(container.getElementsByClassName('p-button').length).toBe(0); | ||
}); | ||
|
||
test('when visible is true Button render correctly', () => { | ||
const { container } = render(<Button label={'test'} visible={true} />); | ||
|
||
expect(container.getElementsByClassName('p-button').length).toBe(1); | ||
}); | ||
|
||
test('when visible is false Button return null', () => { | ||
const { container } = render(<Button label={'test'} iconPos={'bottom'} visible={true} />); | ||
|
||
expect(container.getElementsByClassName('p-button-vertical').length).toBe(1); | ||
}); | ||
|
||
test('when label is empty it returns empty button', async () => { | ||
const { container } = render(<Button visible={true} />); | ||
const button = container.getElementsByClassName('p-button-label p-c'); | ||
|
||
expect(button[0].innerHTML).toBe(' '); | ||
}); | ||
|
||
test('when badge is true it renders Button with badge', () => { | ||
const { container } = render(<Button badge={'test'} />); | ||
|
||
expect(container.getElementsByClassName('p-badge').length).toBe(1); | ||
}); | ||
|
||
test('when badge is true it renders Button with badge', () => { | ||
const { container } = render(<Button />); | ||
|
||
expect(container.getElementsByClassName('p-badge').length).toBe(0); | ||
}); | ||
|
||
test('when click the button if loading is true it renders Button with loading icon', () => { | ||
const { container } = render(<Button loading={'test'} />); | ||
|
||
expect(container.getElementsByClassName('p-button-icon').length).toBe(1); | ||
}); | ||
|
||
test('when click the button if loading is false it renders Button without loading icon', () => { | ||
const { container } = render(<Button />); | ||
|
||
expect(container.getElementsByClassName('p-button-loading-icon').length).toBe(0); | ||
}); | ||
|
||
test('when label is true it renders Button with default aria label', () => { | ||
const { container } = render(<Button />); | ||
const hasAreaLabel = container.getElementsByClassName('p-button')[0].getAttribute('aria-label'); | ||
|
||
expect(hasAreaLabel).toBe(null); | ||
}); | ||
|
||
test('when aria-label prop is not exist aria-label prop should be equal to label prop ', () => { | ||
const { container } = render(<Button label={'test'} />); | ||
const getAreaLabel = container.getElementsByClassName('p-button')[0].getAttribute('aria-label'); | ||
|
||
expect(getAreaLabel).toBe('test'); | ||
}); | ||
|
||
test('when label prop is not exist label prop should be equal to aria-label prop', () => { | ||
const { container } = render(<Button aria-label={'test'} />); | ||
const getAreaLabel = container.getElementsByClassName('p-button')[0].getAttribute('aria-label'); | ||
|
||
expect(getAreaLabel).toBe('test'); | ||
}); | ||
// | ||
test('....', () => { | ||
const { container } = render(<Button label={'test'} badge={'lost'} />); | ||
const getAreaLabel = container.getElementsByClassName('p-button')[0].getAttribute('aria-label'); | ||
|
||
expect(getAreaLabel).toBe('test lost'); | ||
}); | ||
}); |
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,19 @@ | ||
const nextJest = require('next/jest') | ||
|
||
const createJestConfig = nextJest({ | ||
// Provide the path to your Next.js app to load next.config.js and .env files in your test environment | ||
dir: './', | ||
}) | ||
|
||
// Add any custom config to be passed to Jest | ||
/** @type {import('jest').Config} */ | ||
const customJestConfig = { | ||
// Add more setup options before each test is run | ||
// setupFilesAfterEnv: ['<rootDir>/jest.setup.js'], | ||
// if using TypeScript with a baseUrl set to the root directory then you need the below for alias' to work | ||
moduleDirectories: ['node_modules', '<rootDir>/'], | ||
testEnvironment: 'jest-environment-jsdom', | ||
} | ||
|
||
// createJestConfig is exported this way to ensure that next/jest can load the Next.js config which is async | ||
module.exports = createJestConfig(customJestConfig) |
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