Skip to content

Commit

Permalink
fix: bring in handleEmptyCell from #9963 (#9984)
Browse files Browse the repository at this point in the history
For 0.37.0 release only. Resolves a missing dependency from a branch that wasn't cherry picked for the release.
  • Loading branch information
jesse-amano-hpe authored Sep 24, 2024
1 parent 7caf18a commit 0560939
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 0 deletions.
1 change: 1 addition & 0 deletions webui/react/src/pages/FlatRuns/columns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { DURATION_UNIT_MEASURES, durationInEnglish, getTimeInEnglish } from 'uti
import { humanReadableNumber } from 'utils/number';
import { AnyMouseEvent } from 'utils/routes';
import { capitalize, floatToPercent, humanReadableBytes } from 'utils/string';
import { handleEmptyCell } from 'utils/table';
import { getDisplayName } from 'utils/user';

// order used in ColumnPickerMenu
Expand Down
57 changes: 57 additions & 0 deletions webui/react/src/utils/table.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { GridCell, GridCellKind } from '@glideapps/glide-data-grid';
import fc from 'fast-check';

import { EMPTY_CELL, handleEmptyCell } from './table';

const generateGridCell = (value: unknown): GridCell => {
return {
allowOverlay: false,
data: String(value),
displayData: String(value),
kind: GridCellKind.Text,
};
};

describe('Table Utilities', () => {
describe('handleEmptyCell', () => {
it('should return passed cell for any string value', () => {
fc.assert(
fc.property(fc.string(), (value) => {
expect(handleEmptyCell(value, (data) => generateGridCell(data))).toEqual(
generateGridCell(value),
);
}),
);
});
it('should return EMPTY_CELL for undefined value', () => {
const value = undefined;
expect(handleEmptyCell(value, (data) => generateGridCell(data))).not.toEqual(
generateGridCell(value),
);
expect(handleEmptyCell(value, (data) => generateGridCell(data))).toEqual(EMPTY_CELL);
});
it('should return EMPTY_CELL for null value', () => {
const value = null;
expect(handleEmptyCell(value, (data) => generateGridCell(data))).not.toEqual(
generateGridCell(value),
);
expect(handleEmptyCell(value, (data) => generateGridCell(data))).toEqual(EMPTY_CELL);
});
it('should return passed cell for any non-empty string value when allowFalsy is false', () => {
fc.assert(
fc.property(fc.string({ minLength: 1 }), (value) => {
expect(handleEmptyCell(value, (data) => generateGridCell(data))).toEqual(
generateGridCell(value),
);
}),
);
});
it('should return EMPTY_CELL for empty string value when allowFalsy is false', () => {
const value = '';
expect(handleEmptyCell(value, (data) => generateGridCell(data), false)).not.toEqual(
generateGridCell(value),
);
expect(handleEmptyCell(value, (data) => generateGridCell(data), false)).toEqual(EMPTY_CELL);
});
});
});
18 changes: 18 additions & 0 deletions webui/react/src/utils/table.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { GridCell, GridCellKind } from '@glideapps/glide-data-grid';

export const EMPTY_CELL: GridCell = {
allowOverlay: false,
data: '-',
displayData: '-',
kind: GridCellKind.Text,
} as const;

export const handleEmptyCell = <T>(
uncertainData: T | undefined,
cell: (data: T) => GridCell,
allowFalsy = true, // if allowFalsy === false, then falsy values such as 0 or the empty string will be treated as empty
): GridCell => {
if (uncertainData === undefined || uncertainData === null) return EMPTY_CELL;
if (allowFalsy === false && !uncertainData) return EMPTY_CELL;
return cell(uncertainData);
};

0 comments on commit 0560939

Please sign in to comment.