Skip to content

Commit

Permalink
chore(chart-controls): improve test coverage (#1219)
Browse files Browse the repository at this point in the history
  • Loading branch information
kgabryje authored and zhaoyongjie committed Nov 26, 2021
1 parent b608cba commit db71620
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ export const getColorFunction = (
};

export const getColorFormatters = (
columnConfig: ConditionalFormattingConfig[],
columnConfig: ConditionalFormattingConfig[] | undefined,
data: DataRecord[],
) =>
columnConfig?.reduce((acc: ColorFormatters, config: ConditionalFormattingConfig) => {
Expand All @@ -170,4 +170,4 @@ export const getColorFormatters = (
});
}
return acc;
}, []);
}, []) ?? [];
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,17 @@
* specific language governing permissions and limitations
* under the License.
*/
import { COMPARATOR, getOpacity, rgbToRgba, round } from '../../src';
import { getColorFormatters, getColorFunction } from '../../lib';
import { configure } from '@superset-ui/core';
import {
COMPARATOR,
getOpacity,
rgbToRgba,
round,
getColorFormatters,
getColorFunction,
} from '../../src';

configure();
const mockData = [
{ count: 50, sum: 200 },
{ count: 100, sum: 400 },
Expand Down Expand Up @@ -91,6 +99,7 @@ describe('getColorFunction()', () => {
);
expect(colorFunction(50)).toEqual('rgba(255,0,0,0.3)');
expect(colorFunction(100)).toEqual('rgba(255,0,0,1)');
expect(colorFunction(0)).toBeUndefined();
});

it('getColorFunction LESS_OR_EQUAL', () => {
Expand All @@ -105,6 +114,7 @@ describe('getColorFunction()', () => {
);
expect(colorFunction(50)).toEqual('rgba(255,0,0,1)');
expect(colorFunction(100)).toEqual('rgba(255,0,0,0.3)');
expect(colorFunction(150)).toBeUndefined();
});

it('getColorFunction EQUAL', () => {
Expand All @@ -122,16 +132,30 @@ describe('getColorFunction()', () => {
});

it('getColorFunction NOT_EQUAL', () => {
const colorFunction = getColorFunction(
let colorFunction = getColorFunction(
{
operator: COMPARATOR.NOT_EQUAL,
targetValue: 100,
targetValue: 60,
colorScheme: 'rgb(255,0,0)',
column: 'count',
},
countValues,
);
expect(colorFunction(100)).toBeUndefined();
expect(colorFunction(60)).toBeUndefined();
expect(colorFunction(100)).toEqual('rgba(255,0,0,1)');
expect(colorFunction(50)).toEqual('rgba(255,0,0,0.48)');

colorFunction = getColorFunction(
{
operator: COMPARATOR.NOT_EQUAL,
targetValue: 90,
colorScheme: 'rgb(255,0,0)',
column: 'count',
},
countValues,
);
expect(colorFunction(90)).toBeUndefined();
expect(colorFunction(100)).toEqual('rgba(255,0,0,0.48)');
expect(colorFunction(50)).toEqual('rgba(255,0,0,1)');
});

Expand Down Expand Up @@ -163,6 +187,7 @@ describe('getColorFunction()', () => {
);
expect(colorFunction(50)).toEqual('rgba(255,0,0,0.3)');
expect(colorFunction(100)).toEqual('rgba(255,0,0,1)');
expect(colorFunction(150)).toBeUndefined();
});

it('getColorFunction BETWEEN_OR_LEFT_EQUAL', () => {
Expand Down Expand Up @@ -239,6 +264,21 @@ describe('getColorFunction()', () => {
expect(colorFunction(100)).toBeUndefined();
});

it('getColorFunction unsupported operator', () => {
const colorFunction = getColorFunction(
{
// @ts-ignore
operator: 'unsupported operator',
targetValue: 50,
colorScheme: 'rgb(255,0,0)',
column: 'count',
},
countValues,
);
expect(colorFunction(50)).toBeUndefined();
expect(colorFunction(100)).toBeUndefined();
});

it('getColorFunction with operator undefined', () => {
const colorFunction = getColorFunction(
{
Expand Down Expand Up @@ -269,7 +309,7 @@ describe('getColorFunction()', () => {
});

describe('getColorFormatters()', () => {
it('xx', () => {
it('correct column config', () => {
const columnConfig = [
{
operator: COMPARATOR.GREATER_THAN,
Expand All @@ -283,6 +323,13 @@ describe('getColorFormatters()', () => {
colorScheme: 'rgb(255,0,0)',
column: 'sum',
},
{
operator: COMPARATOR.BETWEEN,
targetValueLeft: 75,
targetValueRight: 125,
colorScheme: 'rgb(255,0,0)',
column: 'count',
},
{
operator: COMPARATOR.GREATER_THAN,
targetValue: 150,
Expand All @@ -291,14 +338,21 @@ describe('getColorFormatters()', () => {
},
];
const colorFormatters = getColorFormatters(columnConfig, mockData);

expect(colorFormatters.length).toEqual(2);
expect(colorFormatters.length).toEqual(3);

expect(colorFormatters[0].column).toEqual('count');
expect(colorFormatters[0].getColorFromValue(100)).toEqual('rgba(255,0,0,1)');

expect(colorFormatters[1].column).toEqual('sum');
expect(colorFormatters[1].getColorFromValue(200)).toEqual('rgba(255,0,0,1)');
expect(colorFormatters[1].getColorFromValue(400)).toBeUndefined();

expect(colorFormatters[2].column).toEqual('count');
expect(colorFormatters[2].getColorFromValue(100)).toEqual('rgba(255,0,0,0.65)');
});

it('undefined column config', () => {
const colorFormatters = getColorFormatters(undefined, mockData);
expect(colorFormatters.length).toEqual(0);
});
});

0 comments on commit db71620

Please sign in to comment.