diff --git a/packages/ketcher-core/__tests__/application/editor/actions/bond.test.ts b/packages/ketcher-core/__tests__/application/editor/actions/bond.test.ts index 64ba066fed..b5b1382698 100644 --- a/packages/ketcher-core/__tests__/application/editor/actions/bond.test.ts +++ b/packages/ketcher-core/__tests__/application/editor/actions/bond.test.ts @@ -1,6 +1,6 @@ import * as utils from 'application/editor/actions/utils'; -import { restruct, singleBond } from './data'; +import { restruct, singleBond } from '../../../mock-data'; import { fromBondAddition } from 'application/editor/actions'; // eslint-disable-next-line @typescript-eslint/no-explicit-any diff --git a/packages/ketcher-core/__tests__/application/render/restruct/rergroup.test.ts b/packages/ketcher-core/__tests__/application/render/restruct/rergroup.test.ts new file mode 100644 index 0000000000..eeb87983b0 --- /dev/null +++ b/packages/ketcher-core/__tests__/application/render/restruct/rergroup.test.ts @@ -0,0 +1,19 @@ +import { ReRGroup, ReStruct } from 'application/render/restruct'; +import { restruct } from '../../../mock-data'; +import { RGroup } from 'domain/entities'; +import { mock, mockFn } from 'jest-mock-extended'; +import { Render } from 'src'; + +describe('rergroup should calculate R-Group bounding box correctly', () => { + it('should calculate R-Group attachments points bounding box', () => { + const render = mock(); + render.ctab = { ...restruct.molecule } as unknown as ReStruct; + const rGroup = new RGroup(); + rGroup.frags.add(0); + const rerGroup = new ReRGroup(rGroup); + rerGroup.getAtoms = mockFn().mockReturnValue(restruct.molecule.atoms); + const attachmentsSpy = jest.spyOn(render.ctab, 'getAttachmentsPointsVBox'); + rerGroup.calcBBox(render); + expect(attachmentsSpy).toHaveBeenCalled(); + }); +}); diff --git a/packages/ketcher-core/__tests__/domain/entities/sgroup.test.ts b/packages/ketcher-core/__tests__/domain/entities/sgroup.test.ts new file mode 100644 index 0000000000..e03f222415 --- /dev/null +++ b/packages/ketcher-core/__tests__/domain/entities/sgroup.test.ts @@ -0,0 +1,17 @@ +import { ReStruct } from 'application/render/restruct'; +import { restruct } from '../../mock-data'; +import { SGroup } from 'domain/entities'; +import { mock } from 'jest-mock-extended'; +import { Render } from 'src'; + +describe('sgroup should calculate S-Group bounding box correctly', () => { + it('should calculate S-Group attachments points bounding box', () => { + const render = mock(); + render.ctab = { ...restruct.molecule } as unknown as ReStruct; + const sGroup = new SGroup('MUL'); + sGroup.atoms = [0, 1, 2, 3, 4]; + const attachmentsSpy = jest.spyOn(render.ctab, 'getAttachmentsPointsVBox'); + SGroup.bracketPos(sGroup, restruct.molecule, {}, undefined, render); + expect(attachmentsSpy).toHaveBeenCalled(); + }); +}); diff --git a/packages/ketcher-core/__tests__/application/editor/actions/data.ts b/packages/ketcher-core/__tests__/mock-data.ts similarity index 96% rename from packages/ketcher-core/__tests__/application/editor/actions/data.ts rename to packages/ketcher-core/__tests__/mock-data.ts index e1e21a9cd2..0efa5b5e62 100644 --- a/packages/ketcher-core/__tests__/application/editor/actions/data.ts +++ b/packages/ketcher-core/__tests__/mock-data.ts @@ -1,7 +1,8 @@ /* eslint-disable @typescript-eslint/no-empty-function */ import { ReAtom, ReBond } from 'application/render'; -import { Pool } from 'domain/entities'; +import { Box2Abs, Pool, Vec2 } from 'domain/entities'; +import { mockFn } from 'jest-mock-extended'; const mockAtoms = [ { @@ -634,6 +635,18 @@ const mockFrags = [ stereoFlagPosition: undefined, enhancedStereoFlag: 'ABS', updateStereoFlag() {}, + calcBBox: mockFn().mockReturnValue({ + p0: { + x: 4, + y: 6, + z: 0, + }, + p1: { + x: 6, + y: 8, + z: 0, + }, + }), }, ]; const frags = new Map(); @@ -680,11 +693,26 @@ const molecule = { bondInitHalfBonds() {}, atomAddNeighbor() {}, setImplicitHydrogen() {}, + getAttachmentsPointsVBox: mockFn().mockReturnValue( + new Box2Abs( + new Vec2({ + x: 6, + y: 7, + z: 0, + }), + new Vec2({ + x: 7, + y: 9, + z: 0, + }), + ), + ), }; export const restruct = { atoms: new Map(), bonds: new Map(), + frags, molecule, connectedComponents: new Set(), render: { diff --git a/packages/ketcher-core/src/application/render/restruct/rergroup.js b/packages/ketcher-core/src/application/render/restruct/rergroup.js index 8b3f318c74..75a2a0805d 100644 --- a/packages/ketcher-core/src/application/render/restruct/rergroup.js +++ b/packages/ketcher-core/src/application/render/restruct/rergroup.js @@ -67,11 +67,6 @@ class ReRGroup extends ReObject { : fragBox; } }); - - rGroupBoundingBox = rGroupBoundingBox - ? rGroupBoundingBox.extend(BORDER_EXT, BORDER_EXT) - : rGroupBoundingBox; - let attachmentPointsVBox = render.ctab.getAttachmentsPointsVBox( this.getAtoms(render), ); diff --git a/packages/ketcher-core/src/domain/entities/sgroup.ts b/packages/ketcher-core/src/domain/entities/sgroup.ts index e06312b8ff..b7d5aeec76 100644 --- a/packages/ketcher-core/src/domain/entities/sgroup.ts +++ b/packages/ketcher-core/src/domain/entities/sgroup.ts @@ -459,6 +459,8 @@ export class SGroup { remol?: ReStruct, render?, ): void { + const BORDER_EXT = new Vec2(0.05 * 3, 0.05 * 3); + const PADDING_VECTOR = new Vec2(0.2, 0.4); const atoms = sGroup.atoms; const crossBonds = crossBondsPerAtom ? Object.values(crossBondsPerAtom).flat() @@ -482,7 +484,6 @@ export class SGroup { }; atoms.forEach((aid) => { const atom = getAtom(aid); - const ext = new Vec2(0.05 * 3, 0.05 * 3); let position; let structBoundingBox; if ('getVBoxObj' in atom && render) { @@ -491,7 +492,7 @@ export class SGroup { position = new Vec2(atom.pp); structBoundingBox = new Box2Abs(position, position); } - contentBoxes.push(structBoundingBox.extend(ext, ext)); + contentBoxes.push(structBoundingBox.extend(BORDER_EXT, BORDER_EXT)); }); contentBoxes.forEach((bba) => { let bbb: Box2Abs | null = null; @@ -504,8 +505,18 @@ export class SGroup { }); braketBox = !braketBox ? bbb : Box2Abs.union(braketBox, bbb!); }); - const vext = new Vec2(0.2, 0.4); - if (braketBox) braketBox = (braketBox as Box2Abs).extend(vext, vext); + if (!render) render = global._ui_editor.render; + let attachmentPointsVBox = render.ctab.getAttachmentsPointsVBox(atoms); + attachmentPointsVBox = attachmentPointsVBox + ? attachmentPointsVBox.extend(BORDER_EXT, BORDER_EXT) + : attachmentPointsVBox; + + braketBox = + attachmentPointsVBox && braketBox + ? Box2Abs.union(attachmentPointsVBox, braketBox) + : braketBox; + if (braketBox) + braketBox = (braketBox as Box2Abs).extend(PADDING_VECTOR, PADDING_VECTOR); sGroup.bracketBox = braketBox; }