2018-02-08 03:47:31 -05:00
|
|
|
import MockAdapter from 'axios-mock-adapter';
|
2022-05-13 08:08:49 -04:00
|
|
|
import { setHTMLFixture, resetHTMLFixture } from 'helpers/fixtures';
|
2019-12-10 04:07:51 -05:00
|
|
|
import { TEST_HOST } from 'spec/test_constants';
|
2017-08-15 04:20:35 -04:00
|
|
|
import GpgBadges from '~/gpg_badges';
|
2021-02-14 13:09:20 -05:00
|
|
|
import axios from '~/lib/utils/axios_utils';
|
2017-08-15 04:20:35 -04:00
|
|
|
|
|
|
|
describe('GpgBadges', () => {
|
2018-02-08 03:47:31 -05:00
|
|
|
let mock;
|
2017-08-15 04:20:35 -04:00
|
|
|
const dummyCommitSha = 'n0m0rec0ffee';
|
|
|
|
const dummyBadgeHtml = 'dummy html';
|
|
|
|
const dummyResponse = {
|
2018-07-23 10:58:21 -04:00
|
|
|
signatures: [
|
|
|
|
{
|
|
|
|
commit_sha: dummyCommitSha,
|
|
|
|
html: dummyBadgeHtml,
|
|
|
|
},
|
|
|
|
],
|
2017-08-15 04:20:35 -04:00
|
|
|
};
|
2018-07-23 10:58:21 -04:00
|
|
|
const dummyUrl = `${TEST_HOST}/dummy/signatures`;
|
2017-08-15 04:20:35 -04:00
|
|
|
|
2021-06-17 17:10:25 -04:00
|
|
|
const setForm = ({ utf8 = '✓', search = '' } = {}) => {
|
2022-05-13 08:08:49 -04:00
|
|
|
setHTMLFixture(`
|
2018-05-07 04:41:36 -04:00
|
|
|
<form
|
2018-07-23 10:58:21 -04:00
|
|
|
class="commits-search-form js-signature-container" data-signatures-path="${dummyUrl}" action="${dummyUrl}"
|
2018-02-08 03:47:31 -05:00
|
|
|
method="get">
|
2021-06-17 17:10:25 -04:00
|
|
|
<input name="utf8" type="hidden" value="${utf8}">
|
|
|
|
<input type="search" name="search" value="${search}" id="commits-search"class="form-control search-text-input input-short">
|
2018-02-08 03:47:31 -05:00
|
|
|
</form>
|
2017-08-15 04:20:35 -04:00
|
|
|
<div class="parent-container">
|
|
|
|
<div class="js-loading-gpg-badge" data-commit-sha="${dummyCommitSha}"></div>
|
|
|
|
</div>
|
|
|
|
`);
|
2021-06-17 17:10:25 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
mock = new MockAdapter(axios);
|
|
|
|
setForm();
|
2017-08-15 04:20:35 -04:00
|
|
|
});
|
|
|
|
|
2018-02-08 03:47:31 -05:00
|
|
|
afterEach(() => {
|
|
|
|
mock.restore();
|
2022-05-13 08:08:49 -04:00
|
|
|
resetHTMLFixture();
|
2018-02-08 03:47:31 -05:00
|
|
|
});
|
2017-08-15 04:20:35 -04:00
|
|
|
|
2022-04-13 17:09:57 -04:00
|
|
|
it('does not make a request if there is no container element', async () => {
|
2022-05-13 08:08:49 -04:00
|
|
|
setHTMLFixture('');
|
2019-12-20 10:07:34 -05:00
|
|
|
jest.spyOn(axios, 'get').mockImplementation(() => {});
|
2017-08-15 04:20:35 -04:00
|
|
|
|
2022-04-13 17:09:57 -04:00
|
|
|
await GpgBadges.fetch();
|
|
|
|
expect(axios.get).not.toHaveBeenCalled();
|
2022-05-13 08:08:49 -04:00
|
|
|
resetHTMLFixture();
|
2017-08-15 04:20:35 -04:00
|
|
|
});
|
|
|
|
|
2022-04-13 17:09:57 -04:00
|
|
|
it('throws an error if the endpoint is missing', async () => {
|
2022-05-13 08:08:49 -04:00
|
|
|
setHTMLFixture('<div class="js-signature-container"></div>');
|
2019-12-20 10:07:34 -05:00
|
|
|
jest.spyOn(axios, 'get').mockImplementation(() => {});
|
2017-08-15 04:20:35 -04:00
|
|
|
|
2022-04-13 17:09:57 -04:00
|
|
|
await expect(GpgBadges.fetch()).rejects.toEqual(
|
|
|
|
new Error('Missing commit signatures endpoint!'),
|
|
|
|
);
|
|
|
|
expect(axios.get).not.toHaveBeenCalled();
|
2022-05-13 08:08:49 -04:00
|
|
|
resetHTMLFixture();
|
2018-07-23 10:58:21 -04:00
|
|
|
});
|
|
|
|
|
2021-06-17 17:10:25 -04:00
|
|
|
it('fetches commit signatures', async () => {
|
|
|
|
mock.onGet(dummyUrl).replyOnce(200);
|
|
|
|
|
|
|
|
await GpgBadges.fetch();
|
|
|
|
|
|
|
|
expect(mock.history.get).toHaveLength(1);
|
|
|
|
expect(mock.history.get[0]).toMatchObject({
|
|
|
|
params: { search: '', utf8: '✓' },
|
|
|
|
url: dummyUrl,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('fetches commit signatures with search parameters with spaces', async () => {
|
|
|
|
mock.onGet(dummyUrl).replyOnce(200);
|
|
|
|
setForm({ search: 'my search' });
|
|
|
|
|
|
|
|
await GpgBadges.fetch();
|
|
|
|
|
|
|
|
expect(mock.history.get).toHaveLength(1);
|
|
|
|
expect(mock.history.get[0]).toMatchObject({
|
|
|
|
params: { search: 'my search', utf8: '✓' },
|
|
|
|
url: dummyUrl,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('fetches commit signatures with search parameters with plus symbols', async () => {
|
|
|
|
mock.onGet(dummyUrl).replyOnce(200);
|
|
|
|
setForm({ search: 'my+search' });
|
|
|
|
|
|
|
|
await GpgBadges.fetch();
|
|
|
|
|
|
|
|
expect(mock.history.get).toHaveLength(1);
|
|
|
|
expect(mock.history.get[0]).toMatchObject({
|
|
|
|
params: { search: 'my+search', utf8: '✓' },
|
|
|
|
url: dummyUrl,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-04-13 17:09:57 -04:00
|
|
|
it('displays a loading spinner', async () => {
|
2018-07-23 10:58:21 -04:00
|
|
|
mock.onGet(dummyUrl).replyOnce(200);
|
|
|
|
|
2022-04-13 17:09:57 -04:00
|
|
|
await GpgBadges.fetch();
|
|
|
|
expect(document.querySelector('.js-loading-gpg-badge:empty')).toBe(null);
|
|
|
|
const spinners = document.querySelectorAll('.js-loading-gpg-badge span.gl-spinner');
|
2018-10-09 14:03:09 -04:00
|
|
|
|
2022-04-13 17:09:57 -04:00
|
|
|
expect(spinners.length).toBe(1);
|
2018-07-23 10:58:21 -04:00
|
|
|
});
|
|
|
|
|
2022-04-13 17:09:57 -04:00
|
|
|
it('replaces the loading spinner', async () => {
|
2018-07-23 10:58:21 -04:00
|
|
|
mock.onGet(dummyUrl).replyOnce(200, dummyResponse);
|
|
|
|
|
2022-04-13 17:09:57 -04:00
|
|
|
await GpgBadges.fetch();
|
|
|
|
expect(document.querySelector('.js-loading-gpg-badge')).toBe(null);
|
|
|
|
const parentContainer = document.querySelector('.parent-container');
|
2018-10-09 14:03:09 -04:00
|
|
|
|
2022-04-13 17:09:57 -04:00
|
|
|
expect(parentContainer.innerHTML.trim()).toEqual(dummyBadgeHtml);
|
2017-08-15 04:20:35 -04:00
|
|
|
});
|
|
|
|
});
|