NumberFormatter.test.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 { NumberFormatter } from '@superset-ui/core';
  20. describe('NumberFormatter', () => {
  21. describe('new NumberFormatter(config)', () => {
  22. it('requires config.id', () => {
  23. expect(
  24. () =>
  25. // @ts-ignore
  26. new NumberFormatter({
  27. formatFunc: () => '',
  28. }),
  29. ).toThrow();
  30. });
  31. it('requires config.formatFunc', () => {
  32. expect(
  33. () =>
  34. // @ts-ignore
  35. new NumberFormatter({
  36. id: 'my_format',
  37. }),
  38. ).toThrow();
  39. });
  40. });
  41. describe('formatter is also a format function itself', () => {
  42. const formatter = new NumberFormatter({
  43. id: 'fixed_3',
  44. formatFunc: value => value.toFixed(3),
  45. });
  46. it('returns formatted value', () => {
  47. expect(formatter(12345.67)).toEqual('12345.670');
  48. });
  49. it('formatter(value) is the same with formatter.format(value)', () => {
  50. const value = 12345.67;
  51. expect(formatter(value)).toEqual(formatter.format(value));
  52. });
  53. });
  54. describe('.format(value)', () => {
  55. const formatter = new NumberFormatter({
  56. id: 'fixed_3',
  57. formatFunc: value => value.toFixed(3),
  58. });
  59. it('handles null', () => {
  60. expect(formatter.format(null)).toEqual('null');
  61. });
  62. it('handles undefined', () => {
  63. expect(formatter.format(undefined)).toEqual('undefined');
  64. });
  65. it('handles NaN', () => {
  66. expect(formatter.format(NaN)).toEqual('NaN');
  67. });
  68. it('handles positive and negative infinity', () => {
  69. expect(formatter.format(Number.POSITIVE_INFINITY)).toEqual('∞');
  70. expect(formatter.format(Number.NEGATIVE_INFINITY)).toEqual('-∞');
  71. });
  72. it('otherwise returns formatted value', () => {
  73. expect(formatter.format(12345.67)).toEqual('12345.670');
  74. });
  75. });
  76. describe('.preview(value)', () => {
  77. const formatter = new NumberFormatter({
  78. id: 'fixed_2',
  79. formatFunc: value => value.toFixed(2),
  80. });
  81. it('returns string comparing value before and after formatting', () => {
  82. expect(formatter.preview(100)).toEqual('100 => 100.00');
  83. });
  84. it('uses the default preview value if not specified', () => {
  85. expect(formatter.preview()).toEqual('12345.432 => 12345.43');
  86. });
  87. });
  88. });