From 988d256873411632239bf10f0b87b62d3d00fb9b Mon Sep 17 00:00:00 2001 From: root Date: Tue, 19 May 2020 20:59:29 -0500 Subject: [PATCH] feat: rows can be set later to a table --- core/entities/Table.js | 54 ++++++++++++++--------------- tests/core/tableTests.js | 75 ++++++++++++++++++++++++++++++---------- 2 files changed, 82 insertions(+), 47 deletions(-) diff --git a/core/entities/Table.js b/core/entities/Table.js index 58442c2..6386e52 100644 --- a/core/entities/Table.js +++ b/core/entities/Table.js @@ -1,13 +1,8 @@ class Table { constructor (props) { const validatePropsResponse = this._validateConstructionProps(props) - if (validatePropsResponse.status === 'ERR') { - this.isValid = false - throw validatePropsResponse - } - else { - this._assignProps(props) - } + if (validatePropsResponse.status === 'ERR') throw validatePropsResponse + else this._assignProps(props) } getProperties = () => { @@ -22,14 +17,22 @@ class Table { getRows = () => this.rows + setRows = rows => { + const rowsValidation = this._validateRows(rows) + if (rowsValidation.status === 'ERR') throw rowsValidation + + if (!Array.isArray(rows)) this.rows = [rows] + else this.rows = rows + } + _assignProps = props => { this.id = props.id this.label = props.label this.type = 'Table' this.isValid = true - if (!Array.isArray(props.rows)) this.rows = [props.rows] - else this.rows = props.rows + if (props.rows) this.setRows(props.rows) + else this.rows = [] } _validateConstructionProps = props => { @@ -47,39 +50,34 @@ class Table { } if (!props.id) err.error.messages.push('No id on creation of Table') if (!props.label) err.error.messages.push('No label on creation of Table') - - const validateRowsErrors = this._validateRows(props.rows) - if (validateRowsErrors.length > 0) { - validateRowsErrors.forEach(e => { - err.error.messages.push(e) - }) - } - if (err.error.messages.length === 0){ - return { status: 'OK' } - } else{ - return err - } + if (err.error.messages.length === 0) return { status: 'OK' } + else return err } _validateRows = rowsToImport => { + const err = { + status: 'ERR', + error: { + label: 'Error Creating Table', + messages: [] + } + } + let rows = [] if (!Array.isArray(rowsToImport)) rows = [rowsToImport] else rows = rowsToImport - const errorMesages = [] - - if (rows.length === 0) { - errorMesages.push('No Tables imported') - } + if (rows.length === 0) err.error.messages.push('No Tables imported') for (let r = 0; r < rows.length; r++) { if (typeof rows[r] !== 'object') { - errorMesages.push(`row[${r}] is not an object`) + err.error.messages.push(`row[${r}] is not an object`) } } - return errorMesages + if (err.error.messages.length > 0) return err + else return { status: 'OK' } } } diff --git a/tests/core/tableTests.js b/tests/core/tableTests.js index 97866a2..0a9bc07 100644 --- a/tests/core/tableTests.js +++ b/tests/core/tableTests.js @@ -27,7 +27,8 @@ const getTableProperties = () => { if (JSON.stringify(tableProperties) === JSON.stringify(expectedOutput)) return true else return false } catch (err) { - return err + console.log(err) + return false } } @@ -43,7 +44,8 @@ const getTableRows = () => { if (JSON.stringify(tableRows) === JSON.stringify(expectedOutput)) return true else return false } catch (err) { - return err + console.log(err) + return false } } @@ -72,8 +74,7 @@ const createTableWithOnlyId = () => { error: { label: 'Error Creating Table', messages: [ - 'No label on creation of Table', - 'row[0] is not an object' + 'No label on creation of Table' ] } } @@ -94,8 +95,7 @@ const createTableWithOnlyLabel = () => { error: { label: 'Error Creating Table', messages: [ - 'No id on creation of Table', - 'row[0] is not an object' + 'No id on creation of Table' ] } } @@ -112,21 +112,21 @@ const createTableWithOnlyLabel = () => { const createTableWithEverythingButRows = () => { const input = { id: 'abc', label: 'Test Label' } const expectedOutput = { - status: 'ERR', - error: { - label: 'Error Creating Table', - messages: [ - 'row[0] is not an object' - ] - } + id: 'abc', + label: 'Test Label', + rows: [], + type: 'Table', + isValid: true } try { - new Table(input) - return false - } catch (err) { - if (JSON.stringify(expectedOutput) === JSON.stringify(err)) return true + const table = new Table(input) + const tableProps = table.getProperties() + if (JSON.stringify(expectedOutput) === JSON.stringify(tableProps)) return true else return false + } catch (err) { + console.log(err) + return false } } @@ -181,7 +181,43 @@ const createTableWithRowsAsNotArray = () => { if (JSON.stringify(tableProperties) === JSON.stringify(expectedOutput)) return true else return false } catch (err) { - return err + console.log(err) + return false + } +} + +const setTableRows = () => { + + const initialInput = { + id: 'abc', + label: 'Test Label' + } + + const rows = [ + { id: '2345676', type: 'row', lat: 54, long: 31 }, + { id: '2345676', type: 'lh', lat: 31, long: -71.34 } + ] + + const expectedOutput = { + id: 'abc', + label: 'Test Label', + rows: [ + { id: '2345676', type: 'row', lat: 54, long: 31 }, + { id: '2345676', type: 'lh', lat: 31, long: -71.34 } + ], + type: 'Table', + isValid: true + } + + try { + const table = new Table(initialInput) + table.setRows(rows) + const tableProperties = table.getProperties() + if (JSON.stringify(tableProperties) === JSON.stringify(expectedOutput)) return true + else return false + } catch (err) { + console.log(err) + return false } } @@ -193,5 +229,6 @@ export default [ { name: 'Entiry | Table With Only Label', test: createTableWithOnlyLabel }, { name: 'Entiry | Table With Everything But Rows', test: createTableWithEverythingButRows }, { name: 'Entiry | Table With Invalid Rows', test: createTableWithInvalidRows }, - { name: 'Entiry | Table With Rows as Not Array', test: createTableWithRowsAsNotArray } + { name: 'Entiry | Table With Rows as Not Array', test: createTableWithRowsAsNotArray }, + { name: 'Entiry | Set Table Rows', test: setTableRows } ] \ No newline at end of file