Skip to content

Commit 1b8ec4c

Browse files
committed
ci: coverage
1 parent 397228e commit 1b8ec4c

3 files changed

Lines changed: 27 additions & 10 deletions

File tree

__tests__/unit/scales/linear-breaks.spec.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,18 @@ describe('Linear Scale with Breaks', () => {
77
breaks: [{ start: 40, end: 100, gap: 0.1 }],
88
});
99

10-
const { domain, range, round, tickCount, nice, clamp, unknown } = scale.getOptions();
10+
const { domain, range, round, tickCount, nice, clamp, unknown, tickMethod } = scale.getOptions() as Record<
11+
string,
12+
any
13+
>;
1114
expect(domain).toStrictEqual([0, 40, 100, 150, 200]);
1215
expect(range).toStrictEqual([1, 0.7000000000000001, 0.6, 0.25, 0]);
1316
expect(round).toBeFalsy();
1417
expect(tickCount).toStrictEqual(5);
1518
expect(nice).toBeFalsy();
1619
expect(clamp).toBeFalsy();
1720
expect(unknown).toBeUndefined();
21+
expect(tickMethod(0, 200, 5)).toEqual(domain);
1822
});
1923

2024
test('multiple breaks should compress multiple regions', () => {
@@ -78,6 +82,12 @@ describe('Linear Scale with Breaks', () => {
7882
const scaleOptions = scale.getOptions();
7983
expect(scaleOptions.domain).toStrictEqual([0, 50, 100, 150, 200]);
8084
expect(scaleOptions.range).toStrictEqual([1, 0.75, 0.5, 0.25, 0]);
85+
scale.update({
86+
domain: [0, 200],
87+
breaks: undefined,
88+
});
89+
const scaleOptions2 = scale.getOptions();
90+
expect(scaleOptions2.domain).toStrictEqual([0, 200]);
8191
});
8292

8393
test('no breaks should behave like normal linear scale', () => {
@@ -89,4 +99,14 @@ describe('Linear Scale with Breaks', () => {
8999
expect(domain).toStrictEqual([0, 200]);
90100
expect(range).toStrictEqual([0, 1]);
91101
});
102+
103+
test('linear scale with clone', () => {
104+
const scale = new Linear({
105+
domain: [0, 200],
106+
});
107+
108+
const scale2 = scale.clone();
109+
expect(scale2).toBeInstanceOf(Linear);
110+
expect(scale2.getOptions()).toEqual(scale.getOptions());
111+
});
92112
});

src/scales/base.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,10 @@ export abstract class Base<O extends BaseOptions> {
2626
protected abstract getDefaultOptions(): Partial<O>;
2727

2828
/**
29-
* breaks 转换为适合比例尺的格式
30-
* @param options 比例尺选项
29+
* 如果配置了 breaks,则需要覆盖该方法对 breaks 进行处理
30+
* @param options 需要更新的选项
3131
*/
32-
protected transformBreaks(options: O): O {
33-
return options;
34-
}
32+
protected abstract transformBreaks(options: O): O;
3533

3634
/**
3735
* 比例尺的选项,用于配置数据映射的规则和 ticks 的生成方式

src/scales/linear.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { identity, last } from '@antv/util';
1+
import { identity, isArray, last } from '@antv/util';
22
import { Continuous } from './continuous';
33
import { LinearOptions, Transform } from '../types';
44
import { Base } from './base';
@@ -43,10 +43,9 @@ export class Linear extends Continuous<LinearOptions> {
4343
return reverse ? r0 - ratio * diffRange : r0 + ratio * diffRange;
4444
});
4545
// Compress the range scale according to breaks.
46-
[...sortedBreaks].forEach(({ start, end, gap = 0.05 }) => {
46+
sortedBreaks.forEach(({ start, end, gap = 0.05 }) => {
4747
const startIndex = breaksDomain.indexOf(start);
4848
const endIndex = breaksDomain.indexOf(end);
49-
if (startIndex === -1 || endIndex === -1) return;
5049
const center = (breaksRange[startIndex] + breaksRange[endIndex]) / 2;
5150
const scaledSpan = gap * diffRange;
5251
// Calculate the new start and end values based on the center and scaled span.
@@ -60,7 +59,7 @@ export class Linear extends Continuous<LinearOptions> {
6059

6160
protected transformBreaks(options: LinearOptions): LinearOptions {
6261
const { domain, breaks = [] } = options;
63-
if (options.breaks === undefined) return options;
62+
if (!isArray(options.breaks)) return options;
6463
const domainMax = Math.max(...domain);
6564
const filteredBreaks = breaks.filter(({ end }) => end < domainMax);
6665
const optWithFilteredBreaks = { ...options, breaks: filteredBreaks };

0 commit comments

Comments
 (0)