import './helpers/mockRequestAtlassian'; import api from '../../index'; import { MOCK_BASE_COMPONENT, MOCK_BASE_COMPONENT_WITH_ID, MOCK_CLOUD_ID, MOCK_EXTERNAL_ALIAS, } from '../fixtures/mocks'; import { mockCreateBaseComponent, mockCreateExternalAlias, mockGetComponent, mockGetComponentByExternalAlias, mockRequestGraph, } from './helpers/requestMocks'; import { findCall } from './helpers/matchGql'; import { COMPOUND_MUTATION_NAME, FORGE_GRAPHQL_SDK_ERROR_SOURCE, INTERNAL_SERVER_ERROR_TYPE, NOT_FOUND_ERROR_TYPE, } from '../../helpers'; const compassApp = api.compass.asApp(); const getComponentByExternalAliasSpy = jest.spyOn( compassApp, 'getComponentByExternalAlias', ); const createBaseComponentSpy = jest.spyOn(compassApp, 'createBaseComponent'); const createExternalAliasSpy = jest.spyOn(compassApp, 'createExternalAlias'); const getComponentSpy = jest.spyOn(compassApp, 'getComponent'); const updateComponentSpy = jest.spyOn(compassApp, 'updateComponent'); // @ts-ignore const requestGraphSpy = jest.spyOn(compassApp.api, 'requestGraph'); describe('syncComponentByExternalAlias', () => { beforeEach(() => { jest.clearAllMocks(); jest.spyOn(global.Math, 'random').mockReturnValue(0.0000000001); }); test('creates component if external alias does not exist', async () => { mockGetComponentByExternalAlias(getComponentByExternalAliasSpy, false); mockCreateBaseComponent(createBaseComponentSpy); mockCreateExternalAlias(createExternalAliasSpy); mockGetComponent(getComponentSpy, MOCK_BASE_COMPONENT_WITH_ID); const resp = await compassApp.syncComponentByExternalAlias({ cloudId: MOCK_CLOUD_ID, ...MOCK_BASE_COMPONENT, externalAlias: MOCK_EXTERNAL_ALIAS, options: { createComponentIfNotFound: true, }, }); expect(createExternalAliasSpy).toMatchSnapshot(); expect(updateComponentSpy).not.toHaveBeenCalled(); expect(resp).toEqual({ errors: [], success: true, data: { component: MOCK_BASE_COMPONENT_WITH_ID, }, }); }); test('returns error if createComponentIfNotFound is false and component does not exist', async () => { mockGetComponentByExternalAlias(getComponentByExternalAliasSpy, false); const resp = await compassApp.syncComponentByExternalAlias({ cloudId: MOCK_CLOUD_ID, ...MOCK_BASE_COMPONENT, externalAlias: MOCK_EXTERNAL_ALIAS, }); expect(createBaseComponentSpy).not.toHaveBeenCalled(); expect(resp).toEqual({ errors: [ { message: `Component with external alias id ${MOCK_EXTERNAL_ALIAS.externalId} could not be found`, errorSource: FORGE_GRAPHQL_SDK_ERROR_SOURCE, errorType: NOT_FOUND_ERROR_TYPE, statusCode: 404, }, ], success: false, data: { component: {} }, }); }); test('updates component if external alias is found', async () => { mockGetComponentByExternalAlias( getComponentByExternalAliasSpy, true, MOCK_BASE_COMPONENT_WITH_ID, ); mockGetComponent(getComponentSpy, MOCK_BASE_COMPONENT_WITH_ID); await compassApp.syncComponentByExternalAlias({ cloudId: MOCK_CLOUD_ID, ...MOCK_BASE_COMPONENT, externalAlias: MOCK_EXTERNAL_ALIAS, }); expect(createBaseComponentSpy).not.toHaveBeenCalled(); expect(updateComponentSpy).toHaveBeenCalled(); expect(findCall(requestGraphSpy, COMPOUND_MUTATION_NAME)).toMatchSnapshot(); }); test('returns error if there is an error when looking up external alias', async () => { const invalidAriError = 'ARI is not valid'; mockGetComponentByExternalAlias( getComponentByExternalAliasSpy, true, MOCK_BASE_COMPONENT_WITH_ID, [{ message: invalidAriError }], ); const resp = await compassApp.syncComponentByExternalAlias({ cloudId: MOCK_CLOUD_ID, ...MOCK_BASE_COMPONENT, externalAlias: MOCK_EXTERNAL_ALIAS, }); expect(createBaseComponentSpy).not.toHaveBeenCalled(); expect(updateComponentSpy).not.toHaveBeenCalled(); expect(resp).toEqual({ errors: [{ message: invalidAriError }], success: false, data: { component: {} }, }); }); test('returns error if there is an error when creating component', async () => { const createComponentError = 'Could not create component'; mockGetComponentByExternalAlias(getComponentByExternalAliasSpy, false); mockCreateBaseComponent(createBaseComponentSpy, [ { message: createComponentError }, ]); const resp = await compassApp.syncComponentByExternalAlias({ cloudId: MOCK_CLOUD_ID, ...MOCK_BASE_COMPONENT, externalAlias: MOCK_EXTERNAL_ALIAS, options: { createComponentIfNotFound: true, }, }); expect(createBaseComponentSpy).toMatchSnapshot(); expect(updateComponentSpy).not.toHaveBeenCalled(); expect(getComponentSpy).not.toHaveBeenCalled(); expect(resp).toEqual({ errors: [{ message: createComponentError }], success: false, data: { component: {} }, }); }); test('returns error if there is an error when updating component', async () => { const updateComponentError = 'Could not update component'; mockGetComponentByExternalAlias(getComponentByExternalAliasSpy); mockRequestGraph(requestGraphSpy, [{ message: updateComponentError }]); mockGetComponent(getComponentSpy, MOCK_BASE_COMPONENT_WITH_ID); const resp = await compassApp.syncComponentByExternalAlias({ cloudId: MOCK_CLOUD_ID, ...MOCK_BASE_COMPONENT, externalAlias: MOCK_EXTERNAL_ALIAS, }); expect(findCall(requestGraphSpy, COMPOUND_MUTATION_NAME)).toMatchSnapshot(); expect(createBaseComponentSpy).not.toHaveBeenCalled(); expect(resp).toEqual({ errors: [ { message: updateComponentError, errorSource: FORGE_GRAPHQL_SDK_ERROR_SOURCE, errorType: INTERNAL_SERVER_ERROR_TYPE, }, ], success: false, data: { component: MOCK_BASE_COMPONENT_WITH_ID }, }); }); });