From 7ef4e538f5611c87f33f52d4c2779c043503375a Mon Sep 17 00:00:00 2001 From: nickofthyme Date: Fri, 5 Feb 2021 11:09:03 -0600 Subject: [PATCH] fix: broken unit tests and types --- .../xy_chart/utils/axis_utils.test.ts | 77 +++++++++++++------ src/utils/domain.test.ts | 37 +++++---- 2 files changed, 72 insertions(+), 42 deletions(-) diff --git a/src/chart_types/xy_chart/utils/axis_utils.test.ts b/src/chart_types/xy_chart/utils/axis_utils.test.ts index 589e5c2e17..d044e58545 100644 --- a/src/chart_types/xy_chart/utils/axis_utils.test.ts +++ b/src/chart_types/xy_chart/utils/axis_utils.test.ts @@ -25,6 +25,7 @@ import { MockGlobalSpec, MockSeriesSpec } from '../../../mocks/specs/specs'; import { MockStore } from '../../../mocks/store/store'; import { Scale } from '../../../scales'; import { ScaleType } from '../../../scales/constants'; +import { LogBase } from '../../../scales/scale_continuous'; import { SpecTypes } from '../../../specs/constants'; import { CanvasTextBBoxCalculator } from '../../../utils/bbox/canvas_text_bbox_calculator'; import { SvgTextBBoxCalculator } from '../../../utils/bbox/svg_text_bbox_calculator'; @@ -222,7 +223,7 @@ describe('Axis computational utils', () => { [yDomain], 1, bboxCalculator, - 0, + MockGlobalSpec.settings(), axes, (v) => `${v}`, ); @@ -235,7 +236,7 @@ describe('Axis computational utils', () => { [yDomain], 1, bboxCalculator, - 0, + MockGlobalSpec.settings(), axes, (v) => `${v}`, undefined, @@ -256,7 +257,7 @@ describe('Axis computational utils', () => { [yDomain], 1, bboxCalculator, - 0, + MockGlobalSpec.settings(), axes, (v) => `${v}`, ); @@ -279,7 +280,7 @@ describe('Axis computational utils', () => { [yDomain], 1, bboxCalculator, - 0, + MockGlobalSpec.settings(), axes, (v) => `${v}`, ); @@ -296,7 +297,7 @@ describe('Axis computational utils', () => { [yDomain], 1, bboxCalculator, - 0, + MockGlobalSpec.settings(), axes, (v) => `${v}`, ); @@ -313,7 +314,7 @@ describe('Axis computational utils', () => { [yDomain], 1, bboxCalculator, - 0, + MockGlobalSpec.settings(), axes, (v) => `${v}`, ); @@ -340,7 +341,7 @@ describe('Axis computational utils', () => { }); test('should generate a valid scale', () => { - const yScale = getScaleForAxisSpec(verticalAxisSpec, xDomain, [yDomain], 0, 0, 100, 0); + const yScale = getScaleForAxisSpec(verticalAxisSpec, xDomain, [yDomain], 0, true, 0, 100, LogBase.Common, 0); expect(yScale).toBeDefined(); expect(yScale?.bandwidth).toBe(0); expect(yScale?.domain).toEqual([0, 1]); @@ -348,10 +349,10 @@ describe('Axis computational utils', () => { expect(yScale?.ticks()).toEqual([0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1]); const ungroupedAxisSpec = { ...verticalAxisSpec, groupId: 'foo' }; - const nullYScale = getScaleForAxisSpec(ungroupedAxisSpec, xDomain, [yDomain], 0, 0, 100, 0); + const nullYScale = getScaleForAxisSpec(ungroupedAxisSpec, xDomain, [yDomain], 0, true, 0, 100, LogBase.Common, 0); expect(nullYScale).toBe(null); - const xScale = getScaleForAxisSpec(horizontalAxisSpec, xDomain, [yDomain], 0, 0, 100, 0); + const xScale = getScaleForAxisSpec(horizontalAxisSpec, xDomain, [yDomain], 0, false, 0, 100, LogBase.Common, 0); expect(xScale).toBeDefined(); }); @@ -372,7 +373,7 @@ describe('Axis computational utils', () => { describe('getAvailableTicks', () => { test('should compute to end of domain when histogram mode not enabled', () => { - const scale = getScaleForAxisSpec(verticalAxisSpec, xDomain, [yDomain], 0, 0, 100, 0); + const scale = getScaleForAxisSpec(verticalAxisSpec, xDomain, [yDomain], 0, true, 0, 100, LogBase.Common, 0); const axisPositions = getAvailableTicks(verticalAxisSpec, scale!, 0, false, (v) => `${v}`, 0); const expectedAxisPositions = [ { label: '0', position: 100, value: 0 }, @@ -392,7 +393,7 @@ describe('Axis computational utils', () => { test('should compute positions with rotational offset', () => { const rotationalOffset = 2; - const scale = getScaleForAxisSpec(verticalAxisSpec, xDomain, [yDomain], 0, 0, 100, 0); + const scale = getScaleForAxisSpec(verticalAxisSpec, xDomain, [yDomain], 0, true, 0, 100, LogBase.Common, 0); const axisPositions = getAvailableTicks(verticalAxisSpec, scale!, 0, false, (v) => `${v}`, rotationalOffset); const expectedAxisPositions = [ { label: '0', position: 100 + rotationalOffset, value: 0 }, @@ -419,7 +420,17 @@ describe('Axis computational utils', () => { isBandScale: true, minInterval: 10, }; - const xScale = getScaleForAxisSpec(horizontalAxisSpec, xBandDomain, [yDomain], 1, 0, 100, 0); + const xScale = getScaleForAxisSpec( + horizontalAxisSpec, + xBandDomain, + [yDomain], + 1, + false, + 0, + 100, + LogBase.Common, + 0, + ); const histogramAxisPositions = getAvailableTicks( horizontalAxisSpec, xScale!, @@ -441,7 +452,17 @@ describe('Axis computational utils', () => { isBandScale: true, minInterval: 90000, }; - const xScale = getScaleForAxisSpec(horizontalAxisSpec, xBandDomain, [yDomain], 1, 0, 100, 0); + const xScale = getScaleForAxisSpec( + horizontalAxisSpec, + xBandDomain, + [yDomain], + 1, + false, + 0, + 100, + LogBase.Common, + 0, + ); const histogramAxisPositions = getAvailableTicks( horizontalAxisSpec, xScale!, @@ -480,7 +501,17 @@ describe('Axis computational utils', () => { isBandScale: true, minInterval: 90000, }; - const xScale = getScaleForAxisSpec(horizontalAxisSpec, xBandDomain, [yDomain], 1, 0, 100, 0); + const xScale = getScaleForAxisSpec( + horizontalAxisSpec, + xBandDomain, + [yDomain], + 1, + false, + 0, + 100, + LogBase.Common, + 0, + ); const histogramAxisPositions = getAvailableTicks( horizontalAxisSpec, xScale!, @@ -960,8 +991,6 @@ describe('Axis computational utils', () => { }); test('should compute axis ticks positions with title', () => { - const chartRotation = 0; - // validate assumptions for test expect(verticalAxisSpec.id).toEqual(verticalAxisSpecWTitle.id); @@ -976,7 +1005,7 @@ describe('Axis computational utils', () => { leftMargin: 0, }, LIGHT_THEME, - chartRotation, + MockGlobalSpec.settings(), axisSpecs, axisDims, axesStyles, @@ -1008,7 +1037,7 @@ describe('Axis computational utils', () => { leftMargin: 0, }, LIGHT_THEME, - chartRotation, + MockGlobalSpec.settings(), axisSpecs, axisDims, axesStyles, @@ -1198,8 +1227,6 @@ describe('Axis computational utils', () => { }); test('should not compute axis ticks positions if missaligned specs', () => { - const chartRotation = 0; - const axisSpecs = [verticalAxisSpec]; const axisStyles = new Map(); const axisDims = new Map(); @@ -1211,7 +1238,7 @@ describe('Axis computational utils', () => { leftMargin: 0, }, LIGHT_THEME, - chartRotation, + MockGlobalSpec.settings(), axisSpecs, axisDims, axisStyles, @@ -1284,7 +1311,7 @@ describe('Axis computational utils', () => { leftMargin: 0, }, LIGHT_THEME, - 0, + MockGlobalSpec.settings(), invalidSpecs, axisDims, axisStyles, @@ -1675,7 +1702,7 @@ describe('Axis computational utils', () => { leftMargin: 0, }, LIGHT_THEME, - 0, + MockGlobalSpec.settings(), axisSpecs, axisDims, axesStyles, @@ -1705,7 +1732,7 @@ describe('Axis computational utils', () => { leftMargin: 0, }, LIGHT_THEME, - 0, + MockGlobalSpec.settings(), axisSpecs, axisDims, axesStyles, @@ -1741,7 +1768,7 @@ describe('Axis computational utils', () => { leftMargin: 0, }, LIGHT_THEME, - 0, + MockGlobalSpec.settings(), axisSpecs, axisDims, axesStyles, diff --git a/src/utils/domain.test.ts b/src/utils/domain.test.ts index 0f3a3233cf..27e8600102 100644 --- a/src/utils/domain.test.ts +++ b/src/utils/domain.test.ts @@ -87,7 +87,7 @@ describe('utils/domain', () => { test('should compute continuous data domain: data scaled to extent', () => { const data = [{ x: 12 }, { x: 6 }, { x: 8 }]; const accessor = (datum: any) => datum.x; - const continuousDataDomain = computeContinuousDataDomain(data, accessor, { fit: true }); + const continuousDataDomain = computeContinuousDataDomain(data, accessor, false, { fit: true }); const expectedContinuousDomain = [6, 12]; expect(continuousDataDomain).toEqual(expectedContinuousDomain); @@ -97,7 +97,7 @@ describe('utils/domain', () => { const data = [{ x: 12 }, { x: 6 }, { x: 8 }]; const accessor = (datum: any) => datum.x; - const continuousDataDomain = computeContinuousDataDomain(data, accessor); + const continuousDataDomain = computeContinuousDataDomain(data, accessor, false); const expectedContinuousDomain = [0, 12]; @@ -108,7 +108,7 @@ describe('utils/domain', () => { const data: any[] = []; const accessor = (datum: any) => datum.x; - const continuousDataDomain = computeContinuousDataDomain(data, accessor); + const continuousDataDomain = computeContinuousDataDomain(data, accessor, false); const expectedContinuousDomain = [0, 0]; @@ -117,68 +117,71 @@ describe('utils/domain', () => { describe('YDomainOptions', () => { it('should not effect domain when domain.fit is true', () => { - expect(computeDomainExtent([5, 10], { fit: true })).toEqual([5, 10]); + expect(computeDomainExtent([5, 10], false, { fit: true })).toEqual([5, 10]); }); describe('domain.fit is true', () => { it('should find domain when start & end are positive', () => { - expect(computeDomainExtent([5, 10], { fit: true })).toEqual([5, 10]); + expect(computeDomainExtent([5, 10], false, { fit: true })).toEqual([5, 10]); }); it('should find domain when start & end are negative', () => { - expect(computeDomainExtent([-15, -10], { fit: true })).toEqual([-15, -10]); + expect(computeDomainExtent([-15, -10], false, { fit: true })).toEqual([-15, -10]); }); it('should find domain when start is negative, end is positive', () => { - expect(computeDomainExtent([-15, 10], { fit: true })).toEqual([-15, 10]); + expect(computeDomainExtent([-15, 10], false, { fit: true })).toEqual([-15, 10]); }); }); describe('domain.fit is false', () => { it('should find domain when start & end are positive', () => { - expect(computeDomainExtent([5, 10])).toEqual([0, 10]); + expect(computeDomainExtent([5, 10], false)).toEqual([0, 10]); }); it('should find domain when start & end are negative', () => { - expect(computeDomainExtent([-15, -10])).toEqual([-15, 0]); + expect(computeDomainExtent([-15, -10], false)).toEqual([-15, 0]); }); it('should find domain when start is negative, end is positive', () => { - expect(computeDomainExtent([-15, 10])).toEqual([-15, 10]); + expect(computeDomainExtent([-15, 10], false)).toEqual([-15, 10]); }); }); describe('padding does NOT cause domain to cross zero baseline', () => { it('should get domain from positive domain', () => { - expect(computeDomainExtent([10, 70], { fit: true, padding: 5 })).toEqual([5, 75]); + expect(computeDomainExtent([10, 70], false, { fit: true, padding: 5 })).toEqual([5, 75]); }); it('should get domain from positive & negative domain', () => { - expect(computeDomainExtent([-30, 30], { fit: true, padding: 5 })).toEqual([-35, 35]); + expect(computeDomainExtent([-30, 30], false, { fit: true, padding: 5 })).toEqual([-35, 35]); }); it('should get domain from negative domain', () => { - expect(computeDomainExtent([-70, -10], { fit: true, padding: 5 })).toEqual([-75, -5]); + expect(computeDomainExtent([-70, -10], false, { fit: true, padding: 5 })).toEqual([-75, -5]); }); }); describe('padding caused domain to cross zero baseline', () => { describe('constrainPadding true - default', () => { it('should set min baseline as 0 if original domain is less than zero', () => { - expect(computeDomainExtent([5, 65], { fit: true, padding: 15 })).toEqual([0, 80]); + expect(computeDomainExtent([5, 65], false, { fit: true, padding: 15 })).toEqual([0, 80]); }); it('should set max baseline as 0 if original domain is less than zero', () => { - expect(computeDomainExtent([-65, -5], { fit: true, padding: 15 })).toEqual([-80, 0]); + expect(computeDomainExtent([-65, -5], false, { fit: true, padding: 15 })).toEqual([-80, 0]); }); }); describe('constrainPadding false', () => { it('should allow min past baseline as 0, even if original domain is less than zero', () => { - expect(computeDomainExtent([5, 65], { fit: true, padding: 15, constrainPadding: false })).toEqual([-10, 80]); + expect(computeDomainExtent([5, 65], false, { fit: true, padding: 15, constrainPadding: false })).toEqual([ + -10, + 80, + ]); }); it('should allow max past baseline as 0, even if original domain is less than zero', () => { - expect(computeDomainExtent([-65, -5], { fit: true, padding: 15, constrainPadding: false })).toEqual([ + expect(computeDomainExtent([-65, -5], false, { fit: true, padding: 15, constrainPadding: false })).toEqual([ -80, 10, ]);