|
1 | 1 | import { |
2 | 2 | getChangeInPercentagePoints, |
3 | | - getRelativeChange |
| 3 | + getRelativeChange, |
| 4 | + getLineSegments |
4 | 5 | } from './main-graph-data' |
5 | 6 |
|
6 | 7 | describe(`${getChangeInPercentagePoints.name}`, () => { |
@@ -38,3 +39,75 @@ describe(`${getRelativeChange.name}`, () => { |
38 | 39 | expect(getRelativeChange(50, 100)).toBe(-50) |
39 | 40 | }) |
40 | 41 | }) |
| 42 | + |
| 43 | +const np = (value = 0) => ({ value, isPartial: false, timeLabel: '' }) |
| 44 | +const p = (value = 0) => ({ value, isPartial: true, timeLabel: '' }) |
| 45 | +const gap = () => ({ value: null, isPartial: null, timeLabel: null }) |
| 46 | + |
| 47 | +describe(`${getLineSegments.name}`, () => { |
| 48 | + it('returns empty for empty input', () => { |
| 49 | + expect(getLineSegments([])).toEqual([]) |
| 50 | + }) |
| 51 | + |
| 52 | + it('returns empty for a single point (no edge to draw)', () => { |
| 53 | + expect(getLineSegments([np()])).toEqual([]) |
| 54 | + }) |
| 55 | + |
| 56 | + it('returns empty for a single gap', () => { |
| 57 | + expect(getLineSegments([gap()])).toEqual([]) |
| 58 | + }) |
| 59 | + |
| 60 | + it('returns a full segment for two non-partial points', () => { |
| 61 | + expect(getLineSegments([np(), np()])).toEqual([ |
| 62 | + { startIndexInclusive: 0, stopIndexExclusive: 2, type: 'full' } |
| 63 | + ]) |
| 64 | + }) |
| 65 | + |
| 66 | + it('returns a partial segment for two partial points', () => { |
| 67 | + expect(getLineSegments([p(), p()])).toEqual([ |
| 68 | + { startIndexInclusive: 0, stopIndexExclusive: 2, type: 'partial' } |
| 69 | + ]) |
| 70 | + }) |
| 71 | + |
| 72 | + it('returns partial when connecting non-partial to partial', () => { |
| 73 | + expect(getLineSegments([np(), p()])).toEqual([ |
| 74 | + { startIndexInclusive: 0, stopIndexExclusive: 2, type: 'partial' } |
| 75 | + ]) |
| 76 | + }) |
| 77 | + |
| 78 | + it('returns partial when connecting partial to non-partial', () => { |
| 79 | + expect(getLineSegments([p(), np()])).toEqual([ |
| 80 | + { startIndexInclusive: 0, stopIndexExclusive: 2, type: 'partial' } |
| 81 | + ]) |
| 82 | + }) |
| 83 | + |
| 84 | + it('handles single full period in the middle of two partial periods', () => { |
| 85 | + expect(getLineSegments([p(), np(), p()])).toEqual([ |
| 86 | + { startIndexInclusive: 0, stopIndexExclusive: 3, type: 'partial' } |
| 87 | + ]) |
| 88 | + }) |
| 89 | + |
| 90 | + it('handles partial periods on both ends', () => { |
| 91 | + expect(getLineSegments([p(), np(), np(), p()])).toEqual([ |
| 92 | + { startIndexInclusive: 0, stopIndexExclusive: 2, type: 'partial' }, |
| 93 | + { startIndexInclusive: 1, stopIndexExclusive: 3, type: 'full' }, |
| 94 | + { startIndexInclusive: 2, stopIndexExclusive: 4, type: 'partial' } |
| 95 | + ]) |
| 96 | + }) |
| 97 | + |
| 98 | + it('handles leading gaps', () => { |
| 99 | + expect( |
| 100 | + getLineSegments([gap(), gap(), np(), np(), np(), np(), p()]) |
| 101 | + ).toEqual([ |
| 102 | + { startIndexInclusive: 2, stopIndexExclusive: 6, type: 'full' }, |
| 103 | + { startIndexInclusive: 5, stopIndexExclusive: 7, type: 'partial' } |
| 104 | + ]) |
| 105 | + }) |
| 106 | + |
| 107 | + it('handles trailing gaps', () => { |
| 108 | + expect(getLineSegments([np(), np(), p(), gap(), gap()])).toEqual([ |
| 109 | + { startIndexInclusive: 0, stopIndexExclusive: 2, type: 'full' }, |
| 110 | + { startIndexInclusive: 1, stopIndexExclusive: 3, type: 'partial' } |
| 111 | + ]) |
| 112 | + }) |
| 113 | +}) |
0 commit comments