add if check around using async handler object in VisEditorVisualization (#21454) (#21548)

* add if check around using async loaded object in VisEditorVisualization

* immediatly call _handler.update when handler is loaded and update has occured

* add jest test

* lint fixes

* remove sinon
This commit is contained in:
Nathan Reese 2018-08-01 11:14:48 -06:00 committed by GitHub
parent 1e7d3f7bca
commit 367320c1f4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 75 additions and 3 deletions

View file

@ -69,6 +69,12 @@ class VisEditorVisualization extends Component {
}
}
onUpdate = () => {
this._handler.update({
timeRange: this.props.timeRange
});
}
_loadVisualization() {
getVisualizeLoader().then(loader => {
if (!this._visEl.current) {
@ -83,13 +89,20 @@ class VisEditorVisualization extends Component {
timeRange: this.props.timeRange,
appState: this.props.appState,
});
if (this._handlerUpdateHasAlreadyBeenTriggered) {
this.onUpdate();
}
});
}
componentDidUpdate() {
this._handler.update({
timeRange: this.props.timeRange
});
if (!this._handler) {
this._handlerUpdateHasAlreadyBeenTriggered = true;
return;
}
this.onUpdate();
}
componentDidMount() {

View file

@ -0,0 +1,59 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. 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.
*/
jest.mock('ui/visualize/loader/visualize_loader', () => ({}));
import React from 'react';
import { mount } from 'enzyme';
import VisEditorVisualization from './vis_editor_visualization';
describe('getVisualizeLoader', () => {
let updateStub;
beforeEach(() => {
updateStub = jest.fn();
const handlerMock = {
update: updateStub
};
const loaderMock = {
embedVisualizationWithSavedObject: () => {
return handlerMock;
}
};
require('ui/visualize/loader/visualize_loader').getVisualizeLoader = async () => {
return loaderMock;
};
});
it('should not call _handler.update until getVisualizeLoader returns _handler', async () => {
const wrapper = mount(
<VisEditorVisualization />
);
// Set prop to force DOM change and componentDidUpdate to be triggered
wrapper.setProps({ dirty: true });
// Ensure all promises resolve
await new Promise(resolve => process.nextTick(resolve));
// Ensure the state changes are reflected
wrapper.update();
expect(updateStub).toHaveBeenCalled();
});
});