|
| 1 | +import {coerceNumberProperty} from './number-property'; |
| 2 | + |
| 3 | + |
| 4 | +describe('coerceNumberProperty', () => { |
| 5 | + it('should coerce undefined to 0 or default', () => { |
| 6 | + expect(coerceNumberProperty(undefined)).toBe(0); |
| 7 | + expect(coerceNumberProperty(undefined, 111)).toBe(111); |
| 8 | + }); |
| 9 | + |
| 10 | + it('should coerce null to 0 or default', () => { |
| 11 | + expect(coerceNumberProperty(null)).toBe(0); |
| 12 | + expect(coerceNumberProperty(null, 111)).toBe(111); |
| 13 | + }); |
| 14 | + |
| 15 | + it('should coerce true to 0 or default', () => { |
| 16 | + expect(coerceNumberProperty(true)).toBe(0); |
| 17 | + expect(coerceNumberProperty(true, 111)).toBe(111); |
| 18 | + }); |
| 19 | + |
| 20 | + it('should coerce false to 0 or default', () => { |
| 21 | + expect(coerceNumberProperty(false)).toBe(0); |
| 22 | + expect(coerceNumberProperty(false, 111)).toBe(111); |
| 23 | + |
| 24 | + }); |
| 25 | + |
| 26 | + it('should coerce the empty string to 0 or default', () => { |
| 27 | + expect(coerceNumberProperty('')).toBe(0); |
| 28 | + expect(coerceNumberProperty('', 111)).toBe(111); |
| 29 | + |
| 30 | + }); |
| 31 | + |
| 32 | + it('should coerce the string "1" to 1', () => { |
| 33 | + expect(coerceNumberProperty('1')).toBe(1); |
| 34 | + expect(coerceNumberProperty('1', 111)).toBe(1); |
| 35 | + }); |
| 36 | + |
| 37 | + it('should coerce the string "123.456" to 123.456', () => { |
| 38 | + expect(coerceNumberProperty('123.456')).toBe(123.456); |
| 39 | + expect(coerceNumberProperty('123.456', 111)).toBe(123.456); |
| 40 | + }); |
| 41 | + |
| 42 | + it('should coerce the string "-123.456" to -123.456', () => { |
| 43 | + expect(coerceNumberProperty('-123.456')).toBe(-123.456); |
| 44 | + expect(coerceNumberProperty('-123.456', 111)).toBe(-123.456); |
| 45 | + }); |
| 46 | + |
| 47 | + it('should coerce an arbitrary string to 0 or default', () => { |
| 48 | + expect(coerceNumberProperty('pink')).toBe(0); |
| 49 | + expect(coerceNumberProperty('pink', 111)).toBe(111); |
| 50 | + }); |
| 51 | + |
| 52 | + it('should coerce an arbitrary string prefixed with a number to 0 or default', () => { |
| 53 | + expect(coerceNumberProperty('123pink')).toBe(0); |
| 54 | + expect(coerceNumberProperty('123pink', 111)).toBe(111); |
| 55 | + }); |
| 56 | + |
| 57 | + it('should coerce the number 1 to 1', () => { |
| 58 | + expect(coerceNumberProperty(1)).toBe(1); |
| 59 | + expect(coerceNumberProperty(1, 111)).toBe(1); |
| 60 | + }); |
| 61 | + |
| 62 | + it('should coerce the number 123.456 to 123.456', () => { |
| 63 | + expect(coerceNumberProperty(123.456)).toBe(123.456); |
| 64 | + expect(coerceNumberProperty(123.456, 111)).toBe(123.456); |
| 65 | + }); |
| 66 | + |
| 67 | + it('should coerce the number -123.456 to -123.456', () => { |
| 68 | + expect(coerceNumberProperty(-123.456)).toBe(-123.456); |
| 69 | + expect(coerceNumberProperty(-123.456, 111)).toBe(-123.456); |
| 70 | + }); |
| 71 | + |
| 72 | + it('should coerce an object to 0 or default', () => { |
| 73 | + expect(coerceNumberProperty({})).toBe(0); |
| 74 | + expect(coerceNumberProperty({}, 111)).toBe(111); |
| 75 | + }); |
| 76 | + |
| 77 | + it('should coerce an array to 0 or default', () => { |
| 78 | + expect(coerceNumberProperty([])).toBe(0); |
| 79 | + expect(coerceNumberProperty([], 111)).toBe(111); |
| 80 | + }); |
| 81 | +}); |
0 commit comments