2018-03-27 19:54:05 +05:30
|
|
|
import MockAdapter from 'axios-mock-adapter';
|
2020-01-01 13:55:28 +05:30
|
|
|
import { TEST_HOST } from 'spec/test_constants';
|
2017-09-10 17:25:29 +05:30
|
|
|
import GpgBadges from '~/gpg_badges';
|
2021-03-11 19:13:27 +05:30
|
|
|
import axios from '~/lib/utils/axios_utils';
|
2017-09-10 17:25:29 +05:30
|
|
|
|
|
|
|
describe('GpgBadges', () => {
|
2018-03-27 19:54:05 +05:30
|
|
|
let mock;
|
2017-09-10 17:25:29 +05:30
|
|
|
const dummyCommitSha = 'n0m0rec0ffee';
|
|
|
|
const dummyBadgeHtml = 'dummy html';
|
|
|
|
const dummyResponse = {
|
2018-11-18 11:00:15 +05:30
|
|
|
signatures: [
|
|
|
|
{
|
|
|
|
commit_sha: dummyCommitSha,
|
|
|
|
html: dummyBadgeHtml,
|
|
|
|
},
|
|
|
|
],
|
2017-09-10 17:25:29 +05:30
|
|
|
};
|
2018-11-18 11:00:15 +05:30
|
|
|
const dummyUrl = `${TEST_HOST}/dummy/signatures`;
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2021-09-30 23:02:18 +05:30
|
|
|
const setForm = ({ utf8 = '✓', search = '' } = {}) => {
|
2017-09-10 17:25:29 +05:30
|
|
|
setFixtures(`
|
2018-10-15 14:42:47 +05:30
|
|
|
<form
|
2018-11-18 11:00:15 +05:30
|
|
|
class="commits-search-form js-signature-container" data-signatures-path="${dummyUrl}" action="${dummyUrl}"
|
2018-03-27 19:54:05 +05:30
|
|
|
method="get">
|
2021-09-30 23:02:18 +05:30
|
|
|
<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-03-27 19:54:05 +05:30
|
|
|
</form>
|
2017-09-10 17:25:29 +05:30
|
|
|
<div class="parent-container">
|
|
|
|
<div class="js-loading-gpg-badge" data-commit-sha="${dummyCommitSha}"></div>
|
|
|
|
</div>
|
|
|
|
`);
|
2021-09-30 23:02:18 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
mock = new MockAdapter(axios);
|
|
|
|
setForm();
|
2017-09-10 17:25:29 +05:30
|
|
|
});
|
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
afterEach(() => {
|
|
|
|
mock.restore();
|
|
|
|
});
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
it('does not make a request if there is no container element', (done) => {
|
2018-11-18 11:00:15 +05:30
|
|
|
setFixtures('');
|
2020-03-13 15:44:24 +05:30
|
|
|
jest.spyOn(axios, 'get').mockImplementation(() => {});
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2018-11-18 11:00:15 +05:30
|
|
|
GpgBadges.fetch()
|
|
|
|
.then(() => {
|
|
|
|
expect(axios.get).not.toHaveBeenCalled();
|
|
|
|
})
|
|
|
|
.then(done)
|
|
|
|
.catch(done.fail);
|
2017-09-10 17:25:29 +05:30
|
|
|
});
|
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
it('throws an error if the endpoint is missing', (done) => {
|
2018-11-18 11:00:15 +05:30
|
|
|
setFixtures('<div class="js-signature-container"></div>');
|
2020-03-13 15:44:24 +05:30
|
|
|
jest.spyOn(axios, 'get').mockImplementation(() => {});
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2018-11-18 11:00:15 +05:30
|
|
|
GpgBadges.fetch()
|
|
|
|
.then(() => done.fail('Expected error to be thrown'))
|
2021-03-08 18:12:59 +05:30
|
|
|
.catch((error) => {
|
2018-11-18 11:00:15 +05:30
|
|
|
expect(error.message).toBe('Missing commit signatures endpoint!');
|
|
|
|
expect(axios.get).not.toHaveBeenCalled();
|
|
|
|
})
|
|
|
|
.then(done)
|
|
|
|
.catch(done.fail);
|
|
|
|
});
|
|
|
|
|
2021-09-30 23:02:18 +05:30
|
|
|
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,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
it('displays a loading spinner', (done) => {
|
2018-11-18 11:00:15 +05:30
|
|
|
mock.onGet(dummyUrl).replyOnce(200);
|
|
|
|
|
|
|
|
GpgBadges.fetch()
|
|
|
|
.then(() => {
|
|
|
|
expect(document.querySelector('.js-loading-gpg-badge:empty')).toBe(null);
|
2020-11-24 15:15:51 +05:30
|
|
|
const spinners = document.querySelectorAll('.js-loading-gpg-badge span.gl-spinner');
|
2018-12-13 13:39:08 +05:30
|
|
|
|
2018-11-18 11:00:15 +05:30
|
|
|
expect(spinners.length).toBe(1);
|
|
|
|
done();
|
|
|
|
})
|
|
|
|
.catch(done.fail);
|
|
|
|
});
|
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
it('replaces the loading spinner', (done) => {
|
2018-11-18 11:00:15 +05:30
|
|
|
mock.onGet(dummyUrl).replyOnce(200, dummyResponse);
|
|
|
|
|
|
|
|
GpgBadges.fetch()
|
|
|
|
.then(() => {
|
|
|
|
expect(document.querySelector('.js-loading-gpg-badge')).toBe(null);
|
|
|
|
const parentContainer = document.querySelector('.parent-container');
|
2018-12-13 13:39:08 +05:30
|
|
|
|
2018-11-18 11:00:15 +05:30
|
|
|
expect(parentContainer.innerHTML.trim()).toEqual(dummyBadgeHtml);
|
|
|
|
done();
|
|
|
|
})
|
|
|
|
.catch(done.fail);
|
2017-09-10 17:25:29 +05:30
|
|
|
});
|
|
|
|
});
|