import './helpers/mockRequestAtlassian'; import { CompassComponentType } from '@atlassian/forge-graphql-types'; import api from '../../index'; import { mockRequestGraph } from './helpers/requestMocks'; import { MOCK_CLOUD_ID, MOCK_ERROR_MESSAGE, MOCK_ERROR_TYPE, MOCK_MUTATION_ERROR, } from '../fixtures/mocks'; import { GRAPHQL_GATEWAY_SOURCE } from '../../helpers/constants'; const compassApp = api.compass.asApp(); // @ts-ignore const requestGraphSpy = jest.spyOn(compassApp.api, 'requestGraph'); describe('createCustomFieldDefinition', () => { beforeEach(() => { jest.clearAllMocks(); }); it('creates a customFieldDefinition successfully', async () => { mockRequestGraph(requestGraphSpy, [], { compass: { createCustomFieldDefinition: { success: true, errors: null, customFieldDefinition: { id: 'test-id', name: 'platform', description: '', componentTypes: [CompassComponentType.Service], }, }, }, } as any); const resp = await compassApp.createCustomFieldDefinition({ textFieldDefinition: { cloudId: MOCK_CLOUD_ID, name: 'platform', description: '', componentTypes: [CompassComponentType.Service], }, }); expect(resp.success).toBeTruthy(); expect(resp).toMatchInlineSnapshot(` Object { "data": Object { "customFieldDefinition": Object { "componentTypes": Array [ "SERVICE", ], "description": "", "id": "test-id", "name": "platform", }, }, "errors": Array [], "success": true, } `); }); it('returns error if there was an error executing the graphql request', async () => { requestGraphSpy.mockImplementationOnce(() => { throw new Error('Something went wrong'); }); const resp = await compassApp.createCustomFieldDefinition({ textFieldDefinition: { cloudId: MOCK_CLOUD_ID, name: 'platform', description: '', componentTypes: [CompassComponentType.Service], }, }); expect(resp.success).toBeFalsy(); expect(resp).toMatchInlineSnapshot(` Object { "data": Object { "customFieldDefinition": null, }, "errors": Array [ Object { "errorSource": "FORGE_GRAPHQL", "errorType": "Error", "message": "Something went wrong", "statusCode": 500, }, ], "success": false, } `); }); it('handles the graphql error returned by server', async () => { mockRequestGraph(requestGraphSpy, [ { message: MOCK_ERROR_MESSAGE, extensions: { errorSource: GRAPHQL_GATEWAY_SOURCE, classification: MOCK_ERROR_TYPE, }, }, ]); const resp = await compassApp.createCustomFieldDefinition({ textFieldDefinition: { cloudId: MOCK_CLOUD_ID, name: 'platform', description: '', componentTypes: [CompassComponentType.Service], }, }); expect(resp).toMatchInlineSnapshot(` Object { "data": Object { "customFieldDefinition": null, }, "errors": Array [ Object { "errorSource": "GRAPHQL_GATEWAY", "errorType": "MockErrorType", "message": "Mock error message", }, ], "success": false, } `); }); it('handles the graphql mutation error returned by server', async () => { mockRequestGraph(requestGraphSpy, [], { compass: { createCustomFieldDefinition: { success: false, errors: [MOCK_MUTATION_ERROR], customFieldDefinition: null, }, }, } as any); const resp = await compassApp.createCustomFieldDefinition({ textFieldDefinition: { cloudId: MOCK_CLOUD_ID, name: 'platform', description: '', componentTypes: [CompassComponentType.Service], }, }); expect(resp).toMatchInlineSnapshot(` Object { "data": Object { "customFieldDefinition": null, }, "errors": Array [ Object { "errorSource": "GRAPHQL_GATEWAY", "errorType": "MockErrorType", "message": "Mock mutation error message", "statusCode": 501, }, ], "success": false, } `); }); });