-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7f96d3e
commit 38bd8ee
Showing
2 changed files
with
27 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |