SuperChartCore.test.tsx 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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 '@testing-library/jest-dom';
  20. import mockConsole, { RestoreConsole } from 'jest-mock-console';
  21. import { ChartProps, supersetTheme } from '@superset-ui/core';
  22. import { render, screen, waitFor } from '@superset-ui/core/spec';
  23. import SuperChartCore from '../../../src/chart/components/SuperChartCore';
  24. import {
  25. ChartKeys,
  26. DiligentChartPlugin,
  27. LazyChartPlugin,
  28. SlowChartPlugin,
  29. } from './MockChartPlugins';
  30. describe('SuperChartCore', () => {
  31. const chartProps = new ChartProps();
  32. const plugins = [
  33. new DiligentChartPlugin().configure({ key: ChartKeys.DILIGENT }),
  34. new LazyChartPlugin().configure({ key: ChartKeys.LAZY }),
  35. new SlowChartPlugin().configure({ key: ChartKeys.SLOW }),
  36. ];
  37. let restoreConsole: RestoreConsole;
  38. beforeAll(() => {
  39. jest.setTimeout(30000);
  40. plugins.forEach(p => {
  41. p.unregister().register();
  42. });
  43. });
  44. afterAll(() => {
  45. plugins.forEach(p => {
  46. p.unregister();
  47. });
  48. });
  49. beforeEach(() => {
  50. restoreConsole = mockConsole();
  51. });
  52. afterEach(() => {
  53. restoreConsole();
  54. });
  55. describe('registered charts', () => {
  56. it('renders registered chart', async () => {
  57. const { container } = render(
  58. <SuperChartCore
  59. chartType={ChartKeys.DILIGENT}
  60. chartProps={chartProps}
  61. />,
  62. );
  63. await waitFor(() => {
  64. expect(container.querySelector('.test-component')).toBeInTheDocument();
  65. });
  66. });
  67. it('renders registered chart with lazy loading', async () => {
  68. const { container } = render(
  69. <SuperChartCore chartType={ChartKeys.LAZY} />,
  70. );
  71. await waitFor(() => {
  72. expect(container.querySelector('.test-component')).toBeInTheDocument();
  73. });
  74. });
  75. it('does not render if chartType is not set', async () => {
  76. // @ts-ignore chartType is required
  77. const { container } = render(<SuperChartCore />);
  78. await waitFor(() => {
  79. const testComponent = container.querySelector('.test-component');
  80. expect(testComponent).not.toBeInTheDocument();
  81. });
  82. });
  83. it('adds id to container if specified', async () => {
  84. const { container } = render(
  85. <SuperChartCore chartType={ChartKeys.DILIGENT} id="the-chart" />,
  86. );
  87. await waitFor(() => {
  88. const element = container.querySelector('#the-chart');
  89. expect(element).toBeInTheDocument();
  90. expect(element).toHaveAttribute('id', 'the-chart');
  91. });
  92. });
  93. it('adds class to container if specified', async () => {
  94. const { container } = render(
  95. <SuperChartCore chartType={ChartKeys.DILIGENT} className="the-chart" />,
  96. );
  97. await waitFor(() => {
  98. const element = container.querySelector('.the-chart');
  99. expect(element).toBeInTheDocument();
  100. expect(element).toHaveClass('the-chart');
  101. });
  102. });
  103. it('uses overrideTransformProps when specified', async () => {
  104. render(
  105. <SuperChartCore
  106. chartType={ChartKeys.DILIGENT}
  107. overrideTransformProps={() => ({ message: 'hulk' })}
  108. />,
  109. );
  110. await waitFor(() => {
  111. expect(screen.getByText('hulk')).toBeInTheDocument();
  112. });
  113. });
  114. it('uses preTransformProps when specified', async () => {
  115. const chartPropsWithPayload = new ChartProps({
  116. queriesData: [{ message: 'hulk' }],
  117. theme: supersetTheme,
  118. });
  119. render(
  120. <SuperChartCore
  121. chartType={ChartKeys.DILIGENT}
  122. preTransformProps={() => chartPropsWithPayload}
  123. overrideTransformProps={props => props.queriesData[0]}
  124. />,
  125. );
  126. await waitFor(() => {
  127. expect(screen.getByText('hulk')).toBeInTheDocument();
  128. });
  129. });
  130. it('uses postTransformProps when specified', async () => {
  131. render(
  132. <SuperChartCore
  133. chartType={ChartKeys.DILIGENT}
  134. postTransformProps={() => ({ message: 'hulk' })}
  135. />,
  136. );
  137. await waitFor(() => {
  138. expect(screen.getByText('hulk')).toBeInTheDocument();
  139. });
  140. });
  141. it('renders if chartProps is not specified', async () => {
  142. const { container } = render(
  143. <SuperChartCore chartType={ChartKeys.DILIGENT} />,
  144. );
  145. await waitFor(() => {
  146. expect(container.querySelector('.test-component')).toBeInTheDocument();
  147. });
  148. });
  149. it('does not render anything while waiting for Chart code to load', () => {
  150. const { container } = render(
  151. <SuperChartCore chartType={ChartKeys.SLOW} />,
  152. );
  153. const testComponent = container.querySelector('.test-component');
  154. expect(testComponent).not.toBeInTheDocument();
  155. });
  156. it('eventually renders after Chart is loaded', async () => {
  157. const { container } = render(
  158. <SuperChartCore chartType={ChartKeys.SLOW} />,
  159. );
  160. await waitFor(
  161. () => {
  162. expect(
  163. container.querySelector('.test-component'),
  164. ).toBeInTheDocument();
  165. },
  166. { timeout: 2000 },
  167. );
  168. });
  169. it('does not render if chartProps is null', async () => {
  170. const { container } = render(
  171. <SuperChartCore chartType={ChartKeys.DILIGENT} chartProps={null} />,
  172. );
  173. await waitFor(() => {
  174. // Should not render any chart content, only the antd App wrapper
  175. expect(
  176. container.querySelector('.test-component'),
  177. ).not.toBeInTheDocument();
  178. expect(
  179. container.querySelector('[data-test="chart-container"]'),
  180. ).not.toBeInTheDocument();
  181. });
  182. });
  183. });
  184. describe('unregistered charts', () => {
  185. it('renders error message', async () => {
  186. render(
  187. <SuperChartCore chartType="4d-pie-chart" chartProps={chartProps} />,
  188. );
  189. await waitFor(() => {
  190. expect(screen.getByRole('alert')).toBeInTheDocument();
  191. });
  192. });
  193. });
  194. describe('.processChartProps()', () => {
  195. it('use identity functions for unspecified transforms', () => {
  196. const chart = new SuperChartCore({
  197. chartType: ChartKeys.DILIGENT,
  198. });
  199. const chartProps2 = new ChartProps();
  200. expect(chart.processChartProps({ chartProps: chartProps2 })).toBe(
  201. chartProps2,
  202. );
  203. });
  204. });
  205. });