import 'jest' jest.mock('../amioApiFactory') jest.mock('../amioService') import { createChannel, getChannels, updateChannel, deleteChannel } from '../amioChannels' import { createAmioApi } from '../amioApiFactory' import { handleAmioError } from '../amioService' function setAmioApi(createAmioApi: any, amioApi: any) { createAmioApi.mockImplementation(() => amioApi) } function setHandleAmiError(handleAmioError: any) { handleAmioError.mockClear() } function createMockAmioApi() { return { channels: { create: jest.fn(() => 'CREATED_CHANNEL'), list: jest.fn(() => { return { items: 'LISTED_CHANNELS' } }), update: jest.fn(() => 'UPDATED_CHANNEL'), delete: jest.fn(() => 'CHANNEL_DELETED') } } } function createMockErrorAmioApi() { return { channels: { create: jest.fn(() => { throw new Error('CREATE_ERROR') }), list: jest.fn(() => { throw new Error('LIST_ERROR') }), update: jest.fn(() => { throw new Error('UPDATE_ERROR') }), delete: jest.fn(() => { throw new Error('DELETE_ERROR') }) } } } describe('amioChannels', () => { beforeEach(() => { setHandleAmiError(handleAmioError) }) describe('createChannel', () => { const BOT_ID = 'BOT_ID' const NEW_CHANNEL = { type: 'TEST_TYPE', name: BOT_ID } it('calls create on amioApi with channel data', async () => { const amioApi = createMockAmioApi() setAmioApi(createAmioApi, amioApi) const actual = await createChannel(NEW_CHANNEL, BOT_ID) expect(actual).toEqual('CREATED_CHANNEL') expect(amioApi.channels.create).toBeCalledWith(NEW_CHANNEL) }) it('calls handleAmioError on error', async () => { const amioApi = createMockErrorAmioApi() setAmioApi(createAmioApi, amioApi) await createChannel(NEW_CHANNEL, BOT_ID) expect(handleAmioError).toBeCalledTimes(1) expect(handleAmioError).toBeCalledWith(new Error('CREATE_ERROR')) }) }) describe('getChannels', () => { it('calls list on amioApi', async () => { const amioApi = createMockAmioApi() setAmioApi(createAmioApi, amioApi) const actual = await getChannels() expect(actual).toEqual('LISTED_CHANNELS') expect(amioApi.channels.list).toBeCalled() }) it('calls handleAmioError on error', async () => { const amioApi = createMockErrorAmioApi() setAmioApi(createAmioApi, amioApi) await getChannels() expect(handleAmioError).toBeCalledTimes(1) expect(handleAmioError).toBeCalledWith(new Error('LIST_ERROR')) }) }) describe('updateChannel', () => { const UPDATED_CHANNEL = { name: 'UPDATED_CHANNEL' } const CHANNEL_ID = 'CHANNEL_ID' it('calls update on amioApi', async () => { const amioApi = createMockAmioApi() setAmioApi(createAmioApi, amioApi) const actual = await updateChannel(CHANNEL_ID, UPDATED_CHANNEL) expect(actual).toEqual('UPDATED_CHANNEL') expect(amioApi.channels.update).toBeCalledWith(CHANNEL_ID, UPDATED_CHANNEL) }) it('calls handleAmioError on error', async () => { const amioApi = createMockErrorAmioApi() setAmioApi(createAmioApi, amioApi) await updateChannel(CHANNEL_ID, UPDATED_CHANNEL) expect(handleAmioError).toBeCalledTimes(1) expect(handleAmioError).toBeCalledWith(new Error('UPDATE_ERROR')) }) }) describe('deleteChannel', () => { const CHANNEL_ID = 'CHANNEL_ID' it('calls delete on amioApi', async () => { const amioApi = createMockAmioApi() setAmioApi(createAmioApi, amioApi) const actual = await deleteChannel(CHANNEL_ID) expect(actual).toEqual('CHANNEL_DELETED') expect(amioApi.channels.delete).toBeCalledWith(CHANNEL_ID) }) it('calls handleAmioError on error', async () => { const amioApi = createMockErrorAmioApi() setAmioApi(createAmioApi, amioApi) await deleteChannel(CHANNEL_ID) expect(handleAmioError).toBeCalledTimes(1) expect(handleAmioError).toBeCalledWith(new Error('DELETE_ERROR')) }) }) })