MetricOption.test.tsx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 { render } from '@superset-ui/core/spec';
  21. import {
  22. MetricOption,
  23. MetricOptionProps,
  24. } from '../../src/components/MetricOption';
  25. jest.mock('@superset-ui/core/components/InfoTooltip', () => ({
  26. InfoTooltip: () => <div data-test="mock-tooltip" />,
  27. }));
  28. jest.mock(
  29. '@superset-ui/chart-controls/components/ColumnTypeLabel/ColumnTypeLabel',
  30. () => ({
  31. ColumnTypeLabel: () => <div data-test="mock-column-type-label" />,
  32. }),
  33. );
  34. jest.mock(
  35. '@superset-ui/core/components/Tooltip',
  36. () =>
  37. ({ children }: { children: React.ReactNode }) => (
  38. <div data-test="mock-tooltip">{children}</div>
  39. ),
  40. );
  41. jest.mock('@superset-ui/chart-controls/components/SQLPopover', () => ({
  42. SQLPopover: () => <div data-test="mock-sql-popover" />,
  43. }));
  44. const defaultProps = {
  45. metric: {
  46. metric_name: 'foo',
  47. verbose_name: 'Foo',
  48. expression: 'SUM(foo)',
  49. label: 'test',
  50. description: 'Foo is the greatest metric of all',
  51. warning_text: 'Be careful when using foo',
  52. },
  53. openInNewWindow: false,
  54. showFormula: true,
  55. showType: true,
  56. url: '',
  57. };
  58. const setup = (props: Partial<MetricOptionProps> = {}) =>
  59. render(<MetricOption {...defaultProps} {...props} />);
  60. test('shows a label with verbose_name', () => {
  61. const { container } = setup();
  62. const lbl = container.getElementsByClassName('option-label');
  63. expect(lbl).toHaveLength(1);
  64. expect(`${lbl[0].textContent}`).toEqual(defaultProps.metric.verbose_name);
  65. });
  66. test('shows a InfoTooltip', () => {
  67. const { getByTestId } = setup();
  68. expect(getByTestId('mock-tooltip')).toBeInTheDocument();
  69. });
  70. test('shows SQL Popover trigger', () => {
  71. const { getByTestId } = setup();
  72. expect(getByTestId('mock-sql-popover')).toBeInTheDocument();
  73. });
  74. test('shows a label with metric_name when no verbose_name', () => {
  75. const { getByText } = setup({
  76. metric: {
  77. ...defaultProps.metric,
  78. verbose_name: '',
  79. },
  80. });
  81. expect(getByText(defaultProps.metric.metric_name)).toBeInTheDocument();
  82. });
  83. test('doesnt show InfoTooltip when no warning', () => {
  84. const { queryByText } = setup({
  85. metric: {
  86. ...defaultProps.metric,
  87. warning_text: '',
  88. },
  89. });
  90. expect(queryByText('mock-tooltip')).not.toBeInTheDocument();
  91. });
  92. test('sets target="_blank" when openInNewWindow is true', () => {
  93. const { getByRole } = setup({
  94. url: 'https://github.com/apache/incubator-superset',
  95. openInNewWindow: true,
  96. });
  97. expect(
  98. getByRole('link', { name: defaultProps.metric.verbose_name }),
  99. ).toHaveAttribute('target', '_blank');
  100. });
  101. test('shows a metric type label when showType is true', () => {
  102. const { getByTestId } = setup({
  103. showType: true,
  104. });
  105. expect(getByTestId('mock-column-type-label')).toBeInTheDocument();
  106. });
  107. test('shows a Tooltip for the verbose metric name', () => {
  108. const { getByTestId } = setup();
  109. expect(getByTestId('mock-tooltip')).toBeInTheDocument();
  110. });