summaryrefslogtreecommitdiff
path: root/spec/frontend/ide/components/terminal/session_spec.js
blob: 2399446ed1508a45e82809b6c3b0c23f7b566b81 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import { createLocalVue, shallowMount } from '@vue/test-utils';
import Vuex from 'vuex';
import TerminalSession from '~/ide/components/terminal/session.vue';
import Terminal from '~/ide/components/terminal/terminal.vue';
import {
  STARTING,
  PENDING,
  RUNNING,
  STOPPING,
  STOPPED,
} from '~/ide/stores/modules/terminal/constants';

const TEST_TERMINAL_PATH = 'terminal/path';

const localVue = createLocalVue();
localVue.use(Vuex);

describe('IDE TerminalSession', () => {
  let wrapper;
  let actions;
  let state;

  const factory = (options = {}) => {
    const store = new Vuex.Store({
      modules: {
        terminal: {
          namespaced: true,
          actions,
          state,
        },
      },
    });

    wrapper = shallowMount(TerminalSession, {
      localVue,
      store,
      ...options,
    });
  };

  beforeEach(() => {
    state = {
      session: { status: RUNNING, terminalPath: TEST_TERMINAL_PATH },
    };
    actions = {
      restartSession: jest.fn(),
      stopSession: jest.fn(),
    };
  });

  it('is empty if session is falsey', () => {
    state.session = null;
    factory();

    expect(wrapper.isEmpty()).toBe(true);
  });

  it('shows terminal', () => {
    factory();

    expect(wrapper.find(Terminal).props()).toEqual({
      terminalPath: TEST_TERMINAL_PATH,
      status: RUNNING,
    });
  });

  [STARTING, PENDING, RUNNING].forEach(status => {
    it(`show stop button when status is ${status}`, () => {
      state.session = { status };
      factory();

      const button = wrapper.find('button');
      button.trigger('click');

      return wrapper.vm.$nextTick().then(() => {
        expect(button.text()).toEqual('Stop Terminal');
        expect(actions.stopSession).toHaveBeenCalled();
      });
    });
  });

  [STOPPING, STOPPED].forEach(status => {
    it(`show stop button when status is ${status}`, () => {
      state.session = { status };
      factory();

      const button = wrapper.find('button');
      button.trigger('click');

      return wrapper.vm.$nextTick().then(() => {
        expect(button.text()).toEqual('Restart Terminal');
        expect(actions.restartSession).toHaveBeenCalled();
      });
    });
  });
});