Skip to content

Commit

Permalink
test: 补充utils单测
Browse files Browse the repository at this point in the history
  • Loading branch information
张斌 committed Apr 24, 2024
1 parent 51b4e94 commit a1f2242
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 0 deletions.
32 changes: 32 additions & 0 deletions packages/s2-core/__tests__/unit/utils/condition/condition-spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
getIconPositionCfg,
getIntervalScale,
findFieldCondition,
} from '@/utils/condition/condition';

describe('getIconLayoutPosition Test', () => {
Expand All @@ -24,6 +25,37 @@ describe('getIconLayoutPosition Test', () => {
});
});

describe('getFieldCondition Test', () => {
test('should find the condition where fill is green', () => {
const conditions = [
{
field: 'value',
mapping: () => ({ fill: 'red' }),
},
{ field: 'price', mapping: () => ({ fill: 'blue' }) },
{ field: 'price', mapping: () => ({ fill: 'green' }) },
];
expect(findFieldCondition(conditions, 'price').mapping().fill).toBe(
'green',
);
});

test('should not find the condition where fill is orange', () => {
const conditions = [
{
field: 'value',
mapping: () => ({ fill: 'red' }),
},
{ field: 'price', mapping: () => ({ fill: 'blue' }) },
{ field: /price/, mapping: () => ({ fill: 'orange' }) },
{ field: 'p', mapping: () => ({ fill: 'pink' }) },
];
expect(findFieldCondition(conditions, 'price').mapping().fill).toBe(
'orange',
);
});
});

describe('getIntervalScale Test', () => {
test('should get scale when both of minValue and maxValue are greater then 0', () => {
const getScale = getIntervalScale(100, 200);
Expand Down
84 changes: 84 additions & 0 deletions packages/s2-core/__tests__/unit/utils/layout/icon-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { getDataCellIconStyle, normalizeIconCfg } from '@/utils/layout/icon';

describe('normalizeIconCfg Test', () => {
test('should return a complete IconCfg object', () => {
expect(normalizeIconCfg()).toEqual({
size: 0,
position: 'right',
margin: {
left: 0,
right: 0,
},
});
});

test('should return the input object', () => {
const iconCfg = {
size: 10,
position: 'left',
margin: {
left: 8,
right: 8,
},
};
expect(normalizeIconCfg(iconCfg)).toEqual(iconCfg);
});
});

describe('getDataCellIconStyle Test', () => {
const conditions = [
{
field: 'value',
mapping: () => ({ fill: 'red' }),
},
{ field: 'price', mapping: () => ({ fill: 'blue' }) },
{ field: /price/, mapping: () => ({ fill: 'orange' }), position: 'left' },
{ field: 'p', mapping: () => ({ fill: 'pink' }) },
];
test('should return default iconCfg object', () => {
expect(
getDataCellIconStyle(
{ icon: conditions },
{ size: 10, margin: { left: 4, right: 4 } },
'errorField',
),
).toEqual({
size: 0,
position: 'right',
margin: {
left: 0,
right: 0,
},
});
expect(
getDataCellIconStyle(
{ icon: [] },
{ size: 10, margin: { left: 4, right: 4 } },
'price',
),
).toEqual({
size: 0,
position: 'right',
margin: {
left: 0,
right: 0,
},
});
});
test('should return correct iconCfg object', () => {
expect(
getDataCellIconStyle(
{ icon: conditions },
{ size: 10, margin: { left: 4, right: 4 } },
'price',
),
).toEqual({
size: 10,
position: 'left',
margin: {
left: 4,
right: 4,
},
});
});
});

0 comments on commit a1f2242

Please sign in to comment.