Skip to content

Commit e1f2cc3

Browse files
committed
Add tests
1 parent 9872ff4 commit e1f2cc3

File tree

5 files changed

+133
-10
lines changed

5 files changed

+133
-10
lines changed

jest-setup.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import '@testing-library/jest-dom'

jest.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ module.exports = {
77
},
88
testMatch: ['**/*.spec.js'],
99
snapshotSerializers: ['<rootDir>/node_modules/jest-serializer-vue'],
10+
setupFilesAfterEnv: ['<rootDir>/jest-setup.js'],
1011
}

package-lock.json

Lines changed: 110 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,10 @@
4141
},
4242
"lint-staged": {
4343
"**/*.{css,vue}": "stylelint",
44-
"**/*.{js,ts,vue,md,html}": "npm run lint-arg --"
44+
"**/*.{js,ts,vue,md,html}": [
45+
"npm run test:unit",
46+
"npm run lint-arg --"
47+
]
4548
},
4649
"browserslist": [
4750
"last 2 versions",
@@ -57,6 +60,7 @@
5760
"@pathscale/eslint-plugin-vue3": "0.0.1",
5861
"@rollup/plugin-node-resolve": "^11.0.1",
5962
"@rollup/plugin-replace": "^2.3.4",
63+
"@testing-library/jest-dom": "^5.15.1",
6064
"@testing-library/vue": "^6.4.2",
6165
"@types/babel__traverse": "^7.11.0",
6266
"@types/fs-extra": "^9.0.5",
Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,25 @@
1-
import { render } from '@testing-library/vue'
1+
import { render, fireEvent } from '@testing-library/vue'
22
import Button from './Button.vue'
33

4-
let wrapper
5-
64
describe('Button.vue', () => {
7-
beforeEach(() => {
8-
wrapper = render(Button)
5+
it('render correctly', () => {
6+
expect(render(Button).html()).toMatchSnapshot()
97
})
108

11-
it('is called', () => {
12-
wrapper.getByRole('button')
9+
it('emit a click event', async () => {
10+
const handleClick = jest.fn()
11+
const wrapper = render(<Button onClick={handleClick}>Click Me</Button>)
12+
await fireEvent.click(wrapper.getByText(/click me/i))
13+
expect(handleClick).toHaveBeenCalledTimes(1)
1314
})
15+
it('should be medium', () => {
16+
const wrapper = render(Button, { props: { label: 'medium', size: 'is-medium' } })
1417

15-
it('render correctly', () => {
16-
expect(wrapper.html()).toMatchSnapshot()
18+
expect(wrapper.getByRole('button', { name: /medium/i })).toHaveClass('is-medium')
19+
})
20+
it('should be rounded when prop is set to true', () => {
21+
const wrapper = render(Button, { props: { label: 'rounded', rounded: true } })
22+
23+
expect(wrapper.getByRole('button', { name: /rounded/i })).toHaveClass('is-rounded')
1724
})
1825
})

0 commit comments

Comments
 (0)