Skip to content

Commit

Permalink
test: add test case for vchartspec utils
Browse files Browse the repository at this point in the history
  • Loading branch information
xile611 committed Dec 31, 2024
1 parent 36ade46 commit 152a5ba
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 13 deletions.
46 changes: 45 additions & 1 deletion packages/vmind/__tests__/unit/vchartSpec/util.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { checkDuplicatedKey, convertFunctionString } from '../../../src/atom/VChartSpec/utils';
import { checkDuplicatedKey, convertFunctionString, removeUnneedArrayInSpec } from '../../../src/atom/VChartSpec/utils';

describe('checkDuplicatedKey', () => {
it('should return null when not match', () => {
Expand Down Expand Up @@ -95,3 +95,47 @@ describe('convertFunctionString', () => {
expect(obj1.a).toEqual(spec.a);
});
});

describe('removeUnneedArrayInSpec', () => {
it('should remove unneed array in spec', () => {
expect(
removeUnneedArrayInSpec(
{
legends: [{ orient: 'left' }]
},
'legends',
'legends'
)
).toEqual({ orient: 'left' });

expect(
removeUnneedArrayInSpec(
{
legends: [{ orient: 'left' }]
},
'legends',
'legends[0]'
)
).toEqual({ orient: 'left' });

expect(
removeUnneedArrayInSpec(
{
'legends[0]': { orient: 'left' }
},
'legends',
'legends[0]'
)
).toEqual({ orient: 'left' });

expect(
removeUnneedArrayInSpec(
{
'legends[0]': { orient: 'left' }
},
'legends',
'legends'
)
).toEqual({ orient: 'left' });
});
});
14 changes: 2 additions & 12 deletions packages/vmind/src/atom/VChartSpec/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,16 +313,6 @@ export const removeUnneedArrayInSpec = (leafSpec: any, compKey: string, parentKe
: leafSpec;
};

export const addArrayInSpec = (leafSpec: any, compKey: string, parentKeyPath: string) => {
return leafSpec[compKey]
? isArray(leafSpec[compKey])
? leafSpec[compKey][0]
: leafSpec[compKey]
: parentKeyPath in leafSpec
? leafSpec[parentKeyPath]
: leafSpec;
};

const ALIAS_NAME_KEY = '_alias_name';

export const parseAliasOfPath = (parentKeyPath: string, aliasKeyPath: string, chartSpec: any, leafSpec: any) => {
Expand All @@ -341,7 +331,7 @@ export const parseAliasOfPath = (parentKeyPath: string, aliasKeyPath: string, ch
}
return {
parentKeyPath: subPaths.join('.'),
leafSpec: addArrayInSpec(leafSpec, compKey, parentKeyPath)
leafSpec: removeUnneedArrayInSpec(leafSpec, compKey, parentKeyPath)
};
}

Expand All @@ -359,7 +349,7 @@ export const parseAliasOfPath = (parentKeyPath: string, aliasKeyPath: string, ch
}
return {
parentKeyPath: subPaths.join('.'),
leafSpec: addArrayInSpec(leafSpec, compKey, parentKeyPath)
leafSpec: removeUnneedArrayInSpec(leafSpec, compKey, parentKeyPath)
};
}

Expand Down

0 comments on commit 152a5ba

Please sign in to comment.