-
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: migrate history queries x module to composition api (#1494)
- Loading branch information
1 parent
18dc522
commit cd51e9b
Showing
14 changed files
with
428 additions
and
341 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
2 changes: 2 additions & 0 deletions
2
packages/_vue3-migration-test/src/x-modules/history-queries/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,2 @@ | ||
export { default as TestHistoryQueries } from './test-history-queries.vue'; | ||
export { default as TestMyHistory } from './test-my-history.vue'; |
16 changes: 16 additions & 0 deletions
16
...es/_vue3-migration-test/src/x-modules/history-queries/components/test-history-queries.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,16 @@ | ||
<template> | ||
<SearchInput :maxLength="10" /> | ||
<ClearSearchInput /> | ||
|
||
<HistoryQueries :maxItemsToRender="5" /> | ||
<ClearHistoryQueries class="x-button--ghost x-button--ghost-start"> | ||
<span>Clear previous searches</span> | ||
</ClearHistoryQueries> | ||
</template> | ||
|
||
<script setup> | ||
import HistoryQueries from '../../../../../x-components/src/x-modules/history-queries/components/history-queries.vue'; | ||
import ClearHistoryQueries from '../../../../../x-components/src/x-modules/history-queries/components/clear-history-queries.vue'; | ||
import ClearSearchInput from '../../../../../x-components/src/x-modules/search-box/components/clear-search-input.vue'; | ||
import SearchInput from '../../../../../x-components/src/x-modules/search-box/components/search-input.vue'; | ||
</script> |
33 changes: 33 additions & 0 deletions
33
packages/_vue3-migration-test/src/x-modules/history-queries/components/test-my-history.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,33 @@ | ||
<template> | ||
<MyHistory v-if="_$x.isHistoryQueriesEnabled && _$x.fullHistoryQueries.length"> | ||
<template #date="{ date }"> | ||
<div>{{ date }}</div> | ||
</template> | ||
|
||
<template #suggestion="{ suggestion, formatTime }"> | ||
<HistoryQuery data-test="my-history-query" :suggestion="suggestion"> | ||
<div class="flex flex-row gap-8"> | ||
<p>{{ suggestion.query }}</p> | ||
|
||
<p> | ||
{{ formatTime(suggestion.timestamp) }} | ||
</p> | ||
</div> | ||
|
||
<template #remove-button-content> | ||
<span>x</span> | ||
</template> | ||
</HistoryQuery> | ||
</template> | ||
</MyHistory> | ||
</template> | ||
|
||
<script setup> | ||
import HistoryQuery from '../../../../../x-components/src/x-modules/history-queries/components/history-query.vue'; | ||
import MyHistory from '../../../../../x-components/src/x-modules/history-queries/components/my-history.vue'; | ||
import { use$x } from '../../../../../x-components/src/composables/use-$x'; | ||
const _$x = use$x(); | ||
</script> | ||
|
||
<style scoped></style> |
1 change: 1 addition & 0 deletions
1
packages/_vue3-migration-test/src/x-modules/history-queries/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'; |
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
87 changes: 32 additions & 55 deletions
87
packages/x-components/src/composables/__tests__/use-getter.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,72 +1,49 @@ | ||
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 { useGetter } from '../use-getter'; | ||
import { historyQueriesXStoreModule } from '../../x-modules/history-queries'; | ||
import { AnyXStoreModule } from '../../store/index'; | ||
import { useRegisterXModule } from '../use-register-x-module'; | ||
import { ExtractGetters } from '../../x-modules/x-modules.types'; | ||
|
||
const renderUseGetterTest = (getters: string[]): renderUseGetterTestAPI => { | ||
const testComponent = defineComponent({ | ||
setup() { | ||
const historyQueriesGetter = useGetter( | ||
'historyQueries', | ||
getters as (keyof ExtractGetters<'historyQueries'>)[] | ||
); | ||
return { | ||
historyQueriesGetter | ||
}; | ||
} | ||
}); | ||
|
||
const localVue = createLocalVue(); | ||
localVue.use(Vuex); | ||
|
||
const store = new Store({ | ||
modules: { | ||
x: { | ||
namespaced: true, | ||
modules: { | ||
historyQueries: { namespaced: true, ...historyQueriesXStoreModule } as AnyXStoreModule | ||
} | ||
} | ||
} | ||
import { useStore } from '../use-store'; | ||
import { XPlugin } from '../../plugins'; | ||
import { historyQueriesXModule } from '../../x-modules/history-queries/x-module'; | ||
|
||
jest.mock('../use-store'); | ||
|
||
function render(modulePaths: (keyof ExtractGetters<'historyQueries'>)[]) { | ||
installNewXPlugin(); | ||
(useStore as jest.Mock).mockReturnValue(XPlugin.store); | ||
|
||
const component = defineComponent({ | ||
xModule: 'historyQueries', | ||
setup: () => { | ||
useRegisterXModule(historyQueriesXModule); | ||
const historyQueriesGetter = useGetter('historyQueries', modulePaths); | ||
return { historyQueriesGetter }; | ||
}, | ||
template: `<div/>` | ||
}); | ||
installNewXPlugin({ store }, localVue); | ||
|
||
const wrapper = mount(testComponent, { | ||
localVue, | ||
store | ||
}); | ||
const wrapper = mount(component); | ||
|
||
return { | ||
store, | ||
historyQueriesGetter: (wrapper.vm as any).historyQueriesGetter | ||
}; | ||
}; | ||
return (wrapper as any).vm.historyQueriesGetter; | ||
} | ||
|
||
describe('testing useGetter composable', () => { | ||
it('maps store getters', () => { | ||
const { historyQueriesGetter, store } = renderUseGetterTest(['storageKey', 'historyQueries']); | ||
expect(historyQueriesGetter.storageKey.value).toEqual('history-queries'); | ||
expect(historyQueriesGetter.historyQueries.value).toHaveLength(0); | ||
const { storageKey, historyQueries } = render(['storageKey', 'historyQueries']); | ||
expect(storageKey.value).toEqual('history-queries'); | ||
expect(historyQueries.value).toHaveLength(0); | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-unsafe-call | ||
store.dispatch('x/historyQueries/addQueryToHistory', 'chorizo'); | ||
XPlugin.store.dispatch('x/historyQueries/addQueryToHistory', 'chorizo'); | ||
|
||
expect(historyQueriesGetter.historyQueries.value).toHaveLength(1); | ||
expect(historyQueries.value).toHaveLength(1); | ||
}); | ||
|
||
it('does not return not requested getters', () => { | ||
const { historyQueriesGetter } = renderUseGetterTest(['storageKey']); | ||
expect(historyQueriesGetter.storageKey).toBeDefined(); | ||
expect(historyQueriesGetter.historyQueries).toBeUndefined(); | ||
const { storageKey, historyQueries } = render(['storageKey']); | ||
expect(storageKey).toBeDefined(); | ||
expect(historyQueries).toBeUndefined(); | ||
}); | ||
}); | ||
|
||
type renderUseGetterTestAPI = { | ||
historyQueriesGetter: 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
Oops, something went wrong.