Skip to content

Commit

Permalink
Item key stringify tests (deephaven#1909)
Browse files Browse the repository at this point in the history
  • Loading branch information
bmingles committed Apr 11, 2024
1 parent 962f64c commit 599902d
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 13 deletions.
14 changes: 14 additions & 0 deletions packages/components/src/spectrum/utils/itemUtils.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
ItemElementOrPrimitive,
ItemOrSection,
SectionElement,
itemSelectionToStringSet,
} from './itemUtils';
import type { PickerProps } from '../picker/Picker';
import { Item, Section } from '../shared';
Expand Down Expand Up @@ -256,6 +257,19 @@ describe('isNormalizedSection', () => {
});
});

describe('itemSelectionToStringSet', () => {
it.each([
['all', 'all'],
[new Set([1, 2, 3]), new Set(['1', '2', '3'])],
] as const)(
`should return 'all' or stringify the keys`,
(given, expected) => {
const actual = itemSelectionToStringSet(given);
expect(actual).toEqual(expected);
}
);
});

describe('normalizeItemList', () => {
it.each([children.empty, children.single, children.mixed])(
'should return normalized items: %#: %s',
Expand Down
15 changes: 15 additions & 0 deletions packages/components/src/spectrum/utils/itemUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -309,3 +309,18 @@ export function normalizeTooltipOptions(

return options;
}

/**
* Convert a selection of `ItemKey`s to a selection of strings.
* @param itemKeys The selection of `ItemKey`s
* @returns The selection of strings
*/
export function itemSelectionToStringSet(
itemKeys?: 'all' | Iterable<ItemKey>
): undefined | 'all' | Set<string> {
if (itemKeys == null || itemKeys === 'all') {
return itemKeys as undefined | 'all';
}

return new Set([...itemKeys].map(String));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { renderHook } from '@testing-library/react-hooks';
import { NormalizedItem } from './itemUtils';
import { useStringifiedMultiSelection } from './useStringifiedMultiSelection';

beforeEach(() => {
jest.clearAllMocks();
expect.hasAssertions();
});

describe('useStringifiedMultiSelection', () => {
const normalizedItems: NormalizedItem[] = [1, 2, 3, 4, 5, 6, 7, 8, 9].map(
i => ({
key: i,
item: { key: i, content: `Item ${i}` },
})
);

const selectedKeys = [1, 2, 3];
const defaultSelectedKeys = [4, 5, 6];
const disabledKeys = [7, 8, 9];

const selectedStringKeys = new Set(['1', '2', '3']);
const defaultSelectedStringKeys = new Set(['4', '5', '6']);
const disabledStringKeys = new Set(['7', '8', '9']);

it('should stringify selections', () => {
const { result } = renderHook(() =>
useStringifiedMultiSelection({
normalizedItems,
selectedKeys,
defaultSelectedKeys,
disabledKeys,
})
);

expect(result.current.selectedStringKeys).toEqual(selectedStringKeys);
expect(result.current.defaultSelectedStringKeys).toEqual(
defaultSelectedStringKeys
);
expect(result.current.disabledStringKeys).toEqual(disabledStringKeys);
});

it.each([
['all', 'all'],
[new Set(['1', '2', '3']), new Set([1, 2, 3])],
] as const)(
`should call onChange with 'all' or actual keys`,
(given, expected) => {
const onChange = jest.fn();
const { result } = renderHook(() =>
useStringifiedMultiSelection({
normalizedItems,
selectedKeys,
defaultSelectedKeys,
disabledKeys,
onChange,
})
);

result.current.onStringSelectionChange(given);

expect(onChange).toHaveBeenCalledWith(expected);
}
);
});
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,10 @@ import {
getItemKey,
ItemKey,
ItemSelection,
itemSelectionToStringSet,
NormalizedItem,
} from './itemUtils';

function toStringKeySet(
keys?: 'all' | Iterable<ItemKey>
): undefined | 'all' | Set<Key> {
if (keys == null || keys === 'all') {
return keys as undefined | 'all';
}

return new Set([...keys].map(String));
}

export interface UseStringifiedMultiSelectionOptions {
normalizedItems: NormalizedItem[];
selectedKeys?: 'all' | Iterable<ItemKey>;
Expand Down Expand Up @@ -64,17 +55,17 @@ export function useStringifiedMultiSelection({
onChange,
}: UseStringifiedMultiSelectionOptions): UseStringifiedMultiSelectionResult {
const selectedStringKeys = useMemo(
() => toStringKeySet(selectedKeys),
() => itemSelectionToStringSet(selectedKeys),
[selectedKeys]
);

const defaultSelectedStringKeys = useMemo(
() => toStringKeySet(defaultSelectedKeys),
() => itemSelectionToStringSet(defaultSelectedKeys),
[defaultSelectedKeys]
);

const disabledStringKeys = useMemo(
() => toStringKeySet(disabledKeys),
() => itemSelectionToStringSet(disabledKeys),
[disabledKeys]
);

Expand Down

0 comments on commit 599902d

Please sign in to comment.