diff --git a/packages/_vue3-migration-test/src/x-modules/facets/components/test-facets.vue b/packages/_vue3-migration-test/src/x-modules/facets/components/test-facets.vue index 76bde29292..612e423eb0 100644 --- a/packages/_vue3-migration-test/src/x-modules/facets/components/test-facets.vue +++ b/packages/_vue3-migration-test/src/x-modules/facets/components/test-facets.vue @@ -37,10 +37,12 @@

SelectedFilters

- - + + Selected filters: {{ selectedFilters.length }}

SelectedFiltersList

diff --git a/packages/x-components/src/components/modals/base-id-modal-close.vue b/packages/x-components/src/components/modals/base-id-modal-close.vue index 9850fce2f9..7b06609c19 100644 --- a/packages/x-components/src/components/modals/base-id-modal-close.vue +++ b/packages/x-components/src/components/modals/base-id-modal-close.vue @@ -21,7 +21,7 @@ diff --git a/packages/x-components/src/views/home/display-result-provider.vue b/packages/x-components/src/views/home/display-result-provider.vue index 127938dc12..4df6d9e39c 100644 --- a/packages/x-components/src/views/home/display-result-provider.vue +++ b/packages/x-components/src/views/home/display-result-provider.vue @@ -1,25 +1,13 @@ - - diff --git a/packages/x-components/src/x-modules/extra-params/components/__tests__/renderless-extra-params.spec.ts b/packages/x-components/src/x-modules/extra-params/components/__tests__/renderless-extra-params.spec.ts index 0f97794e65..07e2471fc0 100644 --- a/packages/x-components/src/x-modules/extra-params/components/__tests__/renderless-extra-params.spec.ts +++ b/packages/x-components/src/x-modules/extra-params/components/__tests__/renderless-extra-params.spec.ts @@ -1,6 +1,5 @@ import { Dictionary } from '@empathyco/x-utils'; -import { mount, Wrapper } from '@vue/test-utils'; -import Vue from 'vue'; +import { mount } from '@vue/test-utils'; import { getDataTestSelector, installNewXPlugin } from '../../../../__tests__/utils'; import { getXComponentXModuleName, isXComponent } from '../../../../components'; import { XPlugin } from '../../../../plugins'; @@ -9,71 +8,54 @@ import { extraParamsXModule } from '../../x-module'; import RenderlessExtraParam from '../renderless-extra-param.vue'; import { resetXExtraParamStateWith } from './utils'; -describe('testing RenderlessExtraParam component', () => { - function renderRenderlessExtraParams({ - template = ``, - defaultValue, - name = 'warehouse', - params - }: RenderlessExtraParamsOptions): RenderlessExtraParamsAPI { - const [, localVue] = installNewXPlugin({ initialXModules: [extraParamsXModule] }); - const store = XPlugin.store; - resetXExtraParamStateWith(store, { - params: params ?? {} - }); +function render({ + template = ``, + name = 'warehouse', + params = {} +} = {}) { + installNewXPlugin({ initialXModules: [extraParamsXModule] }); + resetXExtraParamStateWith(XPlugin.store, { params }); - const wrapper = mount( - { - template, - components: { - RenderlessExtraParam - }, - props: ['defaultValue', 'name'] - }, - { - propsData: { - defaultValue, - name - }, - localVue, - store - } - ); + const wrapper = mount({ + template, + components: { + RenderlessExtraParam + }, + data: () => ({ name }) + }); - return { - wrapper: wrapper.findComponent(RenderlessExtraParam) - }; - } + return { + wrapper: wrapper.findComponent(RenderlessExtraParam) + }; +} +describe('testing RenderlessExtraParam component', () => { it('is an XComponent which has an XModule', () => { - const { wrapper } = renderRenderlessExtraParams({}); - expect(isXComponent(wrapper.vm)).toEqual(true); + const { wrapper } = render({}); + + expect(isXComponent(wrapper.vm)).toBeTruthy(); expect(getXComponentXModuleName(wrapper.vm)).toEqual('extraParams'); }); - // eslint-disable-next-line max-len it("doesn't emit ExtraParamsProvided event when the component receives a default value if it's in the store", () => { const extraParamsProvidedCallback = jest.fn(); - const { wrapper } = renderRenderlessExtraParams({ - defaultValue: 1234, - params: { warehouse: 1234 } - }); + render({ params: { warehouse: 1234 } }); - wrapper.vm.$x.on('ExtraParamsProvided', true).subscribe(extraParamsProvidedCallback); + XPlugin.bus.on('ExtraParamsProvided', true).subscribe(extraParamsProvidedCallback); expect(extraParamsProvidedCallback).toHaveBeenCalledTimes(0); }); it('emits UserChangedExtraParams event when the update method is called', () => { const userChangedExtraParamsCallback = jest.fn(); - const { wrapper } = renderRenderlessExtraParams({ + const { wrapper } = render({ template: ` ` }); - wrapper.vm.$x.on('UserChangedExtraParams', true).subscribe(userChangedExtraParamsCallback); + XPlugin.bus.on('UserChangedExtraParams', true).subscribe(userChangedExtraParamsCallback); expect(userChangedExtraParamsCallback).toHaveBeenCalledTimes(0); @@ -89,19 +71,3 @@ describe('testing RenderlessExtraParam component', () => { expect(userChangedExtraParamsCallback).toHaveBeenCalledTimes(1); }); }); - -interface RenderlessExtraParamsOptions { - /** The extra param's default value. */ - defaultValue?: unknown; - /** The extra param's name. */ - name?: unknown; - /** A dictionary with the params to save in the store. */ - params?: Dictionary; - /** The template to render. */ - template?: string; -} - -interface RenderlessExtraParamsAPI { - /** The wrapper for the extra params component. */ - wrapper: Wrapper; -} diff --git a/packages/x-components/src/x-modules/extra-params/components/renderless-extra-param.vue b/packages/x-components/src/x-modules/extra-params/components/renderless-extra-param.vue index c7258312d9..a680dff9e0 100644 --- a/packages/x-components/src/x-modules/extra-params/components/renderless-extra-param.vue +++ b/packages/x-components/src/x-modules/extra-params/components/renderless-extra-param.vue @@ -1,14 +1,8 @@ - - diff --git a/packages/x-components/src/x-modules/facets/components/lists/__tests__/selected-filters.spec.ts b/packages/x-components/src/x-modules/facets/components/lists/__tests__/selected-filters.spec.ts index f139501c8e..513234da1d 100644 --- a/packages/x-components/src/x-modules/facets/components/lists/__tests__/selected-filters.spec.ts +++ b/packages/x-components/src/x-modules/facets/components/lists/__tests__/selected-filters.spec.ts @@ -1,196 +1,169 @@ -import { Facet } from '@empathyco/x-types'; -import { DeepPartial } from '@empathyco/x-utils'; -import { createLocalVue, mount, Wrapper } from '@vue/test-utils'; -import Vue from 'vue'; -import Vuex, { Store } from 'vuex'; +import { Facet, Filter } from '@empathyco/x-types'; +import { mount } from '@vue/test-utils'; +import { nextTick } from 'vue'; import { createSimpleFacetStub } from '../../../../../__stubs__/facets-stubs.factory'; import { installNewXPlugin } from '../../../../../__tests__/utils'; import { getXComponentXModuleName, isXComponent } from '../../../../../components'; +import { useStore } from '../../../../../composables/use-store'; import { XPlugin } from '../../../../../plugins'; -import { RootXStoreState } from '../../../../../store'; import { resetFacetsService } from '../../../__tests__/utils'; import { DefaultFacetsService } from '../../../service/facets.service'; import { facetsXModule } from '../../../x-module'; import { resetXFacetsStateWith } from '../../__tests__/utils'; import SelectedFilters from '../selected-filters.vue'; -/** - * Renders the `SelectedFilters` component, exposing a basic API for testing. - * - * @param options - The options to render the component with. - * @returns The API for testing the `SelectedFilters` component. - */ -function renderSelectedFilters({ - template = '', - facetsIds = [] -}: RenderSelectedFiltersOptions = {}): RenderSelectedFiltersAPI { +jest.mock('../../../../../composables/use-store'); + +const facets: Record = { + gender: createSimpleFacetStub('gender', createFilter => [ + createFilter('Men', false), + createFilter('Women', false) + ]), + brand: createSimpleFacetStub('brand', createFilter => [ + createFilter('Audi', false), + createFilter('BMW', false) + ]), + color: createSimpleFacetStub('color', createFilter => [ + createFilter('red', false), + createFilter('blue', false) + ]) +}; + +function render({ template = '', facetsIds = [] as string[] } = {}) { resetFacetsService(); - const facets: Record = { - gender: createSimpleFacetStub('gender', createFilter => [ - createFilter('Men', false), - createFilter('Women', false) - ]), - brand: createSimpleFacetStub('brand', createFilter => [ - createFilter('Audi', false), - createFilter('BMW', false) - ]), - color: createSimpleFacetStub('color', createFilter => [ - createFilter('red', false), - createFilter('blue', false) - ]) - }; + installNewXPlugin({ initialXModules: [facetsXModule] }); + resetXFacetsStateWith(XPlugin.store, facets); + (useStore as jest.Mock).mockReturnValue(XPlugin.store); - const localVue = createLocalVue(); - localVue.use(Vuex); - const store = new Store>({}); - installNewXPlugin({ store }, localVue); - XPlugin.registerXModule(facetsXModule); - resetXFacetsStateWith(store, facets); - - const wrapper = mount( - { - components: { - SelectedFilters - }, - template, - data() { - return { - facetsIds - }; - } - }, - { - localVue, - store, - propsData: { - facetsIds - } - } - ); + const wrapper = mount({ + components: { SelectedFilters }, + template, + data: () => ({ facetsIds }) + }); const selectedFiltersWrapper = wrapper.findComponent(SelectedFilters); return { wrapper, selectedFiltersWrapper, - toggleFacetNthFilter(facetId, nth) { - const filter = store.getters['x/facets/facets'][facetId].filters[nth]; + toggleFacetNthFilter: (facetId: string, nth: number) => { + const filter: Filter = XPlugin.store.getters['x/facets/facets'][facetId].filters[nth]; DefaultFacetsService.instance.toggle(filter); - return localVue.nextTick(); + return nextTick(); } }; } describe('testing SelectedFilters component', () => { it('is an x-component', () => { - const { selectedFiltersWrapper } = renderSelectedFilters(); - expect(isXComponent(selectedFiltersWrapper.vm)).toEqual(true); + const { selectedFiltersWrapper } = render(); + + expect(isXComponent(selectedFiltersWrapper.vm)).toBeTruthy(); }); it('belongs to the `facets` x-module', () => { - const { selectedFiltersWrapper } = renderSelectedFilters(); + const { selectedFiltersWrapper } = render(); + expect(getXComponentXModuleName(selectedFiltersWrapper.vm)).toEqual('facets'); }); it('renders "nth" by default', async () => { - const { selectedFiltersWrapper, toggleFacetNthFilter } = renderSelectedFilters({ + const { selectedFiltersWrapper, toggleFacetNthFilter } = render({ template: '' }); - expect(selectedFiltersWrapper.text()).toBe('0'); + + expect(selectedFiltersWrapper.text()).toEqual('0'); + await toggleFacetNthFilter('brand', 0); - expect(selectedFiltersWrapper.text()).toBe('1'); + expect(selectedFiltersWrapper.text()).toEqual('1'); + await toggleFacetNthFilter('brand', 1); - expect(selectedFiltersWrapper.text()).toBe('2'); + expect(selectedFiltersWrapper.text()).toEqual('2'); + await toggleFacetNthFilter('gender', 0); - expect(selectedFiltersWrapper.text()).toBe('3'); + expect(selectedFiltersWrapper.text()).toEqual('3'); }); it('renders "nth selected" in its customized slot', async () => { - const { selectedFiltersWrapper, toggleFacetNthFilter } = renderSelectedFilters({ + const { selectedFiltersWrapper, toggleFacetNthFilter } = render({ template: ` - - + + {{ selectedFilters.length }} selected ` }); - expect(selectedFiltersWrapper.text()).toBe('0 selected'); + + expect(selectedFiltersWrapper.text()).toEqual('0 selected'); + await toggleFacetNthFilter('brand', 0); - expect(selectedFiltersWrapper.text()).toBe('1 selected'); + expect(selectedFiltersWrapper.text()).toEqual('1 selected'); + await toggleFacetNthFilter('brand', 1); - expect(selectedFiltersWrapper.text()).toBe('2 selected'); + expect(selectedFiltersWrapper.text()).toEqual('2 selected'); + await toggleFacetNthFilter('gender', 0); - expect(selectedFiltersWrapper.text()).toBe('3 selected'); + expect(selectedFiltersWrapper.text()).toEqual('3 selected'); }); it('renders "nth" by default of the facet ids provided', async () => { - const { selectedFiltersWrapper, toggleFacetNthFilter } = renderSelectedFilters({ + const { selectedFiltersWrapper, toggleFacetNthFilter } = render({ template: '', facetsIds: ['brand', 'gender'] }); - expect(selectedFiltersWrapper.text()).toBe('0'); + + expect(selectedFiltersWrapper.text()).toEqual('0'); + await toggleFacetNthFilter('brand', 0); - expect(selectedFiltersWrapper.text()).toBe('1'); + expect(selectedFiltersWrapper.text()).toEqual('1'); + await toggleFacetNthFilter('brand', 1); - expect(selectedFiltersWrapper.text()).toBe('2'); + expect(selectedFiltersWrapper.text()).toEqual('2'); + await toggleFacetNthFilter('gender', 0); - expect(selectedFiltersWrapper.text()).toBe('3'); + expect(selectedFiltersWrapper.text()).toEqual('3'); + await toggleFacetNthFilter('color', 0); - expect(selectedFiltersWrapper.text()).toBe('3'); + expect(selectedFiltersWrapper.text()).toEqual('3'); }); it('renders "nth selected" in its customized slot of the facet id provided', async () => { - const { selectedFiltersWrapper, toggleFacetNthFilter } = renderSelectedFilters({ + const { selectedFiltersWrapper, toggleFacetNthFilter } = render({ template: ` - - + + {{ selectedFilters.length }} selected `, facetsIds: ['brand'] }); - expect(selectedFiltersWrapper.text()).toBe('0 selected'); + expect(selectedFiltersWrapper.text()).toEqual('0 selected'); + await toggleFacetNthFilter('brand', 0); - expect(selectedFiltersWrapper.text()).toBe('1 selected'); + expect(selectedFiltersWrapper.text()).toEqual('1 selected'); + await toggleFacetNthFilter('brand', 1); - expect(selectedFiltersWrapper.text()).toBe('2 selected'); + expect(selectedFiltersWrapper.text()).toEqual('2 selected'); + await toggleFacetNthFilter('gender', 0); - expect(selectedFiltersWrapper.text()).toBe('2 selected'); + expect(selectedFiltersWrapper.text()).toEqual('2 selected'); }); it('always renders the component if alwaysVisible is true without selected filters', async () => { - const { selectedFiltersWrapper, toggleFacetNthFilter } = renderSelectedFilters({ + const { selectedFiltersWrapper, toggleFacetNthFilter } = render({ template: '' }); - expect(selectedFiltersWrapper.text()).toBe('0'); + expect(selectedFiltersWrapper.text()).toEqual('0'); + await toggleFacetNthFilter('brand', 0); - expect(selectedFiltersWrapper.text()).toBe('1'); + expect(selectedFiltersWrapper.text()).toEqual('1'); }); it("doesn't render the component if alwaysVisible is false and no selected filters", async () => { - const { selectedFiltersWrapper, toggleFacetNthFilter } = renderSelectedFilters(); + const { selectedFiltersWrapper, toggleFacetNthFilter } = render(); + + expect(selectedFiltersWrapper.html()).toEqual(''); - expect(selectedFiltersWrapper.html()).toBe(''); await toggleFacetNthFilter('brand', 0); - expect(selectedFiltersWrapper.text()).toBe('1'); + expect(selectedFiltersWrapper.text()).toEqual('1'); }); }); - -interface RenderSelectedFiltersOptions { - /** The template to be rendered. */ - template?: string; - /** Array of facets ids. */ - facetsIds?: Array; -} - -interface RenderSelectedFiltersAPI { - /** The `selectedFilters` wrapper component. */ - selectedFiltersWrapper: Wrapper; - /** Toggle nth filter of the facet provided. */ - toggleFacetNthFilter: (facetId: string, nth: number) => Promise; - /** The wrapper of the container element. */ - wrapper: Wrapper; -} diff --git a/packages/x-components/src/x-modules/facets/components/lists/exclude-filters-with-no-results.vue b/packages/x-components/src/x-modules/facets/components/lists/exclude-filters-with-no-results.vue index fbc832e5de..eba758f822 100644 --- a/packages/x-components/src/x-modules/facets/components/lists/exclude-filters-with-no-results.vue +++ b/packages/x-components/src/x-modules/facets/components/lists/exclude-filters-with-no-results.vue @@ -18,18 +18,12 @@ name: 'ExcludeFiltersWithNoResults', xModule: facetsXModule.name, props: { - /** - * The list of filters to be rendered as slots. - * - * @public - */ + /** The list of filters to be rendered as slots. */ filters: Array as PropType, /** * This prop is used in the `HierarchicalFilter` component and only in that case. It is necessary * to make the `renderedFilters` to return only the filters of each level of the hierarchy. - * - * @public */ parentId: { type: String as PropType @@ -42,16 +36,15 @@ * Removes the filters that have exactly 0 results associated. * * @returns A sublist of the filters prop, excluding the ones with no results. - * @internal */ - const filtersWithResults = computed((): Filter[] => { - return renderedFilters.value.filter( + const filtersWithResults = computed(() => + renderedFilters.value.filter( filter => !isBooleanFilter(filter) || filter.totalResults !== 0 - ); - }); + ) + ); provide('filters', filtersWithResults); - return () => (slots.default ? slots.default({ filters: filtersWithResults.value }) : h()); + return () => slots.default?.({ filters: filtersWithResults.value }) ?? h(); } }); diff --git a/packages/x-components/src/x-modules/facets/components/lists/selected-filters.vue b/packages/x-components/src/x-modules/facets/components/lists/selected-filters.vue index f98a2985bd..8fde465624 100644 --- a/packages/x-components/src/x-modules/facets/components/lists/selected-filters.vue +++ b/packages/x-components/src/x-modules/facets/components/lists/selected-filters.vue @@ -1,13 +1,6 @@ - - diff --git a/packages/x-components/src/x-modules/facets/components/lists/sorted-filters.vue b/packages/x-components/src/x-modules/facets/components/lists/sorted-filters.vue index 0b836602f0..b69b707569 100644 --- a/packages/x-components/src/x-modules/facets/components/lists/sorted-filters.vue +++ b/packages/x-components/src/x-modules/facets/components/lists/sorted-filters.vue @@ -52,7 +52,7 @@ }); provide('filters', sortedFilters); - return () => (slots.default ? slots.default({ filters: sortedFilters.value }) : h()); + return () => slots.default?.({ filters: sortedFilters.value }) ?? h(); } });