Skip to content

Commit

Permalink
test: follow testing-library best practices
Browse files Browse the repository at this point in the history
  • Loading branch information
visualjerk committed Jul 21, 2020
1 parent d669e23 commit 6ba7888
Show file tree
Hide file tree
Showing 15 changed files with 519 additions and 337 deletions.
1 change: 1 addition & 0 deletions babel.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module.exports = {
plugins: ['@ant-design-vue/babel-plugin-jsx'],
presets: [
[
'@babel/preset-env',
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@
"vue": "^3.0.0-beta.15"
},
"devDependencies": {
"@ant-design-vue/babel-plugin-jsx": "^1.0.0-beta.3",
"@babel/core": "^7.10.4",
"@babel/preset-env": "^7.10.4",
"@babel/preset-typescript": "^7.10.4",
"@testing-library/dom": "^7.21.4",
"@testing-library/user-event": "^12.0.11",
"@types/jest": "^26.0.4",
"@vue/compiler-sfc": "^3.0.0-beta.15",
"@vue/test-utils": "^2.0.0-alpha.7",
Expand Down
5 changes: 5 additions & 0 deletions prettier.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
trailingComma: 'es5',
semi: false,
singleQuote: true,
}
41 changes: 0 additions & 41 deletions src/box/box.spec.ts

This file was deleted.

39 changes: 39 additions & 0 deletions src/box/box.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { Box } from '.'
import { renderJsx, getByText } from '../../test/utils'

describe('Box', () => {
it('renders correctly', () => {
renderJsx(<Box>foo</Box>)
expect(getByText('foo')).toMatchInlineSnapshot(`
<div>
foo
</div>
`)
})

it('renders passed attributes', () => {
renderJsx(
<Box tabIndex="0" class="bar" unicorn>
foo
</Box>
)
expect(getByText('foo')).toMatchInlineSnapshot(`
<div
class="bar"
tabindex="0"
unicorn="true"
>
foo
</div>
`)
})

it('can render as other element types', () => {
renderJsx(<Box as="span">foo</Box>)
expect(getByText('foo')).toMatchInlineSnapshot(`
<span>
foo
</span>
`)
})
})
74 changes: 0 additions & 74 deletions src/clickable/clickable.spec.ts

This file was deleted.

73 changes: 73 additions & 0 deletions src/clickable/clickable.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { Clickable } from '.'
import { renderJsx, getByText, pressSpace, pressEnter } from '../../test/utils'

describe('Clickable', () => {
it('renders correctly', () => {
renderJsx(<Clickable>foo</Clickable>)
expect(getByText('foo')).toMatchInlineSnapshot(`
<div
tabindex="0"
>
foo
</div>
`)
})

it('emits click event on press space', () => {
const clickHandler = jest.fn()
renderJsx(<Clickable onClick={clickHandler}>foo</Clickable>)
pressSpace(getByText('foo'))
expect(clickHandler).toBeCalledTimes(1)
})

it('emits no click event on keydown space when disabled', () => {
const clickHandler = jest.fn()
renderJsx(
<Clickable onClick={clickHandler} disabled>
foo
</Clickable>
)
pressSpace(getByText('foo'))
expect(clickHandler).toBeCalledTimes(0)
})

it('emits only one click event on press space as button', () => {
const clickHandler = jest.fn()
renderJsx(
<Clickable onClick={clickHandler} as="button">
foo
</Clickable>
)
pressSpace(getByText('foo'))
expect(clickHandler).toBeCalledTimes(1)
})

it('emits click event on press enter', () => {
const clickHandler = jest.fn()
renderJsx(<Clickable onClick={clickHandler}>foo</Clickable>)
pressEnter(getByText('foo'))
expect(clickHandler).toBeCalledTimes(1)
})

it('emits no click event on press enter when disabled', () => {
const clickHandler = jest.fn()
renderJsx(
<Clickable onClick={clickHandler} disabled>
foo
</Clickable>
)
pressEnter(getByText('foo'))
expect(clickHandler).toBeCalledTimes(0)
})

it('emits only one click event on press enter as button', async () => {
const clickHandler = jest.fn()
renderJsx(
<Clickable onClick={clickHandler} as="button">
foo
</Clickable>
)
pressEnter(getByText('foo'))
expect(clickHandler).toBeCalledTimes(1)
})
})
7 changes: 5 additions & 2 deletions src/clickable/clickable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@ export const clickableProps: ComponentObjectPropsOptions<ClickableProps> = {
export function useClickable(props: ClickableProps) {
const tabbable = useTabbable(props)

function handleKeydown(event: KeyboardEvent) {
if (['space', 'enter'].includes(event.key)) {
function handleKeydown(event: KeyboardEvent & { target: HTMLElement }) {
if (
event.target?.tagName !== 'BUTTON' &&
[' ', 'Enter'].includes(event.key)
) {
tabbable.onClick(event)
}
}
Expand Down
Loading

0 comments on commit 6ba7888

Please sign in to comment.