-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(search-box): migrate search-box x-module components to Compositi…
…on API (#1476)
- Loading branch information
1 parent
bfc043f
commit 5c2b7bc
Showing
11 changed files
with
602 additions
and
538 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'; |
1 change: 1 addition & 0 deletions
1
packages/_vue3-migration-test/src/x-modules/search-box/components/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default as TestSearchBox } from './test-search-box.vue'; |
43 changes: 43 additions & 0 deletions
43
packages/_vue3-migration-test/src/x-modules/search-box/components/test-search-box.vue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<template> | ||
<div style="display: flex; flex-flow: row nowrap"> | ||
<SearchInput | ||
@UserPressedEnterKey="value = 'You have pressed the enter key'" | ||
@UserFocusedSearchBox="hasFocus = true" | ||
@UserBlurredSearchBox="hasFocus = false" | ||
@UserIsTypingAQuery="value = 'Typing...'" | ||
:maxLength="10" | ||
/> | ||
<ClearSearchInput @UserPressedClearSearchBoxButton="value = 'You clear your query'" /> | ||
<SearchButton @UserPressedSearchButton="value = 'You search a query'">Search</SearchButton> | ||
<SearchInputPlaceholder | ||
:messages="placeholderMessages" | ||
animation="div" | ||
style="position: absolute; pointer-events: none; top: -41rem; left: 1rem" | ||
/> | ||
</div> | ||
<br /> | ||
<span>{{ value }}</span> | ||
<br /> | ||
<span>{{ hasFocus ? 'Focused' : 'Unfocused' }}</span> | ||
</template> | ||
|
||
<script setup lang="ts"> | ||
import { ref } from 'vue'; | ||
import SearchInput from '../../../../../x-components/src/x-modules/search-box/components/search-input.vue'; | ||
import SearchInputPlaceholder from '../../../../../x-components/src/x-modules/search-box/components/search-input-placeholder.vue'; | ||
import SearchButton from '../../../../../x-components/src/x-modules/search-box/components/search-button.vue'; | ||
import ClearSearchInput from '../../../../../x-components/src/x-modules/search-box/components/clear-search-input.vue'; | ||
const value = ref('There are no value yet'); | ||
const hasFocus = ref(false); | ||
const placeholderMessages = [ | ||
'Find shirts', | ||
'Find shoes', | ||
'Find watches', | ||
'Find handbags', | ||
'Find sunglasses' | ||
]; | ||
</script> | ||
|
||
<style scoped></style> |
1 change: 1 addition & 0 deletions
1
packages/_vue3-migration-test/src/x-modules/search-box/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './components'; |
103 changes: 41 additions & 62 deletions
103
packages/x-components/src/composables/__tests__/use-state.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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: `<div/>` | ||
}); | ||
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<ComputedRef<string[]>>; | ||
store: Store<any>; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.