From e3840b093b2c70192e9161f949e0585e176ea1aa Mon Sep 17 00:00:00 2001 From: Stuart Clark Date: Fri, 30 Jun 2023 11:24:43 +1000 Subject: [PATCH] chore(#639): update tests --- packages/druxt/src/stores/druxt.js | 2 +- packages/druxt/test/stores/druxt.test.js | 16 ++++++++++++++++ packages/views/test/stores/views.test.js | 10 ++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/packages/druxt/src/stores/druxt.js b/packages/druxt/src/stores/druxt.js index c08b143df..0db566eee 100644 --- a/packages/druxt/src/stores/druxt.js +++ b/packages/druxt/src/stores/druxt.js @@ -300,7 +300,7 @@ const DruxtStore = ({ store }) => { } // Return if we have the full resource. - if ((storedResource || {})._druxt_full) { + if (!bypassCache && (storedResource || {})._druxt_full) { return storedResource } const isFull = typeof (queryObject.fields || {})[type] !== 'string' diff --git a/packages/druxt/test/stores/druxt.test.js b/packages/druxt/test/stores/druxt.test.js index 827805cf2..155ab394a 100644 --- a/packages/druxt/test/stores/druxt.test.js +++ b/packages/druxt/test/stores/druxt.test.js @@ -143,6 +143,22 @@ describe('DruxtStore', () => { expect(mockAxios.get).toHaveBeenCalledTimes(2) expect(storedResource).toStrictEqual(resource) expect(storedResource).toStrictEqual(expected) + + // Assert that: + // - Cache is bypassed + const bypassedResource = await store.dispatch('druxt/getResource', { ...mockPage.data, bypassCache: true }) + delete resource._druxt_full + delete bypassedResource._druxt_full + expect(mockAxios.get).toHaveBeenCalledTimes(3) + expect(bypassedResource).toStrictEqual(resource) + + // Assert that: + // - When bypassing cache, in case live data is unavailable, fallback to cache. + store.$druxt.getResource = jest.fn(() => { throw new Error() }) + const fallback = await store.dispatch('druxt/getResource', { ...mockPage.data, bypassCache: true }) + delete fallback._druxt_full + expect(mockAxios.get).toHaveBeenCalledTimes(3) + expect(fallback).toStrictEqual(bypassedResource) }) test('getResource - filter', async () => { diff --git a/packages/views/test/stores/views.test.js b/packages/views/test/stores/views.test.js index a8ab74ffd..30670b852 100644 --- a/packages/views/test/stores/views.test.js +++ b/packages/views/test/stores/views.test.js @@ -88,5 +88,15 @@ describe('DruxtViewsStore', () => { expect(mockAxios.get).toHaveBeenCalledTimes(2) await store.dispatch('druxt/views/getResults', query) expect(mockAxios.get).toHaveBeenCalledTimes(2) + + // Bypass cache + const cache = await store.dispatch('druxt/views/getResults', { ...query, bypassCache: true }) + expect(mockAxios.get).toHaveBeenCalledTimes(3) + + // Fallback to cache + store.$druxt.getResource = jest.fn(() => { throw new Error() }) + const fallback = await store.dispatch('druxt/views/getResults', { ...query, bypassCache: true }) + expect(mockAxios.get).toHaveBeenCalledTimes(3) + expect(fallback).toBe(cache) }) })