Skip to content

Commit

Permalink
Unit test for request cancel
Browse files Browse the repository at this point in the history
  • Loading branch information
Mogball committed Nov 28, 2017
1 parent 02d9922 commit bd0b5f0
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ export default class FilterControl extends React.Component {
return { filters: newStateFilters };
});
// if there is an outstanding request to fetch values, cancel it.
if (this.state.activeRequest) this.state.activeRequest.abort();
if (this.state.activeRequest) {
this.state.activeRequest.abort();
}
this.setState({
activeRequest: $.ajax({
type: 'GET',
Expand All @@ -56,7 +58,7 @@ export default class FilterControl extends React.Component {
this.setState((prevState) => {
const newStateFilters = Object.assign([], prevState.filters);
newStateFilters[index] = { valuesLoading: false, valueChoices: data };
return { filters: newStateFilters };
return { filters: newStateFilters, activeRequest: null };
});
},
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,15 @@ describe('FilterControl', () => {
]);
});

before(() => {
sinon.stub($, 'ajax');
});

after(() => {
$.ajax.restore();
});

it('makes a GET request to retrieve value choices', () => {
sinon.stub($, 'ajax');
wrapper.instance().fetchFilterValues(0, 'col1');
expect($.ajax.getCall(0).args[0].type).to.deep.equal('GET');
expect($.ajax.getCall(0).args[0].url).to.deep.equal('/superset/filter/qtable/1/col1/');
Expand Down Expand Up @@ -214,4 +217,31 @@ describe('FilterControl', () => {
},
]);
});

it('tracks an active filter select ajax request', () => {
const spyReq = sinon.spy();
$.ajax.reset();
$.ajax.onFirstCall().returns(spyReq);
wrapper.instance().fetchFilterValues(0, 'col1');
expect(wrapper.state().activeRequest).to.equal(spyReq);
// Sets active to null after success
$.ajax.getCall(0).args[0].success('choices');
expect(wrapper.state().filters[0].valuesLoading).to.equal(false);
expect(wrapper.state().filters[0].valueChoices).to.equal('choices');
expect(wrapper.state().activeRequest).to.equal(null);
});

it('cancels active request if another is submitted', () => {
const spyReq = sinon.spy();
spyReq.abort = sinon.spy();
$.ajax.reset();
$.ajax.onFirstCall().returns(spyReq);
wrapper.instance().fetchFilterValues(0, 'col1');
expect(wrapper.state().activeRequest).to.equal(spyReq);
const spyReq1 = sinon.spy();
$.ajax.onSecondCall().returns(spyReq1);
wrapper.instance().fetchFilterValues(1, 'col2');
expect(spyReq.abort.called).to.equal(true);
expect(wrapper.state().activeRequest).to.equal(spyReq1);
});
});

0 comments on commit bd0b5f0

Please sign in to comment.