labelUtils.test.tsx 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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 { render, screen } from '@superset-ui/core/spec';
  20. import '@testing-library/jest-dom';
  21. import {
  22. getColumnLabelText,
  23. getColumnTooltipNode,
  24. getMetricTooltipNode,
  25. getColumnTypeTooltipNode,
  26. } from '../../src/components/labelUtils';
  27. test("should get column name when column doesn't have verbose_name", () => {
  28. expect(
  29. getColumnLabelText({
  30. id: 123,
  31. column_name: 'column name',
  32. verbose_name: '',
  33. }),
  34. ).toBe('column name');
  35. });
  36. test('should get verbose name when column have verbose_name', () => {
  37. expect(
  38. getColumnLabelText({
  39. id: 123,
  40. column_name: 'column name',
  41. verbose_name: 'verbose name',
  42. }),
  43. ).toBe('verbose name');
  44. });
  45. test('should get null as tooltip', () => {
  46. const ref = { current: { scrollWidth: 100, clientWidth: 100 } };
  47. expect(
  48. getColumnTooltipNode(
  49. {
  50. id: 123,
  51. column_name: 'column name',
  52. verbose_name: '',
  53. description: '',
  54. },
  55. ref,
  56. ),
  57. ).toBe(null);
  58. });
  59. test('should get null for column datatype tooltip when type is blank', () => {
  60. expect(
  61. getColumnTypeTooltipNode({
  62. id: 123,
  63. column_name: 'column name',
  64. verbose_name: '',
  65. description: '',
  66. type: '',
  67. }),
  68. ).toBe(null);
  69. });
  70. test('should get column datatype rendered as tooltip when column has a type', () => {
  71. render(
  72. <>
  73. {getColumnTypeTooltipNode({
  74. id: 123,
  75. column_name: 'column name',
  76. verbose_name: 'verbose name',
  77. description: 'A very important column',
  78. type: 'text',
  79. })}
  80. </>,
  81. );
  82. expect(screen.getByText('Column type')).toBeVisible();
  83. expect(screen.getByText('text')).toBeVisible();
  84. });
  85. test('should get column name, verbose name and description when it has a verbose name', () => {
  86. const ref = { current: { scrollWidth: 100, clientWidth: 100 } };
  87. render(
  88. <>
  89. {getColumnTooltipNode(
  90. {
  91. id: 123,
  92. column_name: 'column name',
  93. verbose_name: 'verbose name',
  94. description: 'A very important column',
  95. },
  96. ref,
  97. )}
  98. </>,
  99. );
  100. expect(screen.getByText('Column name')).toBeVisible();
  101. expect(screen.getByText('column name')).toBeVisible();
  102. expect(screen.getByText('Label')).toBeVisible();
  103. expect(screen.getByText('verbose name')).toBeVisible();
  104. expect(screen.getByText('Description')).toBeVisible();
  105. expect(screen.getByText('A very important column')).toBeVisible();
  106. });
  107. test('should get column name as tooltip if it overflowed', () => {
  108. const ref = { current: { scrollWidth: 200, clientWidth: 100 } };
  109. render(
  110. <>
  111. {getColumnTooltipNode(
  112. {
  113. id: 123,
  114. column_name: 'long long long long column name',
  115. verbose_name: '',
  116. description: '',
  117. },
  118. ref,
  119. )}
  120. </>,
  121. );
  122. expect(screen.getByText('Column name')).toBeVisible();
  123. expect(screen.getByText('long long long long column name')).toBeVisible();
  124. expect(screen.queryByText('Label')).not.toBeInTheDocument();
  125. expect(screen.queryByText('Description')).not.toBeInTheDocument();
  126. });
  127. test('should get column name, verbose name and description as tooltip if it overflowed', () => {
  128. const ref = { current: { scrollWidth: 200, clientWidth: 100 } };
  129. render(
  130. <>
  131. {getColumnTooltipNode(
  132. {
  133. id: 123,
  134. column_name: 'long long long long column name',
  135. verbose_name: 'long long long long verbose name',
  136. description: 'A very important column',
  137. },
  138. ref,
  139. )}
  140. </>,
  141. );
  142. expect(screen.getByText('Column name')).toBeVisible();
  143. expect(screen.getByText('long long long long column name')).toBeVisible();
  144. expect(screen.getByText('Label')).toBeVisible();
  145. expect(screen.getByText('long long long long verbose name')).toBeVisible();
  146. expect(screen.getByText('Description')).toBeVisible();
  147. expect(screen.getByText('A very important column')).toBeVisible();
  148. });
  149. test('should get null as tooltip in metric', () => {
  150. const ref = { current: { scrollWidth: 100, clientWidth: 100 } };
  151. expect(
  152. getMetricTooltipNode(
  153. {
  154. metric_name: 'count',
  155. label: '',
  156. verbose_name: '',
  157. description: '',
  158. },
  159. ref,
  160. ),
  161. ).toBe(null);
  162. });
  163. test('should get metric name, verbose name and description as tooltip in metric', () => {
  164. const ref = { current: { scrollWidth: 100, clientWidth: 100 } };
  165. render(
  166. <>
  167. {getMetricTooltipNode(
  168. {
  169. metric_name: 'count',
  170. label: 'count(*)',
  171. verbose_name: 'count(*)',
  172. description: 'Count metric',
  173. },
  174. ref,
  175. )}
  176. </>,
  177. );
  178. expect(screen.getByText('Metric name')).toBeVisible();
  179. expect(screen.getByText('count')).toBeVisible();
  180. expect(screen.getByText('Label')).toBeVisible();
  181. expect(screen.getByText('count(*)')).toBeVisible();
  182. expect(screen.getByText('Description')).toBeVisible();
  183. expect(screen.getByText('Count metric')).toBeVisible();
  184. });
  185. test('should get metric name as tooltip if it overflowed', () => {
  186. const ref = { current: { scrollWidth: 200, clientWidth: 100 } };
  187. render(
  188. <>
  189. {getMetricTooltipNode(
  190. {
  191. metric_name: 'long long long long metric name',
  192. label: '',
  193. verbose_name: '',
  194. description: '',
  195. },
  196. ref,
  197. )}
  198. </>,
  199. );
  200. expect(screen.getByText('Metric name')).toBeVisible();
  201. expect(screen.getByText('long long long long metric name')).toBeVisible();
  202. expect(screen.queryByText('Label')).not.toBeInTheDocument();
  203. expect(screen.queryByText('Description')).not.toBeInTheDocument();
  204. });
  205. test('should get metric name, verbose name and description in tooltip if it overflowed', () => {
  206. const ref = { current: { scrollWidth: 200, clientWidth: 100 } };
  207. render(
  208. <>
  209. {getMetricTooltipNode(
  210. {
  211. metric_name: 'count',
  212. label: '',
  213. verbose_name: 'longlonglonglonglong verbose metric',
  214. description: 'Count metric',
  215. },
  216. ref,
  217. )}
  218. </>,
  219. );
  220. expect(screen.getByText('Metric name')).toBeVisible();
  221. expect(screen.getByText('count')).toBeVisible();
  222. expect(screen.getByText('Label')).toBeVisible();
  223. expect(screen.getByText('longlonglonglonglong verbose metric')).toBeVisible();
  224. expect(screen.getByText('Description')).toBeVisible();
  225. expect(screen.getByText('Count metric')).toBeVisible();
  226. });