controlPanelUtil.test.tsx 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. ControlPanelConfig,
  21. CustomControlItem,
  22. } from '@superset-ui/chart-controls';
  23. import {
  24. getLayerConfig,
  25. selectedChartMutator,
  26. } from '../../src/util/controlPanelUtil';
  27. describe('controlPanelUtil', () => {
  28. describe('getLayerConfig', () => {
  29. it('returns the correct layer config', () => {
  30. const layerConfigs: CustomControlItem = {
  31. name: 'layer_configs',
  32. config: {
  33. type: 'dummy',
  34. renderTrigger: true,
  35. label: 'Layers',
  36. default: [
  37. {
  38. type: 'XYZ',
  39. url: 'http://example.com/',
  40. title: 'dummy title',
  41. attribution: 'dummy attribution',
  42. },
  43. ],
  44. description: 'The configuration for the map layers',
  45. },
  46. };
  47. const controlPanel: ControlPanelConfig = {
  48. controlPanelSections: [
  49. {
  50. label: 'Configuration',
  51. expanded: true,
  52. controlSetRows: [],
  53. },
  54. {
  55. label: 'Map Options',
  56. expanded: true,
  57. controlSetRows: [
  58. [
  59. {
  60. name: 'map_view',
  61. config: {
  62. type: 'dummy',
  63. },
  64. },
  65. ],
  66. [layerConfigs],
  67. ],
  68. },
  69. {
  70. label: 'Chart Options',
  71. expanded: true,
  72. controlSetRows: [],
  73. },
  74. ],
  75. };
  76. const extractedLayerConfigs = getLayerConfig(controlPanel);
  77. expect(extractedLayerConfigs).toEqual(layerConfigs);
  78. });
  79. });
  80. describe('selectedChartMutator', () => {
  81. it('returns empty array for empty inputs', () => {
  82. const response = {};
  83. const value = undefined;
  84. const result = selectedChartMutator(response, value);
  85. expect(result).toEqual([]);
  86. });
  87. it('returns parsed value if response is empty', () => {
  88. const response = {};
  89. const sliceName = 'foobar';
  90. const value = JSON.stringify({
  91. id: 278,
  92. params: '',
  93. slice_name: sliceName,
  94. viz_type: 'pie',
  95. });
  96. const result = selectedChartMutator(response, value);
  97. expect(result[0].label).toEqual(sliceName);
  98. });
  99. it('returns response options if no value is chosen', () => {
  100. const sliceName1 = 'foo';
  101. const sliceName2 = 'bar';
  102. const response = {
  103. result: [
  104. {
  105. id: 1,
  106. params: '{}',
  107. slice_name: sliceName1,
  108. viz_type: 'viz1',
  109. },
  110. {
  111. id: 2,
  112. params: '{}',
  113. slice_name: sliceName2,
  114. viz_type: 'viz2',
  115. },
  116. ],
  117. };
  118. const value = undefined;
  119. const result = selectedChartMutator(response, value);
  120. expect(result[0].label).toEqual(sliceName1);
  121. expect(result[1].label).toEqual(sliceName2);
  122. });
  123. it('returns correct result if id of chosen config does not exist in response', () => {
  124. const response = {
  125. result: [
  126. {
  127. id: 1,
  128. params: '{}',
  129. slice_name: 'foo',
  130. viz_type: 'viz1',
  131. },
  132. {
  133. id: 2,
  134. params: '{}',
  135. slice_name: 'bar',
  136. viz_type: 'viz2',
  137. },
  138. ],
  139. };
  140. const value = JSON.stringify({
  141. id: 3,
  142. params: '{}',
  143. slice_name: 'my-slice',
  144. viz_type: 'pie',
  145. });
  146. const result = selectedChartMutator(response, value);
  147. // collect all ids in a set to prevent double entries
  148. const ids = new Set();
  149. result.forEach((item: any) => {
  150. const config = JSON.parse(item.value);
  151. const { id } = config;
  152. ids.add(id);
  153. });
  154. const threeDifferentIds = ids.size === 3;
  155. expect(threeDifferentIds).toEqual(true);
  156. });
  157. it('returns correct result if id of chosen config already exists', () => {
  158. const response = {
  159. result: [
  160. {
  161. id: 1,
  162. params: '{}',
  163. slice_name: 'foo',
  164. viz_type: 'viz1',
  165. },
  166. {
  167. id: 2,
  168. params: '{}',
  169. slice_name: 'bar',
  170. viz_type: 'viz2',
  171. },
  172. ],
  173. };
  174. const value = JSON.stringify({
  175. id: 1,
  176. params: '{}',
  177. slice_name: 'my-slice',
  178. viz_type: 'pie',
  179. });
  180. const result = selectedChartMutator(response, value);
  181. const itemsIdWithId1 = result.filter((item: any) => {
  182. const config = JSON.parse(item.value);
  183. const { id } = config;
  184. return id === 1;
  185. });
  186. expect(itemsIdWithId1.length).toEqual(2);
  187. const labelsEqual = itemsIdWithId1[0].label === itemsIdWithId1[1].label;
  188. expect(labelsEqual).toEqual(false);
  189. });
  190. });
  191. });