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

Commit 7217688

Browse files
committed
test(closebutton): add tests for closebutton
1 parent d7ce521 commit 7217688

File tree

4 files changed

+111
-9
lines changed

4 files changed

+111
-9
lines changed
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
<template>
2-
<c-close-button />
2+
<c-close-button @click="handleClose" />
33
</template>
4+
5+
<script lang="ts" setup>
6+
const handleClose = () => console.log('closing modal')
7+
</script>

packages/c-close-button/src/c-close-button.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { h, defineComponent, PropType } from 'vue'
1+
import { h, defineComponent, PropType, computed } from 'vue'
22
import {
33
chakra,
44
DOMElements,
@@ -53,10 +53,12 @@ export const CCloseButton = defineComponent({
5353
},
5454
setup(props, { slots, attrs }) {
5555
return () => {
56-
const themingProps = filterUndefined({
57-
size: props.size,
58-
styleConfig: props.styleConfig,
59-
})
56+
const themingProps = computed(() =>
57+
filterUndefined({
58+
size: props.size,
59+
styleConfig: props.styleConfig,
60+
})
61+
)
6062

6163
const baseStyles: SystemStyleObject = {
6264
outline: 0,
@@ -66,7 +68,7 @@ export const CCloseButton = defineComponent({
6668
flexShrink: 0,
6769
}
6870

69-
const styles = useStyleConfig('CloseButton', themingProps)
71+
const styles = useStyleConfig('CloseButton', themingProps.value)
7072

7173
return h(
7274
chakra(props.as, {
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`should render properly 1`] = `
4+
<DocumentFragment>
5+
<button
6+
aria-label="Close"
7+
class="chakra-icon-button css-fdaoze"
8+
data-testid="closebutton"
9+
type="button"
10+
>
11+
<svg
12+
aria-hidden=""
13+
class="chakra-icon css-13otjrl"
14+
focusable="false"
15+
viewBox="0 0 24 24"
16+
>
17+
18+
19+
<g
20+
stroke="currentColor"
21+
strokewidth="1.5"
22+
>
23+
24+
25+
<path
26+
d="M9,9a3,3,0,1,1,4,2.829,1.5,1.5,0,0,0-1,1.415V14.25"
27+
fill="none"
28+
strokelinecap="round"
29+
/>
30+
31+
32+
<path
33+
d="M12,17.25a.375.375,0,1,0,.375.375A.375.375,0,0,0,12,17.25h0"
34+
fill="currentColor"
35+
strokelinecap="round"
36+
/>
37+
38+
39+
<circle
40+
cx="12"
41+
cy="12"
42+
fill="none"
43+
r="11.25"
44+
strokemiterlimit="10"
45+
/>
46+
47+
48+
</g>
49+
50+
51+
</svg>
52+
</button>
53+
</DocumentFragment>
54+
`;
Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,49 @@
1-
import { render } from '../../test-utils/src'
21
import { CCloseButton } from '../src'
2+
import { render, userEvent, testA11y } from '../../test-utils/src'
3+
import { nextTick } from '@vue/runtime-core'
4+
5+
const renderComponent = (props?: any) => {
6+
const base = {
7+
components: {
8+
CCloseButton,
9+
},
10+
template: '<CCloseButton data-testid="closebutton" />',
11+
...props,
12+
}
13+
return render(base)
14+
}
15+
16+
it('should have no a11y violations', async () => {
17+
await testA11y(renderComponent())
18+
})
319

420
it('should render properly', () => {
5-
const { asFragment } = render(CCloseButton)
21+
const { asFragment } = renderComponent()
622
expect(asFragment()).toMatchSnapshot()
723
})
24+
25+
it('should be in the DOM', () => {
26+
const { getByTestId } = renderComponent()
27+
expect(getByTestId('closebutton')).toBeInTheDocument()
28+
})
29+
30+
it('should have correct aria-label attribute', () => {
31+
const { getByTestId } = renderComponent()
32+
expect(getByTestId('closebutton')).toHaveAttribute('aria-label', 'Close')
33+
})
34+
35+
it('should emit click event on click', async () => {
36+
const handleClick = jest.fn()
37+
const { getByTestId } = renderComponent({
38+
template: '<c-icon-button data-testid="close" @click="handleClick" />',
39+
setup() {
40+
return {
41+
handleClick,
42+
}
43+
},
44+
})
45+
46+
await userEvent.click(getByTestId('close'))
47+
await nextTick()
48+
expect(handleClick).toHaveBeenCalled()
49+
})

0 commit comments

Comments
 (0)