WithLegend.test.tsx 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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 { triggerResizeObserver } from 'resize-observer-polyfill';
  20. import { promiseTimeout, WithLegend } from '@superset-ui/core';
  21. import { render } from '@testing-library/react';
  22. let renderChart = jest.fn();
  23. let renderLegend = jest.fn();
  24. // TODO: rewrite to rtl
  25. describe.skip('WithLegend', () => {
  26. beforeEach(() => {
  27. renderChart = jest.fn(() => <div className="chart" />);
  28. renderLegend = jest.fn(() => <div className="legend" />);
  29. });
  30. it('sets className', () => {
  31. const { container } = render(
  32. <WithLegend
  33. className="test-class"
  34. renderChart={renderChart}
  35. renderLegend={renderLegend}
  36. />,
  37. );
  38. expect(container.querySelectorAll('.test-class')).toHaveLength(1);
  39. });
  40. it('renders when renderLegend is not set', () => {
  41. const { container } = render(
  42. <WithLegend
  43. debounceTime={1}
  44. width={500}
  45. height={500}
  46. renderChart={renderChart}
  47. />,
  48. );
  49. triggerResizeObserver();
  50. // Have to delay more than debounceTime (1ms)
  51. return promiseTimeout(() => {
  52. expect(renderChart).toHaveBeenCalledTimes(1);
  53. expect(container.querySelectorAll('div.chart')).toHaveLength(1);
  54. expect(container.querySelectorAll('div.legend')).toHaveLength(0);
  55. }, 100);
  56. });
  57. it('renders', () => {
  58. const { container } = render(
  59. <WithLegend
  60. debounceTime={1}
  61. width={500}
  62. height={500}
  63. renderChart={renderChart}
  64. renderLegend={renderLegend}
  65. />,
  66. );
  67. triggerResizeObserver();
  68. // Have to delay more than debounceTime (1ms)
  69. return promiseTimeout(() => {
  70. expect(renderChart).toHaveBeenCalledTimes(1);
  71. expect(renderLegend).toHaveBeenCalledTimes(1);
  72. expect(container.querySelectorAll('div.chart')).toHaveLength(1);
  73. expect(container.querySelectorAll('div.legend')).toHaveLength(1);
  74. }, 100);
  75. });
  76. it('renders without width or height', () => {
  77. const { container } = render(
  78. <WithLegend
  79. debounceTime={1}
  80. renderChart={renderChart}
  81. renderLegend={renderLegend}
  82. />,
  83. );
  84. triggerResizeObserver();
  85. // Have to delay more than debounceTime (1ms)
  86. return promiseTimeout(() => {
  87. expect(renderChart).toHaveBeenCalledTimes(1);
  88. expect(renderLegend).toHaveBeenCalledTimes(1);
  89. expect(container.querySelectorAll('div.chart')).toHaveLength(1);
  90. expect(container.querySelectorAll('div.legend')).toHaveLength(1);
  91. }, 100);
  92. });
  93. it('renders legend on the left', () => {
  94. const { container } = render(
  95. <WithLegend
  96. debounceTime={1}
  97. position="left"
  98. renderChart={renderChart}
  99. renderLegend={renderLegend}
  100. />,
  101. );
  102. triggerResizeObserver();
  103. // Have to delay more than debounceTime (1ms)
  104. return promiseTimeout(() => {
  105. expect(renderChart).toHaveBeenCalledTimes(1);
  106. expect(renderLegend).toHaveBeenCalledTimes(1);
  107. expect(container.querySelectorAll('div.chart')).toHaveLength(1);
  108. expect(container.querySelectorAll('div.legend')).toHaveLength(1);
  109. }, 100);
  110. });
  111. it('renders legend on the right', () => {
  112. const { container } = render(
  113. <WithLegend
  114. debounceTime={1}
  115. position="right"
  116. renderChart={renderChart}
  117. renderLegend={renderLegend}
  118. />,
  119. );
  120. triggerResizeObserver();
  121. // Have to delay more than debounceTime (1ms)
  122. return promiseTimeout(() => {
  123. expect(renderChart).toHaveBeenCalledTimes(1);
  124. expect(renderLegend).toHaveBeenCalledTimes(1);
  125. expect(container.querySelectorAll('div.chart')).toHaveLength(1);
  126. expect(container.querySelectorAll('div.legend')).toHaveLength(1);
  127. }, 100);
  128. });
  129. it('renders legend on the top', () => {
  130. const { container } = render(
  131. <WithLegend
  132. debounceTime={1}
  133. position="top"
  134. renderChart={renderChart}
  135. renderLegend={renderLegend}
  136. />,
  137. );
  138. triggerResizeObserver();
  139. // Have to delay more than debounceTime (1ms)
  140. return promiseTimeout(() => {
  141. expect(renderChart).toHaveBeenCalledTimes(1);
  142. expect(renderLegend).toHaveBeenCalledTimes(1);
  143. expect(container.querySelectorAll('div.chart')).toHaveLength(1);
  144. expect(container.querySelectorAll('div.legend')).toHaveLength(1);
  145. }, 100);
  146. });
  147. it('renders legend on the bottom', () => {
  148. const { container } = render(
  149. <WithLegend
  150. debounceTime={1}
  151. position="bottom"
  152. renderChart={renderChart}
  153. renderLegend={renderLegend}
  154. />,
  155. );
  156. triggerResizeObserver();
  157. // Have to delay more than debounceTime (1ms)
  158. return promiseTimeout(() => {
  159. expect(renderChart).toHaveBeenCalledTimes(1);
  160. expect(renderLegend).toHaveBeenCalledTimes(1);
  161. expect(container.querySelectorAll('div.chart')).toHaveLength(1);
  162. expect(container.querySelectorAll('div.legend')).toHaveLength(1);
  163. }, 100);
  164. });
  165. it('renders legend with justifyContent set', () => {
  166. const { container } = render(
  167. <WithLegend
  168. debounceTime={1}
  169. position="right"
  170. legendJustifyContent="flex-start"
  171. renderChart={renderChart}
  172. renderLegend={renderLegend}
  173. />,
  174. );
  175. triggerResizeObserver();
  176. // Have to delay more than debounceTime (1ms)
  177. return promiseTimeout(() => {
  178. expect(renderChart).toHaveBeenCalledTimes(1);
  179. expect(renderLegend).toHaveBeenCalledTimes(1);
  180. expect(container.querySelectorAll('div.chart')).toHaveLength(1);
  181. expect(container.querySelectorAll('div.legend')).toHaveLength(1);
  182. }, 100);
  183. });
  184. });