NumberFormatterRegistry.test.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. NumberFormats,
  21. NumberFormatter,
  22. NumberFormatterRegistry,
  23. } from '@superset-ui/core';
  24. describe('NumberFormatterRegistry', () => {
  25. let registry: NumberFormatterRegistry;
  26. beforeEach(() => {
  27. registry = new NumberFormatterRegistry();
  28. });
  29. it('has SMART_NUMBER as default formatter out of the box', () => {
  30. expect(registry.getDefaultKey()).toBe(NumberFormats.SMART_NUMBER);
  31. });
  32. describe('.get(format)', () => {
  33. it('creates and returns a new formatter if does not exist', () => {
  34. const formatter = registry.get('.2f');
  35. expect(formatter).toBeInstanceOf(NumberFormatter);
  36. expect(formatter.format(100)).toEqual('100.00');
  37. });
  38. it('returns an existing formatter if already exists', () => {
  39. const formatter = registry.get('.2f');
  40. const formatter2 = registry.get('.2f');
  41. expect(formatter).toBe(formatter2);
  42. });
  43. it('falls back to default format if format is not specified', () => {
  44. registry.setDefaultKey('.1f');
  45. const formatter = registry.get();
  46. expect(formatter.format(100)).toEqual('100.0');
  47. });
  48. it('falls back to default format if format is null', () => {
  49. registry.setDefaultKey('.1f');
  50. // @ts-ignore
  51. const formatter = registry.get(null);
  52. expect(formatter.format(100)).toEqual('100.0');
  53. });
  54. it('falls back to default format if format is undefined', () => {
  55. registry.setDefaultKey('.1f');
  56. const formatter = registry.get(undefined);
  57. expect(formatter.format(100)).toEqual('100.0');
  58. });
  59. it('falls back to default format if format is empty string', () => {
  60. registry.setDefaultKey('.1f');
  61. const formatter = registry.get('');
  62. expect(formatter.format(100)).toEqual('100.0');
  63. });
  64. it('removes leading and trailing spaces from format', () => {
  65. const formatter = registry.get(' .2f');
  66. expect(formatter).toBeInstanceOf(NumberFormatter);
  67. expect(formatter.format(100)).toEqual('100.00');
  68. const formatter2 = registry.get('.2f ');
  69. expect(formatter2).toBeInstanceOf(NumberFormatter);
  70. expect(formatter2.format(100)).toEqual('100.00');
  71. const formatter3 = registry.get(' .2f ');
  72. expect(formatter3).toBeInstanceOf(NumberFormatter);
  73. expect(formatter3.format(100)).toEqual('100.00');
  74. });
  75. });
  76. describe('.format(format, value)', () => {
  77. it('return the value with the specified format', () => {
  78. expect(registry.format('.2f', 100)).toEqual('100.00');
  79. expect(registry.format(',d', 100)).toEqual('100');
  80. });
  81. it('falls back to the default formatter if the format is undefined', () => {
  82. expect(registry.format(undefined, 1000)).toEqual('1k');
  83. });
  84. });
  85. });