|
| 1 | +import {render, screen} from "@testing-library/react"; |
| 2 | +import {userEvent as userEventLib} from "@testing-library/user-event"; |
| 3 | +import * as React from "react"; |
| 4 | + |
| 5 | +import {Dependencies} from "@khanacademy/perseus"; |
| 6 | + |
| 7 | +import {testDependencies} from "../../../../../../testing/test-dependencies"; |
| 8 | +import {mockPerseusI18nContext} from "../../../components/i18n-context"; |
| 9 | +import {MafsGraph} from "../mafs-graph"; |
| 10 | +import {getBaseMafsGraphPropsForTests} from "../utils"; |
| 11 | + |
| 12 | +import {describeRayGraph} from "./ray"; |
| 13 | + |
| 14 | +import type {InteractiveGraphState} from "../types"; |
| 15 | +import type {UserEvent} from "@testing-library/user-event"; |
| 16 | + |
| 17 | +const baseMafsGraphProps = getBaseMafsGraphPropsForTests(); |
| 18 | +const baseRayState: InteractiveGraphState = { |
| 19 | + type: "ray", |
| 20 | + coords: [ |
| 21 | + [-5, 5], |
| 22 | + [5, 5], |
| 23 | + ], |
| 24 | + hasBeenInteractedWith: false, |
| 25 | + range: [ |
| 26 | + [-10, 10], |
| 27 | + [-10, 10], |
| 28 | + ], |
| 29 | + snapStep: [1, 1], |
| 30 | +}; |
| 31 | + |
| 32 | +const overallGraphLabel = "A ray on a coordinate plane."; |
| 33 | + |
| 34 | +describe("Linear graph screen reader", () => { |
| 35 | + let userEvent: UserEvent; |
| 36 | + beforeEach(() => { |
| 37 | + userEvent = userEventLib.setup({ |
| 38 | + advanceTimers: jest.advanceTimersByTime, |
| 39 | + }); |
| 40 | + jest.spyOn(Dependencies, "getDependencies").mockReturnValue( |
| 41 | + testDependencies, |
| 42 | + ); |
| 43 | + }); |
| 44 | + |
| 45 | + test("should have aria label and describedby for overall linear graph", () => { |
| 46 | + // Arrange |
| 47 | + render(<MafsGraph {...baseMafsGraphProps} state={baseRayState} />); |
| 48 | + |
| 49 | + // Act |
| 50 | + const linearGraph = screen.getByLabelText( |
| 51 | + "A ray on a coordinate plane.", |
| 52 | + ); |
| 53 | + |
| 54 | + // Assert |
| 55 | + expect(linearGraph).toBeInTheDocument(); |
| 56 | + expect(linearGraph).toHaveAccessibleDescription( |
| 57 | + "The endpoint is at -5 comma 5 and the ray goes through point 5 comma 5.", |
| 58 | + ); |
| 59 | + }); |
| 60 | + |
| 61 | + test.each` |
| 62 | + element | index | expectedValue |
| 63 | + ${"point1"} | ${0} | ${"Endpoint at -5 comma 5."} |
| 64 | + ${"grabHandle"} | ${1} | ${"Ray with endpoint -5 comma 5 going through point 5 comma 5."} |
| 65 | + ${"point2"} | ${2} | ${"Through point at 5 comma 5."} |
| 66 | + `( |
| 67 | + "should have aria label for $element on the line", |
| 68 | + ({index, expectedValue}) => { |
| 69 | + // Arrange |
| 70 | + render(<MafsGraph {...baseMafsGraphProps} state={baseRayState} />); |
| 71 | + |
| 72 | + // Act |
| 73 | + // Moveable elements: point 1, grab handle, point 2 |
| 74 | + const movableElements = screen.getAllByRole("button"); |
| 75 | + const element = movableElements[index]; |
| 76 | + |
| 77 | + // Assert |
| 78 | + expect(element).toHaveAttribute("aria-label", expectedValue); |
| 79 | + }, |
| 80 | + ); |
| 81 | + |
| 82 | + test("points description should include points info", () => { |
| 83 | + // Arrange |
| 84 | + render(<MafsGraph {...baseMafsGraphProps} state={baseRayState} />); |
| 85 | + |
| 86 | + // Act |
| 87 | + const linearGraph = screen.getByLabelText(overallGraphLabel); |
| 88 | + |
| 89 | + // Assert |
| 90 | + expect(linearGraph).toHaveTextContent( |
| 91 | + "The endpoint is at -5 comma 5 and the ray goes through point 5 comma 5.", |
| 92 | + ); |
| 93 | + }); |
| 94 | + |
| 95 | + test("aria label reflects updated values", async () => { |
| 96 | + // Arrange |
| 97 | + |
| 98 | + // Act |
| 99 | + render( |
| 100 | + <MafsGraph |
| 101 | + {...baseMafsGraphProps} |
| 102 | + state={{ |
| 103 | + ...baseRayState, |
| 104 | + // Different points than default (-5, 5) and (5, 5) |
| 105 | + coords: [ |
| 106 | + [-2, 3], |
| 107 | + [3, 3], |
| 108 | + ], |
| 109 | + }} |
| 110 | + />, |
| 111 | + ); |
| 112 | + |
| 113 | + const interactiveElements = screen.getAllByRole("button"); |
| 114 | + const [point1, grabHandle, point2] = interactiveElements; |
| 115 | + |
| 116 | + // Assert |
| 117 | + // Check updated aria-label for the linear graph. |
| 118 | + expect(point1).toHaveAttribute("aria-label", "Endpoint at -2 comma 3."); |
| 119 | + expect(grabHandle).toHaveAttribute( |
| 120 | + "aria-label", |
| 121 | + "Ray with endpoint -2 comma 3 going through point 3 comma 3.", |
| 122 | + ); |
| 123 | + expect(point2).toHaveAttribute( |
| 124 | + "aria-label", |
| 125 | + "Through point at 3 comma 3.", |
| 126 | + ); |
| 127 | + }); |
| 128 | + |
| 129 | + test.each` |
| 130 | + elementName | index |
| 131 | + ${"point1"} | ${0} |
| 132 | + ${"grabHandle"} | ${1} |
| 133 | + ${"point2"} | ${2} |
| 134 | + `( |
| 135 | + "Should update the aria-live when $elementName is moved", |
| 136 | + async ({index}) => { |
| 137 | + // Arrange |
| 138 | + render(<MafsGraph {...baseMafsGraphProps} state={baseRayState} />); |
| 139 | + const interactiveElements = screen.getAllByRole("button"); |
| 140 | + const [point1, grabHandle, point2] = interactiveElements; |
| 141 | + const movingElement = interactiveElements[index]; |
| 142 | + |
| 143 | + // Act - Move the element |
| 144 | + movingElement.focus(); |
| 145 | + await userEvent.keyboard("{ArrowRight}"); |
| 146 | + |
| 147 | + const expectedAriaLive = ["off", "off", "off"]; |
| 148 | + expectedAriaLive[index] = "polite"; |
| 149 | + |
| 150 | + // Assert |
| 151 | + expect(point1).toHaveAttribute("aria-live", expectedAriaLive[0]); |
| 152 | + expect(grabHandle).toHaveAttribute( |
| 153 | + "aria-live", |
| 154 | + expectedAriaLive[1], |
| 155 | + ); |
| 156 | + expect(point2).toHaveAttribute("aria-live", expectedAriaLive[2]); |
| 157 | + }, |
| 158 | + ); |
| 159 | +}); |
| 160 | + |
| 161 | +describe("describeRayGraph", () => { |
| 162 | + test("describes a default ray", () => { |
| 163 | + // Arrange |
| 164 | + |
| 165 | + // Act |
| 166 | + const strings = describeRayGraph(baseRayState, mockPerseusI18nContext); |
| 167 | + |
| 168 | + // Assert |
| 169 | + expect(strings.srRayGraph).toBe("A ray on a coordinate plane."); |
| 170 | + expect(strings.srRayPoints).toBe( |
| 171 | + "The endpoint is at -5 comma 5 and the ray goes through point 5 comma 5.", |
| 172 | + ); |
| 173 | + expect(strings.srRayEndpoint).toBe("Endpoint at -5 comma 5."); |
| 174 | + expect(strings.srRayTerminalPoint).toBe("Through point at 5 comma 5."); |
| 175 | + expect(strings.srRayGrabHandle).toBe( |
| 176 | + "Ray with endpoint -5 comma 5 going through point 5 comma 5.", |
| 177 | + ); |
| 178 | + expect(strings.srRayInteractiveElement).toBe( |
| 179 | + "Interactive elements: A ray on a coordinate plane. The endpoint is at -5 comma 5 and the ray goes through point 5 comma 5.", |
| 180 | + ); |
| 181 | + }); |
| 182 | + |
| 183 | + test("describes a ray with updated points", () => { |
| 184 | + // Arrange |
| 185 | + |
| 186 | + // Act |
| 187 | + const strings = describeRayGraph( |
| 188 | + { |
| 189 | + ...baseRayState, |
| 190 | + coords: [ |
| 191 | + [-1, 2], |
| 192 | + [3, 4], |
| 193 | + ], |
| 194 | + }, |
| 195 | + mockPerseusI18nContext, |
| 196 | + ); |
| 197 | + |
| 198 | + // Assert |
| 199 | + expect(strings.srRayGraph).toBe("A ray on a coordinate plane."); |
| 200 | + expect(strings.srRayPoints).toBe( |
| 201 | + "The endpoint is at -1 comma 2 and the ray goes through point 3 comma 4.", |
| 202 | + ); |
| 203 | + expect(strings.srRayEndpoint).toBe("Endpoint at -1 comma 2."); |
| 204 | + expect(strings.srRayTerminalPoint).toBe("Through point at 3 comma 4."); |
| 205 | + expect(strings.srRayGrabHandle).toBe( |
| 206 | + "Ray with endpoint -1 comma 2 going through point 3 comma 4.", |
| 207 | + ); |
| 208 | + expect(strings.srRayInteractiveElement).toBe( |
| 209 | + "Interactive elements: A ray on a coordinate plane. The endpoint is at -1 comma 2 and the ray goes through point 3 comma 4.", |
| 210 | + ); |
| 211 | + }); |
| 212 | +}); |
0 commit comments