import { CompassLink, CompassLinkType, CreateLinkInput, } from '@atlassian/forge-graphql-types'; import updateLinksSegment from './updateLinksSegment'; import { MOCK_COMPONENT_ID } from '../../__tests__/fixtures/mocks'; describe('updateLinksSegment', () => { beforeEach(() => { jest.resetAllMocks(); }); it('should not delete and update if old and new links are equal', async () => { const oldLinks: Array = [ { id: '1', name: 'One', type: CompassLinkType.Document, url: 'https://localhost:1', }, { id: '2', name: 'Two', type: CompassLinkType.Dashboard, url: 'https://localhost:2', }, ]; const newLinks: Array = [ { name: 'One', type: CompassLinkType.Document, url: 'https://localhost:1', }, { name: 'Two', type: CompassLinkType.Dashboard, url: 'https://localhost:2', }, ]; const gqlSegment = updateLinksSegment( MOCK_COMPONENT_ID, oldLinks, newLinks, ); expect(gqlSegment.mutation).toEqual(''); expect(gqlSegment.parameters).toHaveLength(0); expect(Object.keys(gqlSegment.variables)).toHaveLength(0); }); it('should not delete Document link even if it is eligible for deletion', async () => { const oldLinks: Array = [ { id: '1', name: 'One', type: CompassLinkType.Document, url: 'https://localhost:1', }, { id: '2', name: 'Two', type: CompassLinkType.Dashboard, url: 'https://localhost:2', }, ]; const newLinks: Array = [ { name: 'Two', type: CompassLinkType.Dashboard, url: 'https://localhost:2', }, ]; const gqlSegment = updateLinksSegment( MOCK_COMPONENT_ID, oldLinks, newLinks, ); expect(gqlSegment.mutation).toEqual(''); expect(gqlSegment.parameters).toHaveLength(0); expect(Object.keys(gqlSegment.variables)).toHaveLength(0); }); it('should delete old links and create new ones for non-document link types', async () => { const oldLinks: Array = [ { id: '1', name: 'One', type: CompassLinkType.Project, url: 'https://localhost:1', }, { id: '2', name: 'Two', type: CompassLinkType.Dashboard, url: 'https://localhost:2', }, ]; const newLinks: Array = [ { name: 'Two', type: CompassLinkType.Dashboard, url: 'https://localhost:2', }, { name: 'Three', type: CompassLinkType.OtherLink, url: 'https://localhost:3', }, ]; const gqlSegment = updateLinksSegment( MOCK_COMPONENT_ID, oldLinks, newLinks, ); expect(Object.values(gqlSegment.variables)).toHaveLength(2); expect(Object.values(gqlSegment.variables)[0].componentId).toEqual( MOCK_COMPONENT_ID, ); expect(Object.values(gqlSegment.variables)[0].link).toEqual('1'); // delete by ID expect(Object.values(gqlSegment.variables)[1].link).toEqual(newLinks[1]); }); });