formatters.spec.js 2.7 KB

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