TimeFormatterRegistry.test.ts 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 { TimeLocaleDefinition } from 'd3-time-format';
  20. import { TimeFormats, TimeFormatter, PREVIEW_TIME } from '@superset-ui/core';
  21. import TimeFormatterRegistry from '../../src/time-format/TimeFormatterRegistry';
  22. import { DEFAULT_D3_TIME_FORMAT } from '../../src/time-format';
  23. describe('TimeFormatterRegistry', () => {
  24. let registry: TimeFormatterRegistry;
  25. beforeEach(() => {
  26. registry = new TimeFormatterRegistry();
  27. });
  28. describe('.get(format)', () => {
  29. it('creates and returns a new formatter if does not exist', () => {
  30. const formatter = registry.get(TimeFormats.DATABASE_DATETIME);
  31. expect(formatter).toBeInstanceOf(TimeFormatter);
  32. expect(formatter.format(PREVIEW_TIME)).toEqual('2017-02-14 11:22:33');
  33. });
  34. it('returns an existing formatter if already exists', () => {
  35. const formatter = registry.get(TimeFormats.TIME);
  36. const formatter2 = registry.get(TimeFormats.TIME);
  37. expect(formatter).toBe(formatter2);
  38. });
  39. it('falls back to default format if format is not specified', () => {
  40. registry.setDefaultKey(TimeFormats.INTERNATIONAL_DATE);
  41. const formatter = registry.get();
  42. expect(formatter.format(PREVIEW_TIME)).toEqual('14/02/2017');
  43. });
  44. it('falls back to default format if format is null', () => {
  45. registry.setDefaultKey(TimeFormats.INTERNATIONAL_DATE);
  46. // @ts-ignore
  47. const formatter = registry.get(null);
  48. expect(formatter.format(PREVIEW_TIME)).toEqual('14/02/2017');
  49. });
  50. it('falls back to default format if format is undefined', () => {
  51. registry.setDefaultKey(TimeFormats.INTERNATIONAL_DATE);
  52. const formatter = registry.get(undefined);
  53. expect(formatter.format(PREVIEW_TIME)).toEqual('14/02/2017');
  54. });
  55. it('falls back to default format if format is empty string', () => {
  56. registry.setDefaultKey(TimeFormats.INTERNATIONAL_DATE);
  57. const formatter = registry.get('');
  58. expect(formatter.format(PREVIEW_TIME)).toEqual('14/02/2017');
  59. });
  60. it('removes leading and trailing spaces from format', () => {
  61. const formatter = registry.get(' %Y ');
  62. expect(formatter).toBeInstanceOf(TimeFormatter);
  63. expect(formatter.format(PREVIEW_TIME)).toEqual('2017');
  64. });
  65. });
  66. describe('.format(format, value)', () => {
  67. it('return the value with the specified format', () => {
  68. expect(registry.format(TimeFormats.US_DATE, PREVIEW_TIME)).toEqual(
  69. '02/14/2017',
  70. );
  71. expect(registry.format(TimeFormats.TIME, PREVIEW_TIME)).toEqual(
  72. '11:22:33',
  73. );
  74. });
  75. it('falls back to the default formatter if the format is undefined', () => {
  76. expect(registry.format(undefined, PREVIEW_TIME)).toEqual(
  77. '2017-02-14 11:22:33',
  78. );
  79. });
  80. });
  81. describe('.setD3Format(d3Format)', () => {
  82. describe('when partial value is specified', () => {
  83. const timeFormat: Partial<TimeLocaleDefinition> = {
  84. days: [
  85. 'Domingo',
  86. 'Segunda',
  87. 'Terça',
  88. 'Quarta',
  89. 'Quinta',
  90. 'Sexta',
  91. 'Sábado',
  92. ],
  93. };
  94. beforeEach(() => {
  95. registry.setD3Format(timeFormat);
  96. });
  97. it('sets the specified value and default', () => {
  98. expect(registry.d3Format).toEqual({
  99. ...DEFAULT_D3_TIME_FORMAT,
  100. ...timeFormat,
  101. });
  102. });
  103. it('does not change short days of week name format', () => {
  104. expect(registry.format('%a', PREVIEW_TIME)).toEqual('Tue');
  105. });
  106. it('changes full days of week name format', () => {
  107. expect(registry.format('%A', PREVIEW_TIME)).toEqual('Terça');
  108. });
  109. it('does not change months format', () => {
  110. expect(registry.format('%b', PREVIEW_TIME)).toEqual('Feb');
  111. expect(registry.format('%B', PREVIEW_TIME)).toEqual('February');
  112. });
  113. });
  114. describe('when full value is specified', () => {
  115. const timeFormat: TimeLocaleDefinition = {
  116. dateTime: '%A, %e de %B de %Y. %X',
  117. date: '%d/%m/%Y',
  118. time: '%H:%M:%S',
  119. periods: ['AM', 'PM'],
  120. days: [
  121. 'Domingo',
  122. 'Segunda',
  123. 'Terça',
  124. 'Quarta',
  125. 'Quinta',
  126. 'Sexta',
  127. 'Sábado',
  128. ],
  129. shortDays: ['Dom', 'Seg', 'Ter', 'Qua', 'Qui', 'Sex', 'Sáb'],
  130. months: [
  131. 'Janeiro',
  132. 'Fevereiro',
  133. 'Março',
  134. 'Abril',
  135. 'Maio',
  136. 'Junho',
  137. 'Julho',
  138. 'Agosto',
  139. 'Setembro',
  140. 'Outubro',
  141. 'Novembro',
  142. 'Dezembro',
  143. ],
  144. shortMonths: [
  145. 'Jan',
  146. 'Fev',
  147. 'Mar',
  148. 'Abr',
  149. 'Mai',
  150. 'Jun',
  151. 'Jul',
  152. 'Ago',
  153. 'Set',
  154. 'Out',
  155. 'Nov',
  156. 'Dez',
  157. ],
  158. };
  159. beforeEach(() => {
  160. registry.setD3Format(timeFormat);
  161. });
  162. it('sets the specified value ignoring default', () => {
  163. expect(registry.d3Format).toEqual(timeFormat);
  164. });
  165. it('changes days of week format', () => {
  166. expect(registry.format('%a', PREVIEW_TIME)).toEqual('Ter');
  167. expect(registry.format('%A', PREVIEW_TIME)).toEqual('Terça');
  168. });
  169. it('changes months format', () => {
  170. expect(registry.format('%b', PREVIEW_TIME)).toEqual('Fev');
  171. expect(registry.format('%B', PREVIEW_TIME)).toEqual('Fevereiro');
  172. });
  173. });
  174. });
  175. });