Skip to content

Commit

Permalink
Fix commented out Tests for loading Search Results
Browse files Browse the repository at this point in the history
Fix the commented out tests for loading SearchBox search results and
rendering them with HTML, due to an asynchronous problem when running
tests, where occasionally the answer will change depending on whether
the results come back in time; meaning one of two answers will be true
and the other false, depending on when it is called or if it is called
multiple times; e.g.

```javascript
    it('_getResults (after AJAX request with no params)', function (done) {
      const _subject = new describedClass();

      _subject.reRenderCallback = function expectedResults(component, _) {
        const results = component._getResults();
        expect([
          jasmine.any(NullSearchResult),
          jasmine.any(NullComponent)
        ]).toContain(results);
      };

      _subject._searchFromServer('', '', '', function(_error, searchbox) {
        var results = searchbox._getResults();
        expect(results).toEqual(jasmine.any(NullSearchResult));
        done();
      });
    });
```

The SearchBox will be rendered normally on first load containing a
`NullComponent` as a placeholder component until results are loaded, in
this case it returns no results for an empty search and then renders a
`NullSearchResult` component. However, the test will fail unless it is
true for at least one of them when the other is not present; i.e.

- Is true when it renders `NullComponent`    and not `NullSearchResult`,
  and
- Is true when it renders `NulLSearchResult` and not `NullComponent`,
  but
- Is not true when it only renders one of them (since one or the other
  is rendered at some point in the future)

== Notes:
- Some tests have remained disabled since the
  `'_searchFromServer returns expected flights data'` test
  only returns expected results data if `_searchFromServer()` is not
  called multiple time prior to it (meaning it may fail on other
  computers <sigh>)

== References:
- [Pass callback to wrapper.setState · Issue #509 · airbnb/enzyme]
  (enzymejs/enzyme#509)

- [node.js - How do I change the timeout on a jasmine-node async spec - Stack Overflow]
  (https://stackoverflow.com/questions/9867601/how-do-i-change-the-timeout-on-a-jasmine-node-async-spec)
  • Loading branch information
Sonna committed Nov 16, 2017
1 parent 14a6c6e commit 6b83079
Show file tree
Hide file tree
Showing 2 changed files with 416 additions and 380 deletions.
17 changes: 9 additions & 8 deletions lib/components/SearchBox.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,15 @@ function SearchBox(properties) {
this.setState(properties);
};

SearchBox.prototype.setState = function (properties) {
SearchBox.prototype.setState = function (properties, callback) {
this.state = Object.assign({}, this.state, properties);

var self = this;
var parentEl = this.reRenderLocation;
// this.update(self, parentEl);
this.reRenderCallback(self, parentEl);

(typeof callback === 'function') && callback(null, self);
};

SearchBox.prototype.render = function () {
Expand Down Expand Up @@ -144,7 +146,7 @@ function travelDates(travelDate) {
}

// SearchBox.prototype._updateResults = function (from, to, travel_date) {
SearchBox.prototype._searchFromServer = function (from, to, travelDate) {
SearchBox.prototype._searchFromServer = function (from, to, travelDate, callback) {
var self = this;
var promises = travelDates(travelDate).map(function (newTravelDate) {
return new Promise(function (success, fail) {
Expand Down Expand Up @@ -181,29 +183,28 @@ SearchBox.prototype._searchFromServer = function (from, to, travelDate) {
results: self.state.results
});

return Promise.all(promises)
Promise.all(promises)
.then(function (results) {
// Remove empty results
var reducedResults = results.reduce(function (obj, item) {
if(item.value.length > 0) {
obj[item.key] = item.value;
}
return obj;
}, {});

// Update SearchBox with results and stop loading animation
self.setState({
hasUpdatedResults: true,
from: from,
to: to,
travelDate: travelDate,
loadingResults: false,
results: reducedResults
});
// (typeof callback === 'function') && callback(null, results);
return self;
}, callback);
}).catch(function (error) {
console.log(error);
// (typeof callback === 'function') && callback(error, {});
return error;
(typeof callback === 'function') && callback(error, {});
});
}

Expand Down
Loading

0 comments on commit 6b83079

Please sign in to comment.