|
| 1 | +import { getCurrencyFormatter } from './getCurrencyFormatter'; |
| 2 | + |
| 3 | +describe('contexts/utils/getCurrencyFormatter', () => { |
| 4 | + const formatter = getCurrencyFormatter(); |
| 5 | + it('should return with the dollar sign and two fraction numbers', () => { |
| 6 | + const value = formatter(12); |
| 7 | + expect(value).toBe('$12.00'); |
| 8 | + |
| 9 | + const value2 = formatter(12.1); |
| 10 | + expect(value2).toBe('$12.10'); |
| 11 | + |
| 12 | + const value3 = formatter(12.01); |
| 13 | + expect(value3).toBe('$12.01'); |
| 14 | + |
| 15 | + const value4 = formatter(12.011); |
| 16 | + expect(value4).toBe('$12.01'); |
| 17 | + }); |
| 18 | + it('should return with the dollar sign and the first two fraction numbers when the integer is a zero', () => { |
| 19 | + const value = formatter(0.123); |
| 20 | + expect(value).toBe('$0.12'); |
| 21 | + |
| 22 | + const value2 = formatter(0.02); |
| 23 | + expect(value2).toBe('$0.02'); |
| 24 | + |
| 25 | + const value3 = formatter(0.0234); |
| 26 | + expect(value3).toBe('$0.02'); |
| 27 | + }); |
| 28 | + it('should return with the dollar sign and look for the first non-zero fraction number', () => { |
| 29 | + const value = formatter(0.00023); |
| 30 | + expect(value).toBe('$0.0002'); |
| 31 | + |
| 32 | + const value2 = formatter(0.000023); |
| 33 | + expect(value2).toBe('$0.00002'); |
| 34 | + |
| 35 | + // NOTE: this is the last point (in terms of the number of the fraction part) we handle, below that the scientific notation is not something we deal with at the moment |
| 36 | + const value3 = formatter(0.0000023); |
| 37 | + expect(value3).toBe('$0.000002'); |
| 38 | + |
| 39 | + // this is with scientific notation |
| 40 | + const value4 = formatter(0.00000023); |
| 41 | + expect(value4).not.toBe('$0.0000002'); |
| 42 | + }); |
| 43 | +}); |
0 commit comments