From 5ef7975475f19f8acc0c86fe81876bec4aa0a43b Mon Sep 17 00:00:00 2001 From: reesporte Date: Thu, 28 Oct 2021 15:15:43 -0500 Subject: [PATCH] refactor and add tests --- lattice/src/shared/DataTable/DataTable.tsx | 29 +-------- .../src/shared/utils/formatTableCell.test.tsx | 59 +++++++++++++++++++ lattice/src/shared/utils/formatTableCell.tsx | 26 ++++++++ 3 files changed, 88 insertions(+), 26 deletions(-) create mode 100644 lattice/src/shared/utils/formatTableCell.test.tsx create mode 100644 lattice/src/shared/utils/formatTableCell.tsx diff --git a/lattice/src/shared/DataTable/DataTable.tsx b/lattice/src/shared/DataTable/DataTable.tsx index e080c1cf4..5a84b8329 100644 --- a/lattice/src/shared/DataTable/DataTable.tsx +++ b/lattice/src/shared/DataTable/DataTable.tsx @@ -12,6 +12,7 @@ import TableRow from '@material-ui/core/TableRow'; import Typography from '@material-ui/core/Typography'; import { ColumnInfo } from 'proto/pilosa_pb'; import { Pager } from 'shared/Pager'; +import { formatTableCell } from 'shared/utils/formatTableCell'; import css from './DataTable.module.scss'; type TableProps = { @@ -70,31 +71,7 @@ export const DataTable: FC = ({ } }; - const formatTableCell = (row: any, col: any) => { - if (typeof row[col.name] === 'object') { - return ( -
-        {JSON.stringify(row[col.name], null, 2)}
-      
) - } else if (row[col.name] !== undefined) { - if (col.datatype === '[]string') { - return ( - - {'"' + (row[col.name]) + '"'} - - ) - } - return ( - - {col.datatype === 'timestamp' && row[col.name] - ? moment - .utc(row[col.name]) - .format('MM/DD/YYYY hh:mm:ss a') - : row[col.name].toLocaleString()} - ) - } - return null - } + return ( @@ -143,7 +120,7 @@ export const DataTable: FC = ({ key={`table-cell-${rowIdx}-${colIdx}`} className={css.tableCell} > - {formatTableCell(row, col)} + {formatTableCell(row, col, css)} ))} {autoWidth ? : null} diff --git a/lattice/src/shared/utils/formatTableCell.test.tsx b/lattice/src/shared/utils/formatTableCell.test.tsx new file mode 100644 index 000000000..9db2b0b84 --- /dev/null +++ b/lattice/src/shared/utils/formatTableCell.test.tsx @@ -0,0 +1,59 @@ +import { formatTableCell } from './formatTableCell'; +import React from "react"; +import { render, unmountComponentAtNode } from "react-dom"; +import { act } from "react-dom/test-utils"; + + +let container = null; +beforeEach(() => { + // setup a DOM element as a render target + container = document.createElement("div"); + document.body.appendChild(container); +}); + +afterEach(() => { + // cleanup on exiting + unmountComponentAtNode(container); + container.remove(); + container = null; +}); + +it("it renders strings in quotes", () => { + let row = {thing:"quoted string!"}; + let col = {name: "thing", datatype: "[]string"}; + // this is just so we can actually test it, the value doesn't matter that much + let css = {preFormat: "preFormat"}; + + act(() => { + render(formatTableCell(row, col, css), container); + }); + expect(container.textContent).toBe('"quoted string!"'); +}); + +it("renders objects as stringified", () => { + let row = {thing:{val:"quoted string!"}}; + let col = {name: "thing", datatype: "object"}; + // this is just so we can actually test it, the value doesn't matter that much + let css = {preFormat: "preFormat"}; + + act(() => { + render(formatTableCell(row, col, css), container); + }); + expect(container.textContent).toBe(`{ + "val": "quoted string!" +}`); +}); + +it("it puts timestamps in MM/DD/YYYY hh:mm:ss a format", () => { + let row = {thing:1635452050094}; + let col = {name: "thing", datatype: "timestamp"}; + // this is just so we can actually test it, the value doesn't matter that much + let css = {preFormat: "preFormat"}; + + act(() => { + render(formatTableCell(row, col, css), container); + }); + expect(container.textContent).toBe("10/28/2021 08:14:10 pm"); +}); + + diff --git a/lattice/src/shared/utils/formatTableCell.tsx b/lattice/src/shared/utils/formatTableCell.tsx new file mode 100644 index 000000000..a8ff9f8c4 --- /dev/null +++ b/lattice/src/shared/utils/formatTableCell.tsx @@ -0,0 +1,26 @@ +import moment from 'moment'; +export const formatTableCell = (row: any, col: any, css: any) => { + if (typeof row[col.name] === 'object') { + return ( +
+        {JSON.stringify(row[col.name], null, 2)}
+      
) + } else if (row[col.name] !== undefined) { + if (col.datatype === '[]string') { + return ( + + {'"' + (row[col.name]) + '"'} + + ) + } + return ( + + {col.datatype === 'timestamp' && row[col.name] + ? moment + .utc(row[col.name]) + .format('MM/DD/YYYY hh:mm:ss a') + : row[col.name].toLocaleString()} + ) + } + return null +}