22// SPDX-License-Identifier: Apache-2.0
33import React from 'react' ;
44import { render } from '@testing-library/react' ;
5+
56import ButtonDropdown from '../../../lib/components/button-dropdown' ;
7+ import createWrapper from '../../../lib/components/test-utils/dom' ;
68
79describe ( 'ButtonDropdown data attributes' , ( ) => {
810 test ( 'renders custom data attributes on items' , ( ) => {
9- const { getByText } = render (
11+ const { container } = render (
1012 < ButtonDropdown
1113 items = { [
1214 {
@@ -21,32 +23,37 @@ describe('ButtonDropdown data attributes', () => {
2123 />
2224 ) ;
2325
24- const item = getByText ( 'Edit' ) . closest ( 'li' ) ;
26+ const wrapper = createWrapper ( container ) . findButtonDropdown ( ) ! ;
27+ wrapper . openDropdown ( ) ;
28+ const item = wrapper . findItemById ( 'edit' ) ! . getElement ( ) ;
29+
2530 expect ( item ) . toHaveAttribute ( 'data-analytics-action' , 'edit-product' ) ;
2631 expect ( item ) . toHaveAttribute ( 'data-item-key' , 'product-123' ) ;
2732 } ) ;
2833
29- test ( 'automatically prepends data- prefix' , ( ) => {
30- const { getByText } = render (
34+ test ( 'does not duplicate data- prefix when already present ' , ( ) => {
35+ const { container } = render (
3136 < ButtonDropdown
3237 items = { [
3338 {
34- id : 'delete ' ,
35- text : 'Delete ' ,
36- dataAttributes : { 'custom-attr ' : 'value' } ,
39+ id : 'action ' ,
40+ text : 'Action ' ,
41+ dataAttributes : { 'data-custom ' : 'value' } ,
3742 } ,
3843 ] }
3944 />
4045 ) ;
4146
42- const item = getByText ( 'Delete' ) . closest ( 'li' ) ;
43- expect ( item ) . toHaveAttribute ( 'data-custom-attr' , 'value' ) ;
47+ const wrapper = createWrapper ( container ) . findButtonDropdown ( ) ! ;
48+ wrapper . openDropdown ( ) ;
49+ const item = wrapper . findItemById ( 'action' ) ! . getElement ( ) ;
50+
51+ expect ( item ) . toHaveAttribute ( 'data-custom' , 'value' ) ;
52+ expect ( item ) . not . toHaveAttribute ( 'data-data-custom' ) ;
4453 } ) ;
4554
4655 test ( 'excludes testid from dataAttributes' , ( ) => {
47- const consoleSpy = jest . spyOn ( console , 'warn' ) . mockImplementation ( ) ;
48-
49- const { getByText } = render (
56+ const { container } = render (
5057 < ButtonDropdown
5158 items = { [
5259 {
@@ -58,80 +65,54 @@ describe('ButtonDropdown data attributes', () => {
5865 />
5966 ) ;
6067
61- const item = getByText ( 'Action' ) . closest ( 'li' ) ;
68+ const wrapper = createWrapper ( container ) . findButtonDropdown ( ) ! ;
69+ wrapper . openDropdown ( ) ;
70+ const item = wrapper . findItemById ( 'original-id' ) ! . getElement ( ) ;
71+
6272 expect ( item ) . toHaveAttribute ( 'data-testid' , 'original-id' ) ;
63- expect ( consoleSpy ) . toHaveBeenCalledWith (
64- 'ButtonDropdown: "testid" key is reserved and cannot be overridden via dataAttributes'
65- ) ;
66-
67- consoleSpy . mockRestore ( ) ;
6873 } ) ;
6974
7075 test ( 'handles undefined values' , ( ) => {
71- const { getByText } = render (
76+ const dataAttrs : Record < string , string | undefined > = {
77+ defined : 'value' ,
78+ undefined : undefined ,
79+ } ;
80+
81+ const { container } = render (
7282 < ButtonDropdown
7383 items = { [
7484 {
7585 id : 'action' ,
7686 text : 'Action' ,
77- dataAttributes : {
78- defined : 'value' ,
79- undefined : undefined ,
80- } ,
87+ dataAttributes : dataAttrs as Record < string , string > ,
8188 } ,
8289 ] }
8390 />
8491 ) ;
8592
86- const item = getByText ( 'Action' ) . closest ( 'li' ) ;
93+ const wrapper = createWrapper ( container ) . findButtonDropdown ( ) ! ;
94+ wrapper . openDropdown ( ) ;
95+ const item = wrapper . findItemById ( 'action' ) ! . getElement ( ) ;
96+
8797 expect ( item ) . toHaveAttribute ( 'data-defined' , 'value' ) ;
8898 expect ( item ) . not . toHaveAttribute ( 'data-undefined' ) ;
8999 } ) ;
90100
91- test ( 'works with multiple items' , ( ) => {
92- const { getByText } = render (
101+ test ( 'works with multiple items, disabled items, and checkbox items ' , ( ) => {
102+ const { container } = render (
93103 < ButtonDropdown
94104 items = { [
95105 {
96106 id : 'edit' ,
97107 text : 'Edit' ,
98108 dataAttributes : { action : 'edit' } ,
99109 } ,
100- {
101- id : 'delete' ,
102- text : 'Delete' ,
103- dataAttributes : { action : 'delete' } ,
104- } ,
105- ] }
106- />
107- ) ;
108-
109- expect ( getByText ( 'Edit' ) . closest ( 'li' ) ) . toHaveAttribute ( 'data-action' , 'edit' ) ;
110- expect ( getByText ( 'Delete' ) . closest ( 'li' ) ) . toHaveAttribute ( 'data-action' , 'delete' ) ;
111- } ) ;
112-
113- test ( 'works with disabled items' , ( ) => {
114- const { getByText } = render (
115- < ButtonDropdown
116- items = { [
117110 {
118111 id : 'disabled' ,
119112 text : 'Disabled' ,
120113 disabled : true ,
121114 dataAttributes : { state : 'disabled' } ,
122115 } ,
123- ] }
124- />
125- ) ;
126-
127- const item = getByText ( 'Disabled' ) . closest ( 'li' ) ;
128- expect ( item ) . toHaveAttribute ( 'data-state' , 'disabled' ) ;
129- } ) ;
130-
131- test ( 'works with checkbox items' , ( ) => {
132- const { getByText } = render (
133- < ButtonDropdown
134- items = { [
135116 {
136117 itemType : 'checkbox' ,
137118 id : 'checkbox-item' ,
@@ -143,7 +124,11 @@ describe('ButtonDropdown data attributes', () => {
143124 />
144125 ) ;
145126
146- const item = getByText ( 'Checkbox' ) . closest ( 'li' ) ;
147- expect ( item ) . toHaveAttribute ( 'data-checkbox-id' , 'cb-1' ) ;
127+ const wrapper = createWrapper ( container ) . findButtonDropdown ( ) ! ;
128+ wrapper . openDropdown ( ) ;
129+
130+ expect ( wrapper . findItemById ( 'edit' ) ! . getElement ( ) ) . toHaveAttribute ( 'data-action' , 'edit' ) ;
131+ expect ( wrapper . findItemById ( 'disabled' ) ! . getElement ( ) ) . toHaveAttribute ( 'data-state' , 'disabled' ) ;
132+ expect ( wrapper . findItemById ( 'checkbox-item' ) ! . getElement ( ) ) . toHaveAttribute ( 'data-checkbox-id' , 'cb-1' ) ;
148133 } ) ;
149134} ) ;
0 commit comments