debian-mirror-gitlab/spec/frontend/commits_spec.js

92 lines
2.5 KiB
JavaScript
Raw Normal View History

2021-03-11 19:13:27 +05:30
import MockAdapter from 'axios-mock-adapter';
2018-05-09 12:01:36 +05:30
import $ from 'jquery';
2017-09-10 17:25:29 +05:30
import 'vendor/jquery.endless-scroll';
2022-07-16 23:28:13 +05:30
import { setHTMLFixture, resetHTMLFixture } from 'helpers/fixtures';
2018-03-17 18:26:18 +05:30
import CommitsList from '~/commits';
2021-03-11 19:13:27 +05:30
import axios from '~/lib/utils/axios_utils';
2018-10-15 14:42:47 +05:30
import Pager from '~/pager';
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
describe('Commits List', () => {
2018-03-27 19:54:05 +05:30
let commitsList;
2018-03-17 18:26:18 +05:30
beforeEach(() => {
2022-07-16 23:28:13 +05:30
setHTMLFixture(`
2021-06-08 01:23:25 +05:30
<form class="commits-search-form" action="/h5bp/html5-boilerplate/commits/main">
2018-03-17 18:26:18 +05:30
<input id="commits-search">
</form>
<ol id="commits-list"></ol>
`);
2020-05-24 23:13:21 +05:30
jest.spyOn(Pager, 'init').mockImplementation(() => {});
2018-03-27 19:54:05 +05:30
commitsList = new CommitsList(25);
2018-03-17 18:26:18 +05:30
});
2017-08-17 22:00:37 +05:30
2022-07-16 23:28:13 +05:30
afterEach(() => {
resetHTMLFixture();
});
2018-03-17 18:26:18 +05:30
it('should be defined', () => {
expect(CommitsList).toBeDefined();
});
2017-09-10 17:25:29 +05:30
2018-03-17 18:26:18 +05:30
describe('processCommits', () => {
it('should join commit headers', () => {
2018-03-27 19:54:05 +05:30
commitsList.$contentList = $(`
2018-03-17 18:26:18 +05:30
<div>
2017-09-10 17:25:29 +05:30
<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>
2018-03-17 18:26:18 +05:30
</div>
`);
2017-09-10 17:25:29 +05:30
2018-03-17 18:26:18 +05:30
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-03-27 19:54:05 +05:30
expect(commitsList.processCommits(data).find('li.commit-header').length).toBe(0);
2017-09-10 17:25:29 +05:30
});
2018-03-17 18:26:18 +05:30
});
2017-09-10 17:25:29 +05:30
2018-03-17 18:26:18 +05:30
describe('on entering input', () => {
let ajaxSpy;
let mock;
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
beforeEach(() => {
2018-03-27 19:54:05 +05:30
commitsList.searchField.val('');
2017-08-17 22:00:37 +05:30
2020-05-24 23:13:21 +05:30
jest.spyOn(window.history, 'replaceState').mockImplementation(() => {});
2018-03-17 18:26:18 +05:30
mock = new MockAdapter(axios);
2017-08-17 22:00:37 +05:30
2021-06-08 01:23:25 +05:30
mock.onGet('/h5bp/html5-boilerplate/commits/main').reply(200, {
2018-03-17 18:26:18 +05:30
html: '<li>Result</li>',
2017-08-17 22:00:37 +05:30
});
2020-05-24 23:13:21 +05:30
ajaxSpy = jest.spyOn(axios, 'get');
2018-03-17 18:26:18 +05:30
});
afterEach(() => {
mock.restore();
});
2022-06-21 17:19:12 +05:30
it('should save the last search string', async () => {
2018-03-27 19:54:05 +05:30
commitsList.searchField.val('GitLab');
2022-06-21 17:19:12 +05:30
await commitsList.filterResults();
expect(ajaxSpy).toHaveBeenCalled();
expect(commitsList.lastSearch).toEqual('GitLab');
2018-03-17 18:26:18 +05:30
});
2022-06-21 17:19:12 +05:30
it('should not make ajax call if the input does not change', async () => {
await commitsList.filterResults();
expect(ajaxSpy).not.toHaveBeenCalled();
expect(commitsList.lastSearch).toEqual('');
2017-08-17 22:00:37 +05:30
});
});
2018-03-17 18:26:18 +05:30
});