Skip to content

Commit

Permalink
test(pattern): 增加 pattern adaptor 的单测
Browse files Browse the repository at this point in the history
  • Loading branch information
visiky committed Aug 15, 2021
1 parent eb1ad9b commit ebea51b
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
46 changes: 46 additions & 0 deletions __tests__/unit/adaptor/pattern-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { flow } from '../../../src/utils/flow';
import { Plot } from '../../../src/core/plot';
import { pattern } from '../../../src/adaptor/pattern';
import { createDiv } from '../../utils/dom';
import { createSquarePattern } from '../../../src/utils/pattern/square';

describe('pattern adaptor', () => {
class APlot extends Plot<any> {
type: 'a-plot';

getDefaultOptions() {
return {};
}

getSchemaAdaptor() {
return (params: any) => flow(pattern('xxx'))(params);
}
}

const plot = new APlot(createDiv(), {
xxx: {},
});

it('default', () => {
expect(typeof pattern('xxx')).toBe('function');
let params = pattern('xxx')({ chart: plot.chart, options: plot.options });
expect(params.options.xxx.fill).toBeUndefined();

params = pattern('xxx')({ chart: plot.chart, options: { ...plot.options, pattern: { type: 'dot' } } });
expect(params.options.xxx).not.toBeUndefined();
expect(params.options.xxx.call().fill instanceof CanvasPattern).toBe(true);

// callback
params = pattern('xxx')({ chart: plot.chart, options: { ...plot.options, pattern: () => ({ type: 'line' }) } });
expect(params.options.xxx.call().fill instanceof CanvasPattern).toBe(true);

// canvasPattern
const suqarePattern = createSquarePattern({ size: 10, padding: 0, isStagger: true });
params = pattern('xxx')({ chart: plot.chart, options: { ...plot.options, pattern: suqarePattern } });
expect(params.options.xxx.call().fill instanceof CanvasPattern).toBe(true);
});

afterAll(() => {
plot.destroy();
});
});
4 changes: 2 additions & 2 deletions src/adaptor/pattern.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { deepAssign } from '../utils';
/**
* 使用 Pattern 通道的 options,要求有 colorField、seriesField 作为分类字段(进行颜色映射)
*/
type OptionsRequiredInPattern = Options & {
type OptionsRequiredInPattern = Omit<Options, 'data'> & {
colorField?: string;
seriesField?: string;
};
Expand All @@ -33,7 +33,7 @@ export function pattern(key: string) {
const style: StyleAttr = (datum?: Datum, ...args: any[]) => {
let color = chart.getTheme().defaultColor;

const colorMapping = chart.geometries[0].getAttribute('color')?.callback;
const colorMapping = chart.geometries?.[0]?.getAttribute('color')?.callback;
if (typeof colorMapping === 'function') {
color = colorMapping(datum?.[colorField] || datum?.[seriesField]);
}
Expand Down

0 comments on commit ebea51b

Please sign in to comment.