-
Notifications
You must be signed in to change notification settings - Fork 791
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: add jsdom example and tests (#1530)
* docs: add jsdom example and tests * remove author
- Loading branch information
Showing
2 changed files
with
76 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"name": "axe-jsdom-example", | ||
"description": "Axe JSDOM Example", | ||
"version": "0.0.1", | ||
"private": true, | ||
"dependencies": {}, | ||
"scripts": { | ||
"test": "mocha" | ||
}, | ||
"devDependencies": { | ||
"axe-core": "^3.2.2", | ||
"jsdom": "^15.0.0", | ||
"mocha": "^6.1.4" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* global describe, it */ | ||
const jsdom = require('jsdom'); | ||
const { JSDOM } = jsdom; | ||
const assert = require('assert'); | ||
|
||
describe('axe', () => { | ||
const { window } = new JSDOM(`<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<title>JSDOM Example</title> | ||
</head> | ||
<body> | ||
<div id="working"> | ||
<label for="has-label">Label for this text field.</label> | ||
<input type="text" id="has-label"> | ||
</div> | ||
<div id="broken"> | ||
<p>Not a label</p><input type="text" id="no-label"> | ||
</div> | ||
</body> | ||
</html>`); | ||
|
||
global.document = window.document; | ||
global.window = window; | ||
|
||
// needed by axios lib/helpers/isURLSameOrigin.js | ||
global.navigator = window.navigator; | ||
|
||
// needed by axe /lib/core/public/run.js | ||
global.Node = window.Node; | ||
global.NodeList = window.NodeList; | ||
|
||
// needed by axe /lib/core/base/context.js | ||
global.Element = window.Element; | ||
global.Document = window.Document; | ||
|
||
const axe = require('axe-core'); | ||
const config = { | ||
rules: { | ||
'color-contrast': { enabled: false } | ||
} | ||
}; | ||
|
||
it('should report that good HTML is good', function(done) { | ||
var n = window.document.getElementById('working'); | ||
axe.run(n, config, function(err, result) { | ||
assert.equal(err, null, 'Error is not null'); | ||
assert.equal(result.violations.length, 0, 'Violations is not empty'); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('should report that bad HTML is bad', function(done) { | ||
var n = window.document.getElementById('broken'); | ||
axe.run(n, config, function(err, result) { | ||
assert.equal(err, null, 'Error is not null'); | ||
assert.equal(result.violations.length, 1, 'Violations.length is not 1'); | ||
done(); | ||
}); | ||
}); | ||
}); |
b573b1c
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
doc/examples/jsdom/test/a11y.js