From 8306709ff8addece033bc1494028cda7bcdc3399 Mon Sep 17 00:00:00 2001 From: Laura Martinez Garcia Date: Wed, 14 Aug 2024 09:14:37 +0200 Subject: [PATCH] chore: fix result-variants-provider test --- ...ult-variants-provider-and-selector.spec.ts | 25 ++++++++++++++++++- .../x-modules/queries-preview/store/module.ts | 2 +- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/packages/x-components/src/components/result/__tests__/result-variants-provider-and-selector.spec.ts b/packages/x-components/src/components/result/__tests__/result-variants-provider-and-selector.spec.ts index 4de64c73f1..d5afcf6d72 100644 --- a/packages/x-components/src/components/result/__tests__/result-variants-provider-and-selector.spec.ts +++ b/packages/x-components/src/components/result/__tests__/result-variants-provider-and-selector.spec.ts @@ -33,7 +33,8 @@ const result = createResultStub('jacket', { variants }); const render = ({ template = '', result = {}, - autoSelectDepth = Number.POSITIVE_INFINITY + autoSelectDepth = Number.POSITIVE_INFINITY, + queryPreviewHash = null as string | null } = {}) => { installNewXPlugin(); const emitSpy = jest.spyOn(XPlugin.bus, 'emit'); @@ -50,6 +51,9 @@ const render = ({ ResultVariantsProvider, ResultVariantSelector }, + provide: { + queryPreviewHash + }, data: () => ({ result, autoSelectDepth @@ -170,6 +174,25 @@ describe('results with variants', () => { ); }); + it('emits UserSelectedAResultVariant event when a variant from a query preview is selected', async () => { + const { wrapper, emitSpy } = render({ + result, + autoSelectDepth: 0, + queryPreviewHash: 'abcd' + }); + + const button = wrapper.find(getDataTestSelector('variant-button')); + + await button.trigger('click'); + + expect(emitSpy).toHaveBeenCalledTimes(1); + expect(emitSpy).toHaveBeenCalledWith( + 'UserSelectedAResultVariant', + { result, variant: variants[0], level: 0, queryPreviewHash: 'abcd' }, + expect.anything() + ); + }); + it('selects the first variant of all levels by default', () => { const { findSelectorItemByLevel } = render({ template: ` diff --git a/packages/x-components/src/x-modules/queries-preview/store/module.ts b/packages/x-components/src/x-modules/queries-preview/store/module.ts index 2b54e38ee9..4d4a7adb10 100644 --- a/packages/x-components/src/x-modules/queries-preview/store/module.ts +++ b/packages/x-components/src/x-modules/queries-preview/store/module.ts @@ -58,7 +58,7 @@ export const queriesPreviewXStoreModule: QueriesPreviewXStoreModule = { } }, updateAQueryPreviewResult(state, { result, queryPreviewHash }) { - const queryPreviewResult = state.queriesPreview[queryPreviewHash.value].results.find( + const queryPreviewResult = state.queriesPreview[queryPreviewHash.value]?.results.find( resultPreview => resultPreview.id === result.id ); if (queryPreviewResult) {