Skip to content

Commit

Permalink
feat: #652 split into smaller functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Cuong Vu committed Mar 30, 2020
1 parent 104b7cd commit db2f956
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import {
validatedDataGenerate,
parseCsvFile,
convertToCompatibleData,
createDataWithInvalidRowsRemoved,
} from '../utils'

const onDoubleClickDefault = jest.fn()
Expand Down Expand Up @@ -139,15 +140,18 @@ jest.mock('../utils', () => {
unparseDataToCsvString: jest.fn().mockReturnValue('unparse data'),
validatedDataGenerate: jest.fn().mockReturnValue('validated data'),
changedCellsGenerate: jest.fn().mockReturnValue('changes'),
createDataWithInvalidRowsRemoved: jest.fn().mockReturnValue([...data[0], ...data[1]]),
}
})

const validate = jest.fn(() => [
const validateMatrix = [
[true, true, true, true, true, true, true, true, true, true, true],
[true, true, true, true, true, true, true, true, true, true, true],
[true, true, true, true, true, true, true, true, true, true, false],
[true, true, true, true, true, true, true, true, true, true, false],
])
]

const validate = jest.fn(() => validateMatrix)

afterEach(() => {
jest.clearAllMocks()
Expand Down Expand Up @@ -490,6 +494,10 @@ describe('handleCellsChanged', () => {
})

describe('handleOnChangeInput', () => {
afterAll(() => {
;(createDataWithInvalidRowsRemoved as jest.Mocked<any>).mockReturnValue([...data[0], ...data[1]])
})

it('should return correctly when dont have validation function', async () => {
const fn = handleOnChangeInput({ maxUploadRow: 1, setUploadData, validate: undefined })
const eventMock: any = {
Expand All @@ -505,7 +513,7 @@ describe('handleOnChangeInput', () => {
})

it('should return correctly when have validation function', async () => {
const fn = handleOnChangeInput({ maxUploadRow: 30, setUploadData, validate })
const fn = handleOnChangeInput({ maxUploadRow: 3, setUploadData, validate })
const eventMock: any = {
target: {
files: ['data'],
Expand All @@ -514,7 +522,8 @@ describe('handleOnChangeInput', () => {
const result = await fn(eventMock)
expect(parseCsvFile).toHaveBeenCalledWith('data')
expect(convertToCompatibleData).toHaveBeenCalledWith(parseResult)
expect(validate).toHaveBeenCalledWith(data)
expect(validate).toHaveBeenCalledWith(data.slice(0, 3))
expect(createDataWithInvalidRowsRemoved).toHaveBeenCalledWith(data.slice(0, 3), validateMatrix)
expect(setUploadData).toHaveBeenCalled()
expect(result).toBe('validated')
})
Expand All @@ -531,6 +540,9 @@ describe('handleOnChangeInput', () => {
})

it('should return correctly when error', async () => {
;(createDataWithInvalidRowsRemoved as jest.Mocked<any>).mockImplementation(() => {
throw new Error('error1')
})
const fn = handleOnChangeInput({ maxUploadRow: 30, setUploadData, validate: () => [[true]] })
const eventMock: any = {
target: {
Expand Down
84 changes: 84 additions & 0 deletions packages/elements/src/components/Spreadsheet/__tests__/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
changedCellsGenerate,
validatedDataGenerate,
calculateNumberOfInvalidRows,
createDataWithInvalidRowsRemoved,
} from '../utils'
import fs from 'fs'
import path from 'path'
Expand Down Expand Up @@ -166,3 +167,86 @@ describe('calculateNumberOfInvalidRows', () => {
expect(result).toBe(2)
})
})
describe('calculateNumberOfInvalidRows', () => {
it('should return correctly', () => {
const data = [
[
{ value: 'Office name' },
{ value: 'Building Name' },
{ value: 'Building No.' },
{ value: 'Address 1' },
{ value: 'Address 2' },
{ value: 'Address 3' },
{ value: 'Address 4' },
{ value: 'Post Code' },
{ value: 'Telephone' },
{ value: 'Fax' },
{ value: 'Email' },
],
[
{ value: 'London' },
{ value: 'The White House' },
{ value: '15' },
{ value: 'London 1' },
{ value: '' },
{ value: 'Londom 3' },
{ value: '' },
{ value: 'EC12NH' },
{ value: '0845 0000' },
{ value: '' },
{ value: '[email protected]' },
],
[
{ value: 'London2' },
{ value: 'The Black House' },
{ value: '11' },
{ value: 'Test Addres' },
{ value: '' },
{ value: 'Adress 3' },
{ value: '' },
{ value: 'EC12NH' },
{ value: '087 471 929' },
{ value: '' },
{ value: '[email protected]' },
],
]
const validateMatrix = [
[true, true, true, true, true, true, true, true, true, true, false],
[true, true, true, true, true, true, true, true, true, true, true],
[true, true, true, true, true, true, true, true, true, true, true],
]
const result = createDataWithInvalidRowsRemoved(data, validateMatrix)
const expectedResult = {
dataWithInvalidRowsRemoved: [
[
{ value: 'London', isValidated: true },
{ value: 'The White House', isValidated: true },
{ value: '15', isValidated: true },
{ value: 'London 1', isValidated: true },
{ value: '', isValidated: true },
{ value: 'Londom 3', isValidated: true },
{ value: '', isValidated: true },
{ value: 'EC12NH', isValidated: true },
{ value: '0845 0000', isValidated: true },
{ value: '', isValidated: true },
{ value: '[email protected]', isValidated: true },
],
[
{ value: 'London2', isValidated: true },
{ value: 'The Black House', isValidated: true },
{ value: '11', isValidated: true },
{ value: 'Test Addres', isValidated: true },
{ value: '', isValidated: true },
{ value: 'Adress 3', isValidated: true },
{ value: '', isValidated: true },
{ value: 'EC12NH', isValidated: true },
{ value: '087 471 929', isValidated: true },
{ value: '', isValidated: true },
{ value: '[email protected]', isValidated: true },
],
],
invalidIndies: [{ row: 0, col: 10, cell: { value: 'Email', isValidated: false } }],
}
expect(result).toEqual(expectedResult)
})
})
29 changes: 5 additions & 24 deletions packages/elements/src/components/Spreadsheet/handlers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {
ChangesArray,
SetUploadData,
UploadData,
InvalidIndies,
} from './types'
import {
getMaxRowAndCol,
Expand All @@ -24,6 +23,7 @@ import {
convertDataToCsv,
changedCellsGenerate,
validatedDataGenerate,
createDataWithInvalidRowsRemoved,
} from './utils'

export const valueRenderer = (cell: Cell): string | null => cell.value
Expand Down Expand Up @@ -250,29 +250,10 @@ export const handleOnChangeInput = ({
)
return 'not validated'
}
// remove all invalid rows
let dataWithInvalidRowsRemoved: Cell[][] = []
// store row, col, cell of invalid rows
let invalidIndies: InvalidIndies = []
const validateMatrix = validate(compatibleData)
// loop through, validate each cell
// if invalid cell, push into invalidIndies
// only push into dataWithInvalidRowsRemoved if all cells in that row are valid
slicedData.forEach((row, rowIndex) => {
let currentRowValid = true
const currentRow = [...row]
currentRow.forEach((cell, colIndex) => {
currentRow[colIndex] = { ...currentRow[colIndex], isValidated: validateMatrix[rowIndex][colIndex] }
if (!currentRow[colIndex].isValidated) {
invalidIndies.push({ row: rowIndex, col: colIndex, cell: currentRow[colIndex] })
currentRowValid = false
return
}
})
if (currentRowValid) {
dataWithInvalidRowsRemoved.push(currentRow)
}
})

const validateMatrix = validate(slicedData)

const { dataWithInvalidRowsRemoved, invalidIndies } = createDataWithInvalidRowsRemoved(slicedData, validateMatrix)

setUploadData(
setUploadDataCallback({
Expand Down
31 changes: 31 additions & 0 deletions packages/elements/src/components/Spreadsheet/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,34 @@ export const calculateNumberOfInvalidRows = (invalidIndies: InvalidIndies): numb
})
return Object.keys(hashMap).length
}

/**
* Remove all rows which include at least one invalid row
*/
export const createDataWithInvalidRowsRemoved = (
data: Cell[][],
validateMatrix: boolean[][],
): { dataWithInvalidRowsRemoved: Cell[][]; invalidIndies: InvalidIndies } => {
let dataWithInvalidRowsRemoved: Cell[][] = []
// store row, col, cell of invalid rows
let invalidIndies: InvalidIndies = []
// loop through, check validate each cell
// if invalid cell, push into invalidIndies
// only push into dataWithInvalidRowsRemoved if all cells in that row are valid
data.forEach((row, rowIndex) => {
let currentRowValid = true
const currentRow = [...row]
currentRow.forEach((cell, colIndex) => {
currentRow[colIndex] = { ...currentRow[colIndex], isValidated: validateMatrix[rowIndex][colIndex] }
if (!currentRow[colIndex].isValidated) {
invalidIndies.push({ row: rowIndex, col: colIndex, cell: currentRow[colIndex] })
currentRowValid = false
return
}
})
if (currentRowValid) {
dataWithInvalidRowsRemoved.push(currentRow)
}
})
return { dataWithInvalidRowsRemoved, invalidIndies }
}
2 changes: 1 addition & 1 deletion packages/elements/src/tests/badges/badge-functions.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion packages/elements/src/tests/badges/badge-lines.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion packages/elements/src/tests/badges/badge-statements.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit db2f956

Please sign in to comment.