-
Notifications
You must be signed in to change notification settings - Fork 755
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(utils): make cache global instead of only setup in axe.run (#1535)
* fix(utils): make cache global instead of only setup in axe.run * make cache not public * add integration test for plugins * use backgroundcolor instead of background
- Loading branch information
Showing
9 changed files
with
227 additions
and
21 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,33 @@ | ||
(function() { | ||
'use strict'; | ||
let _cache = {}; | ||
|
||
const cache = { | ||
/** | ||
* Set an item in the cache. | ||
* @param {String} key - Name of the key. | ||
* @param {*} value - Value to store. | ||
*/ | ||
set(key, value) { | ||
_cache[key] = value; | ||
}, | ||
|
||
/** | ||
* Retrieve an item from the cache. | ||
* @param {String} key - Name of the key the value was stored as. | ||
* @returns {*} The item stored | ||
*/ | ||
get(key) { | ||
return _cache[key]; | ||
}, | ||
|
||
/** | ||
* Clear the cache. | ||
*/ | ||
clear() { | ||
_cache = {}; | ||
} | ||
}; | ||
|
||
axe._cache = cache; | ||
})(); |
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,32 @@ | ||
describe('axe._cache', function() { | ||
'use strict'; | ||
|
||
it('should set items from the cache without error', function() { | ||
function fn() { | ||
axe._cache.set('foo', 'bar'); | ||
} | ||
assert.doesNotThrow(fn); | ||
}); | ||
|
||
it('should not throw for non-string keys', function() { | ||
function fn() { | ||
axe._cache.set(1, 'bar'); | ||
axe._cache.set({}, 'bar'); | ||
axe._cache.set(null, 'bar'); | ||
} | ||
assert.doesNotThrow(fn); | ||
}); | ||
|
||
it('should get an item from the cache', function() { | ||
axe._cache.set('foo', 'bar'); | ||
var value = axe._cache.get('foo'); | ||
assert.equal(value, 'bar'); | ||
}); | ||
|
||
it('should clear the cache', function() { | ||
axe._cache.set('foo', 'bar'); | ||
axe._cache.clear(); | ||
var value = axe._cache.get('foo'); | ||
assert.isUndefined(value); | ||
}); | ||
}); |
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,27 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<title>Plugin Test</title> | ||
<link | ||
rel="stylesheet" | ||
type="text/css" | ||
href="/node_modules/mocha/mocha.css" | ||
/> | ||
<script src="/node_modules/mocha/mocha.js"></script> | ||
<script src="/node_modules/chai/chai.js"></script> | ||
<script src="/axe.js"></script> | ||
<script> | ||
mocha.setup({ | ||
timeout: 10000, | ||
ui: 'bdd' | ||
}); | ||
var assert = chai.assert; | ||
</script> | ||
</head> | ||
<body> | ||
<h1 class="my-heading">Hello World</h1> | ||
<main id="mocha"></main> | ||
<script src="plugin.js"></script> | ||
<script src="/test/integration/adapter.js"></script> | ||
</body> | ||
</html> |
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,107 @@ | ||
describe('plugin test', function() { | ||
it('should register the plugin', function() { | ||
axe.registerPlugin({ | ||
id: 'doStuff', | ||
run: function(id, action, options, callback) { | ||
var frames; | ||
var q = axe.utils.queue(); | ||
var that = this; | ||
frames = axe.utils.toArray(document.querySelectorAll('iframe, frame')); | ||
|
||
frames.forEach(function(frame) { | ||
q.defer(function(done) { | ||
axe.utils.sendCommandToFrame( | ||
frame, | ||
{ | ||
options: options, | ||
command: 'run-doStuff', | ||
parameter: id, | ||
action: action | ||
}, | ||
function() { | ||
done(); | ||
} | ||
); | ||
}); | ||
}); | ||
|
||
if (!options.context.length) { | ||
q.defer(function(done) { | ||
that._registry[id][action].call( | ||
that._registry[id], | ||
document, | ||
options, | ||
done | ||
); | ||
}); | ||
} | ||
q.then(callback); | ||
}, | ||
commands: [ | ||
{ | ||
id: 'run-doStuff', | ||
callback: function(data, callback) { | ||
return axe.plugins.doStuff.run( | ||
data.parameter, | ||
data.action, | ||
data.options, | ||
callback | ||
); | ||
} | ||
} | ||
] | ||
}); | ||
|
||
assert.isOk(axe.plugins.doStuff); | ||
}); | ||
|
||
it('should add plugin instance', function() { | ||
var highlight = { | ||
id: 'highlight', | ||
highlighter: function(node) { | ||
node.style.backgroundColor = 'yellow'; | ||
this._node = node; | ||
}, | ||
run: function(contextNode, options, done) { | ||
var that = this; | ||
Array.prototype.slice | ||
.call(contextNode.querySelectorAll(options.selector)) | ||
.forEach(function(node) { | ||
that.highlighter(node, options); | ||
}); | ||
done(); | ||
}, | ||
cleanup: function(done) { | ||
this._node.style.backgroundColor = ''; | ||
done(); | ||
} | ||
}; | ||
|
||
axe.plugins.doStuff.add(highlight); | ||
assert.equal(axe.plugins.doStuff._registry.highlight, highlight); | ||
}); | ||
|
||
it('should run the plugin', function(done) { | ||
var h1 = document.querySelector('.my-heading'); | ||
|
||
axe.plugins.doStuff.run( | ||
'highlight', | ||
'run', | ||
{ | ||
selector: '.my-heading', | ||
context: [] | ||
}, | ||
function() { | ||
assert.equal(h1.style.backgroundColor, 'yellow'); | ||
done(); | ||
} | ||
); | ||
}); | ||
|
||
it('should cleanup the plugin', function() { | ||
var h1 = document.querySelector('.my-heading'); | ||
|
||
axe.cleanup(); | ||
assert.equal(h1.style.backgroundColor, ''); | ||
}); | ||
}); |
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