|
| 1 | +import { calcPercentages } from '../src/Percent'; |
| 2 | + |
| 3 | +describe('Percent', () => { |
| 4 | + it('single value is always 100%', () => { |
| 5 | + expect(calcPercentages([50])).toEqual([1]); |
| 6 | + }); |
| 7 | + |
| 8 | + it('whole percentages add up to 100%', () => { |
| 9 | + expect(calcPercentages([50, 50])).toEqual([0.5, 0.5]); |
| 10 | + }); |
| 11 | + |
| 12 | + it('floating point percentages', () => { |
| 13 | + expect(calcPercentages([403, 597])).toEqual([0.403, 0.597]); |
| 14 | + expect(calcPercentages([249, 249, 502])).toEqual([0.249, 0.249, 0.502]); |
| 15 | + expect(calcPercentages([255, 245, 265, 235])).toEqual([0.255, 0.245, 0.265, 0.235]); |
| 16 | + }); |
| 17 | + |
| 18 | + it('percentages smaller than 1%', () => { |
| 19 | + expect(calcPercentages([5, 6, 1000 - 5 - 6])).toEqual([0.005, 0.006, 0.989]); |
| 20 | + expect(calcPercentages([0, 6, 1000 - 6])).toEqual([0, 0.006, 0.994]); |
| 21 | + }); |
| 22 | + |
| 23 | + it('one part is zero, total is implicitly zero, so it takes 100% of the total', () => { |
| 24 | + expect(calcPercentages([0])).toEqual([1]); |
| 25 | + }); |
| 26 | + |
| 27 | + it('multiple parts are zero, total is implicitly zero, percent is distributed evenly', () => { |
| 28 | + expect(calcPercentages([0, 0, 0, 0])).toEqual([0.25, 0.25, 0.25, 0.25]); |
| 29 | + }); |
| 30 | + |
| 31 | + it('total is adjusted to equal at least as much as the sum of the parts', () => { |
| 32 | + expect(calcPercentages([80], 0)).toEqual([1]); |
| 33 | + }); |
| 34 | + |
| 35 | + it('returns empty array if no parts are provided', () => { |
| 36 | + expect(calcPercentages([])).toEqual([]); |
| 37 | + }); |
| 38 | + |
| 39 | + describe('parts sum less than 100%', () => { |
| 40 | + it('part are zero but total > zero translates to 0% for each part', () => { |
| 41 | + expect(calcPercentages([0, 0], 100)).toEqual([0, 0]); |
| 42 | + }); |
| 43 | + |
| 44 | + it('single 80% value', () => { |
| 45 | + expect(calcPercentages([80], 100)).toEqual([0.8]); |
| 46 | + }); |
| 47 | + |
| 48 | + it('two whole parts adding up to 80%', () => { |
| 49 | + expect(calcPercentages([40, 40], 100)).toEqual([0.4, 0.4]); |
| 50 | + }); |
| 51 | + |
| 52 | + it('multiple rounded parts adding up to 80%', () => { |
| 53 | + expect(calcPercentages([205, 205, 215, 175], 1000)).toEqual([0.205, 0.205, 0.215, 0.175]); |
| 54 | + }); |
| 55 | + }); |
| 56 | +}); |
0 commit comments