|
1 |
| -import indicate, { clearIndicators } from "../indicate" |
2 |
| - |
3 |
| -let fakeEditor, fakeIframe, fakeElem, mockRAF |
| 1 | +import indicate, { |
| 2 | + clearIndicators, |
| 3 | + findChildDepth, |
| 4 | + buildDepthSelector, |
| 5 | + INDICATOR_STYLE, |
| 6 | + A11Y_CHECKER_STYLE_ELEM_ID, |
| 7 | + ensureA11yCheckerStyleElement, |
| 8 | +} from "../indicate" |
4 | 9 |
|
5 | 10 | beforeEach(() => {
|
6 |
| - Element.prototype.getBoundingClientRect = jest.fn(() => { |
7 |
| - return { |
8 |
| - width: 120, |
9 |
| - height: 120, |
10 |
| - top: 0, |
11 |
| - left: 0, |
12 |
| - bottom: 0, |
13 |
| - right: 0, |
14 |
| - } |
15 |
| - }) |
16 |
| - |
17 |
| - fakeElem = document.createElement("div") |
18 |
| - fakeIframe = document.createElement("iframe") |
19 |
| - fakeEditor = { |
20 |
| - getContainer: () => ({ |
21 |
| - querySelector: () => fakeIframe, |
22 |
| - }), |
23 |
| - } |
24 |
| - |
25 |
| - mockRAF = jest.spyOn(window, "requestAnimationFrame") |
| 11 | + document.body.innerHTML = ` |
| 12 | + <p id="p1"> |
| 13 | + first para |
| 14 | + </p> |
| 15 | + <p id="p2">2nd para</p> |
| 16 | + <p id="p3"> |
| 17 | + this is the 3nd para. |
| 18 | + <span id="s1">with first span</span> |
| 19 | + <span id="s2">with 2nd span</span> |
| 20 | + <span id="s3">with 3rd span</span> |
| 21 | + more text |
| 22 | + <span id="s4">target span</span> |
| 23 | + <span id="s5">last span</span> |
| 24 | + </p>` |
26 | 25 | })
|
27 | 26 |
|
28 | 27 | afterEach(() => {
|
29 | 28 | document.fullscreenElement = null
|
30 |
| - window.requestAnimationFrame.mockRestore() |
31 | 29 | })
|
32 | 30 |
|
33 |
| -describe("indicate", () => { |
34 |
| - it("removes any existing indicators when run", () => { |
35 |
| - mockRAF |
36 |
| - .mockImplementationOnce((cb) => cb(15)) // This only allows it to happen twice, preventing an infinite loop |
37 |
| - .mockImplementationOnce((cb) => cb(30)) |
38 |
| - |
39 |
| - const el = document.createElement("div") |
40 |
| - el.className = "a11y-checker-selection-indicator" |
41 |
| - el.id = "this_should_be_gone" |
42 |
| - indicate(fakeEditor, fakeElem) |
43 |
| - expect(document.getElementById("this_should_be_gone")).toBeFalsy() |
| 31 | +describe("findChildDepth", () => { |
| 32 | + it("finds the depth of a child element in its parent", () => { |
| 33 | + let depth = findChildDepth( |
| 34 | + document.getElementById("p3"), |
| 35 | + document.getElementById("s3") |
| 36 | + ) |
| 37 | + expect(depth).toEqual(3) |
44 | 38 | })
|
45 | 39 |
|
46 |
| - it("stops adjusting when the indicator is gone", () => { |
47 |
| - mockRAF |
48 |
| - .mockImplementationOnce((cb) => cb(15)) |
49 |
| - .mockImplementationOnce((cb) => { |
50 |
| - document.querySelector('.a11y-checker-selection-indicator').remove() |
51 |
| - cb(30) |
52 |
| - }) |
53 |
| - .mockImplementationOnce((cb) => cb(45)) |
54 |
| - |
55 |
| - indicate(fakeEditor, fakeElem) |
56 |
| - expect(mockRAF).toHaveBeenCalledTimes(2) |
| 40 | + it("returns 0 if any arguments are missing", () => { |
| 41 | + expect(findChildDepth(null, document.getElementById("p3"))).toEqual(0) |
| 42 | + expect(findChildDepth(document.getElementById("p3"), null)).toEqual(0) |
57 | 43 | })
|
| 44 | +}) |
58 | 45 |
|
59 |
| - it("puts the indicator in the fullscreeenElement if it exists", () => { |
60 |
| - mockRAF |
61 |
| - .mockImplementationOnce((cb) => cb(15)) |
62 |
| - .mockImplementationOnce((cb) => cb(30)) |
63 |
| - |
64 |
| - const d = document.createElement('div') |
65 |
| - d.id = "fullscreen_element" |
66 |
| - document.body.appendChild(d) |
67 |
| - document.fullscreenElement = d |
68 |
| - indicate(fakeEditor, fakeElem) |
69 |
| - expect(d.querySelector('.a11y-checker-selection-indicator')).toBeTruthy() |
70 |
| - |
| 46 | +describe("buildDepthSelector", () => { |
| 47 | + it("returns the css selector to the given element", () => { |
| 48 | + const sel = buildDepthSelector(document.getElementById("s3")) |
| 49 | + expect(sel).toEqual("body>:nth-child(3)>:nth-child(3)") |
71 | 50 | })
|
72 | 51 | })
|
73 | 52 |
|
74 |
| -describe("clearIndicators", () => { |
75 |
| - it("removes any existing indicators when called", () => { |
76 |
| - const el = document.createElement("div") |
77 |
| - el.className = "a11y-checker-selection-indicator" |
78 |
| - el.id = "this_should_be_gone" |
79 |
| - document.body.appendChild(el) |
80 |
| - clearIndicators() |
81 |
| - expect(document.getElementById("this_should_be_gone")).toBeFalsy() |
| 53 | +describe("indicate", () => { |
| 54 | + it("adds the style element to the head if it doesn't exist", () => { |
| 55 | + expect(document.querySelector("style")).toBe(null) |
| 56 | + indicate(document.getElementById("p1")) |
| 57 | + expect( |
| 58 | + document.querySelector(`style#${A11Y_CHECKER_STYLE_ELEM_ID}`) |
| 59 | + ).toBeTruthy() |
| 60 | + indicate(document.getElementById("p1")) |
| 61 | + expect(document.querySelectorAll("style").length).toEqual(1) |
82 | 62 | })
|
83 | 63 |
|
84 |
| - it("removes indicators from the passed in parent", () => { |
85 |
| - const d1 = document.createElement("div") |
86 |
| - d1.className = "a11y-checker-selection-indicator" |
87 |
| - d1.id = "this_should_be_here" |
88 |
| - document.body.appendChild(d1) |
89 |
| - |
90 |
| - const parent = document.createElement("div") |
91 |
| - const el = document.createElement("div") |
92 |
| - el.className = "a11y-checker-selection-indicator" |
93 |
| - el.id = "this_should_be_gone" |
94 |
| - parent.appendChild(el) |
95 |
| - document.body.appendChild(parent) |
96 |
| - clearIndicators(parent) |
97 |
| - expect(document.getElementById("this_should_be_gone")).toBeFalsy() |
98 |
| - expect(document.getElementById("this_should_be_here")).toBeTruthy() |
| 64 | + it("injects the css into the style element", () => { |
| 65 | + indicate(document.getElementById("s3")) |
| 66 | + expect( |
| 67 | + document.getElementById(A11Y_CHECKER_STYLE_ELEM_ID).textContent |
| 68 | + ).toEqual(`body>:nth-child(3)>:nth-child(3){${INDICATOR_STYLE}}`) |
99 | 69 | })
|
| 70 | +}) |
100 | 71 |
|
101 |
| - it("removes indicators from the fullscreenElement", () => { |
102 |
| - const d1 = document.createElement("div") |
103 |
| - d1.className = "a11y-checker-selection-indicator" |
104 |
| - d1.id = "this_should_be_here" |
105 |
| - document.body.appendChild(d1) |
106 |
| - |
107 |
| - const parent = document.createElement("div") |
108 |
| - const el = document.createElement("div") |
109 |
| - el.className = "a11y-checker-selection-indicator" |
110 |
| - el.id = "this_should_be_gone" |
111 |
| - parent.appendChild(el) |
112 |
| - document.body.appendChild(parent) |
113 |
| - document.fullscreenElement = parent |
114 |
| - clearIndicators() |
115 |
| - expect(document.getElementById("this_should_be_gone")).toBeFalsy() |
116 |
| - expect(document.getElementById("this_should_be_here")).toBeTruthy() |
| 72 | +describe("clearIndicators", () => { |
| 73 | + it("removes the indicator css when called", () => { |
| 74 | + ensureA11yCheckerStyleElement(document) |
| 75 | + clearIndicators(document) |
| 76 | + expect( |
| 77 | + document.getElementById(A11Y_CHECKER_STYLE_ELEM_ID).textContent |
| 78 | + ).toEqual("") |
117 | 79 | })
|
118 | 80 | })
|
0 commit comments