11import React from "react" ;
2- import { render , fireEvent , getByRole } from "@testing-library/react" ;
2+ import { render , fireEvent } from "@testing-library/react" ;
33import userEvent from "@testing-library/user-event" ;
44import DxcContextualMenu from "./ContextualMenu" ;
55
@@ -32,16 +32,27 @@ const groups = [
3232] ;
3333
3434describe ( "Contextual menu component tests" , ( ) => {
35- test ( "Default - Renders with correct aria attributes" , ( ) => {
35+ test ( "Single - Renders with correct aria attributes" , async ( ) => {
3636 const { getAllByRole, getByRole } = render ( < DxcContextualMenu items = { items } /> ) ;
3737 expect ( getAllByRole ( "menuitem" ) . length ) . toBe ( 4 ) ;
3838 const actions = getAllByRole ( "button" ) ;
39+ await userEvent . click ( actions [ 0 ] ) ;
3940 expect ( actions [ 0 ] . getAttribute ( "aria-selected" ) ) . toBeTruthy ( ) ;
4041 expect ( getByRole ( "menu" ) ) . toBeTruthy ( ) ;
4142 } ) ;
43+ test ( "Single - An item can appear as selected by default by using the attribute selectedByDefault" , ( ) => {
44+ const test = [
45+ {
46+ label : "Tested item" ,
47+ selectedByDefault : true ,
48+ } ,
49+ ] ;
50+ const { getByRole } = render ( < DxcContextualMenu items = { test } /> ) ;
51+ const item = getByRole ( "button" ) ;
52+ expect ( item . getAttribute ( "aria-selected" ) ) . toBeTruthy ( ) ;
53+ } ) ;
4254 test ( "Group - Group items collapse when clicked" , async ( ) => {
43- const { queryByText, getByText, getAllByRole } = render ( < DxcContextualMenu items = { groups } /> ) ;
44- const group1 = getAllByRole ( "button" ) [ 0 ] ;
55+ const { queryByText, getByText } = render ( < DxcContextualMenu items = { groups } /> ) ;
4556 await userEvent . click ( getByText ( "Grouped Item 1" ) ) ;
4657 expect ( getByText ( "Item 1" ) ) . toBeTruthy ( ) ;
4758 expect ( getByText ( "Grouped Item 2" ) ) . toBeTruthy ( ) ;
@@ -54,16 +65,28 @@ describe("Contextual menu component tests", () => {
5465 expect ( queryByText ( "Item 3" ) ) . toBeFalsy ( ) ;
5566 } ) ;
5667 test ( "Group - Renders with correct aria attributes" , async ( ) => {
57- const { getByText , getAllByRole } = render ( < DxcContextualMenu items = { groups } /> ) ;
68+ const { getAllByRole } = render ( < DxcContextualMenu items = { groups } /> ) ;
5869 const group1 = getAllByRole ( "button" ) [ 0 ] ;
5970 await userEvent . click ( group1 ) ;
6071 expect ( group1 . getAttribute ( "aria-expanded" ) ) . toBeTruthy ( ) ;
6172 expect ( group1 . getAttribute ( "aria-controls" ) ) . toBe ( getAllByRole ( "list" ) [ 0 ] . id ) ;
62- await userEvent . click ( getByText ( "Grouped Item 2" ) ) ;
63- await userEvent . click ( getByText ( "Grouped Item 3" ) ) ;
73+ await userEvent . click ( getAllByRole ( "button" ) [ 2 ] ) ;
74+ await userEvent . click ( getAllByRole ( "button" ) [ 6 ] ) ;
6475 expect ( getAllByRole ( "menuitem" ) . length ) . toBe ( 10 ) ;
65- const actions = getAllByRole ( "button" ) ;
66- expect ( actions [ 4 ] . getAttribute ( "aria-selected" ) ) . toBeTruthy ( ) ;
76+ const optionToBeClicked = getAllByRole ( "button" ) [ 4 ] ;
77+ await userEvent . click ( optionToBeClicked ) ;
78+ expect ( optionToBeClicked . getAttribute ( "aria-selected" ) ) . toBeTruthy ( ) ;
79+ } ) ;
80+ test ( "Group - A grouped item, selected by default, must be visible (expanded group) in the first render of the component" , ( ) => {
81+ const test = [
82+ {
83+ label : "Grouped item" ,
84+ items : [ { label : "Tested item" , selectedByDefault : true } ] ,
85+ } ,
86+ ] ;
87+ const { getByText, getAllByRole } = render ( < DxcContextualMenu items = { test } /> ) ;
88+ expect ( getByText ( "Tested item" ) ) . toBeTruthy ( ) ;
89+ expect ( getAllByRole ( "button" ) [ 1 ] . getAttribute ( "aria-selected" ) ) . toBeTruthy ( ) ;
6790 } ) ;
6891 test ( "Group - Collapsed groups render as selected when containing a selected item" , async ( ) => {
6992 const { getAllByRole } = render ( < DxcContextualMenu items = { groups } /> ) ;
@@ -88,6 +111,8 @@ describe("Contextual menu component tests", () => {
88111 await userEvent . click ( actions [ 0 ] ) ;
89112 expect ( actions [ 0 ] . getAttribute ( "aria-selected" ) ) . toBeTruthy ( ) ;
90113 expect ( getAllByRole ( "group" ) . length ) . toBe ( 2 ) ;
114+ const section = getAllByRole ( "group" ) [ 0 ] ;
115+ expect ( section . getAttribute ( "aria-labelledby" ) ) . toBe ( "Team repositories" ) ;
91116 } ) ;
92117 test ( "The onSelect event from each item is called correctly" , ( ) => {
93118 const test = [
0 commit comments