1 | import {
|
---|
2 | integerFormatter,
|
---|
3 | floatFormatter,
|
---|
4 | keepSignificantDigits,
|
---|
5 | } from "./formatters";
|
---|
6 |
|
---|
7 | describe("Integer formatter", () => {
|
---|
8 | it("should format integer as a string", () => {
|
---|
9 | // given
|
---|
10 | const formatter = integerFormatter(3);
|
---|
11 | // when
|
---|
12 | const result = formatter(123);
|
---|
13 | // then
|
---|
14 | expect(result).toBe("123");
|
---|
15 | });
|
---|
16 |
|
---|
17 | it("should add padding on the left when input has a few digits", () => {
|
---|
18 | // given
|
---|
19 | const formatter = integerFormatter(5);
|
---|
20 | // when
|
---|
21 | const result = formatter(123);
|
---|
22 | // then
|
---|
23 | expect(result).toBe(" 123");
|
---|
24 | });
|
---|
25 | });
|
---|
26 |
|
---|
27 | describe("Integer processor", () => {
|
---|
28 | it("should keep value unchanged when value small enough comapred to number of value digits", () => {
|
---|
29 | // given
|
---|
30 | const processor = keepSignificantDigits(3);
|
---|
31 | // when
|
---|
32 | const result = processor(421);
|
---|
33 | // then
|
---|
34 | expect(result).toBe(421);
|
---|
35 | });
|
---|
36 |
|
---|
37 | it("should lower value when value has more digits than what is needed", () => {
|
---|
38 | // given
|
---|
39 | const processor = keepSignificantDigits(3);
|
---|
40 | // when
|
---|
41 | const result = processor(123456);
|
---|
42 | // then
|
---|
43 | expect(result).toBe(123000);
|
---|
44 | });
|
---|
45 | });
|
---|
46 |
|
---|
47 | describe("Float formatter", () => {
|
---|
48 | it("should format float as a string", () => {
|
---|
49 | // given
|
---|
50 | const formatter = floatFormatter(5, 2);
|
---|
51 | // when
|
---|
52 | const result = formatter(12.34);
|
---|
53 | // then
|
---|
54 | expect(result).toBe("12.34");
|
---|
55 | });
|
---|
56 |
|
---|
57 | it("should format float as a string with given number of fraction digits", () => {
|
---|
58 | // given
|
---|
59 | const formatter = floatFormatter(5, 2);
|
---|
60 | // when
|
---|
61 | const result = formatter(12.342);
|
---|
62 | // then
|
---|
63 | expect(result).toBe("12.34");
|
---|
64 | });
|
---|
65 |
|
---|
66 | it("should format float as a string adding fraction digits", () => {
|
---|
67 | // given
|
---|
68 | const formatter = floatFormatter(5, 2);
|
---|
69 | // when
|
---|
70 | const result = formatter(12.3);
|
---|
71 | // then
|
---|
72 | expect(result).toBe("12.30");
|
---|
73 | });
|
---|
74 |
|
---|
75 | it("should format the whole float input even with lots of digits", () => {
|
---|
76 | // given
|
---|
77 | const formatter = floatFormatter(5, 2);
|
---|
78 | // when
|
---|
79 | const result = formatter(12456789.34);
|
---|
80 | // then
|
---|
81 | expect(result).toBe("12456789.34");
|
---|
82 | });
|
---|
83 |
|
---|
84 | it("should add padding on the left when not enough digits", () => {
|
---|
85 | // given
|
---|
86 | const formatter = floatFormatter(5, 2);
|
---|
87 | // when
|
---|
88 | const result = formatter(9.34);
|
---|
89 | // then
|
---|
90 | expect(result).toBe(" 9.34");
|
---|
91 | });
|
---|
92 | });
|
---|