2018-03-09 15:18:59 -05:00
|
|
|
import $ from 'jquery';
|
2017-05-16 17:01:51 -04:00
|
|
|
import 'vendor/jquery.endless-scroll';
|
2018-01-26 04:41:13 -05:00
|
|
|
import MockAdapter from 'axios-mock-adapter';
|
|
|
|
import axios from '~/lib/utils/axios_utils';
|
2017-10-09 06:46:40 -04:00
|
|
|
import CommitsList from '~/commits';
|
2018-04-24 17:35:32 -04:00
|
|
|
import Pager from '~/pager';
|
2017-01-07 18:29:10 -05:00
|
|
|
|
2017-10-09 06:46:40 -04:00
|
|
|
describe('Commits List', () => {
|
2018-02-15 12:17:29 -05:00
|
|
|
let commitsList;
|
|
|
|
|
2017-10-09 06:46:40 -04:00
|
|
|
beforeEach(() => {
|
|
|
|
setFixtures(`
|
|
|
|
<form class="commits-search-form" action="/h5bp/html5-boilerplate/commits/master">
|
|
|
|
<input id="commits-search">
|
|
|
|
</form>
|
|
|
|
<ol id="commits-list"></ol>
|
|
|
|
`);
|
2020-05-11 11:09:37 -04:00
|
|
|
jest.spyOn(Pager, 'init').mockImplementation(() => {});
|
2018-02-15 12:17:29 -05:00
|
|
|
commitsList = new CommitsList(25);
|
2017-10-09 06:46:40 -04:00
|
|
|
});
|
2017-01-07 18:29:10 -05:00
|
|
|
|
2017-10-09 06:46:40 -04:00
|
|
|
it('should be defined', () => {
|
|
|
|
expect(CommitsList).toBeDefined();
|
|
|
|
});
|
2017-06-02 21:40:20 -04:00
|
|
|
|
2017-10-09 06:46:40 -04:00
|
|
|
describe('processCommits', () => {
|
|
|
|
it('should join commit headers', () => {
|
2018-02-15 12:17:29 -05:00
|
|
|
commitsList.$contentList = $(`
|
2017-10-09 06:46:40 -04:00
|
|
|
<div>
|
2017-06-02 21:40:20 -04:00
|
|
|
<li class="commit-header" data-day="2016-09-20">
|
|
|
|
<span class="day">20 Sep, 2016</span>
|
|
|
|
<span class="commits-count">1 commit</span>
|
|
|
|
</li>
|
|
|
|
<li class="commit"></li>
|
2017-10-09 06:46:40 -04:00
|
|
|
</div>
|
|
|
|
`);
|
2017-06-02 21:40:20 -04:00
|
|
|
|
2017-10-09 06:46:40 -04:00
|
|
|
const data = `
|
|
|
|
<li class="commit-header" data-day="2016-09-20">
|
|
|
|
<span class="day">20 Sep, 2016</span>
|
|
|
|
<span class="commits-count">1 commit</span>
|
|
|
|
</li>
|
|
|
|
<li class="commit"></li>
|
|
|
|
`;
|
|
|
|
|
|
|
|
// The last commit header should be removed
|
|
|
|
// since the previous one has the same data-day value.
|
2018-02-15 12:17:29 -05:00
|
|
|
expect(commitsList.processCommits(data).find('li.commit-header').length).toBe(0);
|
2017-06-02 21:40:20 -04:00
|
|
|
});
|
2017-10-09 06:46:40 -04:00
|
|
|
});
|
2017-06-02 21:40:20 -04:00
|
|
|
|
2017-10-09 06:46:40 -04:00
|
|
|
describe('on entering input', () => {
|
|
|
|
let ajaxSpy;
|
2018-01-26 04:41:13 -05:00
|
|
|
let mock;
|
2017-01-07 18:29:10 -05:00
|
|
|
|
2017-10-09 06:46:40 -04:00
|
|
|
beforeEach(() => {
|
2018-02-15 12:17:29 -05:00
|
|
|
commitsList.searchField.val('');
|
2017-01-28 12:38:30 -05:00
|
|
|
|
2020-05-11 11:09:37 -04:00
|
|
|
jest.spyOn(window.history, 'replaceState').mockImplementation(() => {});
|
2018-01-26 04:41:13 -05:00
|
|
|
mock = new MockAdapter(axios);
|
|
|
|
|
|
|
|
mock.onGet('/h5bp/html5-boilerplate/commits/master').reply(200, {
|
|
|
|
html: '<li>Result</li>',
|
2017-01-07 18:29:10 -05:00
|
|
|
});
|
2018-01-26 04:41:13 -05:00
|
|
|
|
2020-05-11 11:09:37 -04:00
|
|
|
ajaxSpy = jest.spyOn(axios, 'get');
|
2018-01-26 04:41:13 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
mock.restore();
|
2017-10-09 06:46:40 -04:00
|
|
|
});
|
2017-01-07 18:29:10 -05:00
|
|
|
|
2020-12-23 16:10:24 -05:00
|
|
|
it('should save the last search string', (done) => {
|
2018-02-15 12:17:29 -05:00
|
|
|
commitsList.searchField.val('GitLab');
|
2018-04-24 17:35:32 -04:00
|
|
|
commitsList
|
|
|
|
.filterResults()
|
2018-01-26 04:41:13 -05:00
|
|
|
.then(() => {
|
|
|
|
expect(ajaxSpy).toHaveBeenCalled();
|
2018-02-15 12:17:29 -05:00
|
|
|
expect(commitsList.lastSearch).toEqual('GitLab');
|
2018-01-26 04:41:13 -05:00
|
|
|
|
|
|
|
done();
|
|
|
|
})
|
|
|
|
.catch(done.fail);
|
2017-10-09 06:46:40 -04:00
|
|
|
});
|
2017-01-07 18:29:10 -05:00
|
|
|
|
2020-12-23 16:10:24 -05:00
|
|
|
it('should not make ajax call if the input does not change', (done) => {
|
2018-04-24 17:35:32 -04:00
|
|
|
commitsList
|
|
|
|
.filterResults()
|
2018-01-26 04:41:13 -05:00
|
|
|
.then(() => {
|
|
|
|
expect(ajaxSpy).not.toHaveBeenCalled();
|
2018-02-15 12:17:29 -05:00
|
|
|
expect(commitsList.lastSearch).toEqual('');
|
2018-01-26 04:41:13 -05:00
|
|
|
|
|
|
|
done();
|
|
|
|
})
|
|
|
|
.catch(done.fail);
|
2017-01-07 18:29:10 -05:00
|
|
|
});
|
|
|
|
});
|
2017-10-09 06:46:40 -04:00
|
|
|
});
|