Skip to content

Commit

Permalink
Export the cacheStore
Browse files Browse the repository at this point in the history
  • Loading branch information
gilbarbara committed May 2, 2021
1 parent 2659e48 commit b0f67a0
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 3 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,11 @@ Create unique IDs for each icon.
/>
```

## Caching

The internal cache is exported as `cacheStore` if you need to debug or pre-cache some files.
⚠️ Use it at your own risk.

## Browser Support

Any browsers that support inlining [SVGs](https://developer.mozilla.org/en-US/docs/Web/SVG/Element/svg) and [fetch](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) will work.
Expand Down
3 changes: 1 addition & 2 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
} from './helpers';
import { FetchError, Props, State, StorageItem } from './types';

const cacheStore: { [key: string]: StorageItem } = Object.create(null);
export const cacheStore: { [key: string]: StorageItem } = Object.create(null);

export default class InlineSVG extends React.PureComponent<Props, State> {
constructor(props: Props) {
Expand Down Expand Up @@ -221,7 +221,6 @@ export default class InlineSVG extends React.PureComponent<Props, State> {
() => {
const { cacheRequests, src } = this.props;
const cache = cacheRequests && cacheStore[src];

if (cache) {
/* istanbul ignore else */
if (cache.status === STATUS.LOADING) {
Expand Down
7 changes: 7 additions & 0 deletions test/__snapshots__/index.spec.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2206,6 +2206,13 @@ exports[`react-inlinesvg cached requests should skip the cache if \`cacheRequest
</svg>
`;
exports[`react-inlinesvg integration should handle pre-cached entries in the cacheStore 1`] = `
<svg>
<circle>
</circle>
</svg>
`;
exports[`react-inlinesvg integration should handle race condition with fast src changes 1`] = `
<svg width="256px"
height="256px"
Expand Down
28 changes: 27 additions & 1 deletion test/index.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { poll } from '@gilbarbara/helpers';
import { mount, ReactWrapper } from 'enzyme';
import fetchMock from 'jest-fetch-mock';

import ReactInlineSVG, { Props } from '../src/index';
import ReactInlineSVG, { cacheStore, Props } from '../src/index';

const Loader = () => <div id="loader" />;

Expand Down Expand Up @@ -387,6 +387,10 @@ describe('react-inlinesvg', () => {
});

describe('integration', () => {
beforeAll(() => {
fetchMock.resetMocks();
});

it('should handle race condition with fast src changes', async () => {
const mockOnLoad = jest.fn();

Expand Down Expand Up @@ -446,6 +450,28 @@ describe('react-inlinesvg', () => {
await multiSetup();
expect(mockOnLoad).toHaveBeenCalledTimes(4);
});

it('should handle pre-cached entries in the cacheStore', async () => {
fetchMock.enableMocks();
const mockOnLoad = jest.fn();

cacheStore['http://localhost:1337/react.svg'] = {
content: '<svg><circle /></svg>',
status: 'loaded',
queue: [],
};

const wrapper = mount(<ReactInlineSVG src={fixtures.react} onLoad={mockOnLoad} />);

await poll(() => !!mockOnLoad.mock.calls.length);
wrapper.update();

expect(fetchMock).toHaveBeenCalledTimes(0);
expect(wrapper.html()).toMatchSnapshot();

// clean up
fetchMock.disableMocks();
});
});

describe('with errors', () => {
Expand Down

0 comments on commit b0f67a0

Please sign in to comment.