Skip to content
This repository was archived by the owner on Sep 20, 2024. It is now read-only.

[WIP]: test: next #235

Merged
merged 34 commits into from
Jul 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
364ee54
export everything from vtl
koca Jun 7, 2020
b1dbd30
test(aspect-ratio): add tests for padding bottom
koca Jun 7, 2020
77eefca
chore: fix lint errs
koca Jun 13, 2020
8c438d0
test(aspect-ratio): refactor and fix AspectRatio tests
koca Jul 1, 2020
eb768bf
test(alert): add tests for alert icons and refactor
koca Jul 1, 2020
ea6011f
test(alert-dialog): refactor alert dialog tests
koca Jul 1, 2020
d1814b7
feat(test-utils): add getElementStyles to test utils
koca Jul 1, 2020
afaa8bc
fix(avatar): render AvatarName if no src provided and fix all test cases
koca Jul 2, 2020
b2b706d
test(avatar-group): fix AvatarGroup tests
koca Jul 2, 2020
7edd2ec
test(bread-crumb): refactor Breadcrump tests
koca Jul 2, 2020
5e8f58e
test(button): refactor button tests
koca Jul 2, 2020
83e9a99
chore: add .jsconfig for better code completion support in vscode
koca Jul 2, 2020
7809979
chore: add eslint testing library plugin
koca Jul 2, 2020
8a3a590
test(button-group): remove unnecessary test in ButtonGroup
koca Jul 2, 2020
5bcc4d5
test(avatar): remove unused attrs
koca Jul 2, 2020
97807ad
test(accordion): add shift+tab tests for accordion
koca Jul 4, 2020
3c2400d
test(avatar+avatar-group): add comments for the future and one tick i…
koca Jul 4, 2020
5cdb5af
test(badge): remove unnecessary test
koca Jul 4, 2020
0037cb9
test(Box): refactor box tests
koca Jul 4, 2020
2bf0f5e
chore: remove unused screen
koca Jul 4, 2020
e689731
chore: fix eslint errors (husky didnt check for some reason)
koca Jul 4, 2020
068420c
test(checkbox): refactor checkbox tests
koca Jul 6, 2020
7b3d85f
test(checkbox-group): refactor CheckboxGroup tests
koca Jul 6, 2020
d14721c
test(circular-progress): refactor circular progress
koca Jul 6, 2020
a77f8f3
test(close-button): refactor close button tests
koca Jul 6, 2020
725f822
test(code): refactor to screen
koca Jul 6, 2020
1af9a07
test(controlbox): refactor use screen
koca Jul 6, 2020
3aa7a15
test(drawer): refactor drawer tests
koca Jul 6, 2020
470ec73
test(editable): refactor use screen
koca Jul 6, 2020
5edf979
test(flex): refactor use screen
koca Jul 6, 2020
70e9ae2
test(icon-button): refactor use screen
koca Jul 6, 2020
979bb01
test(image): add tests for Image component and fix onerror
koca Jul 6, 2020
e988316
test(input): add tests for input component and fix readonly
koca Jul 6, 2020
efd6965
test(list): add tests for List component
koca Jul 7, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,21 @@ module.exports = {
env: {
node: true
},
'extends': [
plugins: ['testing-library'],
extends: [
'plugin:vue/essential',
'@vue/standard',
'@nuxtjs'
'@nuxtjs',
'plugin:testing-library/recommended',
'plugin:testing-library/vue'
],
rules: {
'no-console': process.env.NODE_ENV === 'production' ? 'error' : 'off',
'no-debugger': process.env.NODE_ENV === 'production' ? 'error' : 'off',
'curly': 'off'
curly: 'off',
'testing-library/no-debug': 'error',
'testing-library/prefer-screen-queries': 'error',
'testing-library/await-fire-event': 'error'
},
parserOptions: {
parser: 'babel-eslint'
Expand All @@ -23,10 +29,7 @@ module.exports = {
},
overrides: [
{
files: [
'**/__tests__/*.{j,t}s?(x)',
'**/tests/*.{j,t}s?(x)'
],
files: ['**/__tests__/*.{j,t}s?(x)', '**/tests/*.{j,t}s?(x)'],
env: {
jest: true
}
Expand Down
2 changes: 1 addition & 1 deletion babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module.exports = {
],
env: {
test: {
plugins: ['transform-es2015-modules-commonjs']
// plugins: ['transform-es2015-modules-commonjs']
}
}
}
3 changes: 2 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,6 @@ module.exports = {
watchPlugins: [
'jest-watch-typeahead/filename',
'jest-watch-typeahead/testname'
]
],
testEnvironmentOptions: { resources: 'usable' }
}
12 changes: 12 additions & 0 deletions jsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"~/*": ["./*"],
"@/*": ["./*"],
"~~/*": ["./*"],
"@@/*": ["./*"]
}
},
"exclude": ["node_modules", ".nuxt", "dist"]
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@
"eslint-loader": "^3.0.3",
"eslint-plugin-nuxt": ">=0.4.2",
"eslint-plugin-prettier": "^3.1.2",
"eslint-plugin-testing-library": "^3.3.1",
"jest": "^25.1.0",
"prettier": "^1.19.1",
"rimraf": "^3.0.2",
Expand Down
180 changes: 87 additions & 93 deletions packages/chakra-ui-core/src/CAccordion/tests/CAccordion.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CAccordion, CAccordionItem, CAccordionHeader, CAccordionPanel, CAccordionIcon } from '..'
import { render, userEvent, fireEvent } from '@/tests/test-utils'
import { render, userEvent, screen } from '@/tests/test-utils'

