gitlab-org--gitlab-foss/spec/javascripts/commits_spec.js

99 lines
2.5 KiB
JavaScript
Raw Normal View History

import $ from 'jquery';
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';
import CommitsList from '~/commits';
2018-04-24 17:35:32 -04:00
import Pager from '~/pager';
describe('Commits List', () => {
2018-02-15 12:17:29 -05:00
let commitsList;
beforeEach(() => {
setFixtures(`
<form class="commits-search-form" action="/h5bp/html5-boilerplate/commits/master">
<input id="commits-search">
</form>
<ol id="commits-list"></ol>
`);
2018-04-24 17:35:32 -04:00
spyOn(Pager, 'init').and.stub();
2018-02-15 12:17:29 -05:00
commitsList = new CommitsList(25);
});
it('should be defined', () => {
expect(CommitsList).toBeDefined();
});
describe('processCommits', () => {
it('should join commit headers', () => {
2018-02-15 12:17:29 -05:00
commitsList.$contentList = $(`
<div>
<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>
</div>
`);
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);
});
});
describe('on entering input', () => {
let ajaxSpy;
2018-01-26 04:41:13 -05:00
let mock;
beforeEach(() => {
2018-02-15 12:17:29 -05:00
commitsList.searchField.val('');
2017-01-28 12:38:30 -05:00
spyOn(window.history, 'replaceState').and.stub();
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>',
});
2018-01-26 04:41:13 -05:00
ajaxSpy = spyOn(axios, 'get').and.callThrough();
});
afterEach(() => {
mock.restore();
});
2018-04-24 17:35:32 -04: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);
});
2018-04-24 17:35:32 -04:00
it('should not make ajax call if the input does not change', done => {
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);
});
});
});