Skip to content

Commit 8f392fa

Browse files
committed
Tests refactored.
1 parent e78d84e commit 8f392fa

File tree

1 file changed

+73
-86
lines changed

1 file changed

+73
-86
lines changed

src/__tests__/CheckboxTree.test.tsx

Lines changed: 73 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,6 @@ describe("halfSelected parent should change status when propagateSelect={false}"
491491
//*[ ] Berries
492492
// ...
493493

494-
console.log(nodes[2].innerHTML);
495494
expect(nodes[0]).toHaveAttribute("aria-checked", "mixed");
496495
expect(nodes[2]).toHaveAttribute("aria-checked", "true");
497496

@@ -522,106 +521,94 @@ describe("halfSelected parent should change status when propagateSelect={false}"
522521
expect(nodes[2]).toHaveAttribute("aria-checked", "true");
523522
};
524523

525-
test("to selected when default selected child and selection changed manually", () => {
526-
const { queryAllByRole } = render(
527-
<CheckboxTree
528-
propagateSelect={false}
529-
defaultSelectedIds={[3]}
530-
defaultExpandedIds={[1]}
531-
/>
532-
);
533-
checkSelectionAndHalfSelection(queryAllByRole("treeitem"));
534-
});
535-
536-
test("to selected when selection changed manually", () => {
537-
const { queryAllByRole } = render(
538-
<CheckboxTree propagateSelect={false} defaultExpandedIds={[1]} />
539-
);
540-
const nodes = queryAllByRole("treeitem");
541-
542-
nodes[0].focus();
543-
if (document.activeElement == null)
544-
throw new Error(
545-
`Expected to find an active element on the document (after focusing the second element with role["treeitem"]), but did not.`
546-
);
547-
548-
fireEvent.click(nodes[2].getElementsByClassName("checkbox-icon")[0]); //select Bananas
549-
550-
expect(nodes[0]).toHaveAttribute("aria-checked", "mixed");
551-
expect(nodes[2]).toHaveAttribute("aria-checked", "true");
552-
553-
checkSelectionAndHalfSelection(nodes);
554-
});
555-
556-
test("to selected when selection changed controlled and manually", () => {
557-
const { queryAllByRole } = render(
558-
<CheckboxTree
559-
propagateSelect={false}
560-
selectedIds={[3]}
561-
defaultExpandedIds={[1]}
562-
/>
563-
);
564-
checkSelectionAndHalfSelection(queryAllByRole("treeitem"));
565-
});
566-
567-
test("to deselected and to selected when no selected children after children where selected manually initially", () => {
568-
const { queryAllByRole } = render(
569-
<CheckboxTree propagateSelect={false} defaultExpandedIds={[1]} />
570-
);
571-
const nodes = queryAllByRole("treeitem");
572-
573-
nodes[0].focus();
574-
if (document.activeElement == null)
575-
throw new Error(
576-
`Expected to find an active element on the document (after focusing the second element with role["treeitem"]), but did not.`
577-
);
578-
579-
fireEvent.click(nodes[2].getElementsByClassName("checkbox-icon")[0]); //select Bananas
580-
581-
expect(nodes[0]).toHaveAttribute("aria-checked", "mixed");
582-
expect(nodes[2]).toHaveAttribute("aria-checked", "true");
583-
584-
checkSelectionAndHalfSelection(nodes);
585-
524+
const checkDeselectionAndSelectionOfParent = (nodes: HTMLElement[]) => {
586525
fireEvent.click(nodes[2].getElementsByClassName("checkbox-icon")[0]); //deselect Bananas
587-
526+
//Initial state
527+
//[ ] Fruits
528+
//*[ ] Avocados
529+
//*[ ] Bananas
530+
//*[ ] Berries
531+
// ...
588532
expect(nodes[0]).toHaveAttribute("aria-checked", "false");
589533
expect(nodes[2]).toHaveAttribute("aria-checked", "false");
590534

591535
fireEvent.click(nodes[0].getElementsByClassName("checkbox-icon")[0]); //select Fruits
592-
536+
//Expected state 1
537+
//[+] Fruits
538+
//*[ ] Avocados
539+
//*[ ] Bananas
540+
//*[ ] Berries
541+
// ...
593542
expect(nodes[0]).toHaveAttribute("aria-checked", "true");
594543
expect(nodes[2]).toHaveAttribute("aria-checked", "false");
595544

596545
fireEvent.click(nodes[0].getElementsByClassName("checkbox-icon")[0]); //deselect Fruits
597-
546+
//Expected state 1
547+
//[ ] Fruits
548+
//*[ ] Avocados
549+
//*[ ] Bananas
550+
//*[ ] Berries
551+
// ...
598552
expect(nodes[0]).toHaveAttribute("aria-checked", "false");
599553
expect(nodes[2]).toHaveAttribute("aria-checked", "false");
600-
});
554+
};
601555

602-
test("to deselected and to selected when no selected children after children where selected controlled initially", () => {
603-
const { queryAllByRole } = render(
604-
<CheckboxTree
605-
propagateSelect={false}
606-
defaultSelectedIds={[3]}
607-
defaultExpandedIds={[1]}
608-
/>
609-
);
610-
const nodes = queryAllByRole("treeitem");
611-
checkSelectionAndHalfSelection(nodes);
612-
fireEvent.click(nodes[2].getElementsByClassName("checkbox-icon")[0]); //deselect Bananas
556+
test(
557+
"1. to selected when selection changed manually," +
558+
"2. to deselected and to selected when no selected children after children where selected manually initially",
559+
() => {
560+
const { queryAllByRole } = render(
561+
<CheckboxTree propagateSelect={false} defaultExpandedIds={[1]} />
562+
);
563+
const nodes = queryAllByRole("treeitem");
613564

614-
expect(nodes[0]).toHaveAttribute("aria-checked", "false");
615-
expect(nodes[2]).toHaveAttribute("aria-checked", "false");
565+
nodes[2].focus();
566+
if (document.activeElement == null)
567+
throw new Error(
568+
`Expected to find an active element on the document (after focusing the second element with role["treeitem"]), but did not.`
569+
);
616570

617-
fireEvent.click(nodes[0].getElementsByClassName("checkbox-icon")[0]); //select Fruits
571+
fireEvent.click(nodes[2].getElementsByClassName("checkbox-icon")[0]); //select Bananas
618572

619-
expect(nodes[0]).toHaveAttribute("aria-checked", "true");
620-
expect(nodes[2]).toHaveAttribute("aria-checked", "false");
573+
expect(nodes[0]).toHaveAttribute("aria-checked", "mixed");
574+
expect(nodes[2]).toHaveAttribute("aria-checked", "true");
621575

622-
fireEvent.click(nodes[0].getElementsByClassName("checkbox-icon")[0]); //deselect Fruits
576+
checkSelectionAndHalfSelection(nodes);
577+
checkDeselectionAndSelectionOfParent(nodes);
578+
}
579+
);
623580

624-
expect(nodes[0]).toHaveAttribute("aria-checked", "false");
625-
expect(nodes[2]).toHaveAttribute("aria-checked", "false");
626-
});
581+
test(
582+
"1. to selected when default selected child and selection changed manually" +
583+
"2. to deselected and to selected when no selected children after children where selected by default initially",
584+
() => {
585+
const { queryAllByRole } = render(
586+
<CheckboxTree
587+
propagateSelect={false}
588+
defaultSelectedIds={[3]}
589+
defaultExpandedIds={[1]}
590+
/>
591+
);
592+
const nodes = queryAllByRole("treeitem");
593+
checkSelectionAndHalfSelection(nodes);
594+
checkDeselectionAndSelectionOfParent(nodes);
595+
}
596+
);
597+
598+
test(
599+
"1. to selected when selection changed controlled and manually" +
600+
"2. to deselected and to selected when no selected children after children where selected controlled initially",
601+
() => {
602+
const { queryAllByRole } = render(
603+
<CheckboxTree
604+
propagateSelect={false}
605+
selectedIds={[3]}
606+
defaultExpandedIds={[1]}
607+
/>
608+
);
609+
const nodes = queryAllByRole("treeitem");
610+
checkSelectionAndHalfSelection(nodes);
611+
checkDeselectionAndSelectionOfParent(nodes);
612+
}
613+
);
627614
});

0 commit comments

Comments
 (0)