Skip to content

Commit

Permalink
useRenderNormalizedItem tests (deephaven#1890)
Browse files Browse the repository at this point in the history
  • Loading branch information
bmingles committed Apr 29, 2024
1 parent dfaaf8b commit 912a64a
Showing 1 changed file with 93 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,38 +1,118 @@
import React, { Key } from 'react';
import { Item } from '@adobe/react-spectrum';
import { renderHook } from '@testing-library/react-hooks';
import ItemContent from '../ItemContent';
import { TestUtils } from '@deephaven/utils';
import { ItemContent } from '../ItemContent';
import { useRenderNormalizedItem } from './useRenderNormalizedItem';
import { getItemKey } from './itemUtils';
import { getItemKey, NormalizedItem } from './itemUtils';
import { wrapIcon, wrapPrimitiveWithText } from './itemWrapperUtils';

jest.mock('./itemWrapperUtils');

const { asMock } = TestUtils;

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

describe.each([null, { placement: 'top' }] as const)(
'useRenderNormalizedItem: %s',
tooltipOptions => {
describe.each([
[true, true, null],
[true, true, { placement: 'top' }],
[true, false, null],
[true, false, { placement: 'top' }],
[false, true, null],
[false, true, { placement: 'top' }],
[false, false, null],
[false, false, { placement: 'top' }],
] as const)(
'useRenderNormalizedItem: %s, %s, %s',
(showItemIcons, showItemDescriptions, tooltipOptions) => {
beforeEach(() => {
asMock(wrapIcon).mockImplementation((a, b) => `wrapIcon(${a}, ${b})`);
asMock(wrapPrimitiveWithText).mockImplementation(
(a, b) => `wrapPrimitiveWithText(${a}, ${b})`
);
});

it.each([
[{}, 'Empty', ''],
[{ item: { content: 'mock.content' } }, 'Empty', 'mock.content'],
[
{ item: { textValue: 'mock.textValue', content: 'mock.content' } },
{ key: 'mock.key', textValue: undefined },
'Empty',
'wrapIcon(undefined, illustration)',
'wrapPrimitiveWithText(undefined, undefined)',
'wrapPrimitiveWithText(undefined, description)',
],
[
{
key: 'mock.key',
item: { content: 'mock.content', textValue: undefined },
},
'Empty',
'wrapIcon(undefined, illustration)',
'wrapPrimitiveWithText(mock.content, undefined)',
'wrapPrimitiveWithText(undefined, description)',
],
[
{
key: 'mock.key',
item: { textValue: 'mock.textValue', content: 'mock.content' },
},
'mock.textValue',
'wrapIcon(undefined, illustration)',
'wrapPrimitiveWithText(mock.content, undefined)',
'wrapPrimitiveWithText(undefined, description)',
],
[
{
key: 'mock.key',
item: {
textValue: 'mock.textValue',
icon: 'mock.icon',
content: 'mock.content',
description: 'mock.description',
},
},
'mock.textValue',
'mock.content',
'wrapIcon(mock.icon, illustration)',
'wrapPrimitiveWithText(mock.content, undefined)',
'wrapPrimitiveWithText(mock.description, description)',
],
])(
] as [NormalizedItem, string, string, string, string][])(
'should return a render function that can be used to render a normalized item in collection components.',
(normalizedItem, textValue, content) => {
(normalizedItem, textValue, icon, content, description) => {
const { result } = renderHook(() =>
useRenderNormalizedItem(tooltipOptions)
useRenderNormalizedItem({
itemIconSlot: 'illustration',
showItemDescriptions,
showItemIcons,
tooltipOptions,
})
);

const actual = result.current(normalizedItem);

if (showItemIcons) {
expect(wrapIcon).toHaveBeenCalledWith(
normalizedItem.item?.icon,
'illustration'
);
}

if (showItemDescriptions) {
expect(wrapPrimitiveWithText).toHaveBeenCalledWith(
normalizedItem.item?.description,
'description'
);
}

expect(actual).toEqual(
<Item key={getItemKey(normalizedItem) as Key} textValue={textValue}>
<ItemContent tooltipOptions={tooltipOptions}>{content}</ItemContent>
<ItemContent tooltipOptions={tooltipOptions}>
{showItemIcons ? icon : null}
{content}
{showItemDescriptions ? description : null}
</ItemContent>
</Item>
);
}
Expand Down

0 comments on commit 912a64a

Please sign in to comment.