-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: create useUpdateOptionListIdMutation hook (#14181)
- Loading branch information
Showing
8 changed files
with
165 additions
and
0 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
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
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 { useAddOptionListMutation } from './useAddOptionListMutation'; | ||
export { useUpdateOptionListMutation } from './useUpdateOptionListMutation'; | ||
export { useUpdateOptionListIdMutation } from './useUpdateOptionListIdMutation'; | ||
export { useUpsertTextResourcesMutation } from './useUpsertTextResourcesMutation'; | ||
export { useUpsertTextResourceMutation } from './useUpsertTextResourceMutation'; | ||
export { useRepoCommitAndPushMutation } from './useRepoCommitAndPushMutation'; |
81 changes: 81 additions & 0 deletions
81
frontend/packages/shared/src/hooks/mutations/useUpdateOptionListIdMutation.test.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,81 @@ | ||
import { app, org } from '@studio/testing/testids'; | ||
import { queriesMock } from 'app-shared/mocks/queriesMock'; | ||
import { renderHookWithProviders } from 'app-shared/mocks/renderHookWithProviders'; | ||
import type { UpdateOptionListIdMutationArgs } from './useUpdateOptionListIdMutation'; | ||
import { useUpdateOptionListIdMutation } from './useUpdateOptionListIdMutation'; | ||
import { createQueryClientMock } from 'app-shared/mocks/queryClientMock'; | ||
import { QueryKey } from 'app-shared/types/QueryKey'; | ||
import type { Option } from 'app-shared/types/Option'; | ||
import type { OptionsLists } from 'app-shared/types/api/OptionsLists'; | ||
|
||
// Test data: | ||
const optionListId: string = 'optionListId'; | ||
const newOptionListId: string = 'newOptionListId'; | ||
const optionListMock: Option[] = [{ value: 'value', label: 'label' }]; | ||
const args: UpdateOptionListIdMutationArgs = { optionListId, newOptionListId }; | ||
|
||
describe('useUpdateOptionListIdMutation', () => { | ||
test('Calls useUpdateOptionIdList with correct parameters', async () => { | ||
const queryClient = createQueryClientMock(); | ||
queryClient.setQueryData([QueryKey.OptionLists, org, app], [{ optionListId: optionListMock }]); | ||
const renderUpdateOptionListMutationResult = renderHookWithProviders( | ||
() => useUpdateOptionListIdMutation(org, app), | ||
{ queryClient }, | ||
).result; | ||
await renderUpdateOptionListMutationResult.current.mutateAsync(args); | ||
expect(queriesMock.updateOptionListId).toHaveBeenCalledTimes(1); | ||
expect(queriesMock.updateOptionListId).toHaveBeenCalledWith( | ||
org, | ||
app, | ||
optionListId, | ||
newOptionListId, | ||
); | ||
}); | ||
|
||
test('Sets the option lists cache with new id in correct alphabetical order', async () => { | ||
const optionListA = 'optionListA'; | ||
const optionListB = 'optionListB'; | ||
const optionListC = 'optionListC'; | ||
const optionListZ = 'optionListZ'; | ||
const queryClient = createQueryClientMock(); | ||
const oldData: OptionsLists = { | ||
optionListA: optionListMock, | ||
optionListB: optionListMock, | ||
optionListZ: optionListMock, | ||
}; | ||
queryClient.setQueryData([QueryKey.OptionLists, org, app], oldData); | ||
const renderUpdateOptionListMutationResult = renderHookWithProviders( | ||
() => useUpdateOptionListIdMutation(org, app), | ||
{ queryClient }, | ||
).result; | ||
await renderUpdateOptionListMutationResult.current.mutateAsync({ | ||
optionListId: optionListA, | ||
newOptionListId: optionListC, | ||
}); | ||
const cacheData = queryClient.getQueryData([QueryKey.OptionLists, org, app]); | ||
const cacheDataKeys = Object.keys(cacheData); | ||
expect(cacheDataKeys[0]).toEqual(optionListB); | ||
expect(cacheDataKeys[1]).toEqual(optionListC); | ||
expect(cacheDataKeys[2]).toEqual(optionListZ); | ||
}); | ||
|
||
test('Invalidates the optionListIds query cache', async () => { | ||
const queryClient = createQueryClientMock(); | ||
const invalidateQueriesSpy = jest.spyOn(queryClient, 'invalidateQueries'); | ||
const oldData: OptionsLists = { | ||
firstOptionList: optionListMock, | ||
optionListId: optionListMock, | ||
lastOptionList: optionListMock, | ||
}; | ||
queryClient.setQueryData([QueryKey.OptionLists, org, app], oldData); | ||
const renderUpdateOptionListMutationResult = renderHookWithProviders( | ||
() => useUpdateOptionListIdMutation(org, app), | ||
{ queryClient }, | ||
).result; | ||
await renderUpdateOptionListMutationResult.current.mutateAsync(args); | ||
expect(invalidateQueriesSpy).toHaveBeenCalledTimes(1); | ||
expect(invalidateQueriesSpy).toHaveBeenCalledWith({ | ||
queryKey: [QueryKey.OptionListIds, org, app], | ||
}); | ||
}); | ||
}); |
41 changes: 41 additions & 0 deletions
41
frontend/packages/shared/src/hooks/mutations/useUpdateOptionListIdMutation.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,41 @@ | ||
import { QueryKey } from 'app-shared/types/QueryKey'; | ||
import type { OptionsLists } from 'app-shared/types/api/OptionsLists'; | ||
import { useQueryClient, useMutation } from '@tanstack/react-query'; | ||
import { useServicesContext } from 'app-shared/contexts/ServicesContext'; | ||
import { ObjectUtils } from '@studio/pure-functions'; | ||
|
||
export interface UpdateOptionListIdMutationArgs { | ||
optionListId: string; | ||
newOptionListId: string; | ||
} | ||
|
||
export const useUpdateOptionListIdMutation = (org: string, app: string) => { | ||
const queryClient = useQueryClient(); | ||
const { updateOptionListId } = useServicesContext(); | ||
|
||
return useMutation({ | ||
mutationFn: async ({ optionListId, newOptionListId }: UpdateOptionListIdMutationArgs) => { | ||
return updateOptionListId(org, app, optionListId, newOptionListId).then(() => ({ | ||
optionListId, | ||
newOptionListId, | ||
})); | ||
}, | ||
onSuccess: ({ optionListId, newOptionListId }) => { | ||
const oldData: OptionsLists = queryClient.getQueryData([QueryKey.OptionLists, org, app]); | ||
const ascSortedData = changeIdAndSortCacheData(optionListId, newOptionListId, oldData); | ||
queryClient.setQueryData([QueryKey.OptionLists, org, app], ascSortedData); | ||
queryClient.invalidateQueries({ queryKey: [QueryKey.OptionListIds, org, app] }); | ||
}, | ||
}); | ||
}; | ||
|
||
const changeIdAndSortCacheData = ( | ||
oldId: string, | ||
newId: string, | ||
oldData: OptionsLists, | ||
): OptionsLists => { | ||
const newData = { ...oldData }; | ||
delete newData[oldId]; | ||
newData[newId] = oldData[oldId]; | ||
return ObjectUtils.sortEntriesInObjectByKeys(newData); | ||
}; |
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