Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(react): revert export to not use default #481

Merged
merged 4 commits into from
Mar 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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