Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set cache on queries preview results #1360

Merged
merged 27 commits into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
747a4de
feat(query-preview): Add updateQueryPreviewHistory action/commits/types
annacv Nov 16, 2023
766639e
feat(query-preview): Call updateQueryPreviewHistory action & fetch on…
annacv Nov 16, 2023
101c6ce
feat(query-preview): Use deepEqual in updateQueryPreviewHistory action
annacv Nov 17, 2023
492815b
feat(query-preview): wip render query preview from history
annacv Nov 17, 2023
f398456
feat(query-preview): fix removeFromQueryPreviewHistory
annacv Nov 20, 2023
3b2ccd3
feat(query-preview): rm popQueryPreviewHistory as it doesn't work pro…
annacv Nov 20, 2023
5a7d661
feat(query-preview): update event payload to suit deleting both from …
annacv Nov 20, 2023
b1b0394
feat(query-preview-list): make clearOnDestroy accessible to pass it a…
annacv Nov 20, 2023
98951e8
doc(query-preview): update queryPreviewHistory description
annacv Nov 20, 2023
f28778f
feat(query-preview): check if the item has been saved in the componen…
annacv Nov 27, 2023
69d09ad
feat(query-preview): rm first queryPreviewHistory item if the list le…
annacv Nov 27, 2023
5d723ae
feat(query-preview): save only queries in the history list
annacv Nov 27, 2023
4692d20
chore: check if loadedQueryPreview exists
annacv Nov 27, 2023
e9ae00a
feat: rollback query as payload in QueryPreviewUnmountedHook event
annacv Nov 28, 2023
c9ddcc1
test: Add updateQueryPreviewHistory action test
annacv Nov 28, 2023
db38849
Update packages/x-components/src/x-modules/queries-preview/components…
annacv Nov 28, 2023
18d11a5
feat: Set cleanOnDestroy false as default
annacv Nov 28, 2023
3ee5177
feat(query-preview): remove queryPreviewHistory list
lauramargar Dec 5, 2023
1b668c6
chore: fix method documentation
lauramargar Dec 5, 2023
42f9c44
feat: update and fix query-preview tests
lauramargar Dec 6, 2023
4f32b65
feat: add test when saveCache is true
lauramargar Dec 6, 2023
56ddc60
feat: complete test implementation
lauramargar Dec 6, 2023
e49867e
chore: fix pr comments
lauramargar Dec 6, 2023
c948340
chore: fix pr comments
lauramargar Dec 6, 2023
ed0c717
feat: update tests and check status in cache
lauramargar Dec 7, 2023
583b30b
chore: fix pr comments
lauramargar Dec 7, 2023
5f2913d
chore: fix query preview test
lauramargar Dec 7, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/x-components/src/views/home/Home.vue
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@
:queriesPreviewInfo="queriesPreviewInfo"
#default="{ queryPreviewInfo, totalResults, results }"
data-test="brand-recommendation"
:saveCache="true"
>
<div class="x-flex x-flex-col x-gap-8 x-mb-16">
<QueryPreviewButton
Expand Down
lauramargar marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ function renderQueryPreview({
queryPreviewInfo = { query: 'milk' },
location,
queryFeature,
saveCache = false,
debounceTimeMs = 0,
template = `<QueryPreview v-bind="$attrs" />`,
queryPreview = {
Expand Down Expand Up @@ -66,7 +67,8 @@ function renderQueryPreview({
maxItemsToRender,
queryPreviewInfo,
queryFeature,
debounceTimeMs
debounceTimeMs,
saveCache
}
}
).findComponent(QueryPreview);
Expand Down Expand Up @@ -101,8 +103,25 @@ describe('query preview', () => {
expect(getXComponentXModuleName(wrapper.vm)).toBe('queriesPreview');
});

it('no sends the `QueryPreviewRequestUpdated` event if saveCache is true, but emits load', () => {
lauramargar marked this conversation as resolved.
Show resolved Hide resolved
const { queryPreviewRequestUpdatedSpy, wrapper } = renderQueryPreview({
saveCache: true,
queryPreviewInfo: {
query: 'shoes',
extraParams: { directory: 'Magrathea' },
filters: ['fit:regular']
}
});

jest.advanceTimersByTime(0); // Wait for first emission.
expect(queryPreviewRequestUpdatedSpy).toHaveBeenCalledTimes(0);
expect(wrapper.emitted('load')?.length).toBe(1);
expect(wrapper.emitted('load')?.[0]).toEqual(['shoes']);
});

it('sends the `QueryPreviewRequestUpdated` event', async () => {
const { queryPreviewRequestUpdatedSpy, wrapper, updateExtraParams } = renderQueryPreview({
saveCache: false,
queryPreviewInfo: {
query: 'shoes',
extraParams: { directory: 'Magrathea' },
Expand Down Expand Up @@ -425,6 +444,8 @@ interface RenderQueryPreviewOptions {
maxItemsToRender?: number;
/** The query preview info for which preview its results. */
queryPreviewInfo?: QueryPreviewInfo;
/** Boolean to save queries preview in the cache. */
saveCache?: boolean;
/** The location of the query preview in the DOM. */
location?: string;
/** The name of the tool that generated the query. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@
*
* @public
*/
@Prop({ default: true })
public clearOnDestroy!: boolean;
@Prop({ default: false })
public saveCache!: boolean;
lauramargar marked this conversation as resolved.
Show resolved Hide resolved

/**
* The results preview of the queries preview mounted.
Expand Down Expand Up @@ -208,6 +208,19 @@
}, this.debounceTimeMs);
}

/**
* Checks whether the current queryPreviewItem query has been saved
* in the queriesPreview in the state.
*
* @internal
*
* @returns True if the query has been saved.
*/
protected get isSavedQuery(): boolean {
const previewItemQuery = this.queryPreviewInfo.query;
return !!this.previewResults[previewItemQuery];
}
lauramargar marked this conversation as resolved.
Show resolved Hide resolved

/**
* Initialises watcher to emit debounced requests, and first value for the requests.
*
Expand All @@ -222,21 +235,26 @@
}
}
);
this.emitQueryPreviewRequestUpdated(this.queryPreviewRequest);

// If the query has been saved it will emit load instead of the emitting the updated request.
if (this.isSavedQuery && this.saveCache) {
this.$emit('load', this.queryPreviewInfo.query);
lauramargar marked this conversation as resolved.
Show resolved Hide resolved
} else {
this.emitQueryPreviewRequestUpdated(this.queryPreviewRequest);
}
}

/**
* Cancels the (remaining) requests when the component is destroyed
* via the `debounce.cancel()` method.
* If the prop 'clearOnDestroy' is set to true, it also removes the QueryPreview
* If the prop 'saveCache' is set to false, it also removes the QueryPreview
* from the state when the component is destroyed.
*
* @internal
*/
protected beforeDestroy(): void {
this.emitQueryPreviewRequestUpdated.cancel();

if (this.clearOnDestroy) {
if (!this.saveCache) {
this.$x.emit('QueryPreviewUnmountedHook', this.queryPreviewInfo.query, {
lauramargar marked this conversation as resolved.
Show resolved Hide resolved
priority: 0,
replaceable: false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import { getMockedAdapter, installNewXPlugin } from '../../../../__tests__/utils
import { SafeStore } from '../../../../store/__tests__/utils';
import { queriesPreviewXStoreModule } from '../module';
import {
QueriesPreviewState,
QueriesPreviewActions,
QueriesPreviewGetters,
QueriesPreviewMutations,
QueriesPreviewActions,
QueriesPreviewState,
QueryPreviewItem
} from '../types';
import { getQueryPreviewRequest } from '../../../../__stubs__/queries-preview-stubs.factory';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,18 @@ import { QueriesPreviewXStoreModule } from '../types';
export const fetchAndSaveQueryPreview: QueriesPreviewXStoreModule['actions']['fetchAndSaveQueryPreview'] =
({ dispatch, commit }, request) => {
const { query } = request;

if (!query) {
return;
}

commit('setQueryPreview', {
request,
results: [],
status: 'loading',
totalResults: 0
});

return dispatch('fetchQueryPreview', request)
.then(response => {
commit('setQueryPreview', {
Expand Down