diff --git a/src/utils/domain.test.ts b/src/utils/domain.test.ts index bcbab252a5..d505a3e260 100644 --- a/src/utils/domain.test.ts +++ b/src/utils/domain.test.ts @@ -7,6 +7,17 @@ import { } from './domain'; describe('utils/domain', () => { + test('should return [0] domain if no data', () => { + const data: any[] = []; + const accessor: AccessorFn = (datum: any) => datum.x; + const isSorted = true; + const removeNull = true; + + const ordinalDataDomain = computeOrdinalDataDomain(data, accessor, isSorted, removeNull); + + expect(ordinalDataDomain).toEqual([0]); + }); + test('should compute ordinal data domain: sort & remove nulls', () => { const data = [{ x: 'd' }, { x: 'a' }, { x: null }, { x: 'b' }]; const accessor: AccessorFn = (datum: any) => datum.x; diff --git a/src/utils/domain.ts b/src/utils/domain.ts index f4d9b5c6bc..c71224b15f 100644 --- a/src/utils/domain.ts +++ b/src/utils/domain.ts @@ -36,6 +36,11 @@ export function computeOrdinalDataDomain( sorted?: boolean, removeNull?: boolean, ): string[] | number[] { + // TODO: Check for empty data before computing domain + if (data.length === 0) { + return [0]; + } + const domain = data.map(accessor).filter((d) => (removeNull ? d !== null : true)); const uniqueValues = [...new Set(domain)]; return sorted