ColumnOption.test.tsx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 { GenericDataType } from '@apache-superset/core/api/core';
  22. import { ColumnOption, ColumnOptionProps } from '../../src';
  23. jest.mock('@superset-ui/chart-controls/components/SQLPopover', () => ({
  24. SQLPopover: () => <div data-test="mock-sql-popover" />,
  25. }));
  26. jest.mock(
  27. '@superset-ui/chart-controls/components/ColumnTypeLabel/ColumnTypeLabel',
  28. () => ({
  29. ColumnTypeLabel: ({ type }: { type: string }) => (
  30. <div data-test="mock-column-type-label">{type}</div>
  31. ),
  32. }),
  33. );
  34. jest.mock('@superset-ui/core/components/InfoTooltip', () => ({
  35. InfoTooltip: () => <div data-test="mock-tooltip" />,
  36. }));
  37. const defaultProps: ColumnOptionProps = {
  38. column: {
  39. column_name: 'foo',
  40. verbose_name: 'Foo',
  41. expression: 'SUM(foo)',
  42. description: 'Foo is the greatest column of all',
  43. },
  44. showType: false,
  45. };
  46. const setup = (props: Partial<ColumnOptionProps> = {}) =>
  47. render(<ColumnOption {...defaultProps} {...props} />);
  48. test('shows a label with verbose_name', () => {
  49. const { container } = setup();
  50. const lbl = container.getElementsByClassName('option-label');
  51. expect(lbl).toHaveLength(1);
  52. expect(`${lbl[0].textContent}`).toEqual(defaultProps.column.verbose_name);
  53. });
  54. test('shows SQL Popover trigger', () => {
  55. const { getByTestId } = setup();
  56. expect(getByTestId('mock-sql-popover')).toBeInTheDocument();
  57. });
  58. test('shows a label with column_name when no verbose_name', () => {
  59. const { getByText } = setup({
  60. column: {
  61. ...defaultProps.column,
  62. verbose_name: undefined,
  63. },
  64. });
  65. expect(getByText(defaultProps.column.column_name)).toBeInTheDocument();
  66. });
  67. test('shows a column type label when showType is true', () => {
  68. const { getByTestId } = setup({
  69. showType: true,
  70. column: {
  71. column_name: 'foo',
  72. type: 'VARCHAR',
  73. type_generic: GenericDataType.String,
  74. },
  75. });
  76. expect(getByTestId('mock-column-type-label')).toBeInTheDocument();
  77. });
  78. test('column with expression has correct column label if showType is true', () => {
  79. const { getByTestId } = setup({
  80. showType: true,
  81. });
  82. expect(getByTestId('mock-column-type-label')).toBeInTheDocument();
  83. expect(getByTestId('mock-column-type-label')).toHaveTextContent('expression');
  84. });
  85. test('shows no column type label when type is null', () => {
  86. const { queryByTestId } = setup({
  87. showType: true,
  88. column: {
  89. column_name: 'foo',
  90. },
  91. });
  92. expect(queryByTestId('mock-column-type-label')).not.toBeInTheDocument();
  93. });
  94. test('dttm column has correct column label if showType is true', () => {
  95. const { getByTestId } = setup({
  96. showType: true,
  97. column: {
  98. ...defaultProps.column,
  99. expression: undefined,
  100. type_generic: GenericDataType.Temporal,
  101. },
  102. });
  103. expect(getByTestId('mock-column-type-label')).toBeInTheDocument();
  104. expect(getByTestId('mock-column-type-label')).toHaveTextContent(
  105. String(GenericDataType.Temporal),
  106. );
  107. });
  108. test('doesnt show InfoTooltip when no warning', () => {
  109. const { queryByText } = setup();
  110. expect(queryByText('mock-tooltip')).not.toBeInTheDocument();
  111. });
  112. test('shows a warning with InfoTooltip when it contains warning', () => {
  113. const { getByTestId } = setup({
  114. ...defaultProps,
  115. column: {
  116. ...defaultProps.column,
  117. warning_text: 'This is a warning',
  118. },
  119. });
  120. expect(getByTestId('mock-tooltip')).toBeInTheDocument();
  121. });