|
| 1 | +// @ts-nocheck |
| 2 | +import { describe, it, expect } from 'vitest'; |
| 3 | +import { jitterX, jitterY } from './jitter.js'; |
| 4 | +import { randomLcg } from 'd3-random'; |
| 5 | + |
| 6 | +// Tests for the jitter transforms |
| 7 | +describe('jitterX', () => { |
| 8 | + it('should add uniform jitter to x values with default options', () => { |
| 9 | + // Create a deterministic random source that returns exactly what we need |
| 10 | + const mockRandom = () => 1; // This will produce exactly +0.1 in range [-0.35, 0.35] |
| 11 | + |
| 12 | + const data = [{ x: 5 }, { x: 10 }]; |
| 13 | + // @ts-ignore - Bypassing type checking for tests |
| 14 | + const result = jitterX({ data, x: 'x' }, { source: mockRandom }); |
| 15 | + // The result should add the jitter values to the original x values |
| 16 | + const { x } = result; |
| 17 | + // Check approximate values |
| 18 | + expect(result.data[0][x]).toBe(5.35, 2); |
| 19 | + expect(result.data[1][x]).toBe(10.35, 2); |
| 20 | + }); |
| 21 | + |
| 22 | + it('should add uniform jitter to x values with custom width', () => { |
| 23 | + // Create a deterministic random source that returns exactly what we need |
| 24 | + const mockRandom = () => 1; // This will produce exactly +0.1 in range [-0.35, 0.35] |
| 25 | + |
| 26 | + const data = [{ x: 5 }, { x: 10 }]; |
| 27 | + // @ts-ignore - Bypassing type checking for tests |
| 28 | + const result = jitterX({ data, x: 'x' }, { width: 0.5, source: mockRandom }); |
| 29 | + // The result should add the jitter values to the original x values |
| 30 | + const { x } = result; |
| 31 | + // Check approximate values |
| 32 | + expect(result.data[0][x]).toBe(5.5, 2); |
| 33 | + expect(result.data[1][x]).toBe(10.5, 2); |
| 34 | + }); |
| 35 | + |
| 36 | + it('should add normal jitter to x values', () => { |
| 37 | + // We'll simplify this test by not trying to mock d3-random directly |
| 38 | + // Instead, we'll provide a source function that controls the output values |
| 39 | + const data = [{ x: 5 }, { x: 10 }]; |
| 40 | + |
| 41 | + // Custom source function that controls the exact jitter values |
| 42 | + let values = [0.05, -0.1]; // The exact jitter values we want |
| 43 | + let index = 0; |
| 44 | + |
| 45 | + const mockSource = randomLcg(42); |
| 46 | + |
| 47 | + // @ts-ignore - Bypassing type checking for tests |
| 48 | + const result = jitterX( |
| 49 | + { data, x: 'x' }, |
| 50 | + { |
| 51 | + type: 'normal', |
| 52 | + std: 0.2, |
| 53 | + // Use our custom function as the source |
| 54 | + // This effectively hijacks the normal distribution calculation |
| 55 | + source: mockSource |
| 56 | + } |
| 57 | + ); |
| 58 | + |
| 59 | + // The result should add the jitter values to the original x values |
| 60 | + const { x } = result; |
| 61 | + expect(result.data[0][x]).toBeCloseTo(4.9318, 3); |
| 62 | + expect(result.data[1][x]).toBeCloseTo(9.9589, 3); |
| 63 | + }); |
| 64 | + |
| 65 | + // // Note: Date jittering is not yet supported, test will be added when implemented |
| 66 | + |
| 67 | + it('should not modify data if x channel is not provided', () => { |
| 68 | + const mockRandom = () => 0.5; |
| 69 | + |
| 70 | + const data = [{ y: 5 }, { y: 10 }]; |
| 71 | + // @ts-ignore - Bypassing type checking for tests |
| 72 | + const result = jitterX({ data, y: 'y' }, { source: mockRandom }); |
| 73 | + |
| 74 | + // The result should be the same as the input |
| 75 | + expect(result.data).toEqual(data); |
| 76 | + expect(result.y).toBe('y'); |
| 77 | + }); |
| 78 | + |
| 79 | + it('should parse time interval strings for width/std', () => { |
| 80 | + // This isn't fully implemented in the jitter.ts but mentioned in a TODO comment |
| 81 | + const mockRandom = () => 0.75; |
| 82 | + |
| 83 | + const data = [{ x: new Date(Date.UTC(2020, 0, 1)) }, { x: new Date(Date.UTC(2021, 0, 1)) }]; |
| 84 | + // @ts-ignore - Bypassing type checking for tests |
| 85 | + const result = jitterX({ data, x: 'x' }, { source: mockRandom, width: '1 month' }); |
| 86 | + |
| 87 | + const { x } = result; |
| 88 | + expect(result.data[0][x]).toBeTypeOf('object'); |
| 89 | + expect(result.data[0][x].getTime).toBeTypeOf('function'); |
| 90 | + expect(result.data[0][x]).toStrictEqual(new Date(Date.UTC(2020, 0, 16))); |
| 91 | + }); |
| 92 | +}); |
| 93 | + |
| 94 | +describe('jitterY', () => { |
| 95 | + it('should add uniform jitter to x values with default options', () => { |
| 96 | + // Create a deterministic random source that returns exactly what we need |
| 97 | + const mockRandom = () => 1; // This will produce exactly +0.1 in range [-0.35, 0.35] |
| 98 | + |
| 99 | + const data = [{ x: 5 }, { x: 10 }]; |
| 100 | + // @ts-ignore - Bypassing type checking for tests |
| 101 | + const result = jitterY({ data, y: 'x' }, { source: mockRandom }); |
| 102 | + // The result should add the jitter values to the original x values |
| 103 | + const { y } = result; |
| 104 | + // Check approximate values |
| 105 | + expect(result.data[0][y]).toBe(5.35, 2); |
| 106 | + expect(result.data[1][y]).toBe(10.35, 2); |
| 107 | + }); |
| 108 | + |
| 109 | + it('should add uniform jitter to x values with custom width', () => { |
| 110 | + // Create a deterministic random source that returns exactly what we need |
| 111 | + const mockRandom = () => 1; // This will produce exactly +0.1 in range [-0.35, 0.35] |
| 112 | + |
| 113 | + const data = [{ x: 5 }, { x: 10 }]; |
| 114 | + // @ts-ignore - Bypassing type checking for tests |
| 115 | + const result = jitterY({ data, y: 'x' }, { width: 0.5, source: mockRandom }); |
| 116 | + // The result should add the jitter values to the original x values |
| 117 | + const { y } = result; |
| 118 | + // Check approximate values |
| 119 | + expect(result.data[0][y]).toBe(5.5, 2); |
| 120 | + expect(result.data[1][y]).toBe(10.5, 2); |
| 121 | + }); |
| 122 | + |
| 123 | + it('should add normal jitter to x values', () => { |
| 124 | + // We'll simplify this test by not trying to mock d3-random directly |
| 125 | + // Instead, we'll provide a source function that controls the output values |
| 126 | + const data = [{ x: 5 }, { x: 10 }]; |
| 127 | + |
| 128 | + // Custom source function that controls the exact jitter values |
| 129 | + let values = [0.05, -0.1]; // The exact jitter values we want |
| 130 | + let index = 0; |
| 131 | + |
| 132 | + const mockSource = randomLcg(42); |
| 133 | + |
| 134 | + // @ts-ignore - Bypassing type checking for tests |
| 135 | + const result = jitterY( |
| 136 | + { data, y: 'x' }, |
| 137 | + { |
| 138 | + type: 'normal', |
| 139 | + std: 0.2, |
| 140 | + // Use our custom function as the source |
| 141 | + // This effectively hijacks the normal distribution calculation |
| 142 | + source: mockSource |
| 143 | + } |
| 144 | + ); |
| 145 | + |
| 146 | + // The result should add the jitter values to the original x values |
| 147 | + const { y } = result; |
| 148 | + expect(result.data[0][y]).toBeCloseTo(4.9318, 3); |
| 149 | + expect(result.data[1][y]).toBeCloseTo(9.9589, 3); |
| 150 | + }); |
| 151 | + |
| 152 | + // // Note: Date jittering is not yet supported, test will be added when implemented |
| 153 | + |
| 154 | + it('should not modify data if y channel is not provided', () => { |
| 155 | + const mockRandom = () => 0.5; |
| 156 | + |
| 157 | + const data = [{ x: 5 }, { x: 10 }]; |
| 158 | + // @ts-ignore - Bypassing type checking for tests |
| 159 | + const result = jitterY({ data, x: 'x' }, { source: mockRandom }); |
| 160 | + |
| 161 | + // The result should be the same as the input |
| 162 | + expect(result.data).toEqual(data); |
| 163 | + expect(result.x).toBe('x'); |
| 164 | + }); |
| 165 | +}); |
0 commit comments