mirror of
https://github.com/apache/superset.git
synced 2026-04-19 08:04:53 +00:00
[superset-client] replace misc ajax calls (#6135)
* [superset-client][misc] replace ajax calls in DashboardTable, TableLoader, utils, common * [superset-client][misc] replace ajax calls in AsyncSelect, HeaderActions, Deck.gl * [superset-client][misc] fix tests * [superset-client] remove unneeded functional setState calls * [superset-client] make welcome a redux app for toasts * [superset-client] make Profile a redux app for toasts * [superset-client] TableLoader don't pass toast props to dom nodes * tweak deckgl Multi syntax
This commit is contained in:
@@ -1,14 +1,22 @@
|
||||
import React from 'react';
|
||||
import Select from 'react-select';
|
||||
import { shallow } from 'enzyme';
|
||||
import sinon from 'sinon';
|
||||
import fetchMock from 'fetch-mock';
|
||||
|
||||
import AsyncSelect from '../../../src/components/AsyncSelect';
|
||||
|
||||
describe('AsyncSelect', () => {
|
||||
afterAll(fetchMock.reset);
|
||||
afterEach(fetchMock.resetHistory);
|
||||
|
||||
const dataEndpoint = '/chart/api/read';
|
||||
const dataGlob = 'glob:*/chart/api/read';
|
||||
fetchMock.get(dataGlob, []);
|
||||
fetchMock.resetHistory();
|
||||
|
||||
const mockedProps = {
|
||||
dataEndpoint: '/chart/api/read',
|
||||
onChange: sinon.spy(),
|
||||
dataEndpoint,
|
||||
onChange: () => {},
|
||||
placeholder: 'Select...',
|
||||
mutator: () => [
|
||||
{ value: 1, label: 'main' },
|
||||
@@ -16,6 +24,7 @@ describe('AsyncSelect', () => {
|
||||
],
|
||||
valueRenderer: opt => opt.label,
|
||||
};
|
||||
|
||||
it('is valid element', () => {
|
||||
expect(
|
||||
React.isValidElement(<AsyncSelect {...mockedProps} />),
|
||||
@@ -30,52 +39,81 @@ describe('AsyncSelect', () => {
|
||||
});
|
||||
|
||||
it('calls onChange on select change', () => {
|
||||
const onChangeSpy = jest.fn();
|
||||
const wrapper = shallow(
|
||||
<AsyncSelect {...mockedProps} />,
|
||||
<AsyncSelect {...mockedProps} onChange={onChangeSpy} />,
|
||||
);
|
||||
|
||||
wrapper.find(Select).simulate('change', { value: 1 });
|
||||
expect(mockedProps.onChange).toHaveProperty('callCount', 1);
|
||||
expect(onChangeSpy.mock.calls).toHaveLength(1);
|
||||
});
|
||||
|
||||
describe('auto select', () => {
|
||||
let server;
|
||||
beforeEach(() => {
|
||||
server = sinon.fakeServer.create();
|
||||
server.respondWith([
|
||||
200, { 'Content-Type': 'application/json' }, JSON.stringify({}),
|
||||
]);
|
||||
});
|
||||
afterEach(() => {
|
||||
server.restore();
|
||||
});
|
||||
it('should be off by default', () => {
|
||||
const wrapper = shallow(
|
||||
<AsyncSelect {...mockedProps} />,
|
||||
);
|
||||
wrapper.instance().fetchOptions();
|
||||
const spy = sinon.spy(wrapper.instance(), 'onChange');
|
||||
expect(spy.callCount).toBe(0);
|
||||
});
|
||||
it('should auto select first option', () => {
|
||||
const wrapper = shallow(
|
||||
<AsyncSelect {...mockedProps} autoSelect />,
|
||||
);
|
||||
const spy = sinon.spy(wrapper.instance(), 'onChange');
|
||||
server.respond();
|
||||
it('should not call onChange if autoSelect=false', (done) => {
|
||||
expect.assertions(2);
|
||||
|
||||
expect(spy.callCount).toBe(1);
|
||||
expect(spy.calledWith(wrapper.instance().state.options[0])).toBe(true);
|
||||
});
|
||||
it('should not auto select when value prop is set', () => {
|
||||
const wrapper = shallow(
|
||||
<AsyncSelect {...mockedProps} value={2} autoSelect />,
|
||||
const onChangeSpy = jest.fn();
|
||||
shallow(
|
||||
<AsyncSelect {...mockedProps} onChange={onChangeSpy} />,
|
||||
);
|
||||
const spy = sinon.spy(wrapper.instance(), 'onChange');
|
||||
wrapper.instance().fetchOptions();
|
||||
server.respond();
|
||||
|
||||
expect(spy.callCount).toBe(0);
|
||||
expect(wrapper.find(Select)).toHaveLength(1);
|
||||
setTimeout(() => {
|
||||
expect(fetchMock.calls(dataGlob)).toHaveLength(1);
|
||||
expect(onChangeSpy.mock.calls).toHaveLength(0);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should auto select the first option if autoSelect=true', (done) => {
|
||||
expect.assertions(3);
|
||||
|
||||
const onChangeSpy = jest.fn();
|
||||
const wrapper = shallow(
|
||||
<AsyncSelect {...mockedProps} onChange={onChangeSpy} autoSelect />,
|
||||
);
|
||||
|
||||
setTimeout(() => {
|
||||
expect(fetchMock.calls(dataGlob)).toHaveLength(1);
|
||||
expect(onChangeSpy.mock.calls).toHaveLength(1);
|
||||
expect(onChangeSpy).toBeCalledWith(wrapper.instance().state.options[0]);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should not auto select when value prop is set and autoSelect=true', (done) => {
|
||||
expect.assertions(3);
|
||||
|
||||
const onChangeSpy = jest.fn();
|
||||
const wrapper = shallow(
|
||||
<AsyncSelect {...mockedProps} value={2} onChange={onChangeSpy} autoSelect />,
|
||||
);
|
||||
|
||||
setTimeout(() => {
|
||||
expect(fetchMock.calls(dataGlob)).toHaveLength(1);
|
||||
expect(onChangeSpy.mock.calls).toHaveLength(0);
|
||||
expect(wrapper.find(Select)).toHaveLength(1);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should call onAsyncError if there is an error fetching options', (done) => {
|
||||
expect.assertions(3);
|
||||
|
||||
const errorEndpoint = 'async/error/';
|
||||
const errorGlob = 'glob:*async/error/';
|
||||
fetchMock.get(errorGlob, { throws: 'error' });
|
||||
|
||||
const onAsyncError = jest.fn();
|
||||
shallow(
|
||||
<AsyncSelect {...mockedProps} dataEndpoint={errorEndpoint} onAsyncError={onAsyncError} />,
|
||||
);
|
||||
|
||||
setTimeout(() => {
|
||||
expect(fetchMock.calls(errorGlob)).toHaveLength(1);
|
||||
expect(onAsyncError.mock.calls).toHaveLength(1);
|
||||
expect(onAsyncError).toBeCalledWith('error');
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user