From 38bd8ee2c73b4a16ca03271823f405ece1f2edcd Mon Sep 17 00:00:00 2001 From: s-sasaki-0529 Date: Tue, 17 Sep 2024 12:03:04 +0900 Subject: [PATCH] =?UTF-8?q?chore:=20Hooks=20=E3=81=AE=E3=83=99=E3=82=B9?= =?UTF-8?q?=E3=83=88=E3=83=97=E3=83=A9=E3=82=AF=E3=83=86=E3=82=A3=E3=82=B9?= =?UTF-8?q?=E3=81=AB=E6=B2=BF=E3=81=A3=E3=81=A6=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/smarthr-ui/src/hooks/useId.test.ts | 25 +++++++++++++++++++++ packages/smarthr-ui/src/hooks/useId.tsx | 5 ++--- 2 files changed, 27 insertions(+), 3 deletions(-) create mode 100644 packages/smarthr-ui/src/hooks/useId.test.ts diff --git a/packages/smarthr-ui/src/hooks/useId.test.ts b/packages/smarthr-ui/src/hooks/useId.test.ts new file mode 100644 index 0000000000..babe9dcb45 --- /dev/null +++ b/packages/smarthr-ui/src/hooks/useId.test.ts @@ -0,0 +1,25 @@ +import { renderHook } from '@testing-library/react' + +import { useId } from './useId' + +describe('useId', () => { + it('defaultId を指定した場合、毎回同じ値を返す', () => { + const { result: id1 } = renderHook(() => useId('test')) + const { result: id2 } = renderHook(() => useId('test')) + const { result: id3 } = renderHook(() => useId('test')) + + expect(id1.current).toEqual('test') + expect(id2.current).toEqual('test') + expect(id3.current).toEqual('test') + }) + + it('defaultId を指定しない場合、異なる値を返す', () => { + const { result: id1 } = renderHook(() => useId()) + const { result: id2 } = renderHook(() => useId()) + const { result: id3 } = renderHook(() => useId()) + + expect(id1.current).not.toEqual(id2.current) + expect(id2.current).not.toEqual(id3.current) + expect(id3.current).not.toEqual(id1.current) + }) +}) diff --git a/packages/smarthr-ui/src/hooks/useId.tsx b/packages/smarthr-ui/src/hooks/useId.tsx index 4fcda408e7..bd5cb6d468 100644 --- a/packages/smarthr-ui/src/hooks/useId.tsx +++ b/packages/smarthr-ui/src/hooks/useId.tsx @@ -1,7 +1,6 @@ import React from 'react' export const useId = (defaultId?: string): string => { - if (defaultId) return defaultId - // eslint-disable-next-line react-hooks/rules-of-hooks - return React.useId() + const id = React.useId() + return defaultId || id }