CurrencyFormatter.test.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. import {
  20. CurrencyFormatter,
  21. getCurrencySymbol,
  22. NumberFormats,
  23. } from '@superset-ui/core';
  24. test('getCurrencySymbol', () => {
  25. expect(
  26. getCurrencySymbol({ symbol: 'PLN', symbolPosition: 'prefix' }),
  27. ).toEqual('PLN');
  28. expect(
  29. getCurrencySymbol({ symbol: 'USD', symbolPosition: 'prefix' }),
  30. ).toEqual('$');
  31. expect(() =>
  32. getCurrencySymbol({ symbol: 'INVALID_CODE', symbolPosition: 'prefix' }),
  33. ).toThrow(RangeError);
  34. });
  35. test('CurrencyFormatter object fields', () => {
  36. const defaultCurrencyFormatter = new CurrencyFormatter({
  37. currency: { symbol: 'USD', symbolPosition: 'prefix' },
  38. });
  39. expect(defaultCurrencyFormatter.d3Format).toEqual(NumberFormats.SMART_NUMBER);
  40. expect(defaultCurrencyFormatter.locale).toEqual('en-US');
  41. expect(defaultCurrencyFormatter.currency).toEqual({
  42. symbol: 'USD',
  43. symbolPosition: 'prefix',
  44. });
  45. const currencyFormatter = new CurrencyFormatter({
  46. currency: { symbol: 'PLN', symbolPosition: 'suffix' },
  47. locale: 'pl-PL',
  48. d3Format: ',.1f',
  49. });
  50. expect(currencyFormatter.d3Format).toEqual(',.1f');
  51. expect(currencyFormatter.locale).toEqual('pl-PL');
  52. expect(currencyFormatter.currency).toEqual({
  53. symbol: 'PLN',
  54. symbolPosition: 'suffix',
  55. });
  56. });
  57. test('CurrencyFormatter:hasValidCurrency', () => {
  58. const currencyFormatter = new CurrencyFormatter({
  59. currency: { symbol: 'USD', symbolPosition: 'prefix' },
  60. });
  61. expect(currencyFormatter.hasValidCurrency()).toBe(true);
  62. const currencyFormatterWithoutPosition = new CurrencyFormatter({
  63. // @ts-ignore
  64. currency: { symbol: 'USD' },
  65. });
  66. expect(currencyFormatterWithoutPosition.hasValidCurrency()).toBe(true);
  67. const currencyFormatterWithoutSymbol = new CurrencyFormatter({
  68. // @ts-ignore
  69. currency: { symbolPosition: 'prefix' },
  70. });
  71. expect(currencyFormatterWithoutSymbol.hasValidCurrency()).toBe(false);
  72. // @ts-ignore
  73. const currencyFormatterWithoutCurrency = new CurrencyFormatter({});
  74. expect(currencyFormatterWithoutCurrency.hasValidCurrency()).toBe(false);
  75. });
  76. test('CurrencyFormatter:getNormalizedD3Format', () => {
  77. const currencyFormatter = new CurrencyFormatter({
  78. currency: { symbol: 'USD', symbolPosition: 'prefix' },
  79. });
  80. expect(currencyFormatter.getNormalizedD3Format()).toEqual(
  81. currencyFormatter.d3Format,
  82. );
  83. const currencyFormatter2 = new CurrencyFormatter({
  84. currency: { symbol: 'USD', symbolPosition: 'prefix' },
  85. d3Format: ',.1f',
  86. });
  87. expect(currencyFormatter2.getNormalizedD3Format()).toEqual(
  88. currencyFormatter2.d3Format,
  89. );
  90. const currencyFormatter3 = new CurrencyFormatter({
  91. currency: { symbol: 'USD', symbolPosition: 'prefix' },
  92. d3Format: '$,.1f',
  93. });
  94. expect(currencyFormatter3.getNormalizedD3Format()).toEqual(',.1f');
  95. const currencyFormatter4 = new CurrencyFormatter({
  96. currency: { symbol: 'USD', symbolPosition: 'prefix' },
  97. d3Format: ',.1%',
  98. });
  99. expect(currencyFormatter4.getNormalizedD3Format()).toEqual(',.1');
  100. });
  101. test('CurrencyFormatter:format', () => {
  102. const VALUE = 56100057;
  103. const currencyFormatterWithPrefix = new CurrencyFormatter({
  104. currency: { symbol: 'USD', symbolPosition: 'prefix' },
  105. });
  106. expect(currencyFormatterWithPrefix(VALUE)).toEqual(
  107. currencyFormatterWithPrefix.format(VALUE),
  108. );
  109. expect(currencyFormatterWithPrefix(VALUE)).toEqual('$ 56.1M');
  110. const currencyFormatterWithSuffix = new CurrencyFormatter({
  111. currency: { symbol: 'USD', symbolPosition: 'suffix' },
  112. });
  113. expect(currencyFormatterWithSuffix(VALUE)).toEqual('56.1M $');
  114. const currencyFormatterWithoutPosition = new CurrencyFormatter({
  115. // @ts-ignore
  116. currency: { symbol: 'USD' },
  117. });
  118. expect(currencyFormatterWithoutPosition(VALUE)).toEqual('56.1M $');
  119. // @ts-ignore
  120. const currencyFormatterWithoutCurrency = new CurrencyFormatter({});
  121. expect(currencyFormatterWithoutCurrency(VALUE)).toEqual('56.1M');
  122. const currencyFormatterWithCustomD3 = new CurrencyFormatter({
  123. currency: { symbol: 'USD', symbolPosition: 'prefix' },
  124. d3Format: ',.1f',
  125. });
  126. expect(currencyFormatterWithCustomD3(VALUE)).toEqual('$ 56,100,057.0');
  127. const currencyFormatterWithPercentD3 = new CurrencyFormatter({
  128. currency: { symbol: 'USD', symbolPosition: 'prefix' },
  129. d3Format: ',.1f%',
  130. });
  131. expect(currencyFormatterWithPercentD3(VALUE)).toEqual('$ 56,100,057.0');
  132. const currencyFormatterWithCurrencyD3 = new CurrencyFormatter({
  133. currency: { symbol: 'PLN', symbolPosition: 'suffix' },
  134. d3Format: '$,.1f',
  135. });
  136. expect(currencyFormatterWithCurrencyD3(VALUE)).toEqual('56,100,057.0 PLN');
  137. });