|
16 | 16 | * specific language governing permissions and limitations
|
17 | 17 | * under the License.
|
18 | 18 | */
|
19 |
| -import { isValidElement } from 'react'; |
20 |
| -import { shallow, ShallowWrapper } from 'enzyme'; |
21 |
| -import { GenericDataType } from '@superset-ui/core'; |
| 19 | +import '@testing-library/jest-dom'; |
| 20 | +import { render } from '@testing-library/react'; |
| 21 | +import { |
| 22 | + ThemeProvider, |
| 23 | + supersetTheme, |
| 24 | + GenericDataType, |
| 25 | +} from '@superset-ui/core'; |
22 | 26 |
|
23 |
| -import { ColumnOption, ColumnOptionProps, ColumnTypeLabel } from '../../src'; |
24 |
| -import { SQLPopover } from '../../src/components/SQLPopover'; |
| 27 | +import { ColumnOption, ColumnOptionProps } from '../../src'; |
25 | 28 |
|
26 |
| -describe('ColumnOption', () => { |
27 |
| - const defaultProps: ColumnOptionProps = { |
| 29 | +jest.mock('../../src/components/SQLPopover', () => ({ |
| 30 | + SQLPopover: () => <div data-test="mock-sql-popover" />, |
| 31 | +})); |
| 32 | +jest.mock('../../src/components/ColumnTypeLabel/ColumnTypeLabel', () => ({ |
| 33 | + ColumnTypeLabel: ({ type }: { type: string }) => ( |
| 34 | + <div data-test="mock-column-type-label">{type}</div> |
| 35 | + ), |
| 36 | +})); |
| 37 | + |
| 38 | +const defaultProps: ColumnOptionProps = { |
| 39 | + column: { |
| 40 | + column_name: 'foo', |
| 41 | + verbose_name: 'Foo', |
| 42 | + expression: 'SUM(foo)', |
| 43 | + description: 'Foo is the greatest column of all', |
| 44 | + }, |
| 45 | + showType: false, |
| 46 | +}; |
| 47 | + |
| 48 | +const setup = (props: Partial<ColumnOptionProps> = {}) => |
| 49 | + render( |
| 50 | + <ThemeProvider theme={supersetTheme}> |
| 51 | + <ColumnOption {...defaultProps} {...props} /> |
| 52 | + </ThemeProvider>, |
| 53 | + ); |
| 54 | +test('shows a label with verbose_name', () => { |
| 55 | + const { container } = setup(); |
| 56 | + const lbl = container.getElementsByClassName('option-label'); |
| 57 | + expect(lbl).toHaveLength(1); |
| 58 | + expect(`${lbl[0].textContent}`).toEqual(defaultProps.column.verbose_name); |
| 59 | +}); |
| 60 | +test('shows SQL Popover trigger', () => { |
| 61 | + const { getByTestId } = setup(); |
| 62 | + expect(getByTestId('mock-sql-popover')).toBeInTheDocument(); |
| 63 | +}); |
| 64 | +test('shows a label with column_name when no verbose_name', () => { |
| 65 | + const { getByText } = setup({ |
28 | 66 | column: {
|
29 |
| - column_name: 'foo', |
30 |
| - verbose_name: 'Foo', |
31 |
| - expression: 'SUM(foo)', |
32 |
| - description: 'Foo is the greatest column of all', |
| 67 | + ...defaultProps.column, |
| 68 | + verbose_name: undefined, |
33 | 69 | },
|
34 |
| - showType: false, |
35 |
| - }; |
36 |
| - |
37 |
| - let wrapper: ShallowWrapper; |
38 |
| - let props: ColumnOptionProps; |
39 |
| - const factory = (o: ColumnOptionProps) => <ColumnOption {...o} />; |
40 |
| - beforeEach(() => { |
41 |
| - wrapper = shallow(factory(defaultProps)); |
42 |
| - props = { ...defaultProps }; |
43 | 70 | });
|
44 |
| - it('is a valid element', () => { |
45 |
| - expect(isValidElement(<ColumnOption {...defaultProps} />)).toBe(true); |
46 |
| - }); |
47 |
| - it('shows a label with verbose_name', () => { |
48 |
| - const lbl = wrapper.find('.option-label'); |
49 |
| - expect(lbl).toHaveLength(1); |
50 |
| - expect(lbl.first().text()).toBe('Foo'); |
51 |
| - }); |
52 |
| - it('shows SQL Popover trigger', () => { |
53 |
| - expect(wrapper.find(SQLPopover)).toHaveLength(1); |
54 |
| - }); |
55 |
| - it('shows a label with column_name when no verbose_name', () => { |
56 |
| - delete props.column.verbose_name; |
57 |
| - wrapper = shallow(factory(props)); |
58 |
| - expect(wrapper.find('.option-label').first().text()).toBe('foo'); |
59 |
| - }); |
60 |
| - it('shows a column type label when showType is true', () => { |
61 |
| - wrapper = shallow( |
62 |
| - factory({ |
63 |
| - ...props, |
64 |
| - showType: true, |
65 |
| - column: { |
66 |
| - column_name: 'foo', |
67 |
| - type: 'VARCHAR', |
68 |
| - type_generic: GenericDataType.String, |
69 |
| - }, |
70 |
| - }), |
71 |
| - ); |
72 |
| - expect(wrapper.find(ColumnTypeLabel)).toHaveLength(1); |
| 71 | + expect(getByText(defaultProps.column.column_name)).toBeInTheDocument(); |
| 72 | +}); |
| 73 | +test('shows a column type label when showType is true', () => { |
| 74 | + const { getByTestId } = setup({ |
| 75 | + showType: true, |
| 76 | + column: { |
| 77 | + column_name: 'foo', |
| 78 | + type: 'VARCHAR', |
| 79 | + type_generic: GenericDataType.String, |
| 80 | + }, |
73 | 81 | });
|
74 |
| - it('column with expression has correct column label if showType is true', () => { |
75 |
| - props.showType = true; |
76 |
| - wrapper = shallow(factory(props)); |
77 |
| - expect(wrapper.find(ColumnTypeLabel)).toHaveLength(1); |
78 |
| - expect(wrapper.find(ColumnTypeLabel).props().type).toBe('expression'); |
| 82 | + expect(getByTestId('mock-column-type-label')).toBeInTheDocument(); |
| 83 | +}); |
| 84 | +test('column with expression has correct column label if showType is true', () => { |
| 85 | + const { getByTestId } = setup({ |
| 86 | + showType: true, |
79 | 87 | });
|
80 |
| - it('shows no column type label when type is null', () => { |
81 |
| - wrapper = shallow( |
82 |
| - factory({ |
83 |
| - ...props, |
84 |
| - showType: true, |
85 |
| - column: { |
86 |
| - column_name: 'foo', |
87 |
| - }, |
88 |
| - }), |
89 |
| - ); |
90 |
| - expect(wrapper.find(ColumnTypeLabel)).toHaveLength(0); |
| 88 | + expect(getByTestId('mock-column-type-label')).toBeInTheDocument(); |
| 89 | + expect(getByTestId('mock-column-type-label')).toHaveTextContent('expression'); |
| 90 | +}); |
| 91 | +test('shows no column type label when type is null', () => { |
| 92 | + const { queryByTestId } = setup({ |
| 93 | + showType: true, |
| 94 | + column: { |
| 95 | + column_name: 'foo', |
| 96 | + }, |
91 | 97 | });
|
92 |
| - it('dttm column has correct column label if showType is true', () => { |
93 |
| - props.showType = true; |
94 |
| - props.column.expression = undefined; |
95 |
| - props.column.type_generic = GenericDataType.Temporal; |
96 |
| - wrapper = shallow(factory(props)); |
97 |
| - expect(wrapper.find(ColumnTypeLabel)).toHaveLength(1); |
98 |
| - expect(wrapper.find(ColumnTypeLabel).props().type).toBe( |
99 |
| - GenericDataType.Temporal, |
100 |
| - ); |
| 98 | + expect(queryByTestId('mock-column-type-label')).not.toBeInTheDocument(); |
| 99 | +}); |
| 100 | +test('dttm column has correct column label if showType is true', () => { |
| 101 | + const { getByTestId } = setup({ |
| 102 | + showType: true, |
| 103 | + column: { |
| 104 | + ...defaultProps.column, |
| 105 | + expression: undefined, |
| 106 | + type_generic: GenericDataType.Temporal, |
| 107 | + }, |
101 | 108 | });
|
| 109 | + expect(getByTestId('mock-column-type-label')).toBeInTheDocument(); |
| 110 | + expect(getByTestId('mock-column-type-label')).toHaveTextContent( |
| 111 | + String(GenericDataType.Temporal), |
| 112 | + ); |
102 | 113 | });
|
0 commit comments