feat: rows can be set later to a table
This commit is contained in:
		
							parent
							
								
									10b7447f7d
								
							
						
					
					
						commit
						988d256873
					
				@ -1,13 +1,8 @@
 | 
				
			|||||||
class Table {
 | 
					class Table {
 | 
				
			||||||
  constructor (props) {
 | 
					  constructor (props) {
 | 
				
			||||||
    const validatePropsResponse = this._validateConstructionProps(props)
 | 
					    const validatePropsResponse = this._validateConstructionProps(props)
 | 
				
			||||||
    if (validatePropsResponse.status === 'ERR') {
 | 
					    if (validatePropsResponse.status === 'ERR') throw validatePropsResponse
 | 
				
			||||||
      this.isValid = false
 | 
					    else this._assignProps(props)
 | 
				
			||||||
      throw validatePropsResponse
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
    else {
 | 
					 | 
				
			||||||
      this._assignProps(props)
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  getProperties = () => {
 | 
					  getProperties = () => {
 | 
				
			||||||
@ -22,14 +17,22 @@ class Table {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
  getRows = () => this.rows
 | 
					  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 => {
 | 
					  _assignProps = props => {
 | 
				
			||||||
    this.id = props.id
 | 
					    this.id = props.id
 | 
				
			||||||
    this.label = props.label
 | 
					    this.label = props.label
 | 
				
			||||||
    this.type = 'Table' 
 | 
					    this.type = 'Table' 
 | 
				
			||||||
    this.isValid = true
 | 
					    this.isValid = true
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (!Array.isArray(props.rows)) this.rows = [props.rows]
 | 
					    if (props.rows) this.setRows(props.rows)
 | 
				
			||||||
    else this.rows = props.rows
 | 
					    else this.rows = []
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  _validateConstructionProps = props => {
 | 
					  _validateConstructionProps = props => {
 | 
				
			||||||
@ -48,38 +51,33 @@ class Table {
 | 
				
			|||||||
    if (!props.id) err.error.messages.push('No id on creation of 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')
 | 
					    if (!props.label) err.error.messages.push('No label on creation of Table')
 | 
				
			||||||
  
 | 
					  
 | 
				
			||||||
    const validateRowsErrors = this._validateRows(props.rows)
 | 
					    if (err.error.messages.length === 0) return { status: 'OK' }
 | 
				
			||||||
    if (validateRowsErrors.length > 0) {
 | 
					    else return err
 | 
				
			||||||
      validateRowsErrors.forEach(e => {
 | 
					 | 
				
			||||||
        err.error.messages.push(e)
 | 
					 | 
				
			||||||
      })
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
  
 | 
					 | 
				
			||||||
    if (err.error.messages.length === 0){
 | 
					 | 
				
			||||||
      return { status: 'OK' }
 | 
					 | 
				
			||||||
    } else{
 | 
					 | 
				
			||||||
      return err
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  _validateRows = rowsToImport => {
 | 
					  _validateRows = rowsToImport => {
 | 
				
			||||||
 | 
					    const err = {
 | 
				
			||||||
 | 
					      status: 'ERR',
 | 
				
			||||||
 | 
					      error: {
 | 
				
			||||||
 | 
					        label: 'Error Creating Table',
 | 
				
			||||||
 | 
					        messages: []
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    let rows = []
 | 
					    let rows = []
 | 
				
			||||||
    if (!Array.isArray(rowsToImport)) rows = [rowsToImport]
 | 
					    if (!Array.isArray(rowsToImport)) rows = [rowsToImport]
 | 
				
			||||||
    else rows = rowsToImport
 | 
					    else rows = rowsToImport
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    const errorMesages = []
 | 
					    if (rows.length === 0) err.error.messages.push('No Tables imported')
 | 
				
			||||||
 | 
					 | 
				
			||||||
    if (rows.length === 0) {
 | 
					 | 
				
			||||||
      errorMesages.push('No Tables imported')
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    for (let r = 0; r < rows.length; r++) {
 | 
					    for (let r = 0; r < rows.length; r++) {
 | 
				
			||||||
      if (typeof rows[r] !== 'object') {
 | 
					      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' }
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -27,7 +27,8 @@ const getTableProperties = () => {
 | 
				
			|||||||
    if (JSON.stringify(tableProperties) === JSON.stringify(expectedOutput)) return true
 | 
					    if (JSON.stringify(tableProperties) === JSON.stringify(expectedOutput)) return true
 | 
				
			||||||
    else return false
 | 
					    else return false
 | 
				
			||||||
  } catch (err) {
 | 
					  } catch (err) {
 | 
				
			||||||
    return err
 | 
					    console.log(err)
 | 
				
			||||||
 | 
					    return false
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -43,7 +44,8 @@ const getTableRows = () => {
 | 
				
			|||||||
    if (JSON.stringify(tableRows) === JSON.stringify(expectedOutput)) return true
 | 
					    if (JSON.stringify(tableRows) === JSON.stringify(expectedOutput)) return true
 | 
				
			||||||
    else return false
 | 
					    else return false
 | 
				
			||||||
  } catch (err) {
 | 
					  } catch (err) {
 | 
				
			||||||
    return err
 | 
					    console.log(err)
 | 
				
			||||||
 | 
					    return false
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -72,8 +74,7 @@ const createTableWithOnlyId = () => {
 | 
				
			|||||||
    error: {
 | 
					    error: {
 | 
				
			||||||
      label: 'Error Creating Table',
 | 
					      label: 'Error Creating Table',
 | 
				
			||||||
      messages: [
 | 
					      messages: [
 | 
				
			||||||
        'No label on creation of Table',
 | 
					        'No label on creation of Table'
 | 
				
			||||||
        'row[0] is not an object'
 | 
					 | 
				
			||||||
      ]
 | 
					      ]
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
@ -94,8 +95,7 @@ const createTableWithOnlyLabel = () => {
 | 
				
			|||||||
    error: {
 | 
					    error: {
 | 
				
			||||||
      label: 'Error Creating Table',
 | 
					      label: 'Error Creating Table',
 | 
				
			||||||
      messages: [
 | 
					      messages: [
 | 
				
			||||||
        'No id on creation of Table',
 | 
					        'No id on creation of Table'
 | 
				
			||||||
        'row[0] is not an object'
 | 
					 | 
				
			||||||
      ]
 | 
					      ]
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
@ -112,21 +112,21 @@ const createTableWithOnlyLabel = () => {
 | 
				
			|||||||
const createTableWithEverythingButRows = () => {
 | 
					const createTableWithEverythingButRows = () => {
 | 
				
			||||||
  const input = { id: 'abc', label: 'Test Label' }
 | 
					  const input = { id: 'abc', label: 'Test Label' }
 | 
				
			||||||
  const expectedOutput = {
 | 
					  const expectedOutput = {
 | 
				
			||||||
    status: 'ERR',
 | 
					    id: 'abc',
 | 
				
			||||||
    error: {
 | 
					    label: 'Test Label',
 | 
				
			||||||
      label: 'Error Creating Table',
 | 
					    rows: [],
 | 
				
			||||||
      messages: [
 | 
					    type: 'Table',
 | 
				
			||||||
        'row[0] is not an object'
 | 
					    isValid: true
 | 
				
			||||||
      ]
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  try {
 | 
					  try {
 | 
				
			||||||
     new Table(input)
 | 
					    const table = new Table(input)
 | 
				
			||||||
     return false
 | 
					    const tableProps = table.getProperties()
 | 
				
			||||||
  } catch (err) {
 | 
					    if (JSON.stringify(expectedOutput) === JSON.stringify(tableProps)) return true
 | 
				
			||||||
    if (JSON.stringify(expectedOutput) === JSON.stringify(err)) return true
 | 
					 | 
				
			||||||
    else return false
 | 
					    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
 | 
					    if (JSON.stringify(tableProperties) === JSON.stringify(expectedOutput)) return true
 | 
				
			||||||
    else return false
 | 
					    else return false
 | 
				
			||||||
  } catch (err) {
 | 
					  } 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 Only Label', test: createTableWithOnlyLabel },
 | 
				
			||||||
  { name: 'Entiry | Table With Everything But Rows', test: createTableWithEverythingButRows },
 | 
					  { name: 'Entiry | Table With Everything But Rows', test: createTableWithEverythingButRows },
 | 
				
			||||||
  { name: 'Entiry | Table With Invalid Rows', test: createTableWithInvalidRows },
 | 
					  { 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 }
 | 
				
			||||||
]
 | 
					]
 | 
				
			||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user