2018-02-08 03:47:31 -05:00
|
|
|
import MockAdapter from 'axios-mock-adapter';
|
2019-12-10 04:07:51 -05:00
|
|
|
import { TEST_HOST } from 'spec/test_constants';
|
2018-02-08 03:47:31 -05:00
|
|
|
import axios from '~/lib/utils/axios_utils';
|
2017-08-15 04:20:35 -04:00
|
|
|
import GpgBadges from '~/gpg_badges';
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
beforeEach(() => {
|
2018-02-08 03:47:31 -05:00
|
|
|
mock = new MockAdapter(axios);
|
2017-08-15 04:20:35 -04:00
|
|
|
setFixtures(`
|
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">
|
|
|
|
<input name="utf8" type="hidden" value="✓">
|
|
|
|
<input type="search" name="search" id="commits-search"class="form-control search-text-input input-short">
|
|
|
|
</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>
|
|
|
|
`);
|
|
|
|
});
|
|
|
|
|
2018-02-08 03:47:31 -05:00
|
|
|
afterEach(() => {
|
|
|
|
mock.restore();
|
|
|
|
});
|
2017-08-15 04:20:35 -04:00
|
|
|
|
2020-12-23 16:10:24 -05:00
|
|
|
it('does not make a request if there is no container element', (done) => {
|
2018-07-23 10:58:21 -04:00
|
|
|
setFixtures('');
|
2019-12-20 10:07:34 -05:00
|
|
|
jest.spyOn(axios, 'get').mockImplementation(() => {});
|
2017-08-15 04:20:35 -04:00
|
|
|
|
2018-07-23 10:58:21 -04:00
|
|
|
GpgBadges.fetch()
|
|
|
|
.then(() => {
|
|
|
|
expect(axios.get).not.toHaveBeenCalled();
|
|
|
|
})
|
|
|
|
.then(done)
|
|
|
|
.catch(done.fail);
|
2017-08-15 04:20:35 -04:00
|
|
|
});
|
|
|
|
|
2020-12-23 16:10:24 -05:00
|
|
|
it('throws an error if the endpoint is missing', (done) => {
|
2018-07-23 10:58:21 -04:00
|
|
|
setFixtures('<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
|
|
|
|
2018-07-23 10:58:21 -04:00
|
|
|
GpgBadges.fetch()
|
|
|
|
.then(() => done.fail('Expected error to be thrown'))
|
2020-12-23 16:10:24 -05:00
|
|
|
.catch((error) => {
|
2018-07-23 10:58:21 -04:00
|
|
|
expect(error.message).toBe('Missing commit signatures endpoint!');
|
|
|
|
expect(axios.get).not.toHaveBeenCalled();
|
|
|
|
})
|
|
|
|
.then(done)
|
|
|
|
.catch(done.fail);
|
|
|
|
});
|
|
|
|
|
2020-12-23 16:10:24 -05:00
|
|
|
it('displays a loading spinner', (done) => {
|
2018-07-23 10:58:21 -04:00
|
|
|
mock.onGet(dummyUrl).replyOnce(200);
|
|
|
|
|
|
|
|
GpgBadges.fetch()
|
|
|
|
.then(() => {
|
|
|
|
expect(document.querySelector('.js-loading-gpg-badge:empty')).toBe(null);
|
2020-09-14 02:09:28 -04:00
|
|
|
const spinners = document.querySelectorAll('.js-loading-gpg-badge span.gl-spinner');
|
2018-10-09 14:03:09 -04:00
|
|
|
|
2018-07-23 10:58:21 -04:00
|
|
|
expect(spinners.length).toBe(1);
|
|
|
|
done();
|
|
|
|
})
|
|
|
|
.catch(done.fail);
|
|
|
|
});
|
|
|
|
|
2020-12-23 16:10:24 -05:00
|
|
|
it('replaces the loading spinner', (done) => {
|
2018-07-23 10:58:21 -04:00
|
|
|
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-10-09 14:03:09 -04:00
|
|
|
|
2018-07-23 10:58:21 -04:00
|
|
|
expect(parentContainer.innerHTML.trim()).toEqual(dummyBadgeHtml);
|
|
|
|
done();
|
|
|
|
})
|
|
|
|
.catch(done.fail);
|
2017-08-15 04:20:35 -04:00
|
|
|
});
|
|
|
|
});
|