const renderComponent = (props) => {
const base = {
Expand All @@ -10,23 +10,21 @@ const renderComponent = (props) => {
}

it('should render correctly', () => {
const { asFragment } = renderComponent(
{
template: `
const { asFragment } = renderComponent({
template: `
<CAccordion>
<CAccordionItem id="ac-1">
<CAccordionHeader>Section 1 title<CAccordionIcon /></CAccordionHeader>
<CAccordionPanel>Section 1 text</CAccordionPanel>
</CAccordionItem>
</CAccordion>
`
}
)
})
expect(asFragment()).toMatchSnapshot()
})

it('uncontrolled: It opens the accordion panel', () => {
const { getByTestId } = renderComponent({
renderComponent({
template: `
<CAccordion>
<CAccordionItem>
Expand All @@ -35,14 +33,13 @@ it('uncontrolled: It opens the accordion panel', () => {
</CAccordionItem>
</CAccordion>
`
}
)
const button = getByTestId('button')
})
const button = screen.getByTestId('button')
expect(button).toHaveAttribute('aria-expanded', 'true')
})

it('uncontrolled: toggles the accordion on click', () => {
const { getByText } = renderComponent({
it('uncontrolled: toggles the accordion on click', async () => {
renderComponent({
template: `
<CAccordion>
<CAccordionItem>
Expand All @@ -52,19 +49,17 @@ it('uncontrolled: toggles the accordion on click', () => {
</CAccordion>`
})

const trigger = getByText('Trigger')

userEvent.click(trigger)
expect(trigger).toHaveAttribute('aria-expanded', 'true')
await userEvent.click(screen.getByText('Trigger'))
expect(screen.getByText('Trigger')).toHaveAttribute('aria-expanded', 'true')

// you can't toggle an accordion without passing `allowToggle`
userEvent.click(trigger)
expect(trigger).toHaveAttribute('aria-expanded', 'true')
await userEvent.click(screen.getByText('Trigger'))
expect(screen.getByText('Trigger')).toHaveAttribute('aria-expanded', 'true')
})

// test the only one accordion can be visible + is not togglable
it('only one accordion can be visible + is not togglable', () => {
const { getByText } = renderComponent({
it('only one accordion can be visible + is not togglable', async () => {
renderComponent({
template: `
<CAccordion>
<CAccordionItem>
Expand All @@ -78,21 +73,19 @@ it('only one accordion can be visible + is not togglable', () => {
</CAccordion>`
})

const firstAccordion = getByText('First section')

userEvent.click(firstAccordion)
expect(firstAccordion).toHaveAttribute('aria-expanded', 'true')
await userEvent.click(screen.getByText('First section'))
expect(screen.getByText('First section')).toHaveAttribute('aria-expanded', 'true')

userEvent.click(firstAccordion)
expect(firstAccordion).toHaveAttribute('aria-expanded', 'true')
await userEvent.click(screen.getByText('First section'))
expect(screen.getByText('First section')).toHaveAttribute('aria-expanded', 'true')
})

// test the only one accordion can be visible + is togglable
it('only one accordion can be visible + is togglable', () => {
const { getByText } = renderComponent({
it('only one accordion can be visible + is togglable', async () => {
renderComponent({
template: `
<CAccordion allowToggle>
<CAccordionItem>
<CAccordionItem data-testid="accordionItem1">
<CAccordionHeader>First section</CAccordionHeader>
<CAccordionPanel>Panel 1</CAccordionPanel>
</CAccordionItem>
Expand All @@ -102,21 +95,19 @@ it('only one accordion can be visible + is togglable', () => {
</CAccordionItem>
</CAccordion>`
})
const firstAccordion = getByText('First section')

// TODO: Why its not working?
// fireEvent.click(firstAccordion)
// expect(firstAccordion).toHaveAttribute('aria-expanded', 'false')
await userEvent.click(screen.getByText('First section'))
expect(screen.getByText('First section')).not.toHaveAttribute('aria-expanded') // false or null?

userEvent.click(firstAccordion)
expect(firstAccordion).toHaveAttribute('aria-expanded', 'true')
await userEvent.click(screen.getByText('First section'))
expect(screen.getByText('First section')).toHaveAttribute('aria-expanded', 'true')
})

// test that multiple accordions can be opened + is togglable
it('multiple accordions can be opened + is togglable', () => {
const { getByText } = renderComponent({
it('multiple accordions can be opened + is togglable', async () => {
renderComponent({
template: `
<CAccordion allowMultiple :defaultIndex="[0]">
<CAccordion allowMultiple :defaultIndex="[0, 1]">
<CAccordionItem>
<CAccordionHeader>First section</CAccordionHeader>
<CAccordionPanel>Panel 1</CAccordionPanel>
Expand All @@ -128,19 +119,16 @@ it('multiple accordions can be opened + is togglable', () => {
</CAccordion>`
})

const firstAccordion = getByText('First section')
const secondAccordion = getByText('Second section')
expect(screen.getByText('First section')).toHaveAttribute('aria-expanded', 'true')
expect(screen.getByText('Second section')).toHaveAttribute('aria-expanded', 'true')

userEvent.click(firstAccordion)
expect(firstAccordion).toHaveAttribute('aria-expanded', 'true')

userEvent.click(secondAccordion)
expect(firstAccordion).toHaveAttribute('aria-expanded', 'true')
await userEvent.click(screen.getByText('First section'))
expect(screen.getByText('First section')).not.toHaveAttribute('aria-expanded')
})

// it has the proper aria attributes
it('has the proper aria attributes', () => {
const { getByText, getByTestId } = renderComponent({
renderComponent({
template: `
<CAccordion>
<CAccordionItem>
Expand All @@ -149,16 +137,48 @@ it('has the proper aria attributes', () => {
</CAccordionItem>
</CAccordion>`
})
const button = getByText('Section 1 title')
const panel = getByTestId('panel1')
const button = screen.getByText('Section 1 title')
const panel = screen.getByTestId('panel1')
expect(button).toHaveAttribute('aria-controls')
expect(button).toHaveAttribute('aria-expanded')
expect(panel).toHaveAttribute('aria-labelledby')
})

// test that tab moves focus to the next focusable element
it('tab moves focus to the next focusable element', () => {
const { getByText } = renderComponent({
it('tab moves focus to the next focusable element', async () => {
renderComponent({
template: `
<CAccordion>
<CAccordionItem>
<CAccordionHeader>First section</CAccordionHeader>
<CAccordionPanel>Panel 1</CAccordionPanel>
</CAccordionItem>
<CAccordionItem>
<CAccordionHeader>Second section</CAccordionHeader>
<CAccordionPanel>Panel 2</CAccordionPanel>
</CAccordionItem>
<CAccordionItem>
<CAccordionHeader>Last section</CAccordionHeader>
<CAccordionPanel>Panel 3</CAccordionPanel>
</CAccordionItem>
</CAccordion>`
})

const first = screen.getByText('First section')
const second = screen.getByText('Second section')
const last = screen.getByText('Last section')

await userEvent.tab()
expect(first).toHaveFocus()

await userEvent.tab()
expect(second).toHaveFocus()

await userEvent.tab()
expect(last).toHaveFocus()
})

it('shift+tab moves focus to the previous focusable element', async () => {
renderComponent({
template: `
<CAccordion>
<CAccordionItem>
Expand All @@ -176,23 +196,25 @@ it('tab moves focus to the next focusable element', () => {
</CAccordion>`
})

const first = getByText('First section')
const second = getByText('Second section')
const last = getByText('Last section')
const first = screen.getByText('First section')
const second = screen.getByText('Second section')
const last = screen.getByText('Last section')

userEvent.tab()
await userEvent.tab()
expect(first).toHaveFocus()

userEvent.tab()
await userEvent.tab()
expect(second).toHaveFocus()

userEvent.tab()
await userEvent.tab()
expect(last).toHaveFocus()

await userEvent.tab({ shift: true }) // shift+tab
expect(second).toHaveFocus()
})

// test that aria-contols for button is same as id for panel
it('aria-contols for button is same as id for panel', () => {
const { getByText, getByTestId } = renderComponent({
renderComponent({
template: `
<CAccordion>
<CAccordionItem>
Expand All @@ -202,14 +224,13 @@ it('aria-contols for button is same as id for panel', () => {
</CAccordion>`
})

const button = getByText('Section 1 title')
const panel = getByTestId('panel1')
const button = screen.getByText('Section 1 title')
const panel = screen.getByTestId('panel1')
expect(button.getAttribute('aria-controls')).toEqual(panel.getAttribute('id'))
})

// test that aria-expanded is true/false when accordion is open/closed
it('aria-expanded is true/false when accordion is open/closed', () => {
const { getByText } = renderComponent({
renderComponent({
template: `
<CAccordion :defaultIndex="[0]">
<CAccordionItem>
Expand All @@ -223,13 +244,12 @@ it('aria-expanded is true/false when accordion is open/closed', () => {
</CAccordion>`
})

const button = getByText('Section 1 title')
const button = screen.getByText('Section 1 title')
expect(button).toHaveAttribute('aria-expanded', 'true')
})

// test that panel has role=region and aria-labelledby
it('panel has role=region and aria-labelledby', () => {
const { getByTestId } = renderComponent({
renderComponent({
template: `
<CAccordion>
<CAccordionItem>
Expand All @@ -238,34 +258,8 @@ it('panel has role=region and aria-labelledby', () => {
</CAccordionItem>
</CAccordion>`
})
const panel = getByTestId('panel1')
const panel = screen.getByTestId('panel1')

expect(panel).toHaveAttribute('aria-labelledby')
expect(panel).toHaveAttribute('role', 'region')
})

// eslint-disable-next-line no-undef
xit('arrow up & down moves focus to next/previous accordion', () => {
const { getByText } = renderComponent({
template: `
<CAccordion>
<CAccordionItem>
<CAccordionHeader>Section 1 title</CAccordionHeader>
<CAccordionPanel>Panel 1</CAccordionPanel>
</CAccordionItem>
<CAccordionItem>
<CAccordionHeader>Section 2 title</CAccordionHeader>
<CAccordionPanel>Panel 2</CAccordionPanel>
</CAccordionItem>
</CAccordion>`
})

const first = getByText('Section 1 title')
const second = getByText('Section 2 title')

fireEvent.keyDown(first, { key: 'ArrowDown', keyCode: 40 })
expect(second).toHaveFocus()

fireEvent.keyDown(second, { key: 'ArrowUp', keyCode: 38 })
expect(first).toHaveFocus()
})
Loading