2021-11-18 22:05:49 +05:30
|
|
|
import MockAdapter from 'axios-mock-adapter';
|
|
|
|
import axios from '~/lib/utils/axios_utils';
|
|
|
|
import { loadCommits, isRequested, resetRequestedCommits } from '~/repository/commits_service';
|
2023-03-17 16:20:25 +05:30
|
|
|
import { HTTP_STATUS_INTERNAL_SERVER_ERROR, HTTP_STATUS_OK } from '~/lib/utils/http_status';
|
2023-05-27 22:25:52 +05:30
|
|
|
import { createAlert } from '~/alert';
|
2021-11-18 22:05:49 +05:30
|
|
|
import { I18N_COMMIT_DATA_FETCH_ERROR } from '~/repository/constants';
|
2023-03-17 16:20:25 +05:30
|
|
|
import { refWithSpecialCharMock } from './mock_data';
|
2021-11-18 22:05:49 +05:30
|
|
|
|
2023-05-27 22:25:52 +05:30
|
|
|
jest.mock('~/alert');
|
2021-11-18 22:05:49 +05:30
|
|
|
|
|
|
|
describe('commits service', () => {
|
|
|
|
let mock;
|
|
|
|
const url = `${gon.relative_url_root || ''}/my-project/-/refs/main/logs_tree/`;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
mock = new MockAdapter(axios);
|
|
|
|
|
2023-03-17 16:20:25 +05:30
|
|
|
mock.onGet(url).reply(HTTP_STATUS_OK, [], {});
|
2021-11-18 22:05:49 +05:30
|
|
|
|
|
|
|
jest.spyOn(axios, 'get');
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
mock.restore();
|
|
|
|
resetRequestedCommits();
|
|
|
|
});
|
|
|
|
|
|
|
|
const requestCommits = (offset, project = 'my-project', path = '', ref = 'main') =>
|
|
|
|
loadCommits(project, path, ref, offset);
|
|
|
|
|
|
|
|
it('calls axios get', async () => {
|
|
|
|
const offset = 10;
|
|
|
|
const project = 'my-project';
|
|
|
|
const path = 'my-path';
|
|
|
|
const ref = 'my-ref';
|
|
|
|
const testUrl = `${gon.relative_url_root || ''}/${project}/-/refs/${ref}/logs_tree/${path}`;
|
|
|
|
|
|
|
|
await requestCommits(offset, project, path, ref);
|
|
|
|
|
|
|
|
expect(axios.get).toHaveBeenCalledWith(testUrl, { params: { format: 'json', offset } });
|
|
|
|
});
|
|
|
|
|
2023-03-17 16:20:25 +05:30
|
|
|
it('encodes the path and ref', async () => {
|
|
|
|
const encodedRef = encodeURIComponent(refWithSpecialCharMock);
|
|
|
|
const encodedUrl = `/some-project/-/refs/${encodedRef}/logs_tree/with%20%24peci%40l%20ch%40rs%2F`;
|
|
|
|
|
|
|
|
await requestCommits(1, 'some-project', 'with $peci@l ch@rs/', refWithSpecialCharMock);
|
2021-11-18 22:05:49 +05:30
|
|
|
|
|
|
|
expect(axios.get).toHaveBeenCalledWith(encodedUrl, expect.anything());
|
|
|
|
});
|
|
|
|
|
|
|
|
it('calls axios get once per batch', async () => {
|
|
|
|
await Promise.all([requestCommits(0), requestCommits(1), requestCommits(23)]);
|
|
|
|
|
|
|
|
expect(axios.get.mock.calls.length).toEqual(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('updates the list of requested offsets', async () => {
|
|
|
|
await requestCommits(200);
|
|
|
|
|
|
|
|
expect(isRequested(200)).toBe(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('resets the list of requested offsets', async () => {
|
|
|
|
await requestCommits(300);
|
|
|
|
|
|
|
|
resetRequestedCommits();
|
|
|
|
expect(isRequested(300)).toBe(false);
|
|
|
|
});
|
|
|
|
|
2022-11-25 23:54:43 +05:30
|
|
|
it('calls `createAlert` when the request fails', async () => {
|
2021-11-18 22:05:49 +05:30
|
|
|
const invalidPath = '/#@ some/path';
|
|
|
|
const invalidUrl = `${url}${invalidPath}`;
|
2023-03-17 16:20:25 +05:30
|
|
|
mock.onGet(invalidUrl).replyOnce(HTTP_STATUS_INTERNAL_SERVER_ERROR, [], {});
|
2021-11-18 22:05:49 +05:30
|
|
|
|
|
|
|
await requestCommits(1, 'my-project', invalidPath);
|
|
|
|
|
2022-11-25 23:54:43 +05:30
|
|
|
expect(createAlert).toHaveBeenCalledWith({ message: I18N_COMMIT_DATA_FETCH_ERROR });
|
2021-11-18 22:05:49 +05:30
|
|
|
});
|
|
|
|
});
|