flow-drag-entities.test.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /**
  2. * Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
  3. * SPDX-License-Identifier: MIT
  4. */
  5. import { beforeEach, describe, expect, it } from 'vitest';
  6. import { Rectangle } from '@flowgram.ai/utils';
  7. import {
  8. FlowDocument,
  9. FlowNodeTransitionData,
  10. FlowTransitionLabelEnum,
  11. LABEL_SIDE_TYPE,
  12. } from '@flowgram.ai/document';
  13. import { EntityManager, PlaygroundConfigEntity, PlaygroundContext } from '@flowgram.ai/core';
  14. import { FlowDragEntity } from '../../src/entities/flow-drag-entity';
  15. import { flowJson } from '../../__mocks__/flow-json.mock';
  16. import {
  17. NOT_SCROLL_EVENT,
  18. SCROLL_BOTTOM_EVENT,
  19. SCROLL_LEFT_EVENT,
  20. SCROLL_RIGHT_EVENT,
  21. SCROLL_TOP_EVENT,
  22. } from '../../__mocks__/flow-drag-entity';
  23. import { createDocumentContainer } from '../../__mocks__/flow-document-container.mock';
  24. // layer 层 drag entity 单测
  25. describe('flow-drag-entity', () => {
  26. let container = createDocumentContainer();
  27. let document: FlowDocument;
  28. let flowDragEntity: FlowDragEntity;
  29. let nodeTransition: FlowNodeTransitionData;
  30. let firstBranchTransition: FlowNodeTransitionData;
  31. const collisionRect = new Rectangle(-50, -50, 100, 100);
  32. const notCollisionRect = new Rectangle(50, 50, 100, 100);
  33. beforeEach(() => {
  34. container = createDocumentContainer();
  35. document = container.get<FlowDocument>(FlowDocument);
  36. const entityManager = container.get<EntityManager>(EntityManager);
  37. container.bind(PlaygroundContext).toConstantValue({});
  38. document.fromJSON(flowJson);
  39. flowDragEntity = new FlowDragEntity({ entityManager });
  40. const playgroundConfigEntity =
  41. entityManager.getEntity<PlaygroundConfigEntity>(PlaygroundConfigEntity);
  42. playgroundConfigEntity?.updateConfig({
  43. clientX: 0,
  44. clientY: 0,
  45. width: 3000,
  46. height: 3000,
  47. });
  48. const nodeEntity = document.getNode('$blockIcon$approval_fc79f9fa62f');
  49. nodeTransition = nodeEntity?.getData<FlowNodeTransitionData>(
  50. FlowNodeTransitionData
  51. ) as FlowNodeTransitionData;
  52. const firstBranchEntity = document.getNode('branch_8864cf1f9d3');
  53. firstBranchTransition = firstBranchEntity?.getData<FlowNodeTransitionData>(
  54. FlowNodeTransitionData
  55. ) as FlowNodeTransitionData;
  56. });
  57. it('flow drag scroll', () => {
  58. const el = global.document.createElement('div');
  59. // 页面不滚动
  60. expect(flowDragEntity.scrollDirection(NOT_SCROLL_EVENT, 0, 0)).toMatchSnapshot();
  61. // 页面滚动
  62. expect(flowDragEntity.scrollDirection(SCROLL_TOP_EVENT, 0, 0)).toMatchSnapshot();
  63. expect(flowDragEntity.scrollDirection(SCROLL_LEFT_EVENT, 0, 0)).toMatchSnapshot();
  64. expect(flowDragEntity.scrollDirection(SCROLL_RIGHT_EVENT, 0, 0)).toMatchSnapshot();
  65. expect(flowDragEntity.scrollDirection(SCROLL_BOTTOM_EVENT, 0, 0)).toMatchSnapshot();
  66. // 停止滚动
  67. flowDragEntity.stopAllScroll();
  68. expect(flowDragEntity.hasScroll).toEqual(false);
  69. });
  70. it('flow drag node collision true', () => {
  71. // 测试默认 offset x y 为 0
  72. expect(flowDragEntity.isCollision(nodeTransition, collisionRect, false)).toEqual({
  73. hasCollision: true,
  74. labelOffsetType: undefined,
  75. });
  76. });
  77. it('flow drag node label empty', () => {
  78. expect(flowDragEntity.isCollision(nodeTransition, notCollisionRect, false)).toEqual({
  79. hasCollision: false,
  80. labelOffsetType: undefined,
  81. });
  82. });
  83. it('flow drag node collision false', () => {
  84. const emptyLabelNodeTransition = {
  85. ...nodeTransition,
  86. labels: [{ type: FlowTransitionLabelEnum.BRANCH_DRAGGING_LABEL, offset: { x: 0, y: 0 } }],
  87. } as FlowNodeTransitionData;
  88. expect(flowDragEntity.isCollision(emptyLabelNodeTransition, collisionRect, false)).toEqual({
  89. hasCollision: false,
  90. labelOffsetType: undefined,
  91. });
  92. });
  93. it('flow drag branch collision true', () => {
  94. const preBranchNodeTransition = {
  95. ...firstBranchTransition,
  96. labels: [
  97. {
  98. type: FlowTransitionLabelEnum.BRANCH_DRAGGING_LABEL,
  99. props: {
  100. side: LABEL_SIDE_TYPE.PRE_BRANCH,
  101. } as any,
  102. offset: { x: 0, y: 0 },
  103. },
  104. ],
  105. } as FlowNodeTransitionData;
  106. // 第一个分支场景,校验 labelOffsetType 场景
  107. expect(flowDragEntity.isCollision(preBranchNodeTransition, collisionRect, true)).toEqual({
  108. hasCollision: true,
  109. labelOffsetType: LABEL_SIDE_TYPE.PRE_BRANCH,
  110. });
  111. });
  112. it('flow drag branch collision false', () => {
  113. const preBranchNodeTransition = {
  114. ...firstBranchTransition,
  115. labels: [
  116. {
  117. type: FlowTransitionLabelEnum.BRANCH_DRAGGING_LABEL,
  118. props: {
  119. side: LABEL_SIDE_TYPE.PRE_BRANCH,
  120. } as any,
  121. offset: { x: 0, y: 0 },
  122. },
  123. ],
  124. } as FlowNodeTransitionData;
  125. expect(flowDragEntity.isCollision(preBranchNodeTransition, notCollisionRect, true)).toEqual({
  126. hasCollision: false,
  127. labelOffsetType: LABEL_SIDE_TYPE.NORMAL_BRANCH,
  128. });
  129. });
  130. it('flow drag dispose', () => {
  131. flowDragEntity.dispose();
  132. expect(flowDragEntity.toDispose.disposed).toEqual(true);
  133. });
  134. });