Skip to content

Commit

Permalink
added test
Browse files Browse the repository at this point in the history
  • Loading branch information
Conglei Shi committed Sep 10, 2018
1 parent 473f48a commit 4fcf9ee
Showing 1 changed file with 57 additions and 1 deletion.
58 changes: 57 additions & 1 deletion packages/xy-chart/test/utils/chartUtils.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import React from 'react';
import { scaleLinear, scaleBand } from '@vx/scale';

import { callOrValue, componentName } from '../../src/utils/chartUtils';
import {
callOrValue,
componentName,
scaleInvert,
getDomainFromExtent,
} from '../../src/utils/chartUtils';

describe('callOrValue', () => {
it('should return non-functions', () => {
Expand Down Expand Up @@ -41,3 +47,53 @@ describe('componentName', () => {
expect(componentName(SFC)).toBe('');
});
});

describe('scaleInvert', () => {
it('should work correct with non-band or no-ordinal Scale', () => {
const linearScale = scaleLinear({
range: [0, 1000],
domain: [0, 100],
});
expect(linearScale.invert(10)).toEqual(scaleInvert(linearScale, 10));
});

it('should work correct with band or ordinal Scales', () => {
const domain = [1, 2, 3, 4, 5];
const bandScale = scaleBand({
rangeRound: [0, 100],
domain,
});
expect(scaleInvert(bandScale, 30)).toEqual(1);
expect(scaleInvert(bandScale, 90)).toEqual(4);
});
});

describe('getDomainFromExtent', () => {
it('should work correct with non-band or no-ordinal Scale', () => {
const linearScale = scaleLinear({
range: [0, 1000],
domain: [0, 100],
});
const delta = 2;
const start = 10;
const end = 20;
const domain = getDomainFromExtent(linearScale, start, end, delta);
expect(domain.start).toEqual(linearScale.invert(start - 2));
expect(domain.end).toEqual(linearScale.invert(end + 2));
});

it('should work correct with band or ordinal Scales', () => {
const domain = ['test1', 'test5', 'test3', 'test3', 'test4'];
const bandScale = scaleBand({
rangeRound: [0, 100],
domain,
});
const delta = 2;
const start = 10;
const end = 30;
const selectedDomain = getDomainFromExtent(bandScale, start, end, delta);
expect(selectedDomain.values).toHaveLength(2);
expect(selectedDomain.values[0]).toEqual('test1');
expect(selectedDomain.values[1]).toEqual('test5');
});
});

0 comments on commit 4fcf9ee

Please sign in to comment.