summaryrefslogtreecommitdiff
path: root/spec/frontend/ide/stores/modules/terminal_sync/actions_spec.js
blob: 448fd909f3976dc81e9e0be0b69e67a1f6d476bd (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
97
98
99
100
101
102
103
104
105
106
107
108
109
import testAction from 'helpers/vuex_action_helper';
import mirror, { canConnect, SERVICE_NAME } from '~/ide/lib/mirror';
import * as actions from '~/ide/stores/modules/terminal_sync/actions';
import * as types from '~/ide/stores/modules/terminal_sync/mutation_types';

jest.mock('~/ide/lib/mirror');

const TEST_SESSION = {
  proxyWebsocketPath: 'test/path',
  services: [SERVICE_NAME],
};

describe('ide/stores/modules/terminal_sync/actions', () => {
  let rootState;

  beforeEach(() => {
    canConnect.mockReturnValue(true);
    rootState = {
      changedFiles: [],
      terminal: {},
    };
  });

  describe('upload', () => {
    it('uploads to mirror and sets success', async () => {
      mirror.upload.mockReturnValue(Promise.resolve());

      await testAction(
        actions.upload,
        null,
        rootState,
        [{ type: types.START_LOADING }, { type: types.SET_SUCCESS }],
        [],
      );
      expect(mirror.upload).toHaveBeenCalledWith(rootState);
    });

    it('sets error when failed', () => {
      const err = { message: 'it failed!' };
      mirror.upload.mockReturnValue(Promise.reject(err));

      return testAction(
        actions.upload,
        null,
        rootState,
        [{ type: types.START_LOADING }, { type: types.SET_ERROR, payload: err }],
        [],
      );
    });
  });

  describe('stop', () => {
    it('disconnects from mirror', async () => {
      await testAction(actions.stop, null, rootState, [{ type: types.STOP }], []);
      expect(mirror.disconnect).toHaveBeenCalled();
    });
  });

  describe('start', () => {
    it.each`
      session                                | canConnectMock | description
      ${null}                                | ${true}        | ${'does not exist'}
      ${{}}                                  | ${true}        | ${'does not have proxyWebsocketPath'}
      ${{ proxyWebsocketPath: 'test/path' }} | ${false}       | ${'can not connect service'}
    `('rejects if session $description', ({ session, canConnectMock }) => {
      canConnect.mockReturnValue(canConnectMock);

      const result = actions.start({ rootState: { terminal: { session } } });

      return expect(result).rejects.toBe(undefined);
    });

    describe('with terminal session in state', () => {
      beforeEach(() => {
        rootState = {
          terminal: { session: TEST_SESSION },
        };
      });

      it('connects to mirror and sets success', async () => {
        mirror.connect.mockReturnValue(Promise.resolve());

        await testAction(
          actions.start,
          null,
          rootState,
          [{ type: types.START_LOADING }, { type: types.SET_SUCCESS }],
          [],
        );
        expect(mirror.connect).toHaveBeenCalledWith(TEST_SESSION.proxyWebsocketPath);
      });

      it('sets error if connection fails', () => {
        const commit = jest.fn();
        const err = new Error('test');
        mirror.connect.mockReturnValue(Promise.reject(err));

        const result = actions.start({ rootState, commit });

        return Promise.all([
          expect(result).rejects.toEqual(err),
          result.catch(() => {
            expect(commit).toHaveBeenCalledWith(types.SET_ERROR, err);
          }),
        ]);
      });
    });
  });
});