utils.test.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. buildCustomFormatters,
  21. Currency,
  22. CurrencyFormatter,
  23. getCustomFormatter,
  24. getNumberFormatter,
  25. getValueFormatter,
  26. NumberFormatter,
  27. ValueFormatter,
  28. } from '@superset-ui/core';
  29. test('buildCustomFormatters without saved metrics returns empty object', () => {
  30. expect(
  31. buildCustomFormatters(
  32. [
  33. {
  34. expressionType: 'SIMPLE',
  35. aggregate: 'COUNT',
  36. column: { column_name: 'test' },
  37. },
  38. ],
  39. {
  40. sum__num: { symbol: 'USD', symbolPosition: 'prefix' },
  41. },
  42. {},
  43. ',.1f',
  44. undefined,
  45. ),
  46. ).toEqual({});
  47. expect(
  48. buildCustomFormatters(
  49. undefined,
  50. {
  51. sum__num: { symbol: 'USD', symbolPosition: 'prefix' },
  52. },
  53. {},
  54. ',.1f',
  55. undefined,
  56. ),
  57. ).toEqual({});
  58. });
  59. test('buildCustomFormatters with saved metrics returns custom formatters object', () => {
  60. const customFormatters: Record<string, ValueFormatter> =
  61. buildCustomFormatters(
  62. [
  63. {
  64. expressionType: 'SIMPLE',
  65. aggregate: 'COUNT',
  66. column: { column_name: 'test' },
  67. },
  68. 'sum__num',
  69. 'count',
  70. ],
  71. {
  72. sum__num: { symbol: 'USD', symbolPosition: 'prefix' },
  73. },
  74. { sum__num: ',.2' },
  75. ',.1f',
  76. undefined,
  77. );
  78. expect(customFormatters).toEqual({
  79. sum__num: expect.any(Function),
  80. count: expect.any(Function),
  81. });
  82. expect(customFormatters.sum__num).toBeInstanceOf(CurrencyFormatter);
  83. expect(customFormatters.count).toBeInstanceOf(NumberFormatter);
  84. expect((customFormatters.sum__num as CurrencyFormatter).d3Format).toEqual(
  85. ',.1f',
  86. );
  87. });
  88. test('buildCustomFormatters uses dataset d3 format if not provided in control panel', () => {
  89. const customFormatters: Record<string, ValueFormatter> =
  90. buildCustomFormatters(
  91. [
  92. {
  93. expressionType: 'SIMPLE',
  94. aggregate: 'COUNT',
  95. column: { column_name: 'test' },
  96. },
  97. 'sum__num',
  98. 'count',
  99. ],
  100. {
  101. sum__num: { symbol: 'USD', symbolPosition: 'prefix' },
  102. },
  103. { sum__num: ',.2' },
  104. undefined,
  105. undefined,
  106. );
  107. expect((customFormatters.sum__num as CurrencyFormatter).d3Format).toEqual(
  108. ',.2',
  109. );
  110. });
  111. test('buildCustomFormatters returns NumberFormatter for a d3format with currency set to {}', () => {
  112. const customFormatters: Record<string, ValueFormatter> =
  113. buildCustomFormatters(
  114. ['count'],
  115. { count: {} as Currency },
  116. { count: ',.2%' },
  117. undefined,
  118. undefined,
  119. );
  120. expect(customFormatters.count).toBeInstanceOf(NumberFormatter);
  121. });
  122. test('getCustomFormatter', () => {
  123. const customFormatters = {
  124. sum__num: new CurrencyFormatter({
  125. currency: { symbol: 'USD', symbolPosition: 'prefix' },
  126. }),
  127. count: getNumberFormatter(),
  128. };
  129. expect(getCustomFormatter(customFormatters, 'count')).toEqual(
  130. customFormatters.count,
  131. );
  132. expect(
  133. getCustomFormatter(customFormatters, ['count', 'sum__num'], 'count'),
  134. ).toEqual(customFormatters.count);
  135. expect(getCustomFormatter(customFormatters, ['count', 'sum__num'])).toEqual(
  136. undefined,
  137. );
  138. });
  139. test('getValueFormatter', () => {
  140. expect(
  141. getValueFormatter(['count', 'sum__num'], {}, {}, ',.1f', undefined),
  142. ).toBeInstanceOf(NumberFormatter);
  143. expect(
  144. getValueFormatter(
  145. ['count', 'sum__num'],
  146. {},
  147. {},
  148. ',.1f',
  149. undefined,
  150. 'count',
  151. ),
  152. ).toBeInstanceOf(NumberFormatter);
  153. expect(
  154. getValueFormatter(
  155. ['count', 'sum__num'],
  156. { count: { symbol: 'USD', symbolPosition: 'prefix' } },
  157. {},
  158. ',.1f',
  159. undefined,
  160. 'count',
  161. ),
  162. ).toBeInstanceOf(CurrencyFormatter);
  163. });
  164. test('getValueFormatter with currency from control panel', () => {
  165. const countFormatter = getValueFormatter(
  166. ['count', 'sum__num'],
  167. { count: { symbol: 'USD', symbolPosition: 'prefix' } },
  168. {},
  169. ',.1f',
  170. { symbol: 'EUR', symbolPosition: 'suffix' },
  171. 'count',
  172. );
  173. expect(countFormatter).toBeInstanceOf(CurrencyFormatter);
  174. expect((countFormatter as CurrencyFormatter).currency).toEqual({
  175. symbol: 'EUR',
  176. symbolPosition: 'suffix',
  177. });
  178. });
  179. test('getValueFormatter with currency from control panel when no saved currencies', () => {
  180. const formatter = getValueFormatter(
  181. ['count', 'sum__num'],
  182. {},
  183. {},
  184. ',.1f',
  185. { symbol: 'EUR', symbolPosition: 'suffix' },
  186. undefined,
  187. );
  188. expect(formatter).toBeInstanceOf(CurrencyFormatter);
  189. expect((formatter as CurrencyFormatter).currency).toEqual({
  190. symbol: 'EUR',
  191. symbolPosition: 'suffix',
  192. });
  193. });
  194. test('getValueFormatter return NumberFormatter when no currency formatters', () => {
  195. const formatter = getValueFormatter(
  196. ['count', 'sum__num'],
  197. {},
  198. {},
  199. ',.1f',
  200. undefined,
  201. undefined,
  202. );
  203. expect(formatter).toBeInstanceOf(NumberFormatter);
  204. });