status_bar.test.js 2.0 KB
Newer Older
1 2 3 4
const assert = require('assert');
const sinon = require('sinon');
const vscode = require('vscode');
const statusBar = require('../../src/status_bar');
T
Tomas Vik 已提交
5
const { tokenService } = require('../../src/services/token_service');
6 7 8
const pipelinesResponse = require('./fixtures/rest/pipelines.json');
const pipelineResponse = require('./fixtures/rest/pipeline.json');
const { getServer, createJsonEndpoint } = require('./test_infrastructure/mock_server');
9
const { GITLAB_URL } = require('./test_infrastructure/constants');
10 11 12 13 14 15 16 17 18 19 20 21

describe('GitLab status bar', () => {
  let server;
  let returnedItems = [];
  const sandbox = sinon.createSandbox();

  const createFakeStatusBarItem = () => {
    const fakeItem = { show: sinon.spy(), hide: sinon.spy(), dispose: sinon.spy() };
    returnedItems.push(fakeItem);
    return fakeItem;
  };

22
  before(async () => {
23 24 25 26
    server = getServer([
      createJsonEndpoint('/projects/278964/pipelines?ref=master', pipelinesResponse),
      createJsonEndpoint('/projects/278964/pipelines/47', pipelineResponse),
    ]);
27
    await tokenService.setToken(GITLAB_URL, 'abcd-secret');
28 29 30 31 32 33 34 35 36 37 38 39 40
  });

  beforeEach(() => {
    server.resetHandlers();
    sandbox.stub(vscode.window, 'createStatusBarItem').callsFake(createFakeStatusBarItem);
  });

  afterEach(() => {
    statusBar.dispose();
    sandbox.restore();
    returnedItems = [];
  });

41
  after(async () => {
42
    server.close();
43
    await tokenService.setToken(GITLAB_URL, undefined);
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
  });

  it('shows the correct pipeline item', async () => {
    await statusBar.init({ subscriptions: [] });

    assert.strictEqual(
      vscode.window.createStatusBarItem.firstCall.firstArg,
      vscode.StatusBarAlignment.Left,
    );
    const pipelineItem = returnedItems[0];
    assert.strictEqual(pipelineItem.text, '$(check) GitLab: Pipeline passed');
    assert.strictEqual(pipelineItem.show.called, true);
    assert.strictEqual(pipelineItem.hide.called, false);
    assert.strictEqual(pipelineItem.command, 'gl.pipelineActions');
  });
});