-
Notifications
You must be signed in to change notification settings - Fork 795
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(setup/teardown): add functions to setup and teardown axe-core in…
…ternal data, deprecate axe._tree (#2738) * feat(setup/teardown): add functions to setup and teardown axe-core internal data * return tree
- Loading branch information
Showing
6 changed files
with
133 additions
and
22 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
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
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,20 @@ | ||
import { getFlattenedTree, getSelectorData } from '../utils'; | ||
|
||
/** | ||
* Setup axe-core so axe.common functions can work properly. | ||
* @param {Node} [node=document.documentElement] optional node. NOTE: passing in anything other than body or the documentElement may result in incomplete results. | ||
*/ | ||
function setup(node) { | ||
if (axe._tree) { | ||
throw new Error( | ||
'Axe is already setup. Call `axe.teardown()` before calling `axe.setup` again.' | ||
); | ||
} | ||
|
||
axe._tree = getFlattenedTree(node); | ||
axe._selectorData = getSelectorData(axe._tree); | ||
|
||
return axe._tree[0]; | ||
} | ||
|
||
export default setup; |
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,20 @@ | ||
import cache from '../base/cache'; | ||
|
||
/** | ||
* Clean up axe-core tree and caches. `axe.run` will call this function at the end of the run so there's no need to call it yourself afterwards. | ||
*/ | ||
function teardown() { | ||
if (cache.get('globalDocumentSet')) { | ||
document = null; | ||
} | ||
if (cache.get('globalWindowSet')) { | ||
window = null; | ||
} | ||
|
||
axe._memoizedFns.forEach(fn => fn.clear()); | ||
cache.clear(); | ||
axe._tree = undefined; | ||
axe._selectorData = undefined; | ||
} | ||
|
||
export default teardown; |
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,43 @@ | ||
describe('axe.setup', function() { | ||
'use strict'; | ||
|
||
afterEach(function() { | ||
axe.teardown(); | ||
}); | ||
|
||
it('should setup the tree', function() { | ||
axe._tree = undefined; | ||
axe.setup(); | ||
assert.exists(axe._tree); | ||
}); | ||
|
||
it('should default the tree to use html element', function() { | ||
axe.setup(); | ||
assert.equal(axe._tree[0].actualNode, document.documentElement); | ||
}); | ||
|
||
it('should use the passed in node as the root of the tree', function() { | ||
axe.setup(document.body); | ||
assert.equal(axe._tree[0].actualNode, document.body); | ||
}); | ||
|
||
it('should return the root node', function() { | ||
var vNode = axe.setup(document.body); | ||
assert.equal(vNode.actualNode, document.body); | ||
}); | ||
|
||
it('should setup selector data', function() { | ||
axe._selectorData = undefined; | ||
axe.setup(); | ||
assert.exists(axe._selectorData); | ||
}); | ||
|
||
it('should throw if called twice in a row', function() { | ||
function fn() { | ||
axe.setup(); | ||
axe.setup(); | ||
} | ||
|
||
assert.throws(fn); | ||
}); | ||
}); |
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,39 @@ | ||
describe('axe.teardown', function() { | ||
'use strict'; | ||
|
||
it('should reset the tree', function() { | ||
axe._tree = 'foo'; | ||
axe.teardown(); | ||
assert.isUndefined(axe._tree); | ||
}); | ||
|
||
it('should reset selector data', function() { | ||
axe._selectorData = 'foo'; | ||
axe.teardown(); | ||
assert.isUndefined(axe._selectorData); | ||
}); | ||
|
||
it('should reset memozied functions', function() { | ||
var orgFn = axe._memoizedFns[0]; | ||
var called = false; | ||
axe._memoizedFns[0] = { | ||
clear: function() { | ||
called = true; | ||
} | ||
}; | ||
axe.teardown(); | ||
assert.isTrue(called); | ||
axe._memoizedFns[0] = orgFn; | ||
}); | ||
|
||
it('should reset the cache', function() { | ||
var orgFn = axe._cache.clear; | ||
var called = false; | ||
axe._cache.clear = function() { | ||
called = true; | ||
}; | ||
axe.teardown(); | ||
assert.isTrue(called); | ||
axe._cache.clear = orgFn; | ||
}); | ||
}); |