import { Component, ComponentByExternalAliasQuery, ComponentPayload, EventSourcePayload, GqlError, Mutation, Query, QueryError, SdkError, } from '@atlassian/forge-graphql-types'; import { MOCK_BASE_COMPONENT_WITH_ID, MOCK_COMPONENT_ID, MOCK_EVENT_SOURCE_ID, } from '../../fixtures/mocks'; import { COMPONENT_NOT_FOUND } from '../../../helpers'; // TODO: Replace mockImplementationOnce with mockResolvedValueOnce function mockCreateEventSource( spy: jest.SpyInstance, eventSourceId: string = MOCK_EVENT_SOURCE_ID, errors: Array = undefined, ): void { let data = null as EventSourcePayload; if (!errors) { data = { eventSource: { id: eventSourceId, }, } as EventSourcePayload; } spy.mockImplementationOnce(async () => ({ errors: errors || [], data, })); } function mockAttachEventSource( spy: jest.SpyInstance, errors?: Array, ): void { spy.mockImplementationOnce(async () => ({ errors: errors || [], data: null, })); } function mockCreateBaseComponent( spy: jest.SpyInstance, errors?: Array, ): void { let data = null as ComponentPayload; if (!errors) { data = { component: { id: MOCK_COMPONENT_ID, }, } as ComponentPayload; } spy.mockImplementationOnce(async () => ({ errors: errors || [], data, })); } function mockUpdateDataManager( spy: jest.SpyInstance, errors?: Array, ): void { spy.mockImplementationOnce(async () => ({ errors: errors || [], data: null, })); } function mockGetComponent( spy: jest.SpyInstance, component: Component = MOCK_BASE_COMPONENT_WITH_ID, success = true, errors: Array = undefined, ): void { spy.mockImplementationOnce(async () => ({ errors: errors || [], success, data: !errors ? { component } : null, })); } function mockCreateExternalAlias( spy: jest.SpyInstance, errors?: Array, ): void { spy.mockImplementationOnce(async () => ({ errors: errors || [], })); } function mockDeleteComponent( spy: jest.SpyInstance, errors?: Array, ): void { spy.mockImplementationOnce(async () => ({ errors: errors || [], data: null, })); } function mockGetComponentByExternalAlias( spy: jest.SpyInstance, found = true, component: Component = MOCK_BASE_COMPONENT_WITH_ID, errors: Array = [], ): void { let data = null as ComponentByExternalAliasQuery; if (!found) { errors.push({ message: COMPONENT_NOT_FOUND }); } if (errors.length === 0) { data = { component: found ? component : null, } as ComponentByExternalAliasQuery; } spy.mockImplementationOnce(async () => ({ errors, data, })); } function mockRequestGraph( spy: jest.SpyInstance, errors?: Array, data?: Query | Mutation, ): void { spy.mockImplementationOnce(async () => ({ json: () => ({ errors, data, }), })); } export { mockGetComponent, mockCreateBaseComponent, mockCreateEventSource, mockAttachEventSource, mockCreateExternalAlias, mockDeleteComponent, mockRequestGraph, mockGetComponentByExternalAlias, mockUpdateDataManager, };