1+ /**
2+ * @license
3+ * Copyright 2021 Google LLC
4+ * SPDX-License-Identifier: Apache-2.0
5+ */
6+
7+ import 'jasmine' ;
8+
9+ import * as strongFocus from '../strong-focus' ;
10+
11+ class MockFocus {
12+ constructor ( public visible = false ) { }
13+ setVisible ( visible : boolean ) {
14+ this . visible = visible ;
15+ }
16+ }
17+
18+ function simulateKeydown ( code : string ) {
19+ const ev = new KeyboardEvent ( 'keydown' , { code, bubbles : true } ) ;
20+ window . dispatchEvent ( ev ) ;
21+ }
22+
23+ describe ( 'Strong Focus' , ( ) => {
24+ describe ( 'standalone operation' , ( ) => {
25+ beforeEach ( ( ) => {
26+ strongFocus . setup ( new MockFocus ( ) , true ) ;
27+ } ) ;
28+
29+ it ( 'does not show strong focus by default' , ( ) => {
30+ expect ( strongFocus . shouldShowStrongFocus ( ) ) . toBeFalse ( ) ;
31+ } ) ;
32+
33+ it ( 'does not force strong focus by default' , ( ) => {
34+ expect ( strongFocus . isStrongFocusForced ( ) ) . toBeFalse ( ) ;
35+ } ) ;
36+
37+ describe ( 'keyboard navigation' , ( ) => {
38+ it ( 'shows strong focus on Tab' , ( ) => {
39+ simulateKeydown ( 'Tab' ) ;
40+ expect ( strongFocus . shouldShowStrongFocus ( ) ) . toBeTrue ( ) ;
41+ } ) ;
42+ it ( 'shows strong focus on ArrowLeft' , ( ) => {
43+ simulateKeydown ( 'ArrowLeft' ) ;
44+ expect ( strongFocus . shouldShowStrongFocus ( ) ) . toBeTrue ( ) ;
45+ } ) ;
46+ it ( 'shows strong focus on ArrowLeft' , ( ) => {
47+ simulateKeydown ( 'ArrowLeft' ) ;
48+ expect ( strongFocus . shouldShowStrongFocus ( ) ) . toBeTrue ( ) ;
49+ } ) ;
50+ it ( 'shows strong focus on ArrowRight' , ( ) => {
51+ simulateKeydown ( 'ArrowRight' ) ;
52+ expect ( strongFocus . shouldShowStrongFocus ( ) ) . toBeTrue ( ) ;
53+ } ) ;
54+ it ( 'shows strong focus on ArrowUp' , ( ) => {
55+ simulateKeydown ( 'ArrowUp' ) ;
56+ expect ( strongFocus . shouldShowStrongFocus ( ) ) . toBeTrue ( ) ;
57+ } ) ;
58+ it ( 'shows strong focus on ArrowDown' , ( ) => {
59+ simulateKeydown ( 'ArrowDown' ) ;
60+ expect ( strongFocus . shouldShowStrongFocus ( ) ) . toBeTrue ( ) ;
61+ } ) ;
62+ } ) ;
63+
64+ describe ( 'pointer interaction' , ( ) => {
65+ it ( 'does not show strong focus' , ( ) => {
66+ simulateKeydown ( 'Tab' ) ;
67+ strongFocus . pointerPress ( ) ;
68+ expect ( strongFocus . shouldShowStrongFocus ( ) ) . toBeFalse ( ) ;
69+ } ) ;
70+ } ) ;
71+ } ) ;
72+
73+ describe ( 'force strong focus' , ( ) => {
74+ beforeAll ( ( ) => {
75+ strongFocus . setForceStrongFocus ( true ) ;
76+ } ) ;
77+ afterAll ( ( ) => {
78+ strongFocus . setForceStrongFocus ( false ) ;
79+ } ) ;
80+
81+ beforeEach ( ( ) => {
82+ strongFocus . setup ( new MockFocus ( ) , true ) ;
83+ } ) ;
84+
85+ it ( 'shows strong focus when forced' , ( ) => {
86+ expect ( strongFocus . shouldShowStrongFocus ( ) ) . toBeTrue ( ) ;
87+ } ) ;
88+
89+ it ( 'reports that strong focus is forced' , ( ) => {
90+ expect ( strongFocus . isStrongFocusForced ( ) ) . toBeTrue ( ) ;
91+ } ) ;
92+
93+ it ( 'shows strong focus after pointer interaction' , ( ) => {
94+ strongFocus . pointerPress ( ) ;
95+ expect ( strongFocus . shouldShowStrongFocus ( ) ) . toBeTrue ( ) ;
96+ } ) ;
97+ } ) ;
98+
99+ describe ( 'shared focus state' , ( ) => {
100+ let focus ! : MockFocus ;
101+
102+ beforeEach ( ( ) => {
103+ focus = new MockFocus ( ) ;
104+ strongFocus . setup ( focus ) ;
105+ } ) ;
106+
107+ it ( 'reads from shared state' , ( ) => {
108+ focus . visible = true ;
109+ expect ( strongFocus . shouldShowStrongFocus ( ) ) . toBeTrue ( ) ;
110+ } ) ;
111+
112+ it ( 'writes to shared state' , ( ) => {
113+ focus . visible = true ;
114+ strongFocus . pointerPress ( ) ;
115+ expect ( focus . visible ) . toBeFalse ( ) ;
116+ } ) ;
117+ } ) ;
118+
119+ describe ( 'setup function' , ( ) => {
120+ it ( 'removes keydown listener when not wanted' , ( ) => {
121+ const focus = new MockFocus ( ) ;
122+ strongFocus . setup ( focus ) ;
123+ simulateKeydown ( 'Tab' ) ;
124+ expect ( focus . visible ) . toBeFalse ( ) ;
125+ } ) ;
126+ } ) ;
127+ } ) ;
0 commit comments