diff --git a/packages/react/cache.ts b/packages/react/cache.ts new file mode 100644 index 00000000..b11ff1b8 --- /dev/null +++ b/packages/react/cache.ts @@ -0,0 +1,17 @@ +const _cache: { [key: string]: string } = {}; + +const cache = { + set(key: string, value: string): void { + _cache[key] = value; + }, + get(key: string): string { + return _cache[key]; + }, + clear(): void { + Object.keys(_cache).forEach(key => { + delete _cache[key]; + }); + } +}; + +export default cache; diff --git a/packages/react/index.ts b/packages/react/index.ts index fd438fa6..6ddd255f 100644 --- a/packages/react/index.ts +++ b/packages/react/index.ts @@ -2,6 +2,7 @@ import axeCore = require('axe-core'); import rIC = require('requestidlecallback'); import after = require('./after'); +import cache from './cache'; const requestIdleCallback = rIC.request; const cancelIdleCallback = rIC.cancel; @@ -43,7 +44,6 @@ let conf: ReactSpec; let _createElement: typeof React.createElement; const components: { [id: number]: React.Component } = {}; const nodes: Node[] = [document.documentElement]; -const cache: { [key: string]: string } = {}; // Returns a function, that, as long as it continues to be invoked, will not // be triggered. The function will be called after it stops being called for @@ -221,8 +221,8 @@ function checkAndReport(node: Node, timeout: number): Promise { results.violations = results.violations.filter(result => { result.nodes = result.nodes.filter(node => { const key: string = node.target.toString() + result.id; - const retVal = !cache[key]; - cache[key] = key; + const retVal = !cache.get(key); + cache.set(key, key); return disableDeduplicate || retVal; }); return !!result.nodes.length; @@ -412,14 +412,4 @@ function reactAxe( return checkAndReport(document.body, timeout); } -// export function just for tests so we can have a clean state -// between tests -export function _reset() { - Object.keys(cache).forEach(key => { - delete cache[key]; - }); - - axeCore.reset(); -} - -export default reactAxe; +export = reactAxe; diff --git a/packages/react/test/index.test.js b/packages/react/test/index.test.js index 01acc369..807ee16d 100644 --- a/packages/react/test/index.test.js +++ b/packages/react/test/index.test.js @@ -5,7 +5,8 @@ import { mount } from 'enzyme'; import sinon from 'sinon'; import { assert } from 'chai'; import axe from 'axe-core'; -import reactAxe, { _reset } from '../dist/index.js'; +import reactAxe from '../dist/index.js'; +import cache from '../dist/cache.js'; class App extends React.Component { constructor(props) { @@ -62,7 +63,8 @@ describe(`@axe-core/react using react@${React.version}`, () => { }); afterEach(() => { - _reset(); + cache.clear(); + axe.reset(); divWrapper.remove(); mountedComps.forEach(comp => comp.unmount());