-
Notifications
You must be signed in to change notification settings - Fork 21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: create useState composable #1402
Merged
CachedaCodes
merged 8 commits into
main
from
feature/EMP-3458-create-a-use-state-composable
Feb 13, 2024
Merged
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
0f86bf2
feat: create useState composable
lauramargar bd42d26
feat: move useState composable into useStore
lauramargar 1aa1105
feat: new approach for useStore composable
lauramargar 2f224db
feat: new approach for useStore composable
lauramargar 0b7e510
chore: separate useState composable from useStore && add useState tests
lauramargar 358540e
chore: separate useState composable from useStore && add useState tests
lauramargar 395b545
chore: fix useState types
lauramargar 2056d23
chore: fix pr comments
lauramargar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
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 |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { defineComponent } from 'vue'; | ||
import Vuex, { Store } from 'vuex'; | ||
import { createLocalVue, mount, Wrapper } from '@vue/test-utils'; | ||
import { installNewXPlugin } from '../../__tests__/utils'; | ||
import { useState } from '../use-state'; | ||
import { searchBoxXStoreModule } from '../../x-modules/search-box/index'; | ||
|
||
const TestComponent = defineComponent({ | ||
setup() { | ||
const searchBoxState = useState('searchBox', ['query', 'inputStatus']); | ||
return { | ||
searchBoxState | ||
}; | ||
} | ||
}); | ||
|
||
describe('testing useState composable', () => { | ||
let component: Wrapper<any>; | ||
|
||
beforeEach(() => { | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-call | ||
component?.vm.$destroy(); | ||
jest.clearAllMocks(); | ||
const localVue = createLocalVue(); | ||
localVue.use(Vuex); | ||
|
||
const store = new Store({ | ||
modules: { | ||
x: { | ||
namespaced: true, | ||
modules: { | ||
searchBox: { namespaced: true, ...searchBoxXStoreModule } as any | ||
} | ||
} | ||
} | ||
}); | ||
installNewXPlugin({ store }, localVue); | ||
|
||
component = mount(TestComponent, { | ||
localVue, | ||
store | ||
}); | ||
}); | ||
|
||
it('maps store state', () => { | ||
expect(component.vm.searchBoxState.query.value).toEqual(''); | ||
expect(component.vm.searchBoxState.inputStatus.value).toEqual('initial'); | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-unsafe-call | ||
component.vm.$store.commit('x/searchBox/setQuery', 'pork shoulder '); | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-call | ||
component.vm.$store.commit('x/searchBox/setInputStatus', 'filled'); | ||
|
||
expect(component.vm.searchBoxState.query.value).toEqual('pork shoulder '); | ||
expect(component.vm.searchBoxState.inputStatus.value).toEqual('filled'); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { computed, ComputedRef } from 'vue'; | ||
import { Dictionary } from '@empathyco/x-utils'; | ||
import { ExtractState, XModuleName } from '../x-modules/x-modules.types'; | ||
import { useStore } from './use-store'; | ||
|
||
/** | ||
* Function which returns the selected state as a dictionary of paths. | ||
* | ||
* @param module - The {@link XModuleName} of the getter. | ||
* @param paths - List of state paths. | ||
* @returns The state properties of the module. | ||
* | ||
* @public | ||
*/ | ||
export function useState<Module extends XModuleName, Paths extends keyof ExtractState<Module>>( | ||
module: Module, | ||
paths: Paths[] | ||
): Dictionary<ComputedRef> { | ||
const store = useStore(); | ||
|
||
return paths.reduce<Dictionary<ComputedRef>>((state, path) => { | ||
state[path as string] = computed(() => store.state.x[module][path]); | ||
return state; | ||
}, {}); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we can type this part as AnyXStoreModule