|
| 1 | +import { Linear } from '../../../src'; |
| 2 | + |
| 3 | +describe('Linear Scale with Breaks', () => { |
| 4 | + test('single break: values before, inside, and after break', () => { |
| 5 | + const scale = new Linear({ |
| 6 | + domain: [0, 200], |
| 7 | + breaks: [{ start: 40, end: 100, gap: 0.1 }], |
| 8 | + }); |
| 9 | + |
| 10 | + const { domain, range, round, tickCount, nice, clamp, unknown, tickMethod } = scale.getOptions() as Record< |
| 11 | + string, |
| 12 | + any |
| 13 | + >; |
| 14 | + expect(domain).toStrictEqual([0, 40, 100, 150, 200]); |
| 15 | + expect(range).toStrictEqual([1, 0.7000000000000001, 0.6, 0.25, 0]); |
| 16 | + expect(round).toBeFalsy(); |
| 17 | + expect(tickCount).toStrictEqual(5); |
| 18 | + expect(nice).toBeFalsy(); |
| 19 | + expect(clamp).toBeFalsy(); |
| 20 | + expect(unknown).toBeUndefined(); |
| 21 | + expect(tickMethod(0, 200, 5)).toEqual(domain); |
| 22 | + }); |
| 23 | + |
| 24 | + test('multiple breaks should compress multiple regions', () => { |
| 25 | + const scale = new Linear({ |
| 26 | + domain: [0, 200], |
| 27 | + breaks: [ |
| 28 | + { start: 40, end: 100, gap: 0.1 }, |
| 29 | + { start: 120, end: 160, gap: 0.1 }, |
| 30 | + ], |
| 31 | + }); |
| 32 | + |
| 33 | + const { domain, range } = scale.getOptions(); |
| 34 | + expect(domain).toStrictEqual([0, 40, 100, 120, 160, 200]); |
| 35 | + expect(range).toStrictEqual([1, 0.7000000000000001, 0.6, 0.35, 0.25, 0]); |
| 36 | + }); |
| 37 | + |
| 38 | + test('multiple breaks should compress multiple regions with out of order', () => { |
| 39 | + const scale = new Linear({ |
| 40 | + domain: [0, 200], |
| 41 | + breaks: [ |
| 42 | + { start: 120, end: 160, gap: 0.1 }, |
| 43 | + { start: 40, end: 100, gap: 0.1 }, |
| 44 | + ], |
| 45 | + }); |
| 46 | + |
| 47 | + const { domain, range } = scale.getOptions(); |
| 48 | + expect(domain).toStrictEqual([0, 40, 100, 120, 160, 200]); |
| 49 | + expect(range).toStrictEqual([1, 0.7000000000000001, 0.6, 0.35, 0.25, 0]); |
| 50 | + }); |
| 51 | + |
| 52 | + test('multiple breaks should compress multiple regions with reverse', () => { |
| 53 | + const scale = new Linear({ |
| 54 | + domain: [0, 980], |
| 55 | + range: [0, 1], |
| 56 | + breaks: [ |
| 57 | + { start: 300, end: 500, gap: 0.1 }, |
| 58 | + { start: 600, end: 800, gap: 0.05 }, |
| 59 | + ], |
| 60 | + }); |
| 61 | + |
| 62 | + const { domain, range } = scale.getOptions(); |
| 63 | + expect(domain).toStrictEqual([0, 200, 300, 500, 600, 800, 980]); |
| 64 | + expect(range).toStrictEqual([ |
| 65 | + 0, 0.20408163265306123, 0.35816326530612247, 0.45816326530612245, 0.6892857142857143, 0.7392857142857143, 1, |
| 66 | + ]); |
| 67 | + }); |
| 68 | + |
| 69 | + test('single break: update', () => { |
| 70 | + const scale = new Linear({ |
| 71 | + domain: [0, 200], |
| 72 | + breaks: [{ start: 40, end: 100, gap: 0.1 }], |
| 73 | + }); |
| 74 | + |
| 75 | + const { domain, range } = scale.getOptions(); |
| 76 | + expect(domain).toStrictEqual([0, 40, 100, 150, 200]); |
| 77 | + expect(range).toStrictEqual([1, 0.7000000000000001, 0.6, 0.25, 0]); |
| 78 | + scale.update({ |
| 79 | + domain: [0, 200], |
| 80 | + breaks: [], |
| 81 | + }); |
| 82 | + const scaleOptions = scale.getOptions(); |
| 83 | + expect(scaleOptions.domain).toStrictEqual([0, 50, 100, 150, 200]); |
| 84 | + 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]); |
| 91 | + }); |
| 92 | + |
| 93 | + test('no breaks should behave like normal linear scale', () => { |
| 94 | + const scale = new Linear({ |
| 95 | + domain: [0, 200], |
| 96 | + }); |
| 97 | + |
| 98 | + const { domain, range } = scale.getOptions(); |
| 99 | + expect(domain).toStrictEqual([0, 200]); |
| 100 | + expect(range).toStrictEqual([0, 1]); |
| 101 | + }); |
| 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 | + }); |
| 112 | +}); |
0 commit comments