You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@superset.apache.org by ru...@apache.org on 2021/03/26 05:07:57 UTC
[superset] branch master updated: test: SelectAsyncControl (#13638)
This is an automated email from the ASF dual-hosted git repository.
rusackas pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/superset.git
The following commit(s) were added to refs/heads/master by this push:
new 416f188 test: SelectAsyncControl (#13638)
416f188 is described below
commit 416f1883282f9112d7144c1ffcc6be0d32c12afc
Author: Bruno Motta <wa...@gmail.com>
AuthorDate: Fri Mar 26 02:07:05 2021 -0300
test: SelectAsyncControl (#13638)
* SelectAsyncControl tests
* fix: lint error
* applying factory to props
---
.../SelectAsyncControl/SelectAsyncControl.test.tsx | 112 +++++++++++++++++++++
.../index.jsx} | 8 +-
2 files changed, 116 insertions(+), 4 deletions(-)
diff --git a/superset-frontend/src/explore/components/controls/SelectAsyncControl/SelectAsyncControl.test.tsx b/superset-frontend/src/explore/components/controls/SelectAsyncControl/SelectAsyncControl.test.tsx
new file mode 100644
index 0000000..bc1ec82
--- /dev/null
+++ b/superset-frontend/src/explore/components/controls/SelectAsyncControl/SelectAsyncControl.test.tsx
@@ -0,0 +1,112 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+import userEvent from '@testing-library/user-event';
+import React from 'react';
+import { render, screen } from 'spec/helpers/testing-library';
+import SelectAsyncControl from '.';
+
+jest.mock('src/components/AsyncSelect', () => ({
+ __esModule: true,
+ default: (props: any) => (
+ <div
+ data-test="select-test"
+ data-data-endpoint={props.dataEndpoint}
+ data-value={JSON.stringify(props.value)}
+ data-placeholder={props.placeholder}
+ data-multi={props.multi ? 'true' : 'false'}
+ >
+ <button
+ type="button"
+ onClick={() => props.onChange(props.multi ? [] : {})}
+ >
+ onChange
+ </button>
+ <button type="button" onClick={() => props.mutator()}>
+ mutator
+ </button>
+
+ <div data-test="valueRenderer">
+ {props.valueRenderer({ label: 'valueRenderer' })}
+ </div>
+ </div>
+ ),
+}));
+
+const createProps = () => ({
+ value: [],
+ dataEndpoint: 'api/v1/dataset/related/owners',
+ multi: true,
+ onAsyncErrorMessage: 'Error while fetching data',
+ placeholder: 'Select ...',
+ onChange: jest.fn(),
+ mutator: jest.fn(),
+});
+
+beforeEach(() => {
+ jest.resetAllMocks();
+});
+
+test('Should render', () => {
+ const props = createProps();
+ render(<SelectAsyncControl {...props} />, { useRedux: true });
+ expect(screen.getByTestId('SelectAsyncControl')).toBeInTheDocument();
+});
+
+test('Should send correct props to AsyncSelect component - value props', () => {
+ const props = createProps();
+ render(<SelectAsyncControl {...props} />, { useRedux: true });
+
+ expect(screen.getByTestId('select-test')).toHaveAttribute(
+ 'data-data-endpoint',
+ props.dataEndpoint,
+ );
+ expect(screen.getByTestId('select-test')).toHaveAttribute(
+ 'data-value',
+ JSON.stringify(props.value),
+ );
+ expect(screen.getByTestId('select-test')).toHaveAttribute(
+ 'data-placeholder',
+ props.placeholder,
+ );
+ expect(screen.getByTestId('select-test')).toHaveAttribute(
+ 'data-multi',
+ 'true',
+ );
+ expect(screen.getByTestId('valueRenderer')).toHaveTextContent(
+ 'valueRenderer',
+ );
+});
+
+test('Should send correct props to AsyncSelect component - function onChange multi:true', () => {
+ const props = createProps();
+ render(<SelectAsyncControl {...props} />, { useRedux: true });
+ expect(props.onChange).toBeCalledTimes(0);
+ userEvent.click(screen.getByText('onChange'));
+ expect(props.onChange).toBeCalledTimes(1);
+});
+
+test('Should send correct props to AsyncSelect component - function onChange multi:false', () => {
+ const props = createProps();
+ render(<SelectAsyncControl {...{ ...props, multi: false }} />, {
+ useRedux: true,
+ });
+ expect(props.onChange).toBeCalledTimes(0);
+ userEvent.click(screen.getByText('onChange'));
+ expect(props.onChange).toBeCalledTimes(1);
+});
diff --git a/superset-frontend/src/explore/components/controls/SelectAsyncControl.jsx b/superset-frontend/src/explore/components/controls/SelectAsyncControl/index.jsx
similarity index 91%
rename from superset-frontend/src/explore/components/controls/SelectAsyncControl.jsx
rename to superset-frontend/src/explore/components/controls/SelectAsyncControl/index.jsx
index 8ccb81f..092498d 100644
--- a/superset-frontend/src/explore/components/controls/SelectAsyncControl.jsx
+++ b/superset-frontend/src/explore/components/controls/SelectAsyncControl/index.jsx
@@ -20,9 +20,9 @@ import React from 'react';
import PropTypes from 'prop-types';
import { t } from '@superset-ui/core';
-import Select from '../../../components/AsyncSelect';
-import ControlHeader from '../ControlHeader';
-import withToasts from '../../../messageToasts/enhancers/withToasts';
+import Select from 'src/components/AsyncSelect';
+import ControlHeader from 'src/explore/components/ControlHeader';
+import withToasts from 'src/messageToasts/enhancers/withToasts';
const propTypes = {
dataEndpoint: PropTypes.string.isRequired,
@@ -68,7 +68,7 @@ const SelectAsyncControl = props => {
};
return (
- <div>
+ <div data-test="SelectAsyncControl">
<ControlHeader {...props} />
<Select
dataEndpoint={dataEndpoint}