diff --git a/.changeset/nice-melons-joke.md b/.changeset/nice-melons-joke.md new file mode 100644 index 0000000000..cd28fdcc91 --- /dev/null +++ b/.changeset/nice-melons-joke.md @@ -0,0 +1,5 @@ +--- +"@channel.io/bezier-react": patch +--- + +fix onDelete function to prevent it from being called when a tag is clicked. diff --git a/packages/bezier-react/src/components/TagBadge/Tag/Tag.stories.tsx b/packages/bezier-react/src/components/TagBadge/Tag/Tag.stories.tsx index 1d6e53d4a3..3bb41477ef 100644 --- a/packages/bezier-react/src/components/TagBadge/Tag/Tag.stories.tsx +++ b/packages/bezier-react/src/components/TagBadge/Tag/Tag.stories.tsx @@ -45,5 +45,7 @@ export const Primary: StoryObj = { variant: TagBadgeVariant.Default, // eslint-disable-next-line no-console onDelete: console.log, + // eslint-disable-next-line no-console + onClick: console.log, }, } diff --git a/packages/bezier-react/src/components/TagBadge/Tag/Tag.test.tsx b/packages/bezier-react/src/components/TagBadge/Tag/Tag.test.tsx index 2498a6e295..0f816c6329 100644 --- a/packages/bezier-react/src/components/TagBadge/Tag/Tag.test.tsx +++ b/packages/bezier-react/src/components/TagBadge/Tag/Tag.test.tsx @@ -1,5 +1,7 @@ import React from 'react' +import userEvent from '@testing-library/user-event' + import { RoundAbsoluteNumber } from '~/src/foundation' import { render } from '~/src/utils/testUtils' @@ -11,12 +13,14 @@ import { import { TAGBADGE_VERTICAL_PADDING } from '~/src/components/TagBadge/TagBadgeCommon/constants/TagBadgeStyle' import { + TAG_DELETE_TEST_ID, TAG_TEST_ID, Tag, } from './Tag' import type TagProps from './Tag.types' describe('Tag test >', () => { + const user = userEvent.setup() let props: TagProps beforeEach(() => { @@ -67,4 +71,35 @@ describe('Tag test >', () => { expect(lTag).toHaveStyle(`border-radius: ${RoundAbsoluteNumber.R6}px`) }) }) + + describe('Click Event Test >', () => { + it('onClick function should be called when the tag is clicked', async () => { + const onClickFn = jest.fn() + const { getByTestId } = renderTag({ onClick: onClickFn }) + + await user.click(getByTestId(TAG_TEST_ID)) + + expect(onClickFn).toBeCalled() + }) + + it('onDelete function should be called when the delete icon is clicked', async () => { + const onClickFn = jest.fn() + const onDeleteFn = jest.fn() + const { getByTestId } = renderTag({ onClick: onClickFn, onDelete: onDeleteFn }) + + await user.click(getByTestId(TAG_DELETE_TEST_ID)) + + expect(onDeleteFn).toBeCalled() + }) + + it('onClick function should not be called when the delete icon is clicked', async () => { + const onClickFn = jest.fn() + const onDeleteFn = jest.fn() + const { getByTestId } = renderTag({ onClick: onClickFn, onDelete: onDeleteFn }) + + await user.click(getByTestId(TAG_DELETE_TEST_ID)) + + expect(onClickFn).not.toBeCalled() + }) + }) }) diff --git a/packages/bezier-react/src/components/TagBadge/Tag/Tag.tsx b/packages/bezier-react/src/components/TagBadge/Tag/Tag.tsx index bbae699876..4034de2e25 100644 --- a/packages/bezier-react/src/components/TagBadge/Tag/Tag.tsx +++ b/packages/bezier-react/src/components/TagBadge/Tag/Tag.tsx @@ -30,6 +30,7 @@ import Styled from './Tag.styled' // TODO: 테스트 코드 작성 export const TAG_TEST_ID = 'bezier-react-tag' +export const TAG_DELETE_TEST_ID = 'bezier-react-tag-delete-icon' export const Tag = memo(forwardRef(function Tag({ size = TagBadgeSize.M, @@ -56,8 +57,12 @@ export const Tag = memo(forwardRef(function Tag({ { + e.stopPropagation() + onDelete(e) + }} /> ), [onDelete]) @@ -77,7 +82,7 @@ export const Tag = memo(forwardRef(function Tag({ horizontalPadding={TAG_TEXT_HORIZONTAL_PADDING} typo={getProperTagBadgeTypo(size)} > - { children } + { children } ) }