ChartPlugin.test.tsx 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. ChartPlugin,
  21. ChartMetadata,
  22. ChartProps,
  23. BuildQueryFunction,
  24. TransformProps,
  25. getChartMetadataRegistry,
  26. getChartComponentRegistry,
  27. getChartTransformPropsRegistry,
  28. getChartBuildQueryRegistry,
  29. getChartControlPanelRegistry,
  30. QueryFormData,
  31. DatasourceType,
  32. supersetTheme,
  33. VizType,
  34. } from '@superset-ui/core';
  35. describe('ChartPlugin', () => {
  36. const FakeChart = () => <span>test</span>;
  37. const metadata = new ChartMetadata({
  38. name: 'test-chart',
  39. thumbnail: '',
  40. });
  41. const buildQuery = () => ({
  42. datasource: { id: 1, type: DatasourceType.Table },
  43. queries: [{ granularity: 'day' }],
  44. force: false,
  45. result_format: 'json',
  46. result_type: 'full',
  47. });
  48. const controlPanel = { abc: 1 };
  49. it('exists', () => {
  50. expect(ChartPlugin).toBeDefined();
  51. });
  52. describe('new ChartPlugin()', () => {
  53. const FORM_DATA = {
  54. datasource: '1__table',
  55. granularity: 'day',
  56. viz_type: VizType.Table,
  57. };
  58. it('creates a new plugin', () => {
  59. const plugin = new ChartPlugin({
  60. metadata,
  61. Chart: FakeChart,
  62. });
  63. expect(plugin).toBeInstanceOf(ChartPlugin);
  64. });
  65. describe('buildQuery', () => {
  66. it('defaults to undefined', () => {
  67. const plugin = new ChartPlugin({
  68. metadata,
  69. Chart: FakeChart,
  70. });
  71. expect(plugin.loadBuildQuery).toBeUndefined();
  72. });
  73. it('uses loadBuildQuery field if specified', () => {
  74. expect.assertions(2);
  75. const plugin = new ChartPlugin({
  76. metadata,
  77. Chart: FakeChart,
  78. loadBuildQuery: () => buildQuery,
  79. });
  80. const fn =
  81. plugin.loadBuildQuery!() as BuildQueryFunction<QueryFormData>;
  82. expect(fn(FORM_DATA).queries[0]).toEqual({ granularity: 'day' });
  83. expect(fn(FORM_DATA).force).toEqual(false);
  84. });
  85. it('uses buildQuery field if specified', () => {
  86. expect.assertions(1);
  87. const plugin = new ChartPlugin({
  88. metadata,
  89. Chart: FakeChart,
  90. buildQuery,
  91. });
  92. const fn =
  93. plugin.loadBuildQuery!() as BuildQueryFunction<QueryFormData>;
  94. expect(fn(FORM_DATA).queries[0]).toEqual({ granularity: 'day' });
  95. });
  96. });
  97. describe('Chart', () => {
  98. it('uses loadChart if specified', () => {
  99. const loadChart = () => FakeChart;
  100. const plugin = new ChartPlugin({
  101. metadata,
  102. loadChart,
  103. });
  104. // the loader is sanitized, so assert on the value
  105. expect(plugin.loadChart()).toBe(loadChart());
  106. });
  107. it('uses Chart field if specified', () => {
  108. const plugin = new ChartPlugin({
  109. metadata,
  110. Chart: FakeChart,
  111. });
  112. expect(plugin.loadChart()).toEqual(FakeChart);
  113. });
  114. it('throws an error if none of Chart or loadChart is specified', () => {
  115. expect(() => new ChartPlugin({ metadata })).toThrow(Error);
  116. });
  117. });
  118. describe('transformProps', () => {
  119. const PROPS = new ChartProps({
  120. formData: FORM_DATA,
  121. width: 400,
  122. height: 400,
  123. queriesData: [{}],
  124. theme: supersetTheme,
  125. });
  126. it('defaults to identity function', () => {
  127. const plugin = new ChartPlugin({
  128. metadata,
  129. Chart: FakeChart,
  130. });
  131. const fn = plugin.loadTransformProps() as TransformProps;
  132. expect(fn(PROPS)).toBe(PROPS);
  133. });
  134. it('uses loadTransformProps field if specified', () => {
  135. const plugin = new ChartPlugin({
  136. metadata,
  137. Chart: FakeChart,
  138. loadTransformProps: () => () => ({ field2: 2 }),
  139. });
  140. const fn = plugin.loadTransformProps() as TransformProps;
  141. expect(fn(PROPS)).toEqual({ field2: 2 });
  142. });
  143. it('uses transformProps field if specified', () => {
  144. const plugin = new ChartPlugin({
  145. metadata,
  146. Chart: FakeChart,
  147. transformProps: () => ({ field2: 2 }),
  148. });
  149. const fn = plugin.loadTransformProps() as TransformProps;
  150. expect(fn(PROPS)).toEqual({ field2: 2 });
  151. });
  152. });
  153. describe('controlPanel', () => {
  154. it('takes controlPanel from input', () => {
  155. const plugin = new ChartPlugin({
  156. metadata,
  157. Chart: FakeChart,
  158. controlPanel,
  159. });
  160. expect(plugin.controlPanel).toBe(controlPanel);
  161. });
  162. it('defaults to empty object', () => {
  163. const plugin = new ChartPlugin({
  164. metadata,
  165. Chart: FakeChart,
  166. });
  167. expect(plugin.controlPanel).toEqual({});
  168. });
  169. });
  170. });
  171. describe('.register()', () => {
  172. let plugin: ChartPlugin;
  173. beforeEach(() => {
  174. plugin = new ChartPlugin({
  175. metadata,
  176. Chart: FakeChart,
  177. buildQuery,
  178. controlPanel,
  179. });
  180. });
  181. it('throws an error if key is not provided', () => {
  182. expect(() => plugin.register()).toThrow(Error);
  183. expect(() => plugin.configure({ key: 'ab' }).register()).not.toThrow(
  184. Error,
  185. );
  186. });
  187. it('add the plugin to the registries', () => {
  188. plugin.configure({ key: 'cd' }).register();
  189. expect(getChartMetadataRegistry().get('cd')).toBe(metadata);
  190. expect(getChartComponentRegistry().get('cd')).toBe(FakeChart);
  191. expect(getChartTransformPropsRegistry().has('cd')).toEqual(true);
  192. expect(getChartBuildQueryRegistry().get('cd')).toBe(buildQuery);
  193. expect(getChartControlPanelRegistry().get('cd')).toBe(controlPanel);
  194. });
  195. it('does not register buildQuery when it is not specified in the ChartPlugin', () => {
  196. new ChartPlugin({
  197. metadata,
  198. Chart: FakeChart,
  199. })
  200. .configure({ key: 'ef' })
  201. .register();
  202. expect(getChartBuildQueryRegistry().has('ef')).toEqual(false);
  203. });
  204. it('returns itself', () => {
  205. expect(plugin.configure({ key: 'gh' }).register()).toBe(plugin);
  206. });
  207. });
  208. describe('.unregister()', () => {
  209. let plugin: ChartPlugin;
  210. beforeEach(() => {
  211. plugin = new ChartPlugin({
  212. metadata,
  213. Chart: FakeChart,
  214. buildQuery,
  215. controlPanel,
  216. });
  217. });
  218. it('throws an error if key is not provided', () => {
  219. expect(() => plugin.unregister()).toThrow(Error);
  220. expect(() => plugin.configure({ key: 'abc' }).unregister()).not.toThrow(
  221. Error,
  222. );
  223. });
  224. it('removes the chart from the registries', () => {
  225. plugin.configure({ key: 'def' }).register();
  226. expect(getChartMetadataRegistry().get('def')).toBe(metadata);
  227. expect(getChartComponentRegistry().get('def')).toBe(FakeChart);
  228. expect(getChartTransformPropsRegistry().has('def')).toEqual(true);
  229. expect(getChartBuildQueryRegistry().get('def')).toBe(buildQuery);
  230. expect(getChartControlPanelRegistry().get('def')).toBe(controlPanel);
  231. plugin.unregister();
  232. expect(getChartMetadataRegistry().has('def')).toBeFalsy();
  233. expect(getChartComponentRegistry().has('def')).toBeFalsy();
  234. expect(getChartTransformPropsRegistry().has('def')).toBeFalsy();
  235. expect(getChartBuildQueryRegistry().has('def')).toBeFalsy();
  236. expect(getChartControlPanelRegistry().has('def')).toBeFalsy();
  237. });
  238. it('returns itself', () => {
  239. expect(plugin.configure({ key: 'xyz' }).unregister()).toBe(plugin);
  240. });
  241. });
  242. });