Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions ghost/admin/app/services/unsplash.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,16 @@ export default Service.extend({
yield timeout(DEBOUNCE_MS);

let url = `${API_URL}/search/photos?query=${term}&per_page=30`;
yield this._makeRequest(url);
yield this._makeRequest(url, {searchTermAtRequest: term});
}).restartable(),

_addPhotosFromResponse(response) {
_addPhotosFromResponse(response, searchTermAtRequest) {
// If a search term was provided at the time of the request,
// we only want to add photos if the search term matches the current one.
if (searchTermAtRequest && searchTermAtRequest !== this.searchTerm) {
return;
}

let photos = response.results || response;

photos.forEach(photo => this._addPhoto(photo));
Expand Down Expand Up @@ -189,9 +195,9 @@ export default Service.extend({

return fetch(url, {headers})
.then(response => this._checkStatus(response))
.then(response => this._extractPagination(response))
.then(response => this._extractPagination(response, options.searchTermAtRequest))
.then(response => response.json())
.then(response => this._addPhotosFromResponse(response))
.then(response => this._addPhotosFromResponse(response, options.searchTermAtRequest))
.catch(() => {
// if the error text isn't already set then we've get a connection error from `fetch`
if (!options.ignoreErrors && !this.error) {
Expand Down Expand Up @@ -233,7 +239,11 @@ export default Service.extend({
});
},

_extractPagination(response) {
_extractPagination(response, searchTermAtRequest) {
if (searchTermAtRequest && searchTermAtRequest !== this.searchTerm) {
return response;
}

let pagination = {};
let linkRegex = new RegExp('<(.*)>; rel="(.*)"');
let {link: links} = response.headers.map;
Expand Down
43 changes: 43 additions & 0 deletions ghost/admin/tests/unit/services/unsplash-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,49 @@ describe('Unit: Service: unsplash', function () {
it('can load next page of search results');
it('clears photos when starting new search');
it('loads new when query is cleared');

it('discards responses that no longer match the current search term', async function () {
server.get('https://api.unsplash.com/photos', function () {
return [200, {'Content-Type': 'application/json'}, JSON.stringify([])];
});
server.get('https://api.unsplash.com/search/photos', function () {
return [200, {
'Content-Type': 'application/json',
Link: '<https://api.unsplash.com/search/photos?query=cat&page=2>; rel="next"'
}, JSON.stringify({results: [{id: 'cat-photo', width: 100, height: 100}]})];
});

let service = this.owner.lookup('service:unsplash');
await settled();

// response for "cat" arrives after the term has changed to "dog"
service.set('searchTerm', 'dog');
await service._makeRequest('https://api.unsplash.com/search/photos?query=cat', {searchTermAtRequest: 'cat'});

expect(service.photos.length).to.equal(0);
expect(service._pagination.next).to.not.exist;
});

it('adds photos and pagination when the response matches the current search term', async function () {
server.get('https://api.unsplash.com/photos', function () {
return [200, {'Content-Type': 'application/json'}, JSON.stringify([])];
});
server.get('https://api.unsplash.com/search/photos', function () {
return [200, {
'Content-Type': 'application/json',
Link: '<https://api.unsplash.com/search/photos?query=cat&page=2>; rel="next"'
}, JSON.stringify({results: [{id: 'cat-photo', width: 100, height: 100}]})];
});

let service = this.owner.lookup('service:unsplash');
await settled();

service.set('searchTerm', 'cat');
await service._makeRequest('https://api.unsplash.com/search/photos?query=cat', {searchTermAtRequest: 'cat'});

expect(service.photos.length).to.equal(1);
expect(service._pagination.next).to.exist;
});
});

describe('columns', function () {
Expand Down
Loading