import './helpers/mockRequestAtlassian'; import api from '../../index'; import { MOCK_COMPONENT_AGG, MOCK_ERROR_MESSAGE, MOCK_ERROR_TYPE, MOCK_MUTATION_ERROR, } from '../fixtures/mocks'; import { mockGetComponent, mockRequestGraph } from './helpers/requestMocks'; import { GRAPHQL_GATEWAY_SOURCE } from '../../helpers/constants'; import { fgqlActionSubjects, fgqlActions, fgqlSources, } from '../../helpers/analyticTypes'; const compassApp = api.compass.asApp(); const getComponentSpy = jest.spyOn(compassApp, 'getComponent'); // @ts-ignore const requestGraphSpy = jest.spyOn(compassApp.api, 'requestGraph'); describe('updateBaseComponent', () => { beforeEach(() => { jest.clearAllMocks(); }); test('transforms GqlErrors', async () => { mockGetComponent(getComponentSpy, MOCK_COMPONENT_AGG); mockRequestGraph(requestGraphSpy, [ { message: MOCK_ERROR_MESSAGE, extensions: { errorSource: GRAPHQL_GATEWAY_SOURCE, classification: MOCK_ERROR_TYPE, }, }, ]); const resp = await compassApp.updateBaseComponent(MOCK_COMPONENT_AGG); expect(resp).toMatchSnapshot(); }); test('transforms MutationErrors', async () => { mockGetComponent(getComponentSpy, MOCK_COMPONENT_AGG); mockRequestGraph(requestGraphSpy, [], { compass: { updateComponent: { componentDetails: null, errors: [MOCK_MUTATION_ERROR], }, }, } as any); const resp = await compassApp.updateBaseComponent(MOCK_COMPONENT_AGG); expect(resp).toMatchSnapshot(); }); test('successfully returns component', async () => { mockGetComponent(getComponentSpy, MOCK_COMPONENT_AGG); mockRequestGraph(requestGraphSpy, [], { compass: { updateComponent: { componentDetails: MOCK_COMPONENT_AGG, }, }, } as any); const resp = await compassApp.updateBaseComponent(MOCK_COMPONENT_AGG); expect(resp).toMatchSnapshot(); }); test('errors if response cannot be parsed', async () => { mockGetComponent(getComponentSpy, MOCK_COMPONENT_AGG); mockRequestGraph(requestGraphSpy, [], { invalid_response: 'hello world', } as any); const resp = await compassApp.updateBaseComponent(MOCK_COMPONENT_AGG); expect(resp).toMatchSnapshot(); }); test('successfully sends updatedFromFile analytics when options are provided', async () => { mockGetComponent(getComponentSpy, MOCK_COMPONENT_AGG); mockRequestGraph(requestGraphSpy, [], { compass: { updateComponent: { componentDetails: MOCK_COMPONENT_AGG, }, }, } as any); await compassApp.updateBaseComponent({ ...MOCK_COMPONENT_AGG, options: { updatedFromFile: { action: fgqlActions.updated, actionSubject: fgqlActionSubjects.component, source: fgqlSources.configAsCode, }, }, }); // Check that the analytics header was passed correctly expect(requestGraphSpy).toHaveBeenCalled(); const callArgs = requestGraphSpy.mock.calls[0]; const analyticsHeader = callArgs[3]; expect(analyticsHeader).toBeDefined(); expect(analyticsHeader.updatedFromFile).toBeDefined(); const parsedAnalytics = JSON.parse(analyticsHeader.updatedFromFile); expect(parsedAnalytics).toMatchObject({ action: fgqlActions.updated, actionSubject: fgqlActionSubjects.component, source: fgqlSources.configAsCode, }); }); });