From 3cb303944dca6911f8be6c24dfff6181c759535c Mon Sep 17 00:00:00 2001 From: Stuart Clark Date: Tue, 27 Jun 2023 14:54:34 +1000 Subject: [PATCH] chore(#639): update tests --- packages/views/src/stores/views.js | 8 ++++---- packages/views/test/stores/views.test.js | 23 +++++++++++++++++++++++ 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/packages/views/src/stores/views.js b/packages/views/src/stores/views.js index 0cc1ed165..d4d8c62bd 100644 --- a/packages/views/src/stores/views.js +++ b/packages/views/src/stores/views.js @@ -60,14 +60,14 @@ const DruxtViewsStore = ({ store }) => { * @mutator {object} addResults=results Removes JSON:API Views results from the Vuex state object. * * @example @lang js - * this.$store.commit('druxt/views/flushResults', { viewId, displayId, prefix, hash }) + * this.$store.commit('druxt/views/purgeResults', { viewId, displayId, prefix, hash }) */ flushResults (state, { viewId, displayId, prefix, hash }) { if (!viewId) Vue.set(state, 'results', {}) - else if (viewId && !displayId) Vue.set(state.results, viewId, {}) - else if (viewId && displayId && !prefix) Vue.set(state.results[viewId], displayId, {}) + else if (viewId && !displayId && !prefix && !hash) Vue.set(state.results, viewId, {}) + else if (viewId && displayId && !prefix && !hash) Vue.set(state.results[viewId], displayId, {}) else if (viewId && displayId && prefix && !hash) Vue.set(state.results[viewId][displayId], prefix, {}) - else if (viewId && displayId && prefix && hash) Vue.set(state.results[viewId][displayId][prefix], hash, {}) + else if (viewId && displayId && (prefix || prefix === undefined) && hash) Vue.set(state.results[viewId][displayId][prefix], hash, {}) }, }, diff --git a/packages/views/test/stores/views.test.js b/packages/views/test/stores/views.test.js index 49e58e428..a8ab74ffd 100644 --- a/packages/views/test/stores/views.test.js +++ b/packages/views/test/stores/views.test.js @@ -52,6 +52,29 @@ describe('DruxtViewsStore', () => { expect(Object.keys(store.state['druxt/views'].results)).toHaveLength(1) }) + test('flushResults', async () => { + // Ensure that the results state is populated. + const results = await store.$druxt.getResource(`views--${viewId}`, displayId) + const prefix = 'en' + store.commit('druxt/views/addResults', { results, viewId, displayId, prefix, hash: '_default' }) + expect(store.state['druxt/views'].results[viewId][displayId][prefix]._default).toBe(results) + + store.commit('druxt/views/flushResults', { viewId, displayId, prefix, hash: '_default'}) + expect(store.state['druxt/views'].results[viewId][displayId][prefix]._default).toStrictEqual({}) + + store.commit('druxt/views/flushResults', { viewId, displayId, prefix}) + expect(store.state['druxt/views'].results[viewId][displayId][prefix]).toStrictEqual({}) + + store.commit('druxt/views/flushResults', { viewId, displayId }) + expect(store.state['druxt/views'].results[viewId][displayId]).toStrictEqual({}) + + store.commit('druxt/views/flushResults', { viewId }) + expect(store.state['druxt/views'].results[viewId]).toStrictEqual({}) + + store.commit('druxt/views/flushResults', {}) + expect(store.state['druxt/views'].results).toStrictEqual({}) + }) + test('getResults', async () => { const query = { viewId, displayId, query: {} } const results = await store.dispatch('druxt/views/getResults', query)