importer_status_spec.js 3.4 KB
Newer Older
1 2 3 4 5
import { ImporterStatus } from '~/importer_status';
import axios from '~/lib/utils/axios_utils';
import MockAdapter from 'axios-mock-adapter';

describe('Importer Status', () => {
6 7 8 9 10 11 12 13 14 15 16
  let instance;
  let mock;

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

  afterEach(() => {
    mock.restore();
  });

17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
  describe('addToImport', () => {
    const importUrl = '/import_url';

    beforeEach(() => {
      setFixtures(`
        <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>
      `);
      spyOn(ImporterStatus.prototype, 'initStatusPage').and.callFake(() => {});
      spyOn(ImporterStatus.prototype, 'setAutoUpdate').and.callFake(() => {});
32 33 34 35
      instance = new ImporterStatus({
        jobsUrl: '',
        importUrl,
      });
36 37 38 39 40 41 42 43 44 45 46 47
    });

    it('sets table row to active after post request', (done) => {
      mock.onPost(importUrl).reply(200, {
        id: 1,
        full_path: '/full_path',
      });

      instance.addToImport({
        currentTarget: document.querySelector('.js-add-to-import'),
      })
      .then(() => {
S
Stan Hu 已提交
48
        expect(document.querySelector('tr').classList.contains('table-active')).toEqual(true);
49 50 51 52
        done();
      })
      .catch(done.fail);
    });
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70

    it('shows error message after failed POST request', (done) => {
      appendSetFixtures('<div class="flash-container"></div>');

      mock.onPost(importUrl).reply(422, {
        errors: 'You forgot your lunch',
      });

      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);
    });
71
  });
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88

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

      spyOn(ImporterStatus.prototype, 'initStatusPage').and.callFake(() => {});
      spyOn(ImporterStatus.prototype, 'setAutoUpdate').and.callFake(() => {});
89 90 91
      instance = new ImporterStatus({
        jobsUrl,
      });
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
    });

    function setupMock(importStatus) {
      mock.onGet(jobsUrl).reply(200, [{
        id: 1,
        import_status: importStatus,
      }]);
    }

    function expectJobStatus(done, status) {
      instance.autoUpdate()
        .then(() => {
          expect(document.querySelector('#project_1').innerText.trim()).toEqual(status);
          done();
        })
        .catch(done.fail);
    }

    it('sets the job status to done', (done) => {
      setupMock('finished');
112
      expectJobStatus(done, 'Done');
113 114 115 116
    });

    it('sets the job status to scheduled', (done) => {
      setupMock('scheduled');
117
      expectJobStatus(done, 'Scheduled');
118 119 120 121
    });

    it('sets the job status to started', (done) => {
      setupMock('started');
122
      expectJobStatus(done, 'Started');
123 124 125 126 127 128 129
    });

    it('sets the job status to custom status', (done) => {
      setupMock('custom status');
      expectJobStatus(done, 'custom status');
    });
  });
130
});