|
| 1 | +import { Range } from '../range'; |
| 2 | +import { mockConfig, mockRenderer, mockElementRef, mockHaptic } from '../../../util/mock-providers'; |
| 3 | +import { Form } from '../../../util/form'; |
| 4 | + |
| 5 | + |
| 6 | +describe('Range', () => { |
| 7 | + describe('valueToRatio', () => { |
| 8 | + it('step=1', () => { |
| 9 | + let range = createRange(); |
| 10 | + range.max = 5000; |
| 11 | + range.min = 2490; |
| 12 | + range.step = 1; |
| 13 | + range.snaps = true; |
| 14 | + expect(range.valueToRatio(5000)).toEqual(1); |
| 15 | + expect(range.valueToRatio(5100)).toEqual(1); |
| 16 | + expect(range.valueToRatio(2490)).toEqual(0); |
| 17 | + expect(range.valueToRatio(2000)).toEqual(0); |
| 18 | + |
| 19 | + let middle = (range.max - range.min) / 2 + range.min; |
| 20 | + expect(range.valueToRatio(middle)).toEqual(0.5); |
| 21 | + }); |
| 22 | + |
| 23 | + it('step>range', () => { |
| 24 | + let range = createRange(); |
| 25 | + range.max = 5000; |
| 26 | + range.min = 2490; |
| 27 | + range.step = 5900; |
| 28 | + range.snaps = true; |
| 29 | + expect(range.valueToRatio(7000)).toEqual(1); |
| 30 | + expect(range.valueToRatio(5000)).toEqual(0); |
| 31 | + expect(range.valueToRatio(2490)).toEqual(0); |
| 32 | + expect(range.valueToRatio(2000)).toEqual(0); |
| 33 | + }); |
| 34 | + }); |
| 35 | + |
| 36 | + describe('ratioToValue', () => { |
| 37 | + it('step=1', () => { |
| 38 | + let range = createRange(); |
| 39 | + range.max = 5000; |
| 40 | + range.min = 2490; |
| 41 | + range.step = 1; |
| 42 | + range.snaps = true; |
| 43 | + expect(range.ratioToValue(0)).toEqual(2490); |
| 44 | + expect(range.ratioToValue(1)).toEqual(5000); |
| 45 | + |
| 46 | + let middle = (range.max - range.min) / 2 + range.min; |
| 47 | + expect(range.ratioToValue(0.5)).toEqual(middle); |
| 48 | + }); |
| 49 | + |
| 50 | + it('step>range', () => { |
| 51 | + let range = createRange(); |
| 52 | + range.max = 5000; |
| 53 | + range.min = 2490; |
| 54 | + range.step = 1; |
| 55 | + expect(range.ratioToValue(0)).toEqual(2490); |
| 56 | + expect(range.ratioToValue(1)).toEqual(5000); |
| 57 | + |
| 58 | + let middle = (range.max - range.min) / 2 + range.min; |
| 59 | + expect(range.ratioToValue(0.5)).toEqual(middle); |
| 60 | + }); |
| 61 | + }); |
| 62 | + |
| 63 | +}); |
| 64 | + |
| 65 | + |
| 66 | +function createRange(): Range { |
| 67 | + let form = new Form(); |
| 68 | + return new Range(form, mockHaptic(), null, mockConfig(), mockElementRef(), mockRenderer()); |
| 69 | +} |
0 commit comments