From 5c2b7bcf47281f9a3bb2ea65a1bfb73c6d850260 Mon Sep 17 00:00:00 2001 From: Andrea Delgado <114981520+andreadlgdo@users.noreply.github.com> Date: Thu, 23 May 2024 16:00:49 +0200 Subject: [PATCH] feat(search-box): migrate search-box x-module components to Composition API (#1476) --- packages/_vue3-migration-test/src/router.ts | 8 +- .../src/x-modules/index.ts | 1 + .../x-modules/search-box/components/index.ts | 1 + .../search-box/components/test-search-box.vue | 43 ++ .../src/x-modules/search-box/index.ts | 1 + .../composables/__tests__/use-state.spec.ts | 103 ++-- .../components/__tests__/search-input.spec.ts | 4 +- .../components/clear-search-input.vue | 55 +-- .../search-box/components/search-button.vue | 89 ++-- .../components/search-input-placeholder.vue | 386 +++++++-------- .../search-box/components/search-input.vue | 449 +++++++++--------- 11 files changed, 602 insertions(+), 538 deletions(-) create mode 100644 packages/_vue3-migration-test/src/x-modules/search-box/components/index.ts create mode 100644 packages/_vue3-migration-test/src/x-modules/search-box/components/test-search-box.vue create mode 100644 packages/_vue3-migration-test/src/x-modules/search-box/index.ts diff --git a/packages/_vue3-migration-test/src/router.ts b/packages/_vue3-migration-test/src/router.ts index aa225fbdbe..743e969b07 100644 --- a/packages/_vue3-migration-test/src/router.ts +++ b/packages/_vue3-migration-test/src/router.ts @@ -15,7 +15,8 @@ import { TestSortDropdown, TestSortList, TestSortPickerList, - TestBaseScroll + TestBaseScroll, + TestSearchBox } from './'; const routes = [ @@ -94,6 +95,11 @@ const routes = [ name: 'SortPickerList', component: TestSortPickerList }, + { + path: '/search-box', + name: 'SearchBox', + component: TestSearchBox + }, { path: '/elements-list', name: 'ElementsList', diff --git a/packages/_vue3-migration-test/src/x-modules/index.ts b/packages/_vue3-migration-test/src/x-modules/index.ts index 5c28632c91..821c5f1a14 100644 --- a/packages/_vue3-migration-test/src/x-modules/index.ts +++ b/packages/_vue3-migration-test/src/x-modules/index.ts @@ -1,5 +1,6 @@ export * from './facets'; export * from './next-queries'; export * from './search'; +export * from './search-box'; export { default as TestElementsList } from './test-elements-list.vue'; export * from './scroll'; diff --git a/packages/_vue3-migration-test/src/x-modules/search-box/components/index.ts b/packages/_vue3-migration-test/src/x-modules/search-box/components/index.ts new file mode 100644 index 0000000000..6e6ca5ee85 --- /dev/null +++ b/packages/_vue3-migration-test/src/x-modules/search-box/components/index.ts @@ -0,0 +1 @@ +export { default as TestSearchBox } from './test-search-box.vue'; diff --git a/packages/_vue3-migration-test/src/x-modules/search-box/components/test-search-box.vue b/packages/_vue3-migration-test/src/x-modules/search-box/components/test-search-box.vue new file mode 100644 index 0000000000..af6aa0caf8 --- /dev/null +++ b/packages/_vue3-migration-test/src/x-modules/search-box/components/test-search-box.vue @@ -0,0 +1,43 @@ + + + + + diff --git a/packages/_vue3-migration-test/src/x-modules/search-box/index.ts b/packages/_vue3-migration-test/src/x-modules/search-box/index.ts new file mode 100644 index 0000000000..07635cbbc8 --- /dev/null +++ b/packages/_vue3-migration-test/src/x-modules/search-box/index.ts @@ -0,0 +1 @@ +export * from './components'; diff --git a/packages/x-components/src/composables/__tests__/use-state.spec.ts b/packages/x-components/src/composables/__tests__/use-state.spec.ts index 508610f9ba..771870e030 100644 --- a/packages/x-components/src/composables/__tests__/use-state.spec.ts +++ b/packages/x-components/src/composables/__tests__/use-state.spec.ts @@ -1,75 +1,54 @@ -import { ComputedRef, defineComponent } from 'vue'; -import Vuex, { Store } from 'vuex'; -import { createLocalVue, mount } from '@vue/test-utils'; -import { Dictionary } from '@empathyco/x-utils'; +import { defineComponent } from 'vue'; +import { mount } from '@vue/test-utils'; import { installNewXPlugin } from '../../__tests__/utils'; -import { useState } from '../use-state'; -import { searchBoxXStoreModule } from '../../x-modules/search-box/index'; -import { AnyXStoreModule } from '../../store/index'; +import { XPlugin } from '../../plugins'; import { ExtractState } from '../../x-modules/x-modules.types'; - -const renderUseStateTest = (modulePropertyPaths: string[]): renderUseStateTestAPI => { - const testComponent = defineComponent({ - setup() { - const searchBoxState = useState( - 'searchBox', - modulePropertyPaths as (keyof ExtractState<'searchBox'>)[] - ); - return { - searchBoxState - }; - } - }); - - const localVue = createLocalVue(); - localVue.use(Vuex); - - const store = new Store({ - modules: { - x: { - namespaced: true, - modules: { - searchBox: { namespaced: true, ...searchBoxXStoreModule } as AnyXStoreModule - } - } - } +import { useRegisterXModule } from '../use-register-x-module'; +import { useState } from '../use-state'; +import { searchBoxXModule } from '../../x-modules/search-box/x-module'; +import { useStore } from '../use-store'; + +jest.mock('../use-store'); + +function render(modulePaths: (keyof ExtractState<'searchBox'> & string)[]) { + installNewXPlugin(); + (useStore as jest.Mock).mockReturnValue(XPlugin.store); + + const component = defineComponent({ + xModule: 'searchBox', + setup: () => { + useRegisterXModule(searchBoxXModule); + const searchBoxUseState = useState('searchBox', modulePaths); + return { searchBoxUseState }; + }, + template: `
` }); - installNewXPlugin({ store }, localVue); - const wrapper = mount(testComponent, { - localVue, - store - }); + const wrapper = mount(component); - return { - searchBoxState: (wrapper.vm as any).searchBoxState, - store - }; -}; + return (wrapper as any).vm.searchBoxUseState; +} describe('testing useState composable', () => { - it('maps store state', () => { - const { searchBoxState, store } = renderUseStateTest(['query', 'inputStatus']); - expect(searchBoxState.query.value).toEqual(''); - expect(searchBoxState.inputStatus.value).toEqual('initial'); + it('should map paths of the store state given', () => { + const { query, inputStatus } = render(['query', 'inputStatus']); + + expect(query).toBeDefined(); + expect(inputStatus).toBeDefined(); + expect(query.value).toEqual(''); + expect(inputStatus.value).toEqual('initial'); - // eslint-disable-next-line @typescript-eslint/no-unsafe-call - store.commit('x/searchBox/setQuery', 'pork shoulder '); - // eslint-disable-next-line @typescript-eslint/no-unsafe-call - store.commit('x/searchBox/setInputStatus', 'filled'); + XPlugin.store.commit('x/searchBox/setQuery', 'pork shoulder'); + XPlugin.store.commit('x/searchBox/setInputStatus', 'filled'); - expect(searchBoxState.query.value).toEqual('pork shoulder '); - expect(searchBoxState.inputStatus.value).toEqual('filled'); + expect(query.value).toEqual('pork shoulder'); + expect(inputStatus.value).toEqual('filled'); }); - it('does not return not requested state properties', () => { - const { searchBoxState } = renderUseStateTest(['query']); - expect(searchBoxState.query).toBeDefined(); - expect(searchBoxState.inputStatus).toBeUndefined(); + it('should not map paths which were not requested', () => { + const { query, inputStatus } = render(['query']); + + expect(query).toBeDefined(); + expect(inputStatus).toBeUndefined(); }); }); - -type renderUseStateTestAPI = { - searchBoxState: Dictionary>; - store: Store; -}; diff --git a/packages/x-components/src/x-modules/search-box/components/__tests__/search-input.spec.ts b/packages/x-components/src/x-modules/search-box/components/__tests__/search-input.spec.ts index a02f2c277c..c5de8dd521 100644 --- a/packages/x-components/src/x-modules/search-box/components/__tests__/search-input.spec.ts +++ b/packages/x-components/src/x-modules/search-box/components/__tests__/search-input.spec.ts @@ -3,7 +3,7 @@ import { createLocalVue, mount, Wrapper } from '@vue/test-utils'; import Vuex, { Store } from 'vuex'; import { getXComponentXModuleName, isXComponent } from '../../../../components/x-component.utils'; import { RootXStoreState } from '../../../../store/store.types'; -import { installNewXPlugin } from '../../../../__tests__/utils'; +import { getDataTestSelector, installNewXPlugin } from '../../../../__tests__/utils'; import { WireMetadata } from '../../../../wiring/wiring.types'; import SearchInput from '../search-input.vue'; import { resetXSearchBoxStateWith } from './utils'; @@ -28,7 +28,7 @@ function mountNewSearchInput(overrideProps: Partial = {}): Tes localVue, propsData: overrideProps }); - const input = wrapper.vm.$refs.input as HTMLInputElement; + const input = wrapper.find(getDataTestSelector('search-input')).element as HTMLInputElement; return { wrapper, diff --git a/packages/x-components/src/x-modules/search-box/components/clear-search-input.vue b/packages/x-components/src/x-modules/search-box/components/clear-search-input.vue index 2bd477d8a9..d1be96646c 100644 --- a/packages/x-components/src/x-modules/search-box/components/clear-search-input.vue +++ b/packages/x-components/src/x-modules/search-box/components/clear-search-input.vue @@ -11,13 +11,11 @@ diff --git a/packages/x-components/src/x-modules/search-box/components/search-button.vue b/packages/x-components/src/x-modules/search-box/components/search-button.vue index 35ed38f6d7..9b8ba21d96 100644 --- a/packages/x-components/src/x-modules/search-box/components/search-button.vue +++ b/packages/x-components/src/x-modules/search-box/components/search-button.vue @@ -1,5 +1,6 @@