Skip to content
Open
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
2 changes: 2 additions & 0 deletions demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ <H1>this is a really big heading tag. 51 chars long even. this is a really big h
<p> - List item</p>
<p>Split breaks:</p>
<p>1. List Item<br>2. List Item</p>
<a href="weird link">Bad Link</a>
<a href="https://google.com">Good Link (cross domain)</a>
</textarea>
</div>
</div>
Expand Down
21 changes: 14 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions src/rules/__tests__/__snapshots__/valid-links.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`data returns the proper object 1`] = `
Object {
"href": null,
}
`;

exports[`form returns the proper object 1`] = `
Array [
Object {
"dataKey": "href",
"disabledIf": [Function],
"label": "Ensure this link is correct.",
},
Object {
"checkbox": true,
"dataKey": "ignore",
"label": "Ignore this link in the future.",
},
]
`;

exports[`message returns the proper message 1`] = `"This link could not be verified."`;

exports[`why returns the proper why message 1`] = `"Links should not be broken."`;
88 changes: 88 additions & 0 deletions src/rules/__tests__/valid-links.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import rule from "../valid-links"

let body, a

beforeEach(() => {
body = document.createElement("body")
a = document.createElement("a")
body.appendChild(a)
a.textContent = "Link Text"
window.fetch = () => Promise.resolve({ ok: true })
})

describe("test", () => {
test("returns true if not A element", async () => {
expect(await rule.test(document.createElement("div"))).toBe(true)
})

test("returns true for valid links", async () => {
window.fetch = () => Promise.resolve({ ok: true })
a.setAttribute("href", "https://www.instructure.com/")
expect(await rule.test(a)).toBe(true)
})

test("returns false for invalid links", async () => {
window.fetch = () => Promise.resolve({ ok: false })
a.setAttribute("href", "http://something weird")
expect(await rule.test(a)).toBe(false)
a.setAttribute("href", "plaintext")
expect(await rule.test(a)).toBe(false)
window.fetch = () => Promise.reject(new Error())
a.setAttribute("href", "http://something-valid-but-not-reachable/123")
expect(await rule.test(a)).toBe(false)
})
})

describe("data", () => {
test("returns the proper object", () => {
expect(rule.data(a)).toMatchSnapshot()
})
})

describe("form", () => {
test("returns the proper object", () => {
expect(rule.form(a)).toMatchSnapshot()
})
})

describe("update", () => {
test("returns same element", () => {
expect(rule.update(a, {})).toBe(a)
})
test("does not change href if href does not change", () => {
const href = "http://example.com"
a.setAttribute("href", href)
expect(rule.update(a, { href })).toBe(a)
})
test("changes href if it has been changed", () => {
const href = "bad"
a.setAttribute("href", href)
rule.update(a, { href: "https://www.instructure.com/" })
expect(a.getAttribute("href")).toBe("https://www.instructure.com/")
})
test("does not change href if href does not change", () => {
const href = "http://example.com"
a.setAttribute("href", href)
expect(
rule.update(a, { ignore: true }).getAttribute("data-ignore-a11y-check")
).toBe("true")
})
})

describe("rootNode", () => {
test("returns the parentNode of an element", () => {
expect(rule.rootNode(a).tagName).toBe("BODY")
})
})

describe("message", () => {
test("returns the proper message", () => {
expect(rule.message()).toMatchSnapshot()
})
})

describe("why", () => {
test("returns the proper why message", () => {
expect(rule.why()).toMatchSnapshot()
})
})
4 changes: 3 additions & 1 deletion src/rules/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import headingsSequence from "./headings-sequence"
import imageAltLength from "./img-alt-length"
import paragraphsForHeadings from "./paragraphs-for-headings"
import listStructure from "./list-structure"
import validLinks from "./valid-links"

export default [
imgAlt,
Expand All @@ -23,5 +24,6 @@ export default [
headingsSequence,
imageAltLength,
paragraphsForHeadings,
listStructure
listStructure,
validLinks
]
Loading