/
index.tsx
772 lines (651 loc) · 22.9 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
/**
* External dependencies
*/
import type { ComponentPropsWithoutRef } from 'react';
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
/**
* WordPress dependencies
*/
import { useState } from '@wordpress/element';
/**
* Internal dependencies
*/
import Button from '../../button';
import {
NavigatorProvider,
NavigatorScreen,
NavigatorButton,
NavigatorBackButton,
NavigatorToParentButton,
useNavigator,
} from '..';
import type { NavigateOptions } from '../types';
const INVALID_HTML_ATTRIBUTE = {
raw: ' "\'><=invalid_path',
escaped: " "'><=invalid_path",
};
const PATHS = {
HOME: '/',
CHILD: '/child',
NESTED: '/child/nested',
PRODUCT_PATTERN: '/product/:productId',
PRODUCT_1: '/product/1',
PRODUCT_2: '/product/2',
INVALID_HTML_ATTRIBUTE: INVALID_HTML_ATTRIBUTE.raw,
NOT_FOUND: '/not-found',
};
const SCREEN_TEXT = {
home: 'This is the home screen.',
child: 'This is the child screen.',
nested: 'This is the nested screen.',
product: 'This is the product screen.',
invalidHtmlPath: 'This is the screen with an invalid HTML value as a path.',
};
const BUTTON_TEXT = {
toNonExistingScreen: 'Navigate to non-existing screen.',
toChildScreen: 'Navigate to child screen.',
toNestedScreen: 'Navigate to nested screen.',
toProductScreen1: 'Navigate to product 1 screen.',
toProductScreen2: 'Navigate to product 2 screen.',
toInvalidHtmlPathScreen:
'Navigate to screen with an invalid HTML value as a path.',
back: 'Go back',
backUsingGoTo: 'Go back using goTo',
goToWithSkipFocus: 'Go to with skipFocus',
};
type CustomTestOnClickHandler = (
args:
| {
type: 'goTo';
path: string;
options?: NavigateOptions;
}
| { type: 'goBack' }
| { type: 'goToParent' }
) => void;
function CustomNavigatorButton( {
path,
onClick,
...props
}: Omit< ComponentPropsWithoutRef< typeof NavigatorButton >, 'onClick' > & {
onClick?: CustomTestOnClickHandler;
} ) {
return (
<NavigatorButton
onClick={ () => {
// Used to spy on the values passed to `navigator.goTo`.
onClick?.( { type: 'goTo', path } );
} }
path={ path }
{ ...props }
/>
);
}
function CustomNavigatorGoToBackButton( {
path,
onClick,
...props
}: Omit< ComponentPropsWithoutRef< typeof NavigatorButton >, 'onClick' > & {
onClick?: CustomTestOnClickHandler;
} ) {
const { goTo } = useNavigator();
return (
<Button
onClick={ () => {
goTo( path, { isBack: true } );
// Used to spy on the values passed to `navigator.goTo`.
onClick?.( { type: 'goTo', path } );
} }
{ ...props }
/>
);
}
function CustomNavigatorGoToSkipFocusButton( {
path,
onClick,
...props
}: Omit< ComponentPropsWithoutRef< typeof NavigatorButton >, 'onClick' > & {
onClick?: CustomTestOnClickHandler;
} ) {
const { goTo } = useNavigator();
return (
<Button
onClick={ () => {
goTo( path, { skipFocus: true } );
// Used to spy on the values passed to `navigator.goTo`.
onClick?.( { type: 'goTo', path } );
} }
{ ...props }
/>
);
}
function CustomNavigatorBackButton( {
onClick,
...props
}: Omit< ComponentPropsWithoutRef< typeof NavigatorBackButton >, 'onClick' > & {
onClick?: CustomTestOnClickHandler;
} ) {
return (
<NavigatorBackButton
onClick={ () => {
// Used to spy on the values passed to `navigator.goBack`.
onClick?.( { type: 'goBack' } );
} }
{ ...props }
/>
);
}
function CustomNavigatorToParentButton( {
onClick,
...props
}: Omit< ComponentPropsWithoutRef< typeof NavigatorBackButton >, 'onClick' > & {
onClick?: CustomTestOnClickHandler;
} ) {
return (
<NavigatorToParentButton
onClick={ () => {
// Used to spy on the values passed to `navigator.goBack`.
onClick?.( { type: 'goToParent' } );
} }
{ ...props }
/>
);
}
const ProductScreen = ( {
onBackButtonClick,
}: {
onBackButtonClick?: CustomTestOnClickHandler;
} ) => {
const { params } = useNavigator();
return (
<NavigatorScreen path={ PATHS.PRODUCT_PATTERN }>
<p>{ SCREEN_TEXT.product }</p>
<p>Product ID is { params.productId }</p>
<CustomNavigatorBackButton onClick={ onBackButtonClick }>
{ BUTTON_TEXT.back }
</CustomNavigatorBackButton>
</NavigatorScreen>
);
};
const MyNavigation = ( {
initialPath = PATHS.HOME,
onNavigatorButtonClick,
}: {
initialPath?: string;
onNavigatorButtonClick?: CustomTestOnClickHandler;
} ) => {
const [ innerInputValue, setInnerInputValue ] = useState( '' );
const [ outerInputValue, setOuterInputValue ] = useState( '' );
return (
<>
<NavigatorProvider initialPath={ initialPath }>
<NavigatorScreen path={ PATHS.HOME }>
<p>{ SCREEN_TEXT.home }</p>
{ /*
* A button useful to test focus restoration. This button is the first
* tabbable item in the screen, but should not receive focus when
* navigating to screen as a result of a backwards navigation.
*/ }
<button>First tabbable home screen button</button>
<CustomNavigatorButton
path={ PATHS.NOT_FOUND }
onClick={ onNavigatorButtonClick }
>
{ BUTTON_TEXT.toNonExistingScreen }
</CustomNavigatorButton>
<CustomNavigatorButton
path={ PATHS.CHILD }
onClick={ onNavigatorButtonClick }
>
{ BUTTON_TEXT.toChildScreen }
</CustomNavigatorButton>
<CustomNavigatorButton
path={ PATHS.PRODUCT_1 }
onClick={ onNavigatorButtonClick }
>
{ BUTTON_TEXT.toProductScreen1 }
</CustomNavigatorButton>
<CustomNavigatorButton
path={ PATHS.PRODUCT_2 }
onClick={ onNavigatorButtonClick }
>
{ BUTTON_TEXT.toProductScreen2 }
</CustomNavigatorButton>
<CustomNavigatorButton
path={ PATHS.INVALID_HTML_ATTRIBUTE }
onClick={ onNavigatorButtonClick }
>
{ BUTTON_TEXT.toInvalidHtmlPathScreen }
</CustomNavigatorButton>
</NavigatorScreen>
<NavigatorScreen path={ PATHS.CHILD }>
<p>{ SCREEN_TEXT.child }</p>
{ /*
* A button useful to test focus restoration. This button is the first
* tabbable item in the screen, but should not receive focus when
* navigating to screen as a result of a backwards navigation.
*/ }
<button>First tabbable child screen button</button>
<CustomNavigatorButton
path={ PATHS.NESTED }
onClick={ onNavigatorButtonClick }
>
{ BUTTON_TEXT.toNestedScreen }
</CustomNavigatorButton>
<CustomNavigatorBackButton
onClick={ onNavigatorButtonClick }
>
{ BUTTON_TEXT.back }
</CustomNavigatorBackButton>
<label htmlFor="test-input-inner">Inner input</label>
<input
name="test-input-inner"
// eslint-disable-next-line no-restricted-syntax
id="test-input-inner"
onChange={ ( e ) => {
setInnerInputValue( e.target.value );
} }
value={ innerInputValue }
/>
</NavigatorScreen>
<NavigatorScreen path={ PATHS.NESTED }>
<p>{ SCREEN_TEXT.nested }</p>
<CustomNavigatorBackButton
onClick={ onNavigatorButtonClick }
>
{ BUTTON_TEXT.back }
</CustomNavigatorBackButton>
</NavigatorScreen>
<ProductScreen onBackButtonClick={ onNavigatorButtonClick } />
<NavigatorScreen path={ PATHS.INVALID_HTML_ATTRIBUTE }>
<p>{ SCREEN_TEXT.invalidHtmlPath }</p>
<CustomNavigatorBackButton
onClick={ onNavigatorButtonClick }
>
{ BUTTON_TEXT.back }
</CustomNavigatorBackButton>
</NavigatorScreen>
{ /* A `NavigatorScreen` with `path={ PATHS.NOT_FOUND }` is purposefully not included. */ }
</NavigatorProvider>
<label htmlFor="test-input-outer">Outer input</label>
<input
name="test-input-outer"
// eslint-disable-next-line no-restricted-syntax
id="test-input-outer"
onChange={ ( e ) => {
setOuterInputValue( e.target.value );
} }
value={ outerInputValue }
/>
</>
);
};
const MyHierarchicalNavigation = ( {
initialPath = PATHS.HOME,
onNavigatorButtonClick,
}: {
initialPath?: string;
onNavigatorButtonClick?: CustomTestOnClickHandler;
} ) => {
return (
<>
<NavigatorProvider initialPath={ initialPath }>
<NavigatorScreen path={ PATHS.HOME }>
<p>{ SCREEN_TEXT.home }</p>
{ /*
* A button useful to test focus restoration. This button is the first
* tabbable item in the screen, but should not receive focus when
* navigating to screen as a result of a backwards navigation.
*/ }
<button>First tabbable home screen button</button>
<CustomNavigatorButton
path={ PATHS.CHILD }
onClick={ onNavigatorButtonClick }
>
{ BUTTON_TEXT.toChildScreen }
</CustomNavigatorButton>
</NavigatorScreen>
<NavigatorScreen path={ PATHS.CHILD }>
<p>{ SCREEN_TEXT.child }</p>
{ /*
* A button useful to test focus restoration. This button is the first
* tabbable item in the screen, but should not receive focus when
* navigating to screen as a result of a backwards navigation.
*/ }
<button>First tabbable child screen button</button>
<CustomNavigatorButton
path={ PATHS.NESTED }
onClick={ onNavigatorButtonClick }
>
{ BUTTON_TEXT.toNestedScreen }
</CustomNavigatorButton>
<CustomNavigatorToParentButton
onClick={ onNavigatorButtonClick }
>
{ BUTTON_TEXT.back }
</CustomNavigatorToParentButton>
</NavigatorScreen>
<NavigatorScreen path={ PATHS.NESTED }>
<p>{ SCREEN_TEXT.nested }</p>
<CustomNavigatorToParentButton
onClick={ onNavigatorButtonClick }
>
{ BUTTON_TEXT.back }
</CustomNavigatorToParentButton>
<CustomNavigatorGoToBackButton
path={ PATHS.CHILD }
onClick={ onNavigatorButtonClick }
>
{ BUTTON_TEXT.backUsingGoTo }
</CustomNavigatorGoToBackButton>
</NavigatorScreen>
<CustomNavigatorGoToSkipFocusButton
path={ PATHS.NESTED }
onClick={ onNavigatorButtonClick }
>
{ BUTTON_TEXT.goToWithSkipFocus }
</CustomNavigatorGoToSkipFocusButton>
</NavigatorProvider>
</>
);
};
const getScreen = ( screenKey: keyof typeof SCREEN_TEXT ) =>
screen.getByText( SCREEN_TEXT[ screenKey ] );
const queryScreen = ( screenKey: keyof typeof SCREEN_TEXT ) =>
screen.queryByText( SCREEN_TEXT[ screenKey ] );
const getNavigationButton = ( buttonKey: keyof typeof BUTTON_TEXT ) =>
screen.getByRole( 'button', { name: BUTTON_TEXT[ buttonKey ] } );
describe( 'Navigator', () => {
const originalGetClientRects = window.Element.prototype.getClientRects;
// `getClientRects` needs to be mocked so that `isVisible` from the `@wordpress/dom`
// `focusable` module can pass, in a JSDOM env where the DOM elements have no width/height.
const mockedGetClientRects = jest.fn( () => [
{
x: 0,
y: 0,
width: 100,
height: 100,
},
] );
beforeAll( () => {
// @ts-expect-error There's no need for an exact mock, this is just needed
// for the tests to pass (see `mockedGetClientRects` inline comments).
window.Element.prototype.getClientRects =
jest.fn( mockedGetClientRects );
} );
afterAll( () => {
window.Element.prototype.getClientRects = originalGetClientRects;
} );
it( 'should render', () => {
render( <MyNavigation /> );
expect( getScreen( 'home' ) ).toBeInTheDocument();
expect( queryScreen( 'child' ) ).not.toBeInTheDocument();
expect( queryScreen( 'nested' ) ).not.toBeInTheDocument();
} );
it( 'should show a different screen on the first render depending on the value of `initialPath`', () => {
render( <MyNavigation initialPath={ PATHS.CHILD } /> );
expect( queryScreen( 'home' ) ).not.toBeInTheDocument();
expect( getScreen( 'child' ) ).toBeInTheDocument();
expect( queryScreen( 'nested' ) ).not.toBeInTheDocument();
} );
it( 'should ignore changes to `initialPath` after the first render', () => {
const { rerender } = render( <MyNavigation /> );
expect( getScreen( 'home' ) ).toBeInTheDocument();
expect( queryScreen( 'child' ) ).not.toBeInTheDocument();
expect( queryScreen( 'nested' ) ).not.toBeInTheDocument();
rerender( <MyNavigation initialPath={ PATHS.CHILD } /> );
expect( getScreen( 'home' ) ).toBeInTheDocument();
expect( queryScreen( 'child' ) ).not.toBeInTheDocument();
expect( queryScreen( 'nested' ) ).not.toBeInTheDocument();
} );
it( 'should not rended anything if the `initialPath` does not match any available screen', () => {
render( <MyNavigation initialPath={ PATHS.NOT_FOUND } /> );
expect( queryScreen( 'home' ) ).not.toBeInTheDocument();
expect( queryScreen( 'child' ) ).not.toBeInTheDocument();
expect( queryScreen( 'nested' ) ).not.toBeInTheDocument();
} );
it( 'should navigate across screens', async () => {
const spy = jest.fn();
const user = userEvent.setup();
render( <MyNavigation onNavigatorButtonClick={ spy } /> );
expect( getScreen( 'home' ) ).toBeInTheDocument();
expect( getNavigationButton( 'toChildScreen' ) ).toBeInTheDocument();
// Navigate to child screen.
await user.click( getNavigationButton( 'toChildScreen' ) );
expect( getScreen( 'child' ) ).toBeInTheDocument();
expect( getNavigationButton( 'back' ) ).toBeInTheDocument();
// Navigate back to home screen.
await user.click( getNavigationButton( 'back' ) );
expect( getScreen( 'home' ) ).toBeInTheDocument();
expect( getNavigationButton( 'toChildScreen' ) ).toBeInTheDocument();
// Navigate again to child screen.
await user.click( getNavigationButton( 'toChildScreen' ) );
expect( getScreen( 'child' ) ).toBeInTheDocument();
expect( getNavigationButton( 'toNestedScreen' ) ).toBeInTheDocument();
// Navigate to nested screen.
await user.click( getNavigationButton( 'toNestedScreen' ) );
expect( getScreen( 'nested' ) ).toBeInTheDocument();
expect( getNavigationButton( 'back' ) ).toBeInTheDocument();
// Navigate back to child screen.
await user.click( getNavigationButton( 'back' ) );
expect( getScreen( 'child' ) ).toBeInTheDocument();
expect( getNavigationButton( 'toNestedScreen' ) ).toBeInTheDocument();
// Navigate back to home screen.
await user.click( getNavigationButton( 'back' ) );
expect( getScreen( 'home' ) ).toBeInTheDocument();
expect( getNavigationButton( 'toChildScreen' ) ).toBeInTheDocument();
// Check the values passed to `navigator.goTo()`.
expect( spy ).toHaveBeenCalledTimes( 6 );
expect( spy ).toHaveBeenNthCalledWith( 1, {
path: PATHS.CHILD,
type: 'goTo',
} );
expect( spy ).toHaveBeenNthCalledWith( 2, {
type: 'goBack',
} );
expect( spy ).toHaveBeenNthCalledWith( 3, {
path: PATHS.CHILD,
type: 'goTo',
} );
expect( spy ).toHaveBeenNthCalledWith( 4, {
path: PATHS.NESTED,
type: 'goTo',
} );
expect( spy ).toHaveBeenNthCalledWith( 5, {
type: 'goBack',
} );
expect( spy ).toHaveBeenNthCalledWith( 6, {
type: 'goBack',
} );
} );
it( 'should not render anything if the path does not match any available screen', async () => {
const spy = jest.fn();
const user = userEvent.setup();
render( <MyNavigation onNavigatorButtonClick={ spy } /> );
expect(
getNavigationButton( 'toNonExistingScreen' )
).toBeInTheDocument();
// Attempt to navigate to non-existing screen. No screens get rendered.
await user.click( getNavigationButton( 'toNonExistingScreen' ) );
expect( queryScreen( 'home' ) ).not.toBeInTheDocument();
expect( queryScreen( 'child' ) ).not.toBeInTheDocument();
expect( queryScreen( 'nested' ) ).not.toBeInTheDocument();
// Check the values passed to `navigator.goTo()`.
expect( spy ).toHaveBeenCalledTimes( 1 );
expect( spy ).toHaveBeenNthCalledWith( 1, {
path: PATHS.NOT_FOUND,
type: 'goTo',
} );
} );
it( 'should escape the value of the `path` prop', async () => {
render( <MyNavigation /> );
expect( getScreen( 'home' ) ).toBeInTheDocument();
expect(
getNavigationButton( 'toInvalidHtmlPathScreen' )
).toBeInTheDocument();
// The following line tests the implementation details, but it's necessary
// as this would be otherwise transparent to the user.
// A potential way would be to check if an invalid HTML attribute could
// be detected in the tests (by JSDom or any other plugin). We could then
// make sure that an invalid path would not error because it's escaped
// correctly.
expect(
getNavigationButton( 'toInvalidHtmlPathScreen' )
).toHaveAttribute( 'id', INVALID_HTML_ATTRIBUTE.escaped );
} );
it( 'should match correctly paths with named arguments', async () => {
const user = userEvent.setup();
render( <MyNavigation /> );
expect( getScreen( 'home' ) ).toBeInTheDocument();
// Navigate to Product 1 screen
await user.click( getNavigationButton( 'toProductScreen1' ) );
expect( getScreen( 'product' ) ).toBeInTheDocument();
// Check that named parameter is extracted correctly
expect( screen.getByText( 'Product ID is 1' ) ).toBeInTheDocument();
// Navigate back to home screen
await user.click( getNavigationButton( 'back' ) );
expect( getScreen( 'home' ) ).toBeInTheDocument();
// Navigate to Product 2 screen
await user.click( getNavigationButton( 'toProductScreen2' ) );
expect( getScreen( 'product' ) ).toBeInTheDocument();
// Check that named parameter is extracted correctly
expect( screen.getByText( 'Product ID is 2' ) ).toBeInTheDocument();
} );
describe( 'focus management', () => {
it( 'should restore focus correctly', async () => {
const user = userEvent.setup();
render( <MyNavigation /> );
// Navigate to child screen.
await user.click( getNavigationButton( 'toChildScreen' ) );
// The first tabbable element receives focus.
expect(
screen.getByRole( 'button', {
name: 'First tabbable child screen button',
} )
).toHaveFocus();
// Navigate to nested screen.
await user.click( getNavigationButton( 'toNestedScreen' ) );
// The first tabbable element receives focus.
expect( getNavigationButton( 'back' ) ).toHaveFocus();
// Navigate back to child screen.
await user.click( getNavigationButton( 'back' ) );
// Focus is restored on the last element that had focus when the
// navigation away from the screen occurred.
expect( getNavigationButton( 'toNestedScreen' ) ).toHaveFocus();
// Navigate back to home screen.
await user.click( getNavigationButton( 'back' ) );
// Focus is restored on the last element that had focus when the
// navigation away from the screen occurred.
expect( getNavigationButton( 'toChildScreen' ) ).toHaveFocus();
// Navigate to product screen for product 2
await user.click( getNavigationButton( 'toProductScreen2' ) );
// The first tabbable element receives focus.
expect( getNavigationButton( 'back' ) ).toHaveFocus();
// Navigate back to home screen.
await user.click( getNavigationButton( 'back' ) );
// Focus is restored on the last element that had focus when the
// navigation away from the screen occurred.
expect( getNavigationButton( 'toProductScreen2' ) ).toHaveFocus();
} );
it( 'should keep focus on an active element inside navigator, while re-rendering', async () => {
const user = userEvent.setup();
render( <MyNavigation /> );
// Navigate to child screen.
await user.click( getNavigationButton( 'toChildScreen' ) );
// The first tabbable element receives focus.
expect(
screen.getByRole( 'button', {
name: 'First tabbable child screen button',
} )
).toHaveFocus();
// Interact with the inner input.
// The focus should stay on the input element.
const innerInput = screen.getByLabelText( 'Inner input' );
await user.type( innerInput, 'd' );
expect( innerInput ).toHaveFocus();
} );
it( 'should keep focus on an active element outside navigator, while re-rendering', async () => {
const user = userEvent.setup();
render( <MyNavigation /> );
// Navigate to child screen.
await user.click( getNavigationButton( 'toChildScreen' ) );
// The first tabbable element receives focus.
expect(
screen.getByRole( 'button', {
name: 'First tabbable child screen button',
} )
).toHaveFocus();
// Interact with the outer input.
// The focus should stay on the input element.
const outerInput = screen.getByLabelText( 'Outer input' );
await user.type( outerInput, 'd' );
expect( outerInput ).toHaveFocus();
} );
it( 'should restore focus correctly even when the `path` needs to be escaped', async () => {
const user = userEvent.setup();
render( <MyNavigation /> );
expect( getScreen( 'home' ) ).toBeInTheDocument();
// Navigate to screen with an invalid HTML value for its `path`.
await user.click(
getNavigationButton( 'toInvalidHtmlPathScreen' )
);
expect( getScreen( 'invalidHtmlPath' ) ).toBeInTheDocument();
// Navigate back to home screen, check that the focus restoration selector
// worked correctly despite the escaping.
await user.click( getNavigationButton( 'back' ) );
expect( getScreen( 'home' ) ).toBeInTheDocument();
expect(
getNavigationButton( 'toInvalidHtmlPathScreen' )
).toHaveFocus();
} );
it( 'should restore focus while using goTo and goToParent', async () => {
const user = userEvent.setup();
render( <MyHierarchicalNavigation /> );
expect( getScreen( 'home' ) ).toBeInTheDocument();
// Navigate to child screen.
await user.click( getNavigationButton( 'toChildScreen' ) );
expect( getScreen( 'child' ) ).toBeInTheDocument();
// Navigate to nested screen.
await user.click( getNavigationButton( 'toNestedScreen' ) );
expect( getScreen( 'nested' ) ).toBeInTheDocument();
expect( getNavigationButton( 'back' ) ).toBeInTheDocument();
// Navigate back to child screen using the back button.
await user.click( getNavigationButton( 'back' ) );
expect( getScreen( 'child' ) ).toBeInTheDocument();
expect( getNavigationButton( 'toNestedScreen' ) ).toHaveFocus();
// Re navigate to nested screen.
await user.click( getNavigationButton( 'toNestedScreen' ) );
expect( getScreen( 'nested' ) ).toBeInTheDocument();
expect(
getNavigationButton( 'backUsingGoTo' )
).toBeInTheDocument();
// Navigate back to child screen using the go to button.
await user.click( getNavigationButton( 'backUsingGoTo' ) );
expect( getScreen( 'child' ) ).toBeInTheDocument();
expect( getNavigationButton( 'toNestedScreen' ) ).toHaveFocus();
// Navigate back to home screen.
await user.click( getNavigationButton( 'back' ) );
expect( getNavigationButton( 'toChildScreen' ) ).toHaveFocus();
} );
it( 'should skip focus based on location `skipFocus` option', async () => {
const user = userEvent.setup();
render( <MyHierarchicalNavigation /> );
// Navigate to child screen with skipFocus.
await user.click( getNavigationButton( 'goToWithSkipFocus' ) );
expect( queryScreen( 'home' ) ).not.toBeInTheDocument();
expect( getScreen( 'nested' ) ).toBeInTheDocument();
// The clicked button should remain focused.
expect( getNavigationButton( 'goToWithSkipFocus' ) ).toHaveFocus();
// Navigate back to parent screen.
await user.click( getNavigationButton( 'back' ) );
expect( getScreen( 'child' ) ).toBeInTheDocument();
// The first tabbable element receives focus.
expect(
screen.getByRole( 'button', {
name: 'First tabbable child screen button',
} )
).toHaveFocus();
} );
} );
} );