-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
trap.spec.js
99 lines (91 loc) · 3.07 KB
/
trap.spec.js
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
import { haveText, test, html, haveFocus, notHaveAttribute, haveAttribute } from '../../utils'
test('can trap focus',
[html`
<div x-data="{ open: false }">
<input type="text" id="1">
<button id="2" @click="open = true">open</button>
<div>
<div x-trap="open">
<input type="text" id="3">
<button @click="open = false" id="4">close</button>
</div>
</div>
</div>
`],
({ get }, reload) => {
get('#1').click()
get('#1').should(haveFocus())
get('#2').click()
get('#3').should(haveFocus())
cy.focused().tab()
get('#4').should(haveFocus())
cy.focused().tab()
get('#3').should(haveFocus())
cy.focused().tab({shift: true})
get('#4').should(haveFocus())
cy.focused().click()
get('#2').should(haveFocus())
},
)
test('works with clone',
[html`
<div id="foo" x-data="{
open: false,
triggerClone() {
var original = document.getElementById('foo');
var copy = original.cloneNode(true);
Alpine.clone(original, copy);
var p = document.createElement('p');
p.textContent = 'bar';
copy.appendChild(p);
original.parentNode.replaceChild(copy, original);
}
}">
<button id="one" @click="open = true">Trap</button>
<div x-trap="open">
<input type="text">
<button id="two" @click="triggerClone()">Test</button>
</div>
</div>
`],
({ get, wait }, reload) => {
get('#one').click()
get('#two').click()
get('p').should(haveText('bar'))
}
)
test('can trap focus with inert',
[html`
<div x-data="{ open: false }">
<h1>I should have aria-hidden when outside trap</h1>
<button id="open" @click="open = true">open</button>
<div x-trap.inert="open">
<button @click="open = false" id="close">close</button>
</div>
</div>
`],
({ get }, reload) => {
get('#open').should(notHaveAttribute('aria-hidden', 'true'))
get('#open').click()
get('#open').should(haveAttribute('aria-hidden', 'true'))
get('#close').click()
get('#open').should(notHaveAttribute('aria-hidden', 'true'))
},
)
test('can trap focus with noscroll',
[html`
<div x-data="{ open: false }">
<button id="open" @click="open = true">open</button>
<div x-trap.noscroll="open">
<button @click="open = false" id="close">close</button>
</div>
<div style="height: 100vh"> </div>
</div>
`],
({ get }, reload) => {
get('#open').click()
get('html').should(haveAttribute('style', 'overflow: hidden; padding-right: 0px;'))
get('#close').click()
get('html').should(notHaveAttribute('style', 'overflow: hidden; padding-right: 0px;'))
},
)