|
| 1 | +import { isFocusable } from "../isFocusable"; |
| 2 | + |
| 3 | +describe("isFocusable", () => { |
| 4 | + it("should default to programmatic focus type", () => { |
| 5 | + const element = document.createElement("div"); |
| 6 | + element.tabIndex = -1; |
| 7 | + |
| 8 | + expect(isFocusable(element)).toBe(true); |
| 9 | + expect(isFocusable(element, "tab")).toBe(false); |
| 10 | + expect(isFocusable(element, "programmatic")).toBe(true); |
| 11 | + }); |
| 12 | + |
| 13 | + it("should work correctly for anchors and areas", () => { |
| 14 | + const anchor = document.createElement("a"); |
| 15 | + const area = document.createElement("area"); |
| 16 | + |
| 17 | + expect(isFocusable(anchor, "tab")).toBe(false); |
| 18 | + expect(isFocusable(area, "tab")).toBe(false); |
| 19 | + expect(isFocusable(anchor, "programmatic")).toBe(false); |
| 20 | + expect(isFocusable(area, "programmatic")).toBe(false); |
| 21 | + |
| 22 | + anchor.href = "#"; |
| 23 | + area.href = "#"; |
| 24 | + expect(isFocusable(anchor, "tab")).toBe(true); |
| 25 | + expect(isFocusable(area, "tab")).toBe(true); |
| 26 | + expect(isFocusable(anchor, "programmatic")).toBe(true); |
| 27 | + expect(isFocusable(area, "programmatic")).toBe(true); |
| 28 | + }); |
| 29 | + |
| 30 | + it("should work correctly for disablable elements", () => { |
| 31 | + const button = document.createElement("button"); |
| 32 | + const select = document.createElement("select"); |
| 33 | + const textarea = document.createElement("textarea"); |
| 34 | + const input = document.createElement("input"); |
| 35 | + input.type = "text"; |
| 36 | + |
| 37 | + expect(isFocusable(button, "tab")).toBe(true); |
| 38 | + expect(isFocusable(select, "tab")).toBe(true); |
| 39 | + expect(isFocusable(textarea, "tab")).toBe(true); |
| 40 | + expect(isFocusable(input, "tab")).toBe(true); |
| 41 | + expect(isFocusable(button, "programmatic")).toBe(true); |
| 42 | + expect(isFocusable(select, "programmatic")).toBe(true); |
| 43 | + expect(isFocusable(textarea, "programmatic")).toBe(true); |
| 44 | + expect(isFocusable(input, "programmatic")).toBe(true); |
| 45 | + |
| 46 | + button.disabled = true; |
| 47 | + select.disabled = true; |
| 48 | + textarea.disabled = true; |
| 49 | + input.disabled = true; |
| 50 | + expect(isFocusable(button, "tab")).toBe(false); |
| 51 | + expect(isFocusable(select, "tab")).toBe(false); |
| 52 | + expect(isFocusable(textarea, "tab")).toBe(false); |
| 53 | + expect(isFocusable(input, "tab")).toBe(false); |
| 54 | + expect(isFocusable(button, "programmatic")).toBe(false); |
| 55 | + expect(isFocusable(select, "programmatic")).toBe(false); |
| 56 | + expect(isFocusable(textarea, "programmatic")).toBe(false); |
| 57 | + expect(isFocusable(input, "programmatic")).toBe(false); |
| 58 | + }); |
| 59 | + |
| 60 | + it("should never include hidden inputs", () => { |
| 61 | + const hidden = document.createElement("input"); |
| 62 | + hidden.type = "hidden"; |
| 63 | + |
| 64 | + expect(isFocusable(hidden, "tab")).toBe(false); |
| 65 | + expect(isFocusable(hidden, "programmatic")).toBe(false); |
| 66 | + }); |
| 67 | + |
| 68 | + it("should allow for elements with a tab index to be focusable", () => { |
| 69 | + const div = document.createElement("div"); |
| 70 | + |
| 71 | + expect(isFocusable(div, "tab")).toBe(false); |
| 72 | + expect(isFocusable(div, "programmatic")).toBe(false); |
| 73 | + |
| 74 | + div.tabIndex = -1; |
| 75 | + expect(isFocusable(div, "tab")).toBe(false); |
| 76 | + expect(isFocusable(div, "programmatic")).toBe(true); |
| 77 | + |
| 78 | + div.tabIndex = 0; |
| 79 | + expect(isFocusable(div, "tab")).toBe(true); |
| 80 | + expect(isFocusable(div, "programmatic")).toBe(true); |
| 81 | + }); |
| 82 | +}); |
0 commit comments