From 8d200af9e937b0156c1dd2e98102396f6f293c72 Mon Sep 17 00:00:00 2001 From: Noah Overcash Date: Fri, 27 Dec 2024 14:11:41 -0500 Subject: [PATCH] [UIPQB-179] Properly display string booleans --- src/QueryBuilder/ResultViewer/utils.js | 19 +++++++--- src/QueryBuilder/ResultViewer/utils.test.js | 39 +++++++++++++++++++++ 2 files changed, 54 insertions(+), 4 deletions(-) create mode 100644 src/QueryBuilder/ResultViewer/utils.test.js diff --git a/src/QueryBuilder/ResultViewer/utils.js b/src/QueryBuilder/ResultViewer/utils.js index 37be439..79863c3 100644 --- a/src/QueryBuilder/ResultViewer/utils.js +++ b/src/QueryBuilder/ResultViewer/utils.js @@ -11,16 +11,27 @@ export const formatValueByDataType = (value, dataType, intl, additionalParams = switch (dataType) { case DATA_TYPES.BooleanType: - return value - ? - : ; + // booleans may be returned as true booleans, or strings 'true' or 'false' + if (typeof value === 'string') { + return value === 'true' ? ( + + ) : ( + + ); + } else { + return value ? ( + + ) : ( + + ); + } case DATA_TYPES.DateType: return ; case DATA_TYPES.ArrayType: if (additionalParams?.isInstanceLanguages) { - return value.map(lang => formattedLanguageName(lang, intl)).join(' | '); + return value.map((lang) => formattedLanguageName(lang, intl)).join(' | '); } return value.join(' | '); diff --git a/src/QueryBuilder/ResultViewer/utils.test.js b/src/QueryBuilder/ResultViewer/utils.test.js new file mode 100644 index 0000000..dfaf2ac --- /dev/null +++ b/src/QueryBuilder/ResultViewer/utils.test.js @@ -0,0 +1,39 @@ +import '@testing-library/jest-dom'; +import { render } from '@testing-library/react'; +import { IntlProvider } from 'react-intl'; +import { DATA_TYPES } from '../../constants/dataTypes'; +import { formatValueByDataType } from './utils'; + +describe('formatValueByDataType returns correct value', () => { + test.each([ + [undefined, DATA_TYPES.StringType, ''], + [null, DATA_TYPES.StringType, ''], + + [true, DATA_TYPES.BooleanType, 'ui-plugin-query-builder.options.true'], + [false, DATA_TYPES.BooleanType, 'ui-plugin-query-builder.options.false'], + ['true', DATA_TYPES.BooleanType, 'ui-plugin-query-builder.options.true'], + ['false', DATA_TYPES.BooleanType, 'ui-plugin-query-builder.options.false'], + + ['2024-01-01T12:30:00Z', DATA_TYPES.StringType, '2024-01-01T12:30:00Z'], + ['2024-01-01T12:30:00Z', DATA_TYPES.DateType, '1/1/2024'], + ['2024-01-01T12:30:00Z', DATA_TYPES.DateType, '1/1/2024'], + + [[], DATA_TYPES.ArrayType, ''], + [['a'], DATA_TYPES.ArrayType, 'a'], + [['a', 'b', 'c'], DATA_TYPES.ArrayType, 'a | b | c'], + + [1234, DATA_TYPES.IntegerType, '1234'], + [12.34, DATA_TYPES.NumberType, '12.34'], + + [false, DATA_TYPES.StringType, ''], + ['foo', DATA_TYPES.StringType, 'foo'], + ])('value=%j of type=%p renders to %p', (value, type, expected) => { + const formatted = formatValueByDataType(value, type, null); + + if (typeof formatted === 'string') { + expect(formatted).toBe(expected); + } else { + expect(render({formatted}).container.innerHTML).toBe(expected); + } + }); +});