Skip to content

Commit

Permalink
Unit tests (#353)
Browse files Browse the repository at this point in the history
  • Loading branch information
bmingles committed Mar 18, 2024
1 parent 2b3caf5 commit 9e73b36
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 25 deletions.
23 changes: 0 additions & 23 deletions plugins/ui/src/js/src/elements/ElementUtils.test.ts

This file was deleted.

135 changes: 135 additions & 0 deletions plugins/ui/src/js/src/elements/ElementUtils.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
import React from 'react';
import { Text } from '@adobe/react-spectrum';
import { Item } from '@deephaven/components';
import type { WidgetExportedObject } from '@deephaven/jsapi-types';
import { TestUtils } from '@deephaven/utils';
import { ELEMENT_KEY, isPrimitive, wrapElementChildren } from './ElementUtils';
import ObjectView from './ObjectView';
import { ITEM_ELEMENT_NAME } from './ElementConstants';

const { createMockProxy } = TestUtils;

describe('isPrimitive', () => {
it.each(['test', 444, true, false])(
'should return true primitive values: %s',
value => {
const actual = isPrimitive(value);
expect(actual).toBe(true);
}
);

it('should return false for non-primitive values', () => {
const actual = isPrimitive({});
expect(actual).toBe(false);
});
});

describe('wrapElementChildren', () => {
const mock = {
exportedA1: createMockProxy<WidgetExportedObject>({
type: 'mock.exported.a',
}),
exportedA2: createMockProxy<WidgetExportedObject>({
type: 'mock.exported.a',
}),
exportedB1: createMockProxy<WidgetExportedObject>({
type: 'mock.exported.b',
}),
itemWithString: <Item>String</Item>,
itemWithNumber: <Item>{123}</Item>,
itemWithTrue: <Item>{true}</Item>,
itemWithFalse: <Item>{false}</Item>,
};

it('should return given element if it has no children', () => {
const actual = wrapElementChildren({ [ELEMENT_KEY]: 'mock.element' });
expect(actual).toBe(actual);
});

it.each([
[
mock.exportedA1,
<ObjectView key={`${mock.exportedA1.type}-0`} object={mock.exportedA1} />,
],
[
[mock.exportedA1, mock.exportedA2, mock.exportedB1],
[
<ObjectView
key={`${mock.exportedA1.type}-0`}
object={mock.exportedA1}
/>,
<ObjectView
key={`${mock.exportedA1.type}-1`}
object={mock.exportedA1}
/>,
<ObjectView
key={`${mock.exportedB1.type}-0`}
object={mock.exportedB1}
/>,
],
],
])(
'should wrap exported object children in ObjectView: %s, %s',
(children, expectedChildren) => {
const actual = wrapElementChildren({
[ELEMENT_KEY]: 'mock.element',
props: { children },
});

expect(actual.props?.children).toEqual(expectedChildren);
}
);

describe.each(['Some text value', undefined])(
'Item element `textValue`: `%s`',
textValue => {
it.each([
/* eslint-disable react/jsx-key */
['String', <Text key="String">String</Text>],
[999, <Text key="999">999</Text>],
[true, <Text key="true">true</Text>],
[false, <Text key="false">false</Text>],
[
['String', 999, true, false],
[
<Text key="String">String</Text>,
<Text key="999">999</Text>,
<Text key="true">true</Text>,
<Text key="false">false</Text>,
],
],
/* eslint-enable react/jsx-key */
])(
'should wrap primitive item element children in Text elements: %s, %s',
(children, expectedChildren) => {
const given =
textValue == null
? {
[ELEMENT_KEY]: ITEM_ELEMENT_NAME,
props: { children },
}
: {
[ELEMENT_KEY]: ITEM_ELEMENT_NAME,
props: { textValue, children },
};

const actual = wrapElementChildren(given);

const fallbackTextValue = Array.isArray(children)
? undefined
: String(children);

const expected = {
[ELEMENT_KEY]: ITEM_ELEMENT_NAME,
props: {
textValue: textValue ?? fallbackTextValue,
children: expectedChildren,
},
};

expect(actual).toEqual(expected);
}
);
}
);
});
4 changes: 2 additions & 2 deletions plugins/ui/src/js/src/elements/ElementUtils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ export function wrapElementChildren(element: ElementNode): ElementNode {
!('textValue' in newProps) &&
isPrimitive(newProps.children)
) {
newProps.textValue = newProps.children;
newProps.textValue = String(newProps.children);
}

// Derive child keys based on type + index of the occurrence of the type
Expand All @@ -177,7 +177,7 @@ export function wrapElementChildren(element: ElementNode): ElementNode {

// Auto wrap primitive children of `Item` elements in `Text` elements
if (isItemElement && isPrimitive(child)) {
return <Text key={String(child)}>{child}</Text>;
return <Text key={String(child)}>{String(child)}</Text>;
}

return child;
Expand Down

0 comments on commit 9e73b36

Please sign in to comment.