diff options
Diffstat (limited to 'spec/frontend/ide/stores/modules/terminal_sync')
-rw-r--r-- | spec/frontend/ide/stores/modules/terminal_sync/actions_spec.js | 118 | ||||
-rw-r--r-- | spec/frontend/ide/stores/modules/terminal_sync/mutations_spec.js | 89 |
2 files changed, 207 insertions, 0 deletions
diff --git a/spec/frontend/ide/stores/modules/terminal_sync/actions_spec.js b/spec/frontend/ide/stores/modules/terminal_sync/actions_spec.js new file mode 100644 index 00000000000..ac976300ed0 --- /dev/null +++ b/spec/frontend/ide/stores/modules/terminal_sync/actions_spec.js @@ -0,0 +1,118 @@ +import * as actions from '~/ide/stores/modules/terminal_sync/actions'; +import mirror, { canConnect, SERVICE_NAME } from '~/ide/lib/mirror'; +import * as types from '~/ide/stores/modules/terminal_sync/mutation_types'; +import testAction from 'helpers/vuex_action_helper'; + +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', done => { + mirror.upload.mockReturnValue(Promise.resolve()); + + testAction( + actions.upload, + null, + rootState, + [{ type: types.START_LOADING }, { type: types.SET_SUCCESS }], + [], + () => { + expect(mirror.upload).toHaveBeenCalledWith(rootState); + done(); + }, + ); + }); + + it('sets error when failed', done => { + const err = { message: 'it failed!' }; + mirror.upload.mockReturnValue(Promise.reject(err)); + + testAction( + actions.upload, + null, + rootState, + [{ type: types.START_LOADING }, { type: types.SET_ERROR, payload: err }], + [], + done, + ); + }); + }); + + describe('stop', () => { + it('disconnects from mirror', done => { + testAction(actions.stop, null, rootState, [{ type: types.STOP }], [], () => { + expect(mirror.disconnect).toHaveBeenCalled(); + done(); + }); + }); + }); + + 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', done => { + mirror.connect.mockReturnValue(Promise.resolve()); + + testAction( + actions.start, + null, + rootState, + [{ type: types.START_LOADING }, { type: types.SET_SUCCESS }], + [], + () => { + expect(mirror.connect).toHaveBeenCalledWith(TEST_SESSION.proxyWebsocketPath); + done(); + }, + ); + }); + + 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); + }), + ]); + }); + }); + }); +}); diff --git a/spec/frontend/ide/stores/modules/terminal_sync/mutations_spec.js b/spec/frontend/ide/stores/modules/terminal_sync/mutations_spec.js new file mode 100644 index 00000000000..ecf35d60e96 --- /dev/null +++ b/spec/frontend/ide/stores/modules/terminal_sync/mutations_spec.js @@ -0,0 +1,89 @@ +import createState from '~/ide/stores/modules/terminal_sync/state'; +import * as types from '~/ide/stores/modules/terminal_sync/mutation_types'; +import mutations from '~/ide/stores/modules/terminal_sync/mutations'; + +const TEST_MESSAGE = 'lorem ipsum dolar sit'; + +describe('ide/stores/modules/terminal_sync/mutations', () => { + let state; + + beforeEach(() => { + state = createState(); + }); + + describe(types.START_LOADING, () => { + it('sets isLoading and resets error', () => { + Object.assign(state, { + isLoading: false, + isError: true, + }); + + mutations[types.START_LOADING](state); + + expect(state).toEqual( + expect.objectContaining({ + isLoading: true, + isError: false, + }), + ); + }); + }); + + describe(types.SET_ERROR, () => { + it('sets isLoading and error message', () => { + Object.assign(state, { + isLoading: true, + isError: false, + message: '', + }); + + mutations[types.SET_ERROR](state, { message: TEST_MESSAGE }); + + expect(state).toEqual( + expect.objectContaining({ + isLoading: false, + isError: true, + message: TEST_MESSAGE, + }), + ); + }); + }); + + describe(types.SET_SUCCESS, () => { + it('sets isLoading and resets error and is started', () => { + Object.assign(state, { + isLoading: true, + isError: true, + isStarted: false, + }); + + mutations[types.SET_SUCCESS](state); + + expect(state).toEqual( + expect.objectContaining({ + isLoading: false, + isError: false, + isStarted: true, + }), + ); + }); + }); + + describe(types.STOP, () => { + it('sets stop values', () => { + Object.assign(state, { + isLoading: true, + isStarted: true, + }); + + mutations[types.STOP](state); + + expect(state).toEqual( + expect.objectContaining({ + isLoading: false, + isStarted: false, + }), + ); + }); + }); +}); |