import './helpers/mockRequestAtlassian'; import { EventSource } from '@atlassian/forge-graphql-types'; import api from '../../index'; import { MOCK_BASE_COMPONENT_WITH_ID, MOCK_COMPONENT_ID, MOCK_EVENT_SOURCE_ID, MOCK_EVENT_SOURCE_ID_2, MOCK_EVENT_SOURCE_INPUT, MOCK_EXTERNAL_EVENT_SOURCE_ID_2, MOCK_GET_COMPONENT_WITH_EVENT_SOURCE, MOCK_GET_COMPONENT_WITH_EVENT_SOURCES, MOCK_GET_EVENT_SOURCE, MOCK_GQL_ERROR, MOCK_MUTATION_ERROR, } from '../fixtures/mocks'; import { mockAttachEventSource, mockCreateEventSource, mockGetComponent, mockRequestGraph, } from './helpers/requestMocks'; import { DETACH_EVENT_SOURCES_MUTATION_NAME } from '../../helpers'; import { findCall } from './helpers/matchGql'; const compassApp = api.compass.asApp(); const createEventSourceSpy = jest.spyOn(compassApp, 'createEventSource'); const attachEventSourceSpy = jest.spyOn(compassApp, 'attachEventSource'); const getComponentSpy = jest.spyOn(compassApp, 'getComponent'); // @ts-ignore const requestGraphSpy = jest.spyOn(compassApp.api, 'requestGraph'); describe('updateEventSources', () => { beforeEach(() => { jest.clearAllMocks(); jest.spyOn(global.Math, 'random').mockReturnValue(0.0000000001); }); test('successfully adds one source', async () => { mockCreateEventSource(createEventSourceSpy); mockAttachEventSource(attachEventSourceSpy); mockGetComponent(getComponentSpy, MOCK_GET_COMPONENT_WITH_EVENT_SOURCE); const eventSourceResp = await compassApp.updateEventSources({ componentId: MOCK_COMPONENT_ID, oldEventSources: [], newEventSources: [MOCK_EVENT_SOURCE_INPUT()], }); expect(createEventSourceSpy).toMatchSnapshot(); expect(attachEventSourceSpy).toMatchSnapshot(); expect(eventSourceResp).toEqual({ errors: [], success: true, data: { eventSources: [MOCK_GET_EVENT_SOURCE()] }, }); }); test('successfully adds multiple sources', async () => { const eventSources = [ MOCK_GET_EVENT_SOURCE(), MOCK_GET_EVENT_SOURCE(MOCK_EXTERNAL_EVENT_SOURCE_ID_2), ]; const eventSourcesInput = [ MOCK_EVENT_SOURCE_INPUT(), MOCK_EVENT_SOURCE_INPUT(MOCK_EXTERNAL_EVENT_SOURCE_ID_2), ]; mockCreateEventSource(createEventSourceSpy); mockAttachEventSource(attachEventSourceSpy); mockCreateEventSource(createEventSourceSpy, MOCK_EVENT_SOURCE_ID_2); mockAttachEventSource(attachEventSourceSpy); mockGetComponent(getComponentSpy, { ...MOCK_BASE_COMPONENT_WITH_ID, eventSources, }); const eventSourceResp = await compassApp.updateEventSources({ componentId: MOCK_COMPONENT_ID, oldEventSources: [], newEventSources: eventSourcesInput, }); expect(createEventSourceSpy).toMatchSnapshot(); expect(attachEventSourceSpy).toMatchSnapshot(); expect(eventSourceResp).toEqual({ errors: [], success: true, data: { eventSources }, }); }); test('successfully detaches sources', async () => { mockGetComponent(getComponentSpy, MOCK_GET_COMPONENT_WITH_EVENT_SOURCE); mockRequestGraph(requestGraphSpy); const eventSourceResp = await compassApp.updateEventSources({ componentId: MOCK_COMPONENT_ID, oldEventSources: [ { ...MOCK_EVENT_SOURCE_INPUT(), id: MOCK_EVENT_SOURCE_ID }, ], newEventSources: [], }); expect( findCall(requestGraphSpy, DETACH_EVENT_SOURCES_MUTATION_NAME), ).toMatchSnapshot(); expect(eventSourceResp).toEqual({ errors: [], success: true, data: { eventSources: MOCK_GET_COMPONENT_WITH_EVENT_SOURCE.eventSources, }, }); }); test('successfully detaches multiple sources', async () => { const eventSources: EventSource[] = [ { ...MOCK_GET_EVENT_SOURCE(), id: MOCK_EVENT_SOURCE_ID }, { ...MOCK_GET_EVENT_SOURCE(MOCK_EXTERNAL_EVENT_SOURCE_ID_2), id: MOCK_EVENT_SOURCE_ID_2, }, ]; mockGetComponent(getComponentSpy, MOCK_GET_COMPONENT_WITH_EVENT_SOURCES); mockRequestGraph(requestGraphSpy); const eventSourceResp = await compassApp.updateEventSources({ componentId: MOCK_COMPONENT_ID, oldEventSources: eventSources, newEventSources: [], }); expect( findCall(requestGraphSpy, DETACH_EVENT_SOURCES_MUTATION_NAME), ).toMatchSnapshot(); expect(eventSourceResp).toEqual({ errors: [], success: true, data: { eventSources }, }); }); test('successfully adds a source and detaches a source', async () => { mockCreateEventSource(createEventSourceSpy); mockAttachEventSource(attachEventSourceSpy); mockGetComponent(getComponentSpy, MOCK_GET_COMPONENT_WITH_EVENT_SOURCE); mockRequestGraph(requestGraphSpy); const eventSourceResp = await compassApp.updateEventSources({ componentId: MOCK_COMPONENT_ID, oldEventSources: [ { ...MOCK_GET_EVENT_SOURCE(MOCK_EXTERNAL_EVENT_SOURCE_ID_2), id: MOCK_EVENT_SOURCE_ID_2, }, ], newEventSources: [MOCK_EVENT_SOURCE_INPUT()], }); expect(createEventSourceSpy).toMatchSnapshot(); expect(attachEventSourceSpy).toMatchSnapshot(); expect( findCall(requestGraphSpy, DETACH_EVENT_SOURCES_MUTATION_NAME), ).toMatchSnapshot(); expect(eventSourceResp).toEqual({ errors: [], success: true, data: { eventSources: [MOCK_GET_EVENT_SOURCE()], }, }); }); test('handles error when adding event source', async () => { mockCreateEventSource(createEventSourceSpy, MOCK_EVENT_SOURCE_ID, [ MOCK_GQL_ERROR, ]); mockGetComponent(getComponentSpy, MOCK_BASE_COMPONENT_WITH_ID); const eventSourceResp = await compassApp.updateEventSources({ componentId: MOCK_COMPONENT_ID, oldEventSources: [], newEventSources: [MOCK_EVENT_SOURCE_INPUT()], }); expect(attachEventSourceSpy).not.toHaveBeenCalled(); expect(eventSourceResp).toMatchSnapshot(); }); test('handles gql error when detaching event source', async () => { mockRequestGraph(requestGraphSpy, [MOCK_GQL_ERROR]); mockGetComponent(getComponentSpy, MOCK_GET_COMPONENT_WITH_EVENT_SOURCE); const eventSourceResp = await compassApp.updateEventSources({ componentId: MOCK_COMPONENT_ID, oldEventSources: [ { ...MOCK_EVENT_SOURCE_INPUT(), id: MOCK_EVENT_SOURCE_ID }, ], newEventSources: [], }); expect(eventSourceResp).toMatchSnapshot(); }); test('handles mutation error when detaching event source', async () => { mockRequestGraph(requestGraphSpy, [], { compass: { detachEventSource_1: { errors: [MOCK_MUTATION_ERROR], }, }, } as any); mockGetComponent(getComponentSpy, MOCK_GET_COMPONENT_WITH_EVENT_SOURCE); const eventSourceResp = await compassApp.updateEventSources({ componentId: MOCK_COMPONENT_ID, oldEventSources: [ { ...MOCK_GET_EVENT_SOURCE(), id: MOCK_EVENT_SOURCE_ID }, ], newEventSources: [], }); expect(eventSourceResp).toMatchSnapshot(); }); test('handles errors when adding and detaching event source', async () => { mockCreateEventSource(createEventSourceSpy, MOCK_EVENT_SOURCE_ID, [ MOCK_GQL_ERROR, ]); mockRequestGraph(requestGraphSpy, [MOCK_GQL_ERROR]); mockGetComponent(getComponentSpy, MOCK_GET_COMPONENT_WITH_EVENT_SOURCE); const eventSourceResp = await compassApp.updateEventSources({ componentId: MOCK_COMPONENT_ID, oldEventSources: [ { ...MOCK_GET_EVENT_SOURCE(MOCK_EXTERNAL_EVENT_SOURCE_ID_2), id: MOCK_EVENT_SOURCE_ID_2, }, ], newEventSources: [MOCK_EVENT_SOURCE_INPUT()], }); expect(eventSourceResp).toMatchSnapshot(); }); });