Skip to content

Commit

Permalink
fix(react): revert export to not use default (#481)
Browse files Browse the repository at this point in the history
* fix(react): revert export to not use default

* remove unneeded interface

* Update packages/react/cache.ts

Co-authored-by: Michael <me@michaelsiek.com>

* Update packages/react/index.ts

Co-authored-by: Michael <me@michaelsiek.com>

Co-authored-by: Michael <me@michaelsiek.com>
  • Loading branch information
straker and michael-siek committed Mar 16, 2022
1 parent 3ac892a commit d932c9d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 16 deletions.
17 changes: 17 additions & 0 deletions packages/react/cache.ts
Original file line number Diff line number Diff line change
@@ -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;
18 changes: 4 additions & 14 deletions packages/react/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -221,8 +221,8 @@ function checkAndReport(node: Node, timeout: number): Promise<void> {
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;
Expand Down Expand Up @@ -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;
6 changes: 4 additions & 2 deletions packages/react/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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());
Expand Down

0 comments on commit d932c9d

Please sign in to comment.