Skip to content

Commit

Permalink
docs: add jsdom example and tests (#1530)
Browse files Browse the repository at this point in the history
* docs: add jsdom example and tests

* remove author
  • Loading branch information
straker committed May 1, 2019
1 parent f835ed8 commit b573b1c
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
15 changes: 15 additions & 0 deletions doc/examples/jsdom/package.json
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"
}
}
61 changes: 61 additions & 0 deletions doc/examples/jsdom/test/a11y.js
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();
});
});
});

1 comment on commit b573b1c

@vickyfandersal
Copy link

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

Please sign in to comment.