getComparisonInfo.test.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. getComparisonInfo,
  21. ComparisonTimeRangeType,
  22. VizType,
  23. } from '@superset-ui/core';
  24. const form_data = {
  25. datasource: '22__table',
  26. viz_type: VizType.BigNumberPeriodOverPeriod,
  27. slice_id: 97,
  28. url_params: {
  29. form_data_key:
  30. 'TaBakyDiAx2VsQ47gLmlsJKeN4foqnoxUKdbQrM05qnKMRjO9PDe42iZN1oxmxZ8',
  31. save_action: 'overwrite',
  32. slice_id: '97',
  33. },
  34. metrics: ['count'],
  35. adhoc_filters: [
  36. {
  37. clause: 'WHERE',
  38. comparator: '2004-02-16 : 2024-02-16',
  39. datasourceWarning: false,
  40. expressionType: 'SIMPLE',
  41. filterOptionName: 'filter_8274fo9pogn_ihi8x28o7a',
  42. isExtra: false,
  43. isNew: false,
  44. operator: 'TEMPORAL_RANGE',
  45. sqlExpression: null,
  46. subject: 'order_date',
  47. } as any,
  48. ],
  49. time_comparison: 'y',
  50. adhoc_custom: [
  51. {
  52. clause: 'WHERE',
  53. comparator: 'No filter',
  54. expressionType: 'SIMPLE',
  55. operator: 'TEMPORAL_RANGE',
  56. subject: 'order_date',
  57. },
  58. ],
  59. row_limit: 10000,
  60. y_axis_format: 'SMART_NUMBER',
  61. header_font_size: 60,
  62. subheader_font_size: 26,
  63. comparison_color_enabled: true,
  64. column_config: {
  65. name: {
  66. visible: true,
  67. },
  68. },
  69. extra_form_data: {},
  70. force: false,
  71. result_format: 'json',
  72. result_type: 'full',
  73. };
  74. const mockExtraFormData = {
  75. time_range: 'new and cool range from extra form data',
  76. };
  77. describe('getComparisonInfo', () => {
  78. it('Keeps the original adhoc_filters since no extra data was passed', () => {
  79. const resultFormData = getComparisonInfo(
  80. form_data,
  81. ComparisonTimeRangeType.Year,
  82. {},
  83. );
  84. expect(resultFormData).toEqual(form_data);
  85. });
  86. it('Updates the time_range of the adhoc_filters when extra form data is passed', () => {
  87. const resultFormData = getComparisonInfo(
  88. form_data,
  89. ComparisonTimeRangeType.Month,
  90. mockExtraFormData,
  91. );
  92. const expectedFilters = [
  93. {
  94. clause: 'WHERE',
  95. comparator: 'new and cool range from extra form data',
  96. datasourceWarning: false,
  97. expressionType: 'SIMPLE',
  98. filterOptionName: 'filter_8274fo9pogn_ihi8x28o7a',
  99. isExtra: false,
  100. isNew: false,
  101. operator: 'TEMPORAL_RANGE',
  102. sqlExpression: null,
  103. subject: 'order_date',
  104. } as any,
  105. ];
  106. expect(resultFormData.adhoc_filters?.length).toEqual(1);
  107. expect(resultFormData.adhoc_filters).toEqual(expectedFilters);
  108. });
  109. it('handles no time range filters', () => {
  110. const resultFormData = getComparisonInfo(
  111. {
  112. ...form_data,
  113. adhoc_filters: [
  114. {
  115. expressionType: 'SIMPLE',
  116. subject: 'address_line1',
  117. operator: 'IN',
  118. comparator: ['7734 Strong St.'],
  119. clause: 'WHERE',
  120. isExtra: false,
  121. },
  122. ],
  123. },
  124. ComparisonTimeRangeType.Week,
  125. {},
  126. );
  127. const expectedFilters = [
  128. {
  129. expressionType: 'SIMPLE',
  130. subject: 'address_line1',
  131. operator: 'IN',
  132. comparator: ['7734 Strong St.'],
  133. clause: 'WHERE',
  134. isExtra: false,
  135. },
  136. ];
  137. expect(resultFormData.adhoc_filters?.length).toEqual(1);
  138. expect(resultFormData.adhoc_filters?.[0]).toEqual(expectedFilters[0]);
  139. });
  140. it('If adhoc_filter is undefined the code wont break', () => {
  141. const resultFormData = getComparisonInfo(
  142. {
  143. ...form_data,
  144. adhoc_filters: undefined,
  145. },
  146. ComparisonTimeRangeType.InheritedRange,
  147. {},
  148. );
  149. expect(resultFormData.adhoc_filters?.length).toEqual(0);
  150. expect(resultFormData.adhoc_filters).toEqual([]);
  151. });
  152. it('Handles the custom time filters and return the correct time shift text', () => {
  153. const resultFormData = getComparisonInfo(
  154. form_data,
  155. ComparisonTimeRangeType.Custom,
  156. {},
  157. );
  158. const expectedFilters = [
  159. {
  160. clause: 'WHERE',
  161. comparator: 'No filter',
  162. expressionType: 'SIMPLE',
  163. operator: 'TEMPORAL_RANGE',
  164. subject: 'order_date',
  165. },
  166. ];
  167. expect(resultFormData.adhoc_filters?.length).toEqual(1);
  168. expect(resultFormData.adhoc_filters).toEqual(expectedFilters);
  169. });
  170. it('Updates comparison display values when toggled', () => {
  171. const resultFormData = getComparisonInfo(
  172. {
  173. ...form_data,
  174. column_config: {
  175. name: {
  176. visible: false,
  177. },
  178. },
  179. },
  180. ComparisonTimeRangeType.Year,
  181. {},
  182. );
  183. expect(resultFormData.column_config.name.visible).toEqual(false);
  184. });
  185. });