TimeFormatter.test.ts 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 { TimeFormatter, PREVIEW_TIME } from '@superset-ui/core';
  20. describe('TimeFormatter', () => {
  21. describe('new TimeFormatter(config)', () => {
  22. it('requires config.id', () => {
  23. expect(
  24. () =>
  25. // @ts-ignore -- intentionally pass invalid input
  26. new TimeFormatter({
  27. formatFunc: () => 'test',
  28. }),
  29. ).toThrow();
  30. });
  31. it('requires config.formatFunc', () => {
  32. expect(
  33. () =>
  34. // @ts-ignore -- intentionally pass invalid input
  35. new TimeFormatter({
  36. id: 'my_format',
  37. }),
  38. ).toThrow();
  39. });
  40. });
  41. describe('formatter is also a format function itself', () => {
  42. const formatter = new TimeFormatter({
  43. id: 'year_only',
  44. formatFunc: (value: Date) => `${value.getFullYear()}`,
  45. });
  46. it('returns formatted value', () => {
  47. expect(formatter(PREVIEW_TIME)).toEqual('2017');
  48. });
  49. it('formatter(value) is the same with formatter.format(value)', () => {
  50. const value = PREVIEW_TIME;
  51. expect(formatter(value)).toEqual(formatter.format(value));
  52. });
  53. });
  54. describe('.format(value)', () => {
  55. const formatter = new TimeFormatter({
  56. id: 'year_only',
  57. formatFunc: value => `${value.getFullYear()}`,
  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 number, treating it as a timestamp', () => {
  66. expect(formatter.format(PREVIEW_TIME.getTime())).toEqual('2017');
  67. });
  68. it('otherwise returns formatted value', () => {
  69. expect(formatter.format(PREVIEW_TIME)).toEqual('2017');
  70. });
  71. });
  72. describe('.preview(value)', () => {
  73. const formatter = new TimeFormatter({
  74. id: 'year_only',
  75. formatFunc: value => `${value.getFullYear()}`,
  76. });
  77. it('returns string comparing value before and after formatting', () => {
  78. const time = new Date(Date.UTC(2018, 10, 21, 22, 11, 44));
  79. expect(formatter.preview(time)).toEqual(
  80. 'Wed, 21 Nov 2018 22:11:44 GMT => 2018',
  81. );
  82. });
  83. it('uses the default preview value if not specified', () => {
  84. expect(formatter.preview()).toEqual(
  85. 'Tue, 14 Feb 2017 11:22:33 GMT => 2017',
  86. );
  87. });
  88. });
  89. });