Skip to content

Commit

Permalink
chore(#639): update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Decipher committed Jul 3, 2023
1 parent 8eff354 commit 53ede24
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/druxt/src/stores/druxt.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
16 changes: 16 additions & 0 deletions packages/druxt/test/stores/druxt.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down
10 changes: 10 additions & 0 deletions packages/views/test/stores/views.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})

0 comments on commit 53ede24

Please sign in to comment.