|
1 | | -import pattern from "./autofocus"; |
| 1 | +import "regenerator-runtime/runtime"; // needed for ``await`` support |
| 2 | +import "./autofocus"; |
| 3 | +import registry from "../../core/registry"; |
2 | 4 | import utils from "../../core/utils"; |
3 | 5 |
|
4 | 6 | describe("pat-autofocus", function () { |
5 | | - beforeEach(function () { |
6 | | - const el = document.createElement("div"); |
7 | | - el.setAttribute("id", "lab"); |
8 | | - document.body.append(el); |
9 | | - }); |
10 | | - |
11 | | - afterEach(function () { |
12 | | - document.body.innerHTML = ""; |
13 | | - }); |
14 | | - |
15 | 7 | it("Focus the first element.", async () => { |
16 | | - const container = document.querySelector("#lab"); |
17 | | - container.innerHTML = ` |
| 8 | + document.body.innerHTML = ` |
18 | 9 | <input name="i1" type="text" class="pat-autofocus"/> |
19 | 10 | <input name="i2" type="text" class="pat-autofocus"/> |
20 | 11 | <input name="i3" type="text" class="pat-autofocus"/> |
21 | 12 | `; |
22 | | - pattern.init(container); |
23 | | - await utils.timeout(20); |
| 13 | + registry.scan(document.body); |
| 14 | + await utils.timeout(100); |
24 | 15 |
|
25 | 16 | const should_be_active = document.querySelector("input[name=i1]"); |
26 | 17 | expect(document.activeElement).toBe(should_be_active); |
27 | 18 | }); |
28 | 19 |
|
29 | | - it("Focus the non-empty element, if available.", async () => { |
30 | | - const container = document.querySelector("#lab"); |
31 | | - container.innerHTML = ` |
| 20 | + it("Focus the first empty element, if available.", async () => { |
| 21 | + document.body.innerHTML = ` |
32 | 22 | <input name="i1" type="text" class="pat-autofocus" value="okay"/> |
33 | 23 | <input name="i2" type="text" class="pat-autofocus"/> |
34 | 24 | <input name="i3" type="text" class="pat-autofocus"/> |
35 | 25 | `; |
36 | | - pattern.init(container); |
37 | | - await utils.timeout(20); |
| 26 | + registry.scan(document.body); |
| 27 | + await utils.timeout(100); |
38 | 28 |
|
39 | 29 | const should_be_active = document.querySelector("input[name=i2]"); |
40 | 30 | expect(document.activeElement).toBe(should_be_active); |
41 | 31 | }); |
| 32 | + |
| 33 | + it("Don't focus hidden elements.", async () => { |
| 34 | + document.body.innerHTML = ` |
| 35 | + <input name="i1" type="text" class="pat-autofocus" value="okay"/> |
| 36 | + <input name="i2" type="text" class="pat-autofocus" hidden/> |
| 37 | + <input name="i3" type="text" class="pat-autofocus"/> |
| 38 | + `; |
| 39 | + registry.scan(document.body); |
| 40 | + await utils.timeout(100); |
| 41 | + |
| 42 | + const should_be_active = document.querySelector("input[name=i3]"); |
| 43 | + expect(document.activeElement).toBe(should_be_active); |
| 44 | + }); |
42 | 45 | }); |
0 commit comments