From b0f67a0295b5c16e65af423002bd89c91da764f5 Mon Sep 17 00:00:00 2001 From: Gil Barbara Date: Sun, 2 May 2021 10:52:39 -0300 Subject: [PATCH] Export the `cacheStore` --- README.md | 5 +++++ src/index.tsx | 3 +-- test/__snapshots__/index.spec.tsx.snap | 7 +++++++ test/index.spec.tsx | 28 +++++++++++++++++++++++++- 4 files changed, 40 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index b31be4e..b088b09 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/src/index.tsx b/src/index.tsx index 69e3aed..8365f94 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -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 { constructor(props: Props) { @@ -221,7 +221,6 @@ export default class InlineSVG extends React.PureComponent { () => { const { cacheRequests, src } = this.props; const cache = cacheRequests && cacheStore[src]; - if (cache) { /* istanbul ignore else */ if (cache.status === STATUS.LOADING) { diff --git a/test/__snapshots__/index.spec.tsx.snap b/test/__snapshots__/index.spec.tsx.snap index ec15af4..4d80951 100644 --- a/test/__snapshots__/index.spec.tsx.snap +++ b/test/__snapshots__/index.spec.tsx.snap @@ -2206,6 +2206,13 @@ exports[`react-inlinesvg cached requests should skip the cache if \`cacheRequest `; +exports[`react-inlinesvg integration should handle pre-cached entries in the cacheStore 1`] = ` + + + + +`; + exports[`react-inlinesvg integration should handle race condition with fast src changes 1`] = `
; @@ -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(); @@ -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: '', + status: 'loaded', + queue: [], + }; + + const wrapper = mount(); + + await poll(() => !!mockOnLoad.mock.calls.length); + wrapper.update(); + + expect(fetchMock).toHaveBeenCalledTimes(0); + expect(wrapper.html()).toMatchSnapshot(); + + // clean up + fetchMock.disableMocks(); + }); }); describe('with errors', () => {