2020-01-01 13:55:28 +05:30
|
|
|
import MockAdapter from 'axios-mock-adapter';
|
2018-03-27 19:54:05 +05:30
|
|
|
import { ImporterStatus } from '~/importer_status';
|
|
|
|
import axios from '~/lib/utils/axios_utils';
|
|
|
|
|
|
|
|
describe('Importer Status', () => {
|
|
|
|
let instance;
|
|
|
|
let mock;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
mock = new MockAdapter(axios);
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
mock.restore();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('addToImport', () => {
|
|
|
|
const importUrl = '/import_url';
|
2020-06-23 00:09:42 +05:30
|
|
|
const fixtures = `
|
|
|
|
<table>
|
2018-03-27 19:54:05 +05:30
|
|
|
<tr id="repo_123">
|
|
|
|
<td class="import-target"></td>
|
|
|
|
<td class="import-actions job-status">
|
|
|
|
<button name="button" type="submit" class="btn btn-import js-add-to-import">
|
|
|
|
</button>
|
|
|
|
</td>
|
|
|
|
</tr>
|
2020-06-23 00:09:42 +05:30
|
|
|
</table>
|
|
|
|
`;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
setFixtures(fixtures);
|
|
|
|
jest.spyOn(ImporterStatus.prototype, 'initStatusPage').mockImplementation(() => {});
|
|
|
|
jest.spyOn(ImporterStatus.prototype, 'setAutoUpdate').mockImplementation(() => {});
|
2018-03-27 19:54:05 +05:30
|
|
|
instance = new ImporterStatus({
|
|
|
|
jobsUrl: '',
|
|
|
|
importUrl,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
it('sets table row to active after post request', done => {
|
2018-03-27 19:54:05 +05:30
|
|
|
mock.onPost(importUrl).reply(200, {
|
|
|
|
id: 1,
|
|
|
|
full_path: '/full_path',
|
|
|
|
});
|
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
instance
|
|
|
|
.addToImport({
|
|
|
|
currentTarget: document.querySelector('.js-add-to-import'),
|
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
expect(document.querySelector('tr').classList.contains('table-active')).toEqual(true);
|
|
|
|
done();
|
|
|
|
})
|
|
|
|
.catch(done.fail);
|
2018-11-08 19:23:39 +05:30
|
|
|
});
|
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
it('shows error message after failed POST request', done => {
|
2020-06-23 00:09:42 +05:30
|
|
|
setFixtures(`${fixtures}<div class="flash-container"></div>`);
|
2018-11-08 19:23:39 +05:30
|
|
|
|
|
|
|
mock.onPost(importUrl).reply(422, {
|
|
|
|
errors: 'You forgot your lunch',
|
|
|
|
});
|
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
instance
|
|
|
|
.addToImport({
|
|
|
|
currentTarget: document.querySelector('.js-add-to-import'),
|
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
const flashMessage = document.querySelector('.flash-text');
|
|
|
|
|
|
|
|
expect(flashMessage.textContent.trim()).toEqual(
|
|
|
|
'An error occurred while importing project: You forgot your lunch',
|
|
|
|
);
|
|
|
|
done();
|
|
|
|
})
|
|
|
|
.catch(done.fail);
|
2018-03-27 19:54:05 +05:30
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('autoUpdate', () => {
|
|
|
|
const jobsUrl = '/jobs_url';
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
const div = document.createElement('div');
|
|
|
|
div.innerHTML = `
|
|
|
|
<div id="project_1">
|
|
|
|
<div class="job-status">
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
`;
|
|
|
|
|
|
|
|
document.body.appendChild(div);
|
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
jest.spyOn(ImporterStatus.prototype, 'initStatusPage').mockImplementation(() => {});
|
|
|
|
jest.spyOn(ImporterStatus.prototype, 'setAutoUpdate').mockImplementation(() => {});
|
2018-03-27 19:54:05 +05:30
|
|
|
instance = new ImporterStatus({
|
|
|
|
jobsUrl,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
function setupMock(importStatus) {
|
2018-12-13 13:39:08 +05:30
|
|
|
mock.onGet(jobsUrl).reply(200, [
|
|
|
|
{
|
|
|
|
id: 1,
|
|
|
|
import_status: importStatus,
|
|
|
|
},
|
|
|
|
]);
|
2018-03-27 19:54:05 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
function expectJobStatus(done, status) {
|
2018-12-13 13:39:08 +05:30
|
|
|
instance
|
|
|
|
.autoUpdate()
|
2018-03-27 19:54:05 +05:30
|
|
|
.then(() => {
|
|
|
|
expect(document.querySelector('#project_1').innerText.trim()).toEqual(status);
|
|
|
|
done();
|
|
|
|
})
|
|
|
|
.catch(done.fail);
|
|
|
|
}
|
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
it('sets the job status to done', done => {
|
2018-03-27 19:54:05 +05:30
|
|
|
setupMock('finished');
|
|
|
|
expectJobStatus(done, 'Done');
|
|
|
|
});
|
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
it('sets the job status to scheduled', done => {
|
2018-03-27 19:54:05 +05:30
|
|
|
setupMock('scheduled');
|
|
|
|
expectJobStatus(done, 'Scheduled');
|
|
|
|
});
|
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
it('sets the job status to started', done => {
|
2018-03-27 19:54:05 +05:30
|
|
|
setupMock('started');
|
|
|
|
expectJobStatus(done, 'Started');
|
|
|
|
});
|
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
it('sets the job status to custom status', done => {
|
2018-03-27 19:54:05 +05:30
|
|
|
setupMock('custom status');
|
|
|
|
expectJobStatus(done, 'custom status');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|