Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[UIPQB-145] Display raw booleans in nested table #166

Merged
merged 5 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions src/QueryBuilder/ResultViewer/DynamicTable/DynamicTable.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
import React, { useMemo } from 'react';
import PropTypes from 'prop-types';
import React, { useMemo } from 'react';
import { FormattedMessage } from 'react-intl';
import { DATA_TYPES } from '../../../constants/dataTypes';
import css from './DynamicTable.css';

const columnStyle = { width: '180px', minWidth: '180px' };

function getCellValue(row, property) {
// typeof check to ensure we don't try to display null/undefined as a booleans
if (property.dataType.dataType === DATA_TYPES.BooleanType && typeof row[property.property] === 'boolean') {
return row[property.property]
? <FormattedMessage id="ui-plugin-query-builder.options.true" />
: <FormattedMessage id="ui-plugin-query-builder.options.false" />;
}

return row[property.property];
}

export const DynamicTable = ({ properties, values }) => {
const tableBodyRows = useMemo(() => JSON.parse(values ?? '[]'), [values]);

Expand All @@ -27,7 +40,7 @@ export const DynamicTable = ({ properties, values }) => {
<tr key={index}>
{properties?.map((cell) => (
<td key={cell.property} style={columnStyle}>
{row[cell.property]}
{getCellValue(row, cell)}
</td>
))}
</tr>
Expand Down
62 changes: 47 additions & 15 deletions src/QueryBuilder/ResultViewer/DynamicTable/DynamicTable.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,30 @@ describe('DynamicTable component', () => {
labelAlias: 'Value',
property: 'value',
},
{
name: 'is_cool',
dataType: {
dataType: 'booleanType',
},
labelAlias: 'Is cool',
property: 'isCool',
},
{
name: 'is_not_cool',
dataType: {
dataType: 'booleanType',
},
labelAlias: 'Is not cool',
property: 'isNotCool',
},
{
name: 'is_empty_bool',
dataType: {
dataType: 'booleanType',
},
labelAlias: 'Empty bool column',
property: 'isEmptyBool',
},
];

it.each(['[]', undefined, null])(
Expand All @@ -55,8 +79,19 @@ describe('DynamicTable component', () => {
},
);

const values =
'[{"code": "STATE-MONOSER", "value": 100.0, "fundId": "bbd4a5e1-c9f3-44b9-bfdf-d184e04f0ba0", "encumbrance": "eb506834-6c70-4239-8d1a-6414a5b08010", "distributionType": "percentage"}]';
const values = `
[
{
"code": "STATE-MONOSER",
"value": 100.0,
"fundId": "bbd4a5e1-c9f3-44b9-bfdf-d184e04f0ba0",
"encumbrance": "eb506834-6c70-4239-8d1a-6414a5b08010",
"distributionType": "percentage",
"isCool": true,
"isNotCool": false,
"isEmptyBool": null
}
]`;

it('renders table with correct properties and values', () => {
const { getByText } = render(<DynamicTable properties={properties} values={values} />);
Expand All @@ -67,20 +102,17 @@ describe('DynamicTable component', () => {
expect(label).toBeInTheDocument();
});

const codeValue = getByText('STATE-MONOSER');

expect(codeValue).toBeInTheDocument();

const encumbranceValue = getByText('eb506834-6c70-4239-8d1a-6414a5b08010');

expect(encumbranceValue).toBeInTheDocument();

const fundIdValue = getByText('bbd4a5e1-c9f3-44b9-bfdf-d184e04f0ba0');

expect(fundIdValue).toBeInTheDocument();
expect(getByText('STATE-MONOSER')).toBeInTheDocument();
expect(getByText('eb506834-6c70-4239-8d1a-6414a5b08010')).toBeInTheDocument();
expect(getByText('bbd4a5e1-c9f3-44b9-bfdf-d184e04f0ba0')).toBeInTheDocument();
expect(getByText('100')).toBeInTheDocument();

const numberValue = getByText('100');
// will fail if multiple, to ensure multiple case works
const trueCell = getByText('ui-plugin-query-builder.options.true');
const falseCell = getByText('ui-plugin-query-builder.options.false');

expect(numberValue).toBeInTheDocument();
expect(trueCell).toBeInTheDocument();
expect(falseCell).toBeInTheDocument();
expect(trueCell.compareDocumentPosition(falseCell)).toBe(Node.DOCUMENT_POSITION_FOLLOWING);
});
});
Loading