From 9e73b369195be27cdc91807c2b86f2151c23f5be Mon Sep 17 00:00:00 2001 From: Brian Ingles Date: Mon, 18 Mar 2024 14:35:12 -0500 Subject: [PATCH] Unit tests (#353) --- .../src/js/src/elements/ElementUtils.test.ts | 23 --- .../src/js/src/elements/ElementUtils.test.tsx | 135 ++++++++++++++++++ .../ui/src/js/src/elements/ElementUtils.tsx | 4 +- 3 files changed, 137 insertions(+), 25 deletions(-) delete mode 100644 plugins/ui/src/js/src/elements/ElementUtils.test.ts create mode 100644 plugins/ui/src/js/src/elements/ElementUtils.test.tsx diff --git a/plugins/ui/src/js/src/elements/ElementUtils.test.ts b/plugins/ui/src/js/src/elements/ElementUtils.test.ts deleted file mode 100644 index 2e8994b4d..000000000 --- a/plugins/ui/src/js/src/elements/ElementUtils.test.ts +++ /dev/null @@ -1,23 +0,0 @@ -import { ELEMENT_KEY, isPrimitive, wrapElementChildren } from './ElementUtils'; - -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', () => { - it('should return given element if it has no children', () => { - const actual = wrapElementChildren({ [ELEMENT_KEY]: 'mock.element' }); - expect(actual).toBe(actual); - }); -}); diff --git a/plugins/ui/src/js/src/elements/ElementUtils.test.tsx b/plugins/ui/src/js/src/elements/ElementUtils.test.tsx new file mode 100644 index 000000000..7a11260b6 --- /dev/null +++ b/plugins/ui/src/js/src/elements/ElementUtils.test.tsx @@ -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({ + type: 'mock.exported.a', + }), + exportedA2: createMockProxy({ + type: 'mock.exported.a', + }), + exportedB1: createMockProxy({ + type: 'mock.exported.b', + }), + itemWithString: String, + itemWithNumber: {123}, + itemWithTrue: {true}, + itemWithFalse: {false}, + }; + + 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, + , + ], + [ + [mock.exportedA1, mock.exportedA2, 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', String], + [999, 999], + [true, true], + [false, false], + [ + ['String', 999, true, false], + [ + String, + 999, + true, + false, + ], + ], + /* 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); + } + ); + } + ); +}); diff --git a/plugins/ui/src/js/src/elements/ElementUtils.tsx b/plugins/ui/src/js/src/elements/ElementUtils.tsx index 0c9e9eeb7..da03f69fb 100644 --- a/plugins/ui/src/js/src/elements/ElementUtils.tsx +++ b/plugins/ui/src/js/src/elements/ElementUtils.tsx @@ -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 @@ -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 {child}; + return {String(child)}; } return child;