Skip to content

Commit

Permalink
move sortObject to ObjectUtils and fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
standeren committed Nov 27, 2024
1 parent d3a64af commit ab909da
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,30 @@ describe('objectUtils', () => {
expect(ObjectUtils.flattenObjectValues(object)).toEqual(['value1', 'value2', 'value3']);
});
});

describe('sortEntriesInObjectByKeys', () => {
it('Sorts all entries in an object by its keys', () => {
const unSortedObject = { b: 'b', a: 'a', c: 'c' };
const sortedObject = ObjectUtils.sortEntriesInObjectByKeys(unSortedObject);
const sortedObjectKeys = Object.keys(sortedObject);
expect(sortedObjectKeys[0]).toBe('a');
expect(sortedObjectKeys[1]).toBe('b');
expect(sortedObjectKeys[2]).toBe('c');
});

it('Returns same order if entries in is already sorted', () => {
const unSortedObject = { a: 'a', b: 'b', c: 'c' };
const sortedObject = ObjectUtils.sortEntriesInObjectByKeys(unSortedObject);
const sortedObjectKeys = Object.keys(sortedObject);
expect(sortedObjectKeys[0]).toBe('a');
expect(sortedObjectKeys[1]).toBe('b');
expect(sortedObjectKeys[2]).toBe('c');
});

it('Returns empty list if entries to sort is empty', () => {
const emptyObject = {};
const sortedObject = ObjectUtils.sortEntriesInObjectByKeys(emptyObject);
expect(sortedObject).toEqual({});
});
});
});
10 changes: 10 additions & 0 deletions frontend/libs/studio-pure-functions/src/ObjectUtils/ObjectUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,14 @@ export class ObjectUtils {
})
.flat();
};

/**
* Sorts all entries in an object by its keys.
* @param object The object to sort.
* @returns A new object with the entries sorted by key.
*/
static sortEntriesInObjectByKeys = <T extends object>(object: T): T =>
Object.fromEntries(
Object.entries(object).sort(([keyA], [keyB]) => keyA.localeCompare(keyB)),
) as T;
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ describe('useUpdateOptionListIdMutation', () => {
});

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,
Expand All @@ -45,15 +49,14 @@ describe('useUpdateOptionListIdMutation', () => {
{ queryClient },
).result;
await renderUpdateOptionListMutationResult.current.mutateAsync({
optionListId: 'optionListA',
newOptionListId: 'optionListC',
optionListId: optionListA,
newOptionListId: optionListC,
});
const cacheData = queryClient.getQueryData([QueryKey.OptionLists, org, app]);
expect(cacheData).toEqual({
optionListB: optionListMock,
optionListC: optionListMock,
optionListZ: optionListMock,
});
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 () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ 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;
Expand All @@ -21,22 +22,20 @@ export const useUpdateOptionListIdMutation = (org: string, app: string) => {
},
onSuccess: ({ optionListId, newOptionListId }) => {
const oldData: OptionsLists = queryClient.getQueryData([QueryKey.OptionLists, org, app]);
const ascSortedData = ChangeIdAndSortCacheData(optionListId, newOptionListId, oldData);
const ascSortedData = changeIdAndSortCacheData(optionListId, newOptionListId, oldData);
queryClient.setQueryData([QueryKey.OptionLists, org, app], ascSortedData);
queryClient.invalidateQueries({ queryKey: [QueryKey.OptionListIds, org, app] });
},
});
};

const ChangeIdAndSortCacheData = (
const changeIdAndSortCacheData = (
oldId: string,
newId: string,
oldData: OptionsLists,
): OptionsLists => {
const newData = { ...oldData };
delete newData[oldId];
newData[newId] = oldData[oldId];
return Object.fromEntries(
Object.entries(newData).sort(([keyA], [keyB]) => keyA.localeCompare(keyB)),
);
return ObjectUtils.sortEntriesInObjectByKeys(newData);
};

0 comments on commit ab909da

Please sign in to comment.