NumberFormatterRegistrySingleton.test.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. NumberFormatterRegistry,
  21. getNumberFormatterRegistry,
  22. setD3Format,
  23. getNumberFormatter,
  24. formatNumber,
  25. } from '@superset-ui/core';
  26. describe('NumberFormatterRegistrySingleton', () => {
  27. describe('getNumberFormatterRegistry()', () => {
  28. it('returns a NumberFormatterRegistry', () => {
  29. expect(getNumberFormatterRegistry()).toBeInstanceOf(
  30. NumberFormatterRegistry,
  31. );
  32. });
  33. });
  34. describe('getNumberFormatter(format)', () => {
  35. it('returns a format function', () => {
  36. const format = getNumberFormatter('.3s');
  37. expect(format(12345)).toEqual('12.3k');
  38. });
  39. it('returns a format function even given invalid format', () => {
  40. const format = getNumberFormatter('xkcd');
  41. expect(format(12345)).toEqual('12345 (Invalid format: xkcd)');
  42. });
  43. it('falls back to default format if format is not specified', () => {
  44. const formatter = getNumberFormatter();
  45. expect(formatter.format(100)).toEqual('100');
  46. });
  47. });
  48. describe('formatNumber(format, value)', () => {
  49. it('format the given number using the specified format', () => {
  50. const output = formatNumber('.3s', 12345);
  51. expect(output).toEqual('12.3k');
  52. });
  53. it('falls back to the default formatter if the format is undefined', () => {
  54. expect(formatNumber(undefined, 1000)).toEqual('1k');
  55. });
  56. });
  57. describe('setD3Format()', () => {
  58. it('sets a specific FormatLocaleDefinition', () => {
  59. setD3Format({
  60. decimal: ';',
  61. thousands: '-',
  62. currency: ['€', ''],
  63. grouping: [2],
  64. });
  65. const formatter = getNumberFormatter('$,.2f');
  66. expect(formatter.format(12345.67)).toEqual('€1-23-45;67');
  67. });
  68. it('falls back to default value for unspecified locale format parameters', () => {
  69. setD3Format({
  70. currency: ['€', ''],
  71. });
  72. const formatter = getNumberFormatter('$,.1f');
  73. expect(formatter.format(12345.67)).toEqual('€12,345.7');
  74. });
  75. });
  76. });