Skip to content

Commit

Permalink
fix: broken unit tests and types
Browse files Browse the repository at this point in the history
  • Loading branch information
nickofthyme committed Feb 5, 2021
1 parent 42ce439 commit 7ef4e53
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 42 deletions.
77 changes: 52 additions & 25 deletions src/chart_types/xy_chart/utils/axis_utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -222,7 +223,7 @@ describe('Axis computational utils', () => {
[yDomain],
1,
bboxCalculator,
0,
MockGlobalSpec.settings(),
axes,
(v) => `${v}`,
);
Expand All @@ -235,7 +236,7 @@ describe('Axis computational utils', () => {
[yDomain],
1,
bboxCalculator,
0,
MockGlobalSpec.settings(),
axes,
(v) => `${v}`,
undefined,
Expand All @@ -256,7 +257,7 @@ describe('Axis computational utils', () => {
[yDomain],
1,
bboxCalculator,
0,
MockGlobalSpec.settings(),
axes,
(v) => `${v}`,
);
Expand All @@ -279,7 +280,7 @@ describe('Axis computational utils', () => {
[yDomain],
1,
bboxCalculator,
0,
MockGlobalSpec.settings(),
axes,
(v) => `${v}`,
);
Expand All @@ -296,7 +297,7 @@ describe('Axis computational utils', () => {
[yDomain],
1,
bboxCalculator,
0,
MockGlobalSpec.settings(),
axes,
(v) => `${v}`,
);
Expand All @@ -313,7 +314,7 @@ describe('Axis computational utils', () => {
[yDomain],
1,
bboxCalculator,
0,
MockGlobalSpec.settings(),
axes,
(v) => `${v}`,
);
Expand All @@ -340,18 +341,18 @@ 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]);
expect(yScale?.range).toEqual([100, 0]);
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();
});

Expand All @@ -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 },
Expand All @@ -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 },
Expand All @@ -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!,
Expand All @@ -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!,
Expand Down Expand Up @@ -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!,
Expand Down Expand Up @@ -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);

Expand All @@ -976,7 +1005,7 @@ describe('Axis computational utils', () => {
leftMargin: 0,
},
LIGHT_THEME,
chartRotation,
MockGlobalSpec.settings(),
axisSpecs,
axisDims,
axesStyles,
Expand Down Expand Up @@ -1008,7 +1037,7 @@ describe('Axis computational utils', () => {
leftMargin: 0,
},
LIGHT_THEME,
chartRotation,
MockGlobalSpec.settings(),
axisSpecs,
axisDims,
axesStyles,
Expand Down Expand Up @@ -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<AxisId, AxisTicksDimensions>();
Expand All @@ -1211,7 +1238,7 @@ describe('Axis computational utils', () => {
leftMargin: 0,
},
LIGHT_THEME,
chartRotation,
MockGlobalSpec.settings(),
axisSpecs,
axisDims,
axisStyles,
Expand Down Expand Up @@ -1284,7 +1311,7 @@ describe('Axis computational utils', () => {
leftMargin: 0,
},
LIGHT_THEME,
0,
MockGlobalSpec.settings(),
invalidSpecs,
axisDims,
axisStyles,
Expand Down Expand Up @@ -1675,7 +1702,7 @@ describe('Axis computational utils', () => {
leftMargin: 0,
},
LIGHT_THEME,
0,
MockGlobalSpec.settings(),
axisSpecs,
axisDims,
axesStyles,
Expand Down Expand Up @@ -1705,7 +1732,7 @@ describe('Axis computational utils', () => {
leftMargin: 0,
},
LIGHT_THEME,
0,
MockGlobalSpec.settings(),
axisSpecs,
axisDims,
axesStyles,
Expand Down Expand Up @@ -1741,7 +1768,7 @@ describe('Axis computational utils', () => {
leftMargin: 0,
},
LIGHT_THEME,
0,
MockGlobalSpec.settings(),
axisSpecs,
axisDims,
axesStyles,
Expand Down
37 changes: 20 additions & 17 deletions src/utils/domain.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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];

Expand All @@ -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];

Expand All @@ -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,
]);
Expand Down

0 comments on commit 7ef4e53

Please sign in to comment.