import './helpers/mockRequestAtlassian'; import { CompassRelationshipType } from '@atlassian/forge-graphql-types'; import api from '../../index'; import { MOCK_COMPONENT_ID, MOCK_COMPONENT_ID_2, MOCK_ERROR_MESSAGE, MOCK_ERROR_TYPE, MOCK_MUTATION_ERROR, MOCK_RELATIONSHIP, } from '../fixtures/mocks'; import { mockRequestGraph } from './helpers/requestMocks'; import { GRAPHQL_GATEWAY_SOURCE } from '../../helpers/constants'; const compassApp = api.compass.asApp(); // @ts-ignore const requestGraphSpy = jest.spyOn(compassApp.api, 'requestGraph'); const REQUEST_ARG = { endNodeId: MOCK_COMPONENT_ID, startNodeId: MOCK_COMPONENT_ID_2, type: CompassRelationshipType.DependsOn, }; describe('createRelationship', () => { beforeEach(() => { jest.clearAllMocks(); }); test('transforms GqlErrors', async () => { mockRequestGraph(requestGraphSpy, [ { message: MOCK_ERROR_MESSAGE, extensions: { errorSource: GRAPHQL_GATEWAY_SOURCE, classification: MOCK_ERROR_TYPE, }, }, ]); const resp = await compassApp.createRelationship(REQUEST_ARG); expect(resp).toMatchSnapshot(); }); test('transforms MutationErrors', async () => { mockRequestGraph(requestGraphSpy, [], { compass: { createRelationship: { createdCompassRelationship: null, errors: [MOCK_MUTATION_ERROR], }, }, } as any); const resp = await compassApp.createRelationship(REQUEST_ARG); expect(resp).toMatchSnapshot(); }); test('successfully returns created relationship', async () => { mockRequestGraph(requestGraphSpy, [], { compass: { createRelationship: { createdCompassRelationship: MOCK_RELATIONSHIP, }, }, } as any); const resp = await compassApp.createRelationship(REQUEST_ARG); expect(resp).toMatchSnapshot(); }); test('errors if response cannot be parsed', async () => { mockRequestGraph(requestGraphSpy, [], { invalid_response: 'hello world', } as any); const resp = await compassApp.createRelationship(REQUEST_ARG); expect(resp).toMatchSnapshot(); }); });