transformPropsUtil.test.ts 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. groupByLocation,
  21. getChartConfigs,
  22. parseSelectedChart,
  23. getGeojsonColumns,
  24. createColumnName,
  25. groupByLocationGenericX,
  26. stripGeomFromColnamesAndTypes,
  27. stripGeomColumnFromLabelMap,
  28. } from '../../src/util/transformPropsUtil';
  29. import {
  30. nonTimeSeriesChartData,
  31. groupedTimeseriesChartData,
  32. geom1,
  33. geom2,
  34. groupedTimeseriesLabelMap,
  35. } from '../testData';
  36. describe('transformPropsUtil', () => {
  37. const groupedTimeseriesParams = {
  38. x_axis: 'mydate',
  39. };
  40. const groupedTimeseriesQueryData = {
  41. label_map: groupedTimeseriesLabelMap,
  42. };
  43. describe('getGeojsonColumns', () => {
  44. it('gets the GeoJSON columns', () => {
  45. const columns = ['foo', 'bar', geom1];
  46. const result = getGeojsonColumns(columns);
  47. expect(result).toHaveLength(1);
  48. expect(result[0]).toEqual(2);
  49. });
  50. it('gets multiple GeoJSON columns', () => {
  51. const columns = ['foo', geom2, 'bar', geom1];
  52. const result = getGeojsonColumns(columns);
  53. expect(result).toHaveLength(2);
  54. expect(result[0]).toEqual(1);
  55. expect(result[1]).toEqual(3);
  56. });
  57. it('returns empty array when no GeoJSON is included', () => {
  58. const columns = ['foo', 'bar'];
  59. const result = getGeojsonColumns(columns);
  60. expect(result).toHaveLength(0);
  61. });
  62. });
  63. describe('createColumnName', () => {
  64. it('creates a columns name', () => {
  65. const columns = ['foo', 'bar'];
  66. const result = createColumnName(columns, []);
  67. expect(result).toEqual('foo, bar');
  68. });
  69. it('ignores items provided by ignoreIdx', () => {
  70. const columns = ['foo', 'bar', 'baz'];
  71. const ignoreIdx = [1];
  72. const result = createColumnName(columns, ignoreIdx);
  73. expect(result).toEqual('foo, baz');
  74. });
  75. });
  76. describe('groupByLocationGenericX', () => {
  77. it('groups in the correct count of geometries', () => {
  78. const result = groupByLocationGenericX(
  79. groupedTimeseriesChartData,
  80. groupedTimeseriesParams,
  81. groupedTimeseriesQueryData,
  82. );
  83. const countOfGeometries = Object.keys(result).length;
  84. expect(countOfGeometries).toEqual(2);
  85. });
  86. it('groups items by same geometry', () => {
  87. const result = groupByLocationGenericX(
  88. groupedTimeseriesChartData,
  89. groupedTimeseriesParams,
  90. groupedTimeseriesQueryData,
  91. );
  92. const allGeom1 = result[geom1].length === 2;
  93. const allGeom2 = result[geom2].length === 2;
  94. expect(allGeom1 && allGeom2).toBe(true);
  95. });
  96. });
  97. describe('groupByLocation', () => {
  98. it('groups in the correct count of geometries', () => {
  99. const geometryColumn = 'geom';
  100. const result = groupByLocation(nonTimeSeriesChartData, geometryColumn);
  101. const countOfGeometries = Object.keys(result).length;
  102. expect(countOfGeometries).toEqual(2);
  103. });
  104. it('groups items by same geometry', () => {
  105. const geometryColumn = 'geom';
  106. const result = groupByLocation(nonTimeSeriesChartData, geometryColumn);
  107. const allGeom1 = result[geom1].length === 6;
  108. const allGeom2 = result[geom2].length === 4;
  109. expect(allGeom1 && allGeom2).toBe(true);
  110. });
  111. });
  112. describe('stripGeomFromColnamesAndTypes', () => {
  113. it('strips the geom from colnames with geom column', () => {
  114. const queryData = {
  115. colnames: ['foo', 'geom'],
  116. coltypes: [0, 0],
  117. };
  118. const result = stripGeomFromColnamesAndTypes(queryData, 'geom');
  119. expect(result).toEqual({
  120. colnames: ['foo'],
  121. coltypes: [0],
  122. });
  123. });
  124. it('strips the geom from colnames with grouped columns', () => {
  125. const queryData = {
  126. colnames: ['foo', `bar, ${geom1}`],
  127. coltypes: [0, 0],
  128. };
  129. const result = stripGeomFromColnamesAndTypes(queryData, 'geom');
  130. expect(result).toEqual({
  131. colnames: ['foo', 'bar'],
  132. coltypes: [0, 0],
  133. });
  134. });
  135. it('strips the geom from colnames with grouped columns without geom', () => {
  136. const queryData = {
  137. colnames: ['foo', `bar, baz`],
  138. coltypes: [0, 0],
  139. };
  140. const result = stripGeomFromColnamesAndTypes(queryData, 'geom');
  141. expect(result).toEqual({
  142. colnames: ['foo', 'bar, baz'],
  143. coltypes: [0, 0],
  144. });
  145. });
  146. });
  147. describe('stripGeomColumnFromLabelMap', () => {
  148. it('strips the geom column from label_map', () => {
  149. const labelMap = {
  150. [`apple, ${geom1}`]: ['apple', geom1],
  151. [`${geom2}, lemon`]: [geom2, 'lemon'],
  152. geom: ['geom'],
  153. };
  154. const result = stripGeomColumnFromLabelMap(labelMap, 'geom');
  155. expect(result).toEqual({
  156. apple: ['apple'],
  157. lemon: ['lemon'],
  158. });
  159. });
  160. });
  161. describe('getChartConfigs', () => {
  162. let chartTransformer: jest.MockedFunction<any>;
  163. const geomColumn = 'geom';
  164. const pieChartConfig = {
  165. params: {},
  166. viz_type: 'pie',
  167. };
  168. const chartProps: any = {
  169. queriesData: [
  170. {
  171. data: nonTimeSeriesChartData,
  172. },
  173. ],
  174. };
  175. beforeEach(() => {
  176. chartTransformer = jest.fn();
  177. });
  178. it('calls the transformProps function for every location', () => {
  179. getChartConfigs(pieChartConfig, geomColumn, chartProps, chartTransformer);
  180. expect(chartTransformer).toHaveBeenCalledTimes(2);
  181. });
  182. it('returns a geojson', () => {
  183. const result = getChartConfigs(
  184. pieChartConfig,
  185. geomColumn,
  186. chartProps,
  187. chartTransformer,
  188. );
  189. expect(result).toEqual(
  190. expect.objectContaining({
  191. type: 'FeatureCollection',
  192. features: expect.arrayContaining([
  193. expect.objectContaining({
  194. type: 'Feature',
  195. }),
  196. ]),
  197. }),
  198. );
  199. });
  200. it('returns a feature for each location', () => {
  201. const result = getChartConfigs(
  202. pieChartConfig,
  203. geomColumn,
  204. chartProps,
  205. chartTransformer,
  206. );
  207. expect(result.features).toHaveLength(2);
  208. expect(result.features[0].geometry).toEqual(JSON.parse(geom1));
  209. expect(result.features[1].geometry).toEqual(JSON.parse(geom2));
  210. });
  211. });
  212. describe('parseSelectedChart', () => {
  213. it('parses the inline stringified JSON', () => {
  214. const selectedChartObject = {
  215. id: 278,
  216. params:
  217. '{"adhoc_filters":[],"applied_time_extras":{},"datasource":"24__table","viz_type":"pie","time_range":"No filter","groupby":["nuclide"],"metric":{"expressionType":"SIMPLE","column":{"advanced_data_type":null,"certification_details":null,"certified_by":null,"column_name":"nuclide","description":null,"expression":null,"filterable":true,"groupby":true,"id":772,"is_certified":false,"is_dttm":false,"python_date_format":null,"type":"TEXT","type_generic":1,"verbose_name":null,"warning_markdown":null},"aggregate":"COUNT","sqlExpression":null,"isNew":false,"datasourceWarning":false,"hasCustomLabel":false,"label":"COUNT(nuclide)","optionName":"metric_k6d9mt9zujc_7v9szd1i0pl"},"dashboards":[]}',
  218. slice_name: 'pie',
  219. viz_type: 'pie',
  220. };
  221. const selectedChartString = JSON.stringify(selectedChartObject);
  222. const result = parseSelectedChart(selectedChartString);
  223. const expectedParams = JSON.parse(selectedChartObject.params);
  224. expect(result.params).toEqual(expectedParams);
  225. });
  226. });
  227. });