···
1
+
import React, { useState, useRef } from 'react';
2
+
import { mount } from '@cypress/react';
4
+
import { useDismissable } from '../useDismissable';
6
+
const Dialog = ({ focusLoss }: { focusLoss?: boolean }) => {
7
+
const [visible, setVisible] = useState(true);
8
+
const ref = useRef<HTMLDivElement>(null);
10
+
const onDismiss = () => setVisible(false);
11
+
useDismissable(ref, onDismiss, { focusLoss, disabled: !visible });
14
+
<div ref={ref} role="dialog" style={{ display: visible ? 'block' : 'none' }}>
15
+
<button className="inside">focusable</button>
20
+
it('is dismissed by clicking outside', () => {
23
+
<button className="outside">outside</button>
28
+
cy.get('.inside').as('inside').click();
29
+
cy.get('@inside').should('be.visible');
30
+
cy.get('.outside').first().click();
31
+
cy.get('@inside').should('not.be.visible');
34
+
it('is not dismissed by clicking outside when it does not have priority', () => {
37
+
<button className="outside">outside</button>
43
+
cy.get('.inside').as('inside').should('be.visible');
44
+
// at first not dismissed
45
+
cy.get('.outside').first().click();
46
+
cy.get('@inside').should('be.visible');
47
+
// dismissed when the second Dialog loses focus
48
+
cy.get('.outside').first().click();
49
+
cy.get('@inside').should('not.be.visible');
52
+
it('is dismissed by pressing Escape', () => {
55
+
<button className="outside">outside</button>
60
+
cy.get('.inside').as('inside').should('be.visible');
61
+
cy.realPress('Escape');
62
+
cy.get('@inside').should('not.be.visible');
65
+
it('is not dismissed by pressing Escape when it does not have priority', () => {
68
+
<button className="outside">outside</button>
74
+
cy.get('.inside').as('inside').should('be.visible');
75
+
// at first not dismissed
76
+
cy.realPress('Escape');
77
+
cy.get('@inside').should('be.visible');
78
+
// dismissed when the second Dialog loses focus
79
+
cy.realPress('Escape');
80
+
cy.get('@inside').should('not.be.visible');
83
+
it('is dismissed without priority when it has focus', () => {
84
+
const Second = () => {
85
+
const ref = useRef<HTMLDivElement>(null);
86
+
useDismissable(ref, () => {});
87
+
return <div ref={ref} />;
92
+
<button className="outside">outside</button>
98
+
cy.get('.inside').as('inside').should('be.visible');
99
+
// not dismissed with escape press
100
+
cy.realPress('Escape');
101
+
cy.get('@inside').should('be.visible');
102
+
// is dismissed when it has focus
103
+
cy.get('@inside').focus();
104
+
cy.realPress('Escape');
105
+
cy.get('@inside').should('not.be.visible');
108
+
it('is dismissed when focus moves out of it, with focus loss active', () => {
111
+
<button className="outside">outside</button>
112
+
<Dialog focusLoss />
116
+
cy.get('.inside').as('inside').should('be.visible');
117
+
cy.get('@inside').focus();
118
+
cy.get('@inside').should('be.visible');
119
+
// is dismissed when it loses focus
120
+
cy.realPress(['Shift', 'Tab']);
121
+
cy.focused().contains('outside');
122
+
cy.get('@inside').should('not.be.visible');