|
| 1 | +import $ from "jquery"; |
1 | 2 | import "./autofocus";
|
2 | 3 | import registry from "../../core/registry";
|
3 | 4 | import utils from "../../core/utils";
|
@@ -44,4 +45,88 @@ describe("pat-autofocus", function () {
|
44 | 45 | const should_be_active = document.querySelector("input[name=i3]");
|
45 | 46 | expect(document.activeElement).toBe(should_be_active);
|
46 | 47 | });
|
| 48 | + |
| 49 | + describe("pat-update and pat-inject support", function () { |
| 50 | + it("4.1 - Re-focus on pat-update.", async () => { |
| 51 | + document.body.innerHTML = ` |
| 52 | + <div id="c1"> |
| 53 | + <input name="i1" type="text" class="pat-autofocus"/> |
| 54 | + </div> |
| 55 | + <div id="c2"> |
| 56 | + <input name="i2" type="text" class="pat-autofocus" hidden/> |
| 57 | + </div> |
| 58 | + `; |
| 59 | + registry.scan(document.body); |
| 60 | + await utils.timeout(1); // Wait for async pattern initialization. |
| 61 | + await utils.timeout(100); // Wait for autofocus timeout. |
| 62 | + |
| 63 | + expect(document.activeElement).toBe( |
| 64 | + document.querySelector("input[name=i1]") |
| 65 | + ); |
| 66 | + |
| 67 | + document.querySelector("input[name=i2]").removeAttribute("hidden"); |
| 68 | + $(document).trigger("pat-update", { dom: document.querySelector("#c2") }); |
| 69 | + await utils.timeout(100); // Wait for autofocus timeout. |
| 70 | + |
| 71 | + expect(document.activeElement).toBe( |
| 72 | + document.querySelector("input[name=i2]") |
| 73 | + ); |
| 74 | + }); |
| 75 | + |
| 76 | + it("4.2 - Re-focus on pat-update / collapsible.", async () => { |
| 77 | + await import("../collapsible/collapsible"); |
| 78 | + |
| 79 | + document.body.innerHTML = ` |
| 80 | + <input name="i1" type="text" class="pat-autofocus"/> |
| 81 | + <div class="pat-collapsible closed" |
| 82 | + data-pat-collapsible="transition: none"> |
| 83 | + <h4>Open collapsible.</h4> |
| 84 | + <input name="i2" class="pat-autofocus" /> |
| 85 | + </div> |
| 86 | + `; |
| 87 | + registry.scan(document.body); |
| 88 | + await utils.timeout(1); // Wait for async pattern initialization. |
| 89 | + await utils.timeout(100); // Wait for autofocus timeout. |
| 90 | + |
| 91 | + expect(document.activeElement).toBe( |
| 92 | + document.querySelector("input[name=i1]") |
| 93 | + ); |
| 94 | + |
| 95 | + document.querySelector(".pat-collapsible h4").click(); |
| 96 | + await utils.timeout(100); // Wait for autofocus timeout. |
| 97 | + |
| 98 | + expect(document.activeElement).toBe( |
| 99 | + document.querySelector("input[name=i2]") |
| 100 | + ); |
| 101 | + }); |
| 102 | + |
| 103 | + it("4.3 - Re-focus on pat-inject.", async () => { |
| 104 | + await import("../inject/inject"); |
| 105 | + |
| 106 | + document.body.innerHTML = ` |
| 107 | + <input name="i1" type="text" class="pat-autofocus"/> |
| 108 | + <a href="#autofocus-demo-input" |
| 109 | + class="pat-inject" |
| 110 | + data-pat-inject="target: self::after">inject</a> |
| 111 | + <template id="autofocus-demo-input"> |
| 112 | + <input name="i2" class="pat-autofocus" /> |
| 113 | + </template> |
| 114 | + `; |
| 115 | + registry.scan(document.body); |
| 116 | + await utils.timeout(1); // Wait for async pattern initialization. |
| 117 | + await utils.timeout(100); // Wait for autofocus timeout. |
| 118 | + |
| 119 | + expect(document.activeElement).toBe( |
| 120 | + document.querySelector("input[name=i1]") |
| 121 | + ); |
| 122 | + |
| 123 | + document.querySelector("a.pat-inject").click(); |
| 124 | + await utils.timeout(2); // Wait for inject. |
| 125 | + await utils.timeout(100); // Wait for autofocus timeout. |
| 126 | + |
| 127 | + expect(document.activeElement).toBe( |
| 128 | + document.querySelector("input[name=i2]") |
| 129 | + ); |
| 130 | + }); |
| 131 | + }); |
47 | 132 | });
|
0 commit comments