changed commits.js to axios

This commit is contained in:
Phil Hughes 2018-01-26 09:41:13 +00:00
parent a16cdd0b3e
commit 39e9a17b4b
No known key found for this signature in database
GPG key ID: 32245528C52E0F9F
2 changed files with 49 additions and 28 deletions

View file

@ -5,6 +5,7 @@
import { pluralize } from './lib/utils/text_utility'; import { pluralize } from './lib/utils/text_utility';
import { localTimeAgo } from './lib/utils/datetime_utility'; import { localTimeAgo } from './lib/utils/datetime_utility';
import Pager from './pager'; import Pager from './pager';
import axios from './lib/utils/axios_utils';
export default (function () { export default (function () {
const CommitsList = {}; const CommitsList = {};
@ -43,29 +44,30 @@ export default (function () {
CommitsList.filterResults = function () { CommitsList.filterResults = function () {
const form = $('.commits-search-form'); const form = $('.commits-search-form');
const search = CommitsList.searchField.val(); const search = CommitsList.searchField.val();
if (search === CommitsList.lastSearch) return; if (search === CommitsList.lastSearch) return Promise.resolve();
const commitsUrl = form.attr('action') + '?' + form.serialize(); const commitsUrl = form.attr('action') + '?' + form.serialize();
CommitsList.content.fadeTo('fast', 0.5); CommitsList.content.fadeTo('fast', 0.5);
return $.ajax({ const params = form.serializeArray().reduce((acc, obj) => Object.assign(acc, {
type: 'GET', [obj.name]: obj.value,
url: form.attr('action'), }), {});
data: form.serialize(),
complete: function () { return axios.get(form.attr('action'), {
return CommitsList.content.fadeTo('fast', 1.0); params,
}, })
success: function (data) { .then(({ data }) => {
CommitsList.lastSearch = search; CommitsList.lastSearch = search;
CommitsList.content.html(data.html); CommitsList.content.html(data.html);
return history.replaceState({ CommitsList.content.fadeTo('fast', 1.0);
page: commitsUrl,
// Change url so if user reload a page - search results are saved // Change url so if user reload a page - search results are saved
history.replaceState({
page: commitsUrl,
}, document.title, commitsUrl); }, document.title, commitsUrl);
}, })
error: function () { .catch(() => {
CommitsList.content.fadeTo('fast', 1.0);
CommitsList.lastSearch = null; CommitsList.lastSearch = null;
}, });
dataType: 'json',
});
}; };
// Prepare loaded data. // Prepare loaded data.

View file

@ -1,4 +1,6 @@
import 'vendor/jquery.endless-scroll'; import 'vendor/jquery.endless-scroll';
import MockAdapter from 'axios-mock-adapter';
import axios from '~/lib/utils/axios_utils';
import CommitsList from '~/commits'; import CommitsList from '~/commits';
describe('Commits List', () => { describe('Commits List', () => {
@ -43,30 +45,47 @@ describe('Commits List', () => {
describe('on entering input', () => { describe('on entering input', () => {
let ajaxSpy; let ajaxSpy;
let mock;
beforeEach(() => { beforeEach(() => {
CommitsList.init(25); CommitsList.init(25);
CommitsList.searchField.val(''); CommitsList.searchField.val('');
spyOn(history, 'replaceState').and.stub(); spyOn(history, 'replaceState').and.stub();
ajaxSpy = spyOn(jQuery, 'ajax').and.callFake((req) => { mock = new MockAdapter(axios);
req.success({
data: '<li>Result</li>', mock.onGet('/h5bp/html5-boilerplate/commits/master').reply(200, {
}); html: '<li>Result</li>',
}); });
ajaxSpy = spyOn(axios, 'get').and.callThrough();
}); });
it('should save the last search string', () => { afterEach(() => {
mock.restore();
});
it('should save the last search string', (done) => {
CommitsList.searchField.val('GitLab'); CommitsList.searchField.val('GitLab');
CommitsList.filterResults(); CommitsList.filterResults()
expect(ajaxSpy).toHaveBeenCalled(); .then(() => {
expect(CommitsList.lastSearch).toEqual('GitLab'); expect(ajaxSpy).toHaveBeenCalled();
expect(CommitsList.lastSearch).toEqual('GitLab');
done();
})
.catch(done.fail);
}); });
it('should not make ajax call if the input does not change', () => { it('should not make ajax call if the input does not change', (done) => {
CommitsList.filterResults(); CommitsList.filterResults()
expect(ajaxSpy).not.toHaveBeenCalled(); .then(() => {
expect(CommitsList.lastSearch).toEqual(''); expect(ajaxSpy).not.toHaveBeenCalled();
expect(CommitsList.lastSearch).toEqual('');
done();
})
.catch(done.fail);
}); });
}); });
}); });