Skip to content

Commit a7e0f0a

Browse files
authored
Simplify internal tests (#3720)
This PR simplifies the internal tests a bit. 1. Don't explicitly test if a component has a specific ID 1. Don't mock the `useId` hook if it's not necessary What we care about more is that 2 components (E.g.: `MenuButton` and `MenuItems`) are connected to each other. This is done via `id` and `aria-controls` attributes. The exact ID is not important. The main motivation for this is that every time we introduce some `useId()` hook call somewhere, the IDs will shift and it will look like some tests are broken. If we are not explicitly testing the IDs, we also don't really care about deterministic incrementing IDs in tests, so therefore we can remove some `useId` mocking. Note: some tests still have mocks like this (e.g.: `description.test.ts` & `label.test.ts`) but that's because they have some snapshot tests.
1 parent c9f8f30 commit a7e0f0a

File tree

9 files changed

+215
-922
lines changed

9 files changed

+215
-922
lines changed

packages/@headlessui-react/src/components/combobox/combobox.test.tsx

Lines changed: 90 additions & 364 deletions
Large diffs are not rendered by default.

packages/@headlessui-react/src/components/dialog/dialog.test.tsx

Lines changed: 9 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ import { Popover } from '../popover/popover'
2121
import { Transition } from '../transition/transition'
2222
import { Dialog } from './dialog'
2323

24-
jest.mock('../../hooks/use-id')
25-
2624
afterAll(() => jest.restoreAllMocks())
2725

2826
function nextFrame() {
@@ -65,10 +63,7 @@ describe('Safe guards', () => {
6563
</Dialog>
6664
)
6765

68-
assertDialog({
69-
state: DialogState.InvisibleUnmounted,
70-
attributes: { id: 'headlessui-dialog-1' },
71-
})
66+
assertDialog({ state: DialogState.InvisibleUnmounted })
7267
})
7368
)
7469
})
@@ -584,10 +579,7 @@ describe('Rendering', () => {
584579

585580
await nextFrame()
586581

587-
assertDialog({
588-
state: DialogState.Visible,
589-
attributes: { id: 'headlessui-dialog-1' },
590-
})
582+
assertDialog({ state: DialogState.Visible })
591583
assertDialogTitle({
592584
state: DialogState.Visible,
593585
textContent: JSON.stringify({ open: true }),
@@ -609,10 +601,7 @@ describe('Rendering', () => {
609601

610602
await nextFrame()
611603

612-
assertDialog({
613-
state: DialogState.Visible,
614-
attributes: { id: 'headlessui-dialog-1' },
615-
})
604+
assertDialog({ state: DialogState.Visible })
616605
assertDialogDescription({
617606
state: DialogState.Visible,
618607
textContent: JSON.stringify({ open: true, disabled: false }),
@@ -754,10 +743,7 @@ describe('Keyboard interactions', () => {
754743
await click(document.getElementById('trigger'))
755744

756745
// Verify it is open
757-
assertDialog({
758-
state: DialogState.Visible,
759-
attributes: { id: 'headlessui-dialog-1' },
760-
})
746+
assertDialog({ state: DialogState.Visible })
761747

762748
// Close dialog
763749
await press(Keys.Escape)
@@ -793,10 +779,7 @@ describe('Keyboard interactions', () => {
793779
await click(document.getElementById('trigger'))
794780

795781
// Verify it is open
796-
assertDialog({
797-
state: DialogState.Visible,
798-
attributes: { id: 'headlessui-dialog-1' },
799-
})
782+
assertDialog({ state: DialogState.Visible })
800783

801784
// Close dialog
802785
await press(Keys.Escape)
@@ -838,10 +821,7 @@ describe('Keyboard interactions', () => {
838821
await click(document.getElementById('trigger'))
839822

840823
// Verify it is open
841-
assertDialog({
842-
state: DialogState.Visible,
843-
attributes: { id: 'headlessui-dialog-1' },
844-
})
824+
assertDialog({ state: DialogState.Visible })
845825

846826
// Try to close the dialog
847827
await press(Keys.Escape)
@@ -885,10 +865,7 @@ describe('Keyboard interactions', () => {
885865
await click(document.getElementById('trigger'))
886866

887867
// Verify it is open
888-
assertDialog({
889-
state: DialogState.Visible,
890-
attributes: { id: 'headlessui-dialog-1' },
891-
})
868+
assertDialog({ state: DialogState.Visible })
892869

893870
// Verify that the input field is focused
894871
assertActiveElement(document.getElementById('b'))
@@ -935,10 +912,7 @@ describe('Keyboard interactions', () => {
935912
await click(document.getElementById('trigger'))
936913

937914
// Verify it is open
938-
assertDialog({
939-
state: DialogState.Visible,
940-
attributes: { id: 'headlessui-dialog-1' },
941-
})
915+
assertDialog({ state: DialogState.Visible })
942916

943917
// Verify that the input field is focused
944918
assertActiveElement(document.getElementById('a'))
@@ -985,10 +959,7 @@ describe('Keyboard interactions', () => {
985959
await click(document.getElementById('trigger'))
986960

987961
// Verify it is open
988-
assertDialog({
989-
state: DialogState.Visible,
990-
attributes: { id: 'headlessui-dialog-1' },
991-
})
962+
assertDialog({ state: DialogState.Visible })
992963

993964
// Verify that the input field is focused
994965
assertActiveElement(document.getElementById('a'))

0 commit comments

Comments
 (0)