@@ -75,34 +75,15 @@ describe('Hooks', () => {
75
75
expect ( targetContent . focus ) . toHaveBeenCalled ( ) ;
76
76
} ) ;
77
77
78
- test ( 'should trigger on "Enter" key' , async ( ) => {
79
- render ( < TestComponent /> ) ;
80
-
81
- const skipLink = screen . getByRole ( 'link' , { name : / s k i p t o c o n t e n t / i } ) ;
82
- const targetContent = screen . getByTestId ( 'target-content' ) ;
83
-
84
- targetContent . focus = jest . fn ( ) ;
85
-
86
- skipLink . focus ( ) ;
87
-
88
- await userEvent . keyboard ( '{Enter}' ) ;
89
-
90
- await waitFor ( ( ) => {
91
- expect ( global . scrollTo ) . toHaveBeenCalledWith ( {
92
- top : expect . any ( Number ) , behavior : 'smooth' ,
93
- } ) ;
94
- } ) ;
95
- expect ( targetContent . focus ) . toHaveBeenCalled ( ) ;
96
- } ) ;
97
-
98
78
test ( 'should warn if element is not focusable' , async ( ) => {
99
79
render ( < TestComponent /> ) ;
100
80
101
- const skipLink = screen . getByRole ( ' link', { name : / s k i p t o c o n t e n t / i } ) ;
81
+ const skipLink = screen . getByTestId ( 'skip- link') ;
102
82
const targetContent = screen . getByTestId ( 'target-content' ) ;
103
83
104
- jest . spyOn ( targetContent , 'focus' ) . mockImplementationOnce ( ( ) => {
105
- throw new Error ( 'focus is not a function' ) ;
84
+ Object . defineProperty ( targetContent , 'focus' , {
85
+ value : undefined ,
86
+ configurable : true ,
106
87
} ) ;
107
88
108
89
await userEvent . click ( skipLink ) ;
@@ -119,13 +100,20 @@ describe('Hooks', () => {
119
100
} ) ;
120
101
121
102
test ( 'should warn if target element is not found' , async ( ) => {
122
- render ( < TestComponent /> ) ;
103
+ const ComponentWithoutTarget = ( ) => {
104
+ useScrollToContent ( ) ;
105
+ return (
106
+ < >
107
+ < a href = "#main-content" data-testid = "skip-link" > Skip to content</ a >
108
+ { /* Нет #main-content */ }
109
+ </ >
110
+ ) ;
111
+ } ;
112
+
113
+ render ( < ComponentWithoutTarget /> ) ;
123
114
124
115
const skipLink = screen . getByRole ( 'link' , { name : / s k i p t o c o n t e n t / i } ) ;
125
116
126
- const targetElement = document . getElementById ( 'main-content' ) ;
127
- targetElement ?. parentNode ?. removeChild ( targetElement ) ;
128
-
129
117
await userEvent . click ( skipLink ) ;
130
118
131
119
await waitFor ( ( ) => {
0 commit comments