summaryrefslogtreecommitdiff
path: root/spec/frontend/repository/commits_service_spec.js
blob: e56975d021a65f3037f0dce7a8735444bf9fad12 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import MockAdapter from 'axios-mock-adapter';
import axios from '~/lib/utils/axios_utils';
import { loadCommits, isRequested, resetRequestedCommits } from '~/repository/commits_service';
import { HTTP_STATUS_INTERNAL_SERVER_ERROR, HTTP_STATUS_OK } from '~/lib/utils/http_status';
import { createAlert } from '~/flash';
import { I18N_COMMIT_DATA_FETCH_ERROR } from '~/repository/constants';
import { refWithSpecialCharMock } from './mock_data';

jest.mock('~/flash');

describe('commits service', () => {
  let mock;
  const url = `${gon.relative_url_root || ''}/my-project/-/refs/main/logs_tree/`;

  beforeEach(() => {
    mock = new MockAdapter(axios);

    mock.onGet(url).reply(HTTP_STATUS_OK, [], {});

    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 } });
  });

  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);

    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);
  });

  it('calls `createAlert` when the request fails', async () => {
    const invalidPath = '/#@ some/path';
    const invalidUrl = `${url}${invalidPath}`;
    mock.onGet(invalidUrl).replyOnce(HTTP_STATUS_INTERNAL_SERVER_ERROR, [], {});

    await requestCommits(1, 'my-project', invalidPath);

    expect(createAlert).toHaveBeenCalledWith({ message: I18N_COMMIT_DATA_FETCH_ERROR });
  });
});