-
Notifications
You must be signed in to change notification settings - Fork 356
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
1 parent
7caf18a
commit 0560939
Showing
3 changed files
with
76 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |