@@ -34,24 +34,24 @@ describe('Unit Button', () => {
34
34
35
35
it ( 'hides title by default' , ( ) => {
36
36
render ( < UnitButton { ...mockData } /> , { wrapWithRouter : true } ) ;
37
- expect ( screen . getByRole ( 'link ' ) ) . not . toHaveTextContent ( unit . display_name ) ;
37
+ expect ( screen . getByRole ( 'tabpanel ' ) ) . not . toHaveTextContent ( unit . display_name ) ;
38
38
} ) ;
39
39
40
40
it ( 'shows title' , ( ) => {
41
- render ( < UnitButton { ...mockData } showTitle /> ) ;
41
+ render ( < UnitButton { ...mockData } showTitle /> , { wrapWithRouter : true } ) ;
42
42
expect ( screen . getByRole ( 'tabpanel' ) ) . toHaveTextContent ( unit . display_name ) ;
43
43
} ) ;
44
44
45
45
it ( 'check button attributes' , ( ) => {
46
- render ( < UnitButton { ...mockData } showTitle /> ) ;
46
+ render ( < UnitButton { ...mockData } showTitle /> , { wrapWithRouter : true } ) ;
47
47
expect ( screen . getByRole ( 'tabpanel' ) ) . toHaveAttribute ( 'id' , `${ unit . display_name } -${ courseMetadata . id } ` ) ;
48
48
expect ( screen . getByRole ( 'tabpanel' ) ) . toHaveAttribute ( 'aria-controls' , unit . display_name ) ;
49
49
expect ( screen . getByRole ( 'tabpanel' ) ) . toHaveAttribute ( 'aria-labelledby' , unit . display_name ) ;
50
50
expect ( screen . getByRole ( 'tabpanel' ) ) . toHaveAttribute ( 'tabindex' , '-1' ) ;
51
51
} ) ;
52
52
53
53
it ( 'button with isActive prop has tabindex 0' , ( ) => {
54
- render ( < UnitButton { ...mockData } isActive /> ) ;
54
+ render ( < UnitButton { ...mockData } isActive /> , { wrapWithRouter : true } ) ;
55
55
expect ( screen . getByRole ( 'tabpanel' ) ) . toHaveAttribute ( 'tabindex' , '0' ) ;
56
56
} ) ;
57
57
@@ -93,7 +93,33 @@ describe('Unit Button', () => {
93
93
it ( 'handles the click' , ( ) => {
94
94
const onClick = jest . fn ( ) ;
95
95
render ( < UnitButton { ...mockData } onClick = { onClick } /> , { wrapWithRouter : true } ) ;
96
- fireEvent . click ( screen . getByRole ( 'link ' ) ) ;
96
+ fireEvent . click ( screen . getByRole ( 'tabpanel ' ) ) ;
97
97
expect ( onClick ) . toHaveBeenCalledTimes ( 1 ) ;
98
98
} ) ;
99
+
100
+ it ( 'handles keydown events for Enter and Space and focuses bookmark button' , async ( ) => {
101
+ const onClick = jest . fn ( ) ;
102
+ render ( < UnitButton { ...mockData } onClick = { onClick } /> , { wrapWithRouter : true } ) ;
103
+
104
+ const button = screen . getByRole ( 'tabpanel' ) ;
105
+
106
+ fireEvent . keyDown ( button , { key : 'Enter' } ) ;
107
+ expect ( onClick ) . toHaveBeenCalledTimes ( 1 ) ;
108
+
109
+ fireEvent . keyDown ( button , { key : ' ' } ) ;
110
+ expect ( onClick ) . toHaveBeenCalledTimes ( 2 ) ;
111
+
112
+ const bookmarkButton = document . createElement ( 'button' ) ;
113
+ bookmarkButton . id = 'bookmark-button' ;
114
+ document . body . appendChild ( bookmarkButton ) ;
115
+
116
+ fireEvent . keyDown ( button , { key : 'Enter' } ) ;
117
+
118
+ // eslint-disable-next-line no-promise-executor-return
119
+ await new Promise ( resolve => requestAnimationFrame ( ( ) => requestAnimationFrame ( resolve ) ) ) ;
120
+
121
+ expect ( document . activeElement ) . toBe ( bookmarkButton ) ;
122
+
123
+ document . body . removeChild ( bookmarkButton ) ;
124
+ } ) ;
99
125
} ) ;
0 commit comments