2019-01-15 18:53:27 -05:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2016-11-18 17:56:02 -05:00
|
|
|
import React from 'react';
|
2017-08-10 20:04:44 -04:00
|
|
|
import configureStore from 'redux-mock-store';
|
|
|
|
import thunk from 'redux-thunk';
|
2019-04-24 17:43:13 -04:00
|
|
|
import { bindActionCreators } from 'redux';
|
2017-08-10 20:04:44 -04:00
|
|
|
|
style: Pass at propagating (and enhancing) Button component throughout Superset (#10649)
* getting rid of weird focus/active outline ring
* Buttons... buttons _everywhere_
* linting
* Nixing views/CRUD/dataset/Button component
* fixing 2 typing errors
* fixing more TS errors
* prefer src path for include
* one more real button, one less CSS class
* one more "button" to "Button"
* Published Status is now a proper clickable Label
* nixing the CRUD button again
* touching up stories, with SupersetButton story
* SIP-34 button colors
* adding polished package to mix colors
* updating button colors to match Superset theme
* abstracting away from bootstrap-specific props (might pivot libraries soon!)
* more abstraction from bsStyle/bsSize props
* exchanging styles for a prop
* linting
* restoring feature flag to stock
* using src alias
* last <button> replacement
* this classname would never be applied
* more linting action
* fixing unsupported bsSize 'medium', and cta typing error
* more cta action
* unnecessary styles
* errant bsSize prop
* cleanup
* tweaks to make new New button work
* Linting
* fixing a couple tests
* fixing theme based test failure
* margin tweak for NEW button
* another fixed test
* another fixed test
* fixing two more tests
* fixing last broken tests.
* always be linting
* Adding tertiary/dashed buttons
* cleaning up QueryAndSave buttons
* fixing "link" button styles
* fixing/updating link button styles
* cta buttons on Modal component
* linting.
* exporting button story knobs, making ALL knobs safe for export.
* capitalizing a file... no big whoop
* Basic button tests
* renaming button - temporarily
* renaming file to fix capitalization issue
* passing theme through to a difficult popover.
* fixin' a newly busted unit test
* lint fixin'
* oops, shouldn't have changed this prop!
* adding a dive() to themedShallow, and fixing a cypress/jest test
* addressing lint stuff
* touching up stories, with SupersetButton story
* SIP-34 button colors
* updating button colors to match Superset theme
* abstracting away from bootstrap-specific props (might pivot libraries soon!)
* linting
* restoring feature flag to stock
* cleanup
* Linting
* renaming button - temporarily
* renaming file to fix capitalization issue
* oops, shouldn't have changed this prop!
* adding a dive() to themedShallow, and fixing a cypress/jest test
* addressing lint stuff
* nixing new modal button
* Fixing another popover/button issue that should break cypress
* lint :sparkles:
* passing classNames through to new button (should fix some tests)
* cleaning unused classes, making cypress tests use data attrs
* fixin' the test
* fixing another class-based test with data-test attr
* no longer passing theme as prop to buttons in popovers... themeprovider is better
* outline/border tweaks!
2020-08-28 20:34:28 -04:00
|
|
|
import { shallow } from 'enzyme';
|
|
|
|
import { styledMount as mount } from 'spec/helpers/theming';
|
|
|
|
import { FormControl, Modal, Radio } from 'react-bootstrap';
|
|
|
|
import Button from 'src/components/Button';
|
2016-11-18 17:56:02 -05:00
|
|
|
import sinon from 'sinon';
|
2018-10-16 17:54:31 -04:00
|
|
|
import fetchMock from 'fetch-mock';
|
2016-11-18 17:56:02 -05:00
|
|
|
|
2020-05-07 17:18:10 -04:00
|
|
|
import * as exploreUtils from 'src/explore/exploreUtils';
|
|
|
|
import * as saveModalActions from 'src/explore/actions/saveModalActions';
|
|
|
|
import SaveModal from 'src/explore/components/SaveModal';
|
2016-11-18 17:56:02 -05:00
|
|
|
|
|
|
|
describe('SaveModal', () => {
|
2017-08-10 20:04:44 -04:00
|
|
|
const middlewares = [thunk];
|
|
|
|
const mockStore = configureStore(middlewares);
|
|
|
|
const initialState = {
|
|
|
|
chart: {},
|
|
|
|
saveModal: {
|
|
|
|
dashboards: [],
|
|
|
|
},
|
|
|
|
explore: {
|
|
|
|
can_overwrite: true,
|
2017-11-13 19:06:45 -05:00
|
|
|
user_id: '1',
|
2017-08-10 20:04:44 -04:00
|
|
|
datasource: {},
|
|
|
|
slice: {
|
|
|
|
slice_id: 1,
|
|
|
|
slice_name: 'title',
|
|
|
|
},
|
|
|
|
alert: null,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
const store = mockStore(initialState);
|
2016-11-18 17:56:02 -05:00
|
|
|
|
2017-08-10 20:04:44 -04:00
|
|
|
const defaultProps = {
|
|
|
|
onHide: () => ({}),
|
2019-11-27 17:02:48 -05:00
|
|
|
actions: bindActionCreators(saveModalActions, arg => {
|
2019-04-24 17:43:13 -04:00
|
|
|
if (typeof arg === 'function') {
|
|
|
|
return arg(jest.fn);
|
|
|
|
}
|
|
|
|
return arg;
|
|
|
|
}),
|
2018-02-13 20:21:15 -05:00
|
|
|
form_data: { datasource: '107__table' },
|
2017-08-10 20:04:44 -04:00
|
|
|
};
|
|
|
|
const mockEvent = {
|
|
|
|
target: {
|
|
|
|
value: 'mock event target',
|
|
|
|
},
|
2020-07-23 03:26:29 -04:00
|
|
|
value: 10,
|
2017-08-10 20:04:44 -04:00
|
|
|
};
|
2018-10-16 17:54:31 -04:00
|
|
|
|
|
|
|
const getWrapper = () =>
|
|
|
|
shallow(<SaveModal {...defaultProps} />, {
|
|
|
|
context: { store },
|
|
|
|
}).dive();
|
2016-11-18 17:56:02 -05:00
|
|
|
|
2020-07-23 03:26:29 -04:00
|
|
|
it('renders a Modal with the right set of components', () => {
|
2017-08-10 20:04:44 -04:00
|
|
|
const wrapper = getWrapper();
|
2020-07-29 13:53:06 -04:00
|
|
|
expect(wrapper.find(Modal)).toExist();
|
|
|
|
expect(wrapper.find(FormControl)).toExist();
|
2020-07-23 03:26:29 -04:00
|
|
|
expect(wrapper.find(Button)).toHaveLength(3);
|
|
|
|
expect(wrapper.find(Radio)).toHaveLength(2);
|
2016-11-18 17:56:02 -05:00
|
|
|
});
|
2017-03-14 14:31:33 -04:00
|
|
|
|
2020-07-23 03:26:29 -04:00
|
|
|
it('overwrite radio button is disabled for new slice', () => {
|
|
|
|
const wrapper = getWrapper();
|
|
|
|
wrapper.setProps({ slice: null });
|
|
|
|
expect(wrapper.find('#overwrite-radio').prop('disabled')).toBe(true);
|
2017-03-14 14:31:33 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('disable overwrite option for non-owner', () => {
|
2017-08-10 20:04:44 -04:00
|
|
|
const wrapperForNonOwner = getWrapper();
|
|
|
|
wrapperForNonOwner.setProps({ can_overwrite: false });
|
2017-03-14 14:31:33 -04:00
|
|
|
const overwriteRadio = wrapperForNonOwner.find('#overwrite-radio');
|
2018-10-15 16:10:18 -04:00
|
|
|
expect(overwriteRadio).toHaveLength(1);
|
|
|
|
expect(overwriteRadio.prop('disabled')).toBe(true);
|
2017-03-14 14:31:33 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('saves a new slice', () => {
|
2017-08-10 20:04:44 -04:00
|
|
|
const wrapperForNewSlice = getWrapper();
|
|
|
|
wrapperForNewSlice.setProps({ can_overwrite: false });
|
|
|
|
wrapperForNewSlice.instance().changeAction('saveas');
|
2017-03-14 14:31:33 -04:00
|
|
|
const saveasRadio = wrapperForNewSlice.find('#saveas-radio');
|
|
|
|
saveasRadio.simulate('click');
|
2018-10-15 16:10:18 -04:00
|
|
|
expect(wrapperForNewSlice.state().action).toBe('saveas');
|
2017-03-14 14:31:33 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('overwrite a slice', () => {
|
2017-08-10 20:04:44 -04:00
|
|
|
const wrapperForOverwrite = getWrapper();
|
2017-03-14 14:31:33 -04:00
|
|
|
const overwriteRadio = wrapperForOverwrite.find('#overwrite-radio');
|
|
|
|
overwriteRadio.simulate('click');
|
2018-10-15 16:10:18 -04:00
|
|
|
expect(wrapperForOverwrite.state().action).toBe('overwrite');
|
2017-03-14 14:31:33 -04:00
|
|
|
});
|
2017-08-10 20:04:44 -04:00
|
|
|
|
|
|
|
it('componentDidMount', () => {
|
|
|
|
sinon.spy(SaveModal.prototype, 'componentDidMount');
|
2019-04-24 17:43:13 -04:00
|
|
|
sinon.spy(defaultProps.actions, 'fetchDashboards');
|
2017-08-10 20:04:44 -04:00
|
|
|
mount(<SaveModal {...defaultProps} />, {
|
|
|
|
context: { store },
|
|
|
|
});
|
2018-10-15 16:10:18 -04:00
|
|
|
expect(SaveModal.prototype.componentDidMount.calledOnce).toBe(true);
|
2019-04-24 17:43:13 -04:00
|
|
|
expect(defaultProps.actions.fetchDashboards.calledOnce).toBe(true);
|
2017-08-10 20:04:44 -04:00
|
|
|
|
|
|
|
SaveModal.prototype.componentDidMount.restore();
|
2019-04-24 17:43:13 -04:00
|
|
|
defaultProps.actions.fetchDashboards.restore();
|
2017-08-10 20:04:44 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('onChange', () => {
|
|
|
|
const wrapper = getWrapper();
|
|
|
|
|
2020-07-23 03:26:29 -04:00
|
|
|
wrapper.instance().onSliceNameChange(mockEvent);
|
2018-10-15 16:10:18 -04:00
|
|
|
expect(wrapper.state().newSliceName).toBe(mockEvent.target.value);
|
2017-08-10 20:04:44 -04:00
|
|
|
|
2020-07-23 03:26:29 -04:00
|
|
|
wrapper.instance().onDashboardSelectChange(mockEvent);
|
2018-10-15 16:10:18 -04:00
|
|
|
expect(wrapper.state().saveToDashboardId).toBe(mockEvent.value);
|
2017-08-10 20:04:44 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('saveOrOverwrite', () => {
|
|
|
|
beforeEach(() => {
|
2020-05-18 12:19:05 -04:00
|
|
|
sinon.stub(exploreUtils, 'getExploreUrl').callsFake(() => 'mockURL');
|
2018-10-16 17:54:31 -04:00
|
|
|
|
2019-11-27 17:02:48 -05:00
|
|
|
sinon.stub(defaultProps.actions, 'saveSlice').callsFake(() =>
|
|
|
|
Promise.resolve({
|
2019-12-18 19:17:48 -05:00
|
|
|
data: {
|
2020-07-23 03:26:29 -04:00
|
|
|
dashboard_url: 'http://localhost/mock_dashboard/',
|
2019-12-18 19:17:48 -05:00
|
|
|
slice: { slice_url: '/mock_slice/' },
|
|
|
|
},
|
2019-11-27 17:02:48 -05:00
|
|
|
}),
|
|
|
|
);
|
2017-08-10 20:04:44 -04:00
|
|
|
});
|
2018-10-16 17:54:31 -04:00
|
|
|
|
2017-08-10 20:04:44 -04:00
|
|
|
afterEach(() => {
|
2020-05-18 12:19:05 -04:00
|
|
|
exploreUtils.getExploreUrl.restore();
|
2019-04-24 17:43:13 -04:00
|
|
|
defaultProps.actions.saveSlice.restore();
|
2017-08-10 20:04:44 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should save slice', () => {
|
|
|
|
const wrapper = getWrapper();
|
|
|
|
wrapper.instance().saveOrOverwrite(true);
|
2020-09-14 13:11:20 -04:00
|
|
|
const { args } = defaultProps.actions.saveSlice.getCall(0);
|
2018-10-15 16:10:18 -04:00
|
|
|
expect(args[0]).toEqual(defaultProps.form_data);
|
2017-08-10 20:04:44 -04:00
|
|
|
});
|
2018-10-16 17:54:31 -04:00
|
|
|
|
2017-08-10 20:04:44 -04:00
|
|
|
it('existing dashboard', () => {
|
|
|
|
const wrapper = getWrapper();
|
|
|
|
const saveToDashboardId = 100;
|
|
|
|
|
|
|
|
wrapper.setState({ saveToDashboardId });
|
|
|
|
wrapper.instance().saveOrOverwrite(true);
|
2020-09-14 13:11:20 -04:00
|
|
|
const { args } = defaultProps.actions.saveSlice.getCall(0);
|
2018-10-15 16:10:18 -04:00
|
|
|
expect(args[1].save_to_dashboard_id).toBe(saveToDashboardId);
|
2017-08-10 20:04:44 -04:00
|
|
|
});
|
2018-10-16 17:54:31 -04:00
|
|
|
|
2017-08-10 20:04:44 -04:00
|
|
|
it('new dashboard', () => {
|
|
|
|
const wrapper = getWrapper();
|
|
|
|
const newDashboardName = 'new dashboard name';
|
|
|
|
|
|
|
|
wrapper.setState({ newDashboardName });
|
|
|
|
wrapper.instance().saveOrOverwrite(true);
|
2020-09-14 13:11:20 -04:00
|
|
|
const { args } = defaultProps.actions.saveSlice.getCall(0);
|
2018-10-15 16:10:18 -04:00
|
|
|
expect(args[1].new_dashboard_name).toBe(newDashboardName);
|
2017-08-10 20:04:44 -04:00
|
|
|
});
|
2019-12-18 19:17:48 -05:00
|
|
|
|
|
|
|
describe('should always reload or redirect', () => {
|
|
|
|
let wrapper;
|
2020-03-19 17:57:39 -04:00
|
|
|
let windowLocation;
|
|
|
|
|
2019-12-18 19:17:48 -05:00
|
|
|
beforeEach(() => {
|
|
|
|
wrapper = getWrapper();
|
2020-03-19 17:57:39 -04:00
|
|
|
windowLocation = window.location;
|
|
|
|
// To bypass "TypeError: Cannot redefine property: assign"
|
|
|
|
Object.defineProperty(window, 'location', {
|
|
|
|
value: { ...windowLocation, assign: () => {} },
|
|
|
|
});
|
2019-12-18 19:17:48 -05:00
|
|
|
sinon.stub(window.location, 'assign');
|
|
|
|
});
|
|
|
|
afterEach(() => {
|
|
|
|
window.location.assign.restore();
|
2020-03-19 17:57:39 -04:00
|
|
|
Object.defineProperty(window, 'location', windowLocation);
|
2019-12-18 19:17:48 -05:00
|
|
|
});
|
|
|
|
|
2020-06-29 00:37:04 -04:00
|
|
|
it('Save & go to dashboard', () => {
|
|
|
|
return new Promise(done => {
|
|
|
|
wrapper.instance().saveOrOverwrite(true);
|
|
|
|
defaultProps.actions.saveSlice().then(() => {
|
|
|
|
expect(window.location.assign.callCount).toEqual(1);
|
|
|
|
expect(window.location.assign.getCall(0).args[0]).toEqual(
|
|
|
|
'http://localhost/mock_dashboard/',
|
|
|
|
);
|
|
|
|
done();
|
|
|
|
});
|
2019-12-18 19:17:48 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-06-29 00:37:04 -04:00
|
|
|
it('saveas new slice', () => {
|
|
|
|
return new Promise(done => {
|
|
|
|
wrapper.setState({
|
|
|
|
action: 'saveas',
|
|
|
|
newSliceName: 'new slice name',
|
|
|
|
});
|
|
|
|
wrapper.instance().saveOrOverwrite(false);
|
|
|
|
defaultProps.actions.saveSlice().then(() => {
|
|
|
|
expect(window.location.assign.callCount).toEqual(1);
|
|
|
|
expect(window.location.assign.getCall(0).args[0]).toEqual(
|
|
|
|
'/mock_slice/',
|
|
|
|
);
|
|
|
|
done();
|
|
|
|
});
|
2019-12-18 19:17:48 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-06-29 00:37:04 -04:00
|
|
|
it('overwrite original slice', () => {
|
|
|
|
return new Promise(done => {
|
|
|
|
wrapper.setState({ action: 'overwrite' });
|
|
|
|
wrapper.instance().saveOrOverwrite(false);
|
|
|
|
defaultProps.actions.saveSlice().then(() => {
|
|
|
|
expect(window.location.assign.callCount).toEqual(1);
|
|
|
|
expect(window.location.assign.getCall(0).args[0]).toEqual(
|
|
|
|
'/mock_slice/',
|
|
|
|
);
|
|
|
|
done();
|
|
|
|
});
|
2019-12-18 19:17:48 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2017-08-10 20:04:44 -04:00
|
|
|
});
|
|
|
|
|
2018-10-16 17:54:31 -04:00
|
|
|
describe('fetchDashboards', () => {
|
2017-08-10 20:04:44 -04:00
|
|
|
let dispatch;
|
2018-10-16 17:54:31 -04:00
|
|
|
let actionThunk;
|
2017-08-10 20:04:44 -04:00
|
|
|
const userID = 1;
|
2018-10-16 17:54:31 -04:00
|
|
|
|
|
|
|
const mockDashboardData = {
|
|
|
|
pks: ['id'],
|
|
|
|
result: [{ id: 'id', dashboard_title: 'dashboard title' }],
|
|
|
|
};
|
|
|
|
|
|
|
|
const saveEndpoint = `glob:*/dashboardasync/api/read?_flt_0_owners=${1}`;
|
|
|
|
|
|
|
|
beforeAll(() => {
|
|
|
|
fetchMock.get(saveEndpoint, mockDashboardData);
|
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(fetchMock.restore);
|
|
|
|
|
2017-08-10 20:04:44 -04:00
|
|
|
beforeEach(() => {
|
|
|
|
dispatch = sinon.spy();
|
|
|
|
});
|
2018-10-16 17:54:31 -04:00
|
|
|
|
2017-08-10 20:04:44 -04:00
|
|
|
afterEach(() => {
|
2018-10-16 17:54:31 -04:00
|
|
|
fetchMock.resetHistory();
|
2017-08-10 20:04:44 -04:00
|
|
|
});
|
2018-10-16 17:54:31 -04:00
|
|
|
|
2017-08-10 20:04:44 -04:00
|
|
|
const makeRequest = () => {
|
2018-10-16 17:54:31 -04:00
|
|
|
actionThunk = saveModalActions.fetchDashboards(userID);
|
|
|
|
return actionThunk(dispatch);
|
2017-08-10 20:04:44 -04:00
|
|
|
};
|
|
|
|
|
2019-11-27 17:02:48 -05:00
|
|
|
it('makes the fetch request', () =>
|
2018-10-16 17:54:31 -04:00
|
|
|
makeRequest().then(() => {
|
|
|
|
expect(fetchMock.calls(saveEndpoint)).toHaveLength(1);
|
2017-08-10 20:04:44 -04:00
|
|
|
|
2018-10-16 17:54:31 -04:00
|
|
|
return Promise.resolve();
|
2019-11-27 17:02:48 -05:00
|
|
|
}));
|
2018-10-16 17:54:31 -04:00
|
|
|
|
2019-11-27 17:02:48 -05:00
|
|
|
it('calls correct actions on success', () =>
|
2018-10-16 17:54:31 -04:00
|
|
|
makeRequest().then(() => {
|
|
|
|
expect(dispatch.callCount).toBe(1);
|
|
|
|
expect(dispatch.getCall(0).args[0].type).toBe(
|
|
|
|
saveModalActions.FETCH_DASHBOARDS_SUCCEEDED,
|
|
|
|
);
|
|
|
|
|
|
|
|
return Promise.resolve();
|
2019-11-27 17:02:48 -05:00
|
|
|
}));
|
2017-08-10 20:04:44 -04:00
|
|
|
|
|
|
|
it('calls correct actions on error', () => {
|
2019-11-27 17:02:48 -05:00
|
|
|
fetchMock.get(
|
|
|
|
saveEndpoint,
|
|
|
|
{ throws: 'error' },
|
|
|
|
{ overwriteRoutes: true },
|
|
|
|
);
|
2017-08-10 20:04:44 -04:00
|
|
|
|
2018-10-16 17:54:31 -04:00
|
|
|
return makeRequest().then(() => {
|
|
|
|
expect(dispatch.callCount).toBe(1);
|
2019-11-27 17:02:48 -05:00
|
|
|
expect(dispatch.getCall(0).args[0].type).toBe(
|
|
|
|
saveModalActions.FETCH_DASHBOARDS_FAILED,
|
|
|
|
);
|
2018-10-16 17:54:31 -04:00
|
|
|
|
2019-11-27 17:02:48 -05:00
|
|
|
fetchMock.get(saveEndpoint, mockDashboardData, {
|
|
|
|
overwriteRoutes: true,
|
|
|
|
});
|
2018-10-16 17:54:31 -04:00
|
|
|
|
|
|
|
return Promise.resolve();
|
|
|
|
});
|
2017-08-10 20:04:44 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('removeAlert', () => {
|
2019-04-24 17:43:13 -04:00
|
|
|
sinon.spy(defaultProps.actions, 'removeSaveModalAlert');
|
2017-08-10 20:04:44 -04:00
|
|
|
const wrapper = getWrapper();
|
|
|
|
wrapper.setProps({ alert: 'old alert' });
|
|
|
|
|
|
|
|
wrapper.instance().removeAlert();
|
2019-04-24 17:43:13 -04:00
|
|
|
expect(defaultProps.actions.removeSaveModalAlert.callCount).toBe(1);
|
2018-10-15 16:10:18 -04:00
|
|
|
expect(wrapper.state().alert).toBeNull();
|
2019-04-24 17:43:13 -04:00
|
|
|
defaultProps.actions.removeSaveModalAlert.restore();
|
2017-08-10 20:04:44 -04:00
|
|
|
});
|
2016-11-18 17:56:02 -05:00
|
|
|
});
|