Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
### ✨ New features/enhancements

- Made the graph's zoom, reset view and sidebar buttons keyboard and screen reader accessible (accessible names, visible focus outlines, and activation with Enter/Space)
- Added an accessible name and description to the graph SVG, labelled its course areas and courses groups, labelled each course with its text, and hid decorative parts (arrows, and/or nodes, region shapes, and shadow and blur effects) from screen readers
- Changed the colour of hybrid nodes to avoid a visual clash with the darkened background when selecting a focus
- Updated section codes on the grid page to match the official timetable builder (listed as LEC/TUT/PRA instead of L/T/P)
- Updated text on the course panel of the grid page to better communicate the panel's behaviour
Expand Down
34 changes: 29 additions & 5 deletions js/components/graph/Graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import Edge from "./Edge"
import Node from "./Node"
import Button from "./Button"
import InfoBox from "./InfoBox"
import { GRAPH_A11Y_TEXT, getGraphA11yDescription, getGraphA11yName } from "./a11y"
import GraphCanvas, { NODE_DROPSHADOW_FILTER_ID } from "./GraphCanvas"
import Sidebar from "./Sidebar"
import { numCredits, transformPoint, formatTransform } from "./util"
Expand Down Expand Up @@ -921,7 +922,13 @@ export class Graph extends React.Component {
)
}

return <g id="bools">{Object.values(boolsJSON).map(generateBool)}</g>
// Hidden from screen readers: an "and"/"or" read on its own, away from the
// courses it connects, is meaningless
return (
<g id="bools" aria-hidden="true">
{Object.values(boolsJSON).map(generateBool)}
</g>
)
}

/**
Expand Down Expand Up @@ -970,7 +977,12 @@ export class Graph extends React.Component {
}
})

return <g id="edges">{edgesCopy.map(generateEdge)}</g>
// Hidden from screen readers: the arrows are purely visual
return (
<g id="edges" aria-hidden="true">
{edgesCopy.map(generateEdge)}
</g>
)
}

/**
Expand All @@ -995,7 +1007,7 @@ export class Graph extends React.Component {
nodeDropshadowFilter
) => {
return (
<g id="nodes">
<g id="nodes" role="group" aria-label={GRAPH_A11Y_TEXT.nodesGroup}>
{Object.values(hybridsJSON).map(entry => {
return (
<Node
Expand Down Expand Up @@ -1054,7 +1066,15 @@ export class Graph extends React.Component {
})

const pathStyle = { fill: entry.fill }
return <path {...pathAttrs} key={value} className="region" style={pathStyle} />
return (
<path
{...pathAttrs}
key={value}
className="region"
style={pathStyle}
aria-hidden="true"
/>
)
})
}

Expand Down Expand Up @@ -1084,7 +1104,7 @@ export class Graph extends React.Component {

renderRegionsLabels(regionsJSON, labelsJSON) {
return (
<g id="regions">
<g id="regions" role="group" aria-label={GRAPH_A11Y_TEXT.regionsGroup}>
{this.renderRegions(regionsJSON)}
{this.renderLabels(labelsJSON)}
</g>
Expand Down Expand Up @@ -1204,6 +1224,10 @@ export class Graph extends React.Component {
zoomFactor={this.state.zoomFactor}
horizontalPanFactor={this.state.horizontalPanFactor}
verticalPanFactor={this.state.verticalPanFactor}
a11yName={getGraphA11yName(this.state.graphName)}
a11yDescription={getGraphA11yDescription(
Object.values(this.state.labelsJSON).map(label => label.text)
)}
{...svgMouseEvents}
>
{this.renderRegionsLabels(this.state.regionsJSON, this.state.labelsJSON)}
Expand Down
11 changes: 10 additions & 1 deletion js/components/graph/GraphCanvas.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ interface GraphCanvasProps {
zoomFactor: number
horizontalPanFactor: number
verticalPanFactor: number
/** Accessible name of the graph, announced by screen readers */
a11yName?: string
/** Longer accessible description of the graph, rendered in a `<desc>` */
a11yDescription?: string
onMouseDown?: React.MouseEventHandler<SVGSVGElement>
onMouseUp?: React.MouseEventHandler<SVGSVGElement>
onMouseMove?: React.MouseEventHandler<SVGSVGElement>
Expand All @@ -38,6 +42,8 @@ export default function GraphCanvas({
zoomFactor,
horizontalPanFactor,
verticalPanFactor,
a11yName,
a11yDescription,
onMouseDown,
onMouseUp,
onMouseMove,
Expand Down Expand Up @@ -77,12 +83,15 @@ export default function GraphCanvas({
height="100%"
viewBox={`${viewboxX} ${viewboxY} ${newViewboxWidth} ${newViewboxHeight}`}
preserveAspectRatio="xMidYMid"
role="graphics-document"
aria-label={a11yName}
onMouseDown={onMouseDown}
onMouseUp={onMouseUp}
onMouseMove={onMouseMove}
onTouchStart={onTouchStart}
>
<defs>
{a11yDescription && <desc>{a11yDescription}</desc>}
<defs aria-hidden="true">
<filter id={NODE_DROPSHADOW_FILTER_ID} height="130%">
<feGaussianBlur in="SourceAlpha" stdDeviation="3" />
<feOffset dx="2" dy="2" result="offsetblur" />
Expand Down
3 changes: 3 additions & 0 deletions js/components/graph/Node.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from "react"
import type { GraphNodeJSON } from "./types"
import { getNodeA11yLabel } from "./a11y"

interface NodeProps {
className: string
Expand Down Expand Up @@ -113,6 +114,8 @@ export default function Node(props: NodeProps) {
className={newClassName}
data-testid={getDataTestId()}
transform={props.transform}
role="img"
aria-label={getNodeA11yLabel(props.JSON.text.map(textTag => textTag.text))}
>
{ellipse}
<rect
Expand Down
104 changes: 103 additions & 1 deletion js/components/graph/__tests__/Graph.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,111 @@
import React from "react"
import TestGraph from "./TestGraph"
import { fireEvent, screen, within } from "@testing-library/react"
import { fireEvent, render, screen, within } from "@testing-library/react"
import { ZOOM_INCREMENT, KEYBOARD_PANNING_INCREMENT } from "../Graph"
import { Graph } from "../Graph"
import { getGraphA11yDescription } from "../a11y"
import testData from "../__mocks__/defaultTestData"
import fetchMock from "fetch-mock"
import userEvent from "@testing-library/user-event"

describe("Graph accessibility", () => {
it("names the graph after the displayed graph and describes it", async () => {
await TestGraph.build()
const svg = document.querySelector("#main-graph")
expect(svg.getAttribute("aria-label")).toBe("Computer Science prerequisite graph")
// The test graph has no area labels, so the description has no examples
expect(svg.querySelector(":scope > desc").textContent).toBe(
getGraphA11yDescription([])
)
})

it("gives the graph's own area labels as examples in its description", async () => {
const labelledData = {
...testData,
texts: [
...testData.texts,
{ graph: 1, rId: "tspan1", text: "Theory of Computing", pos: [0, 0], fill: "" },
{ graph: 1, rId: "tspan2", text: "Computer Systems", pos: [0, 20], fill: "" },
],
}
fetchMock.get(
"http://localhost/get-json-data?graphName=Labelled+Graph",
labelledData
)

render(<Graph graphName="Labelled Graph" start_blank={false} />)
await screen.findByText("Theory of Computing")

const svg = document.querySelector("#main-graph")
expect(svg.getAttribute("aria-label")).toBe("Labelled Graph prerequisite graph")
expect(svg.querySelector(":scope > desc").textContent).toContain(
"such as Theory of Computing, and Computer Systems."
)
})

it("labels the course areas and courses groups", async () => {
await TestGraph.build()
expect(screen.getByRole("group", { name: "Course areas" }).id).toBe("regions")
expect(screen.getByRole("group", { name: "Courses" }).id).toBe("nodes")
})

it("hides the and/or nodes and arrows from screen readers", async () => {
await TestGraph.build()
expect(document.querySelector("#bools").getAttribute("aria-hidden")).toBe("true")
expect(document.querySelector("#edges").getAttribute("aria-hidden")).toBe("true")
})

it("hides the region shapes but keeps the region labels readable", () => {
// The test graph data has no regions or labels, so render them directly
const graph = new Graph(Graph.defaultProps)
const regionsJSON = {
region1: {
id_: "region1",
points: [
[0, 0],
[10, 0],
[10, 10],
],
fill: "#fff",
},
}
const labelsJSON = {
tspan1: { rId: "tspan1", pos: [5, 5], fill: "#000", text: "Theory of Computing" },
}
const { container } = render(
<svg>{graph.renderRegionsLabels(regionsJSON, labelsJSON)}</svg>
)

expect(container.querySelector(".region").getAttribute("aria-hidden")).toBe("true")
const label = screen.getByText("Theory of Computing")
expect(label.hasAttribute("aria-hidden")).toBe(false)
})

it("labels each course and hybrid node with its text, as a single image", async () => {
await TestGraph.build()
const courseNode = document.querySelector("#nodes .node")
const hybridNode = document.querySelector("#nodes .hybrid")
expect(hybridNode).not.toBeNull()
for (const node of [courseNode, hybridNode]) {
// The img role stops screen readers from navigating into the node's contents
expect(node.getAttribute("role")).toBe("img")
expect(node.getAttribute("aria-label")).toBe(node.textContent)
}
expect(screen.getByRole("img", { name: "AAA100" }).id).toBe("aaa100")
})

it("keeps the spotlight around hovered courses inside the labelled node", async () => {
const user = userEvent.setup()
await TestGraph.build()
await user.hover(screen.getByText("AAA303"))
const spotlights = document.querySelectorAll(".spotlight, .spotlight-focus")
expect(spotlights.length).toBeGreaterThan(0)
spotlights.forEach(spotlight =>
expect(spotlight.closest('[role="img"]')).not.toBeNull()
)
})
})

const getViewBox = () => document.querySelector("#main-graph").getAttribute("viewBox")

describe("Graph controls", () => {
Expand Down
43 changes: 43 additions & 0 deletions js/components/graph/__tests__/GraphCanvas.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,47 @@ describe("GraphCanvas", () => {

expect(document.querySelector("#main-graph > #test-child")).not.toBeNull()
})

describe("accessibility", () => {
it("labels the graph and describes it with a <desc>", () => {
createContainer(1000, 500, false)
const { container } = render(
<GraphCanvas
{...canvasProps}
a11yName="Computer Science prerequisite graph"
a11yDescription="Arrows point from each course to the courses that require it."
/>
)
const svg = container.querySelector("#main-graph")
expect(svg.getAttribute("role")).toBe("graphics-document")
expect(svg.getAttribute("aria-label")).toBe("Computer Science prerequisite graph")
// A direct child <desc> is automatically the <svg>'s description
expect(svg.querySelector(":scope > desc").textContent).toBe(
"Arrows point from each course to the courses that require it."
)
})

it("doesn't also link the description with aria-describedby", () => {
// Safari exposes a <desc> linked with aria-describedby twice, so VoiceOver reads it twice
createContainer(1000, 500, false)
const { container } = render(
<GraphCanvas {...canvasProps} a11yDescription="A description" />
)
expect(
container.querySelector("#main-graph").hasAttribute("aria-describedby")
).toBe(false)
})

it("leaves out the description when none is given", () => {
createContainer(1000, 500, false)
const { container } = render(<GraphCanvas {...canvasProps} />)
expect(container.querySelector("#main-graph desc")).toBeNull()
})

it("hides the filter and marker definitions from screen readers", () => {
createContainer(1000, 500, false)
const { container } = render(<GraphCanvas {...canvasProps} />)
expect(container.querySelector("defs").getAttribute("aria-hidden")).toBe("true")
})
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,26 @@

exports[`BoolGroup BoolGroup should match shallow snapshot 1`] = `
<g
aria-hidden="true"
id="bools"
/>
`;

exports[`EdgeGroup should match shallow snapshot 1`] = `
<g
aria-hidden="true"
id="edges"
/>
`;

exports[`Graph rendering RegionGroup should match shallow snapshot 1`] = `
<g
aria-label="Course areas"
id="regions"
role="group"
>
<path
aria-hidden="true"
className="region"
d="M17.386348,281.07376883 17.386348,14.41568908 316.385978,14.41576883 316.386348,281.07376883 "
style={
Expand All @@ -43,6 +48,8 @@ exports[`Graph rendering RegionGroup should match shallow snapshot 1`] = `

exports[`NodeGroup should match shallow snapshot 1`] = `
<g
aria-label="Courses"
id="nodes"
role="group"
/>
`;
4 changes: 4 additions & 0 deletions js/components/graph/__tests__/__snapshots__/Node.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

exports[`Course Node should match snapshot 1`] = `
<g
aria-label="CSC108"
class="node takeable"
data-testid="csc108"
id="csc108"
role="img"
shape-rendering="geometricPrecision"
text-rendering="geometricPrecision"
>
Expand All @@ -29,9 +31,11 @@ exports[`Course Node should match snapshot 1`] = `

exports[`Hybrid Node should match snapshot 1`] = `
<g
aria-label="CSC318/418/301/384"
class="hybrid inactive"
data-testid="h(csc301,csc318,csc384,csc418)"
id="h46"
role="img"
shape-rendering="geometricPrecision"
text-rendering="geometricPrecision"
>
Expand Down
Loading