flow-operation-service.test.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /**
  2. * Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
  3. * SPDX-License-Identifier: MIT
  4. */
  5. import { describe, it, expect, beforeEach, vi } from 'vitest';
  6. import { FlowDocument, FlowNodeEntity } from '@flowgram.ai/editor';
  7. import { getNodeChildrenIds } from '../utils';
  8. import { createContainer } from '../create-container';
  9. import { FlowOperationService } from '../../src/types';
  10. import { baseMock } from '../../__mocks__/flow.mock';
  11. describe('flow-operation-service', () => {
  12. let flowOperationService: FlowOperationService;
  13. let flowDocument: FlowDocument;
  14. beforeEach(() => {
  15. const container = createContainer({});
  16. flowDocument = container.get(FlowDocument);
  17. flowOperationService = container.get(FlowOperationService);
  18. flowDocument.fromJSON(baseMock);
  19. });
  20. it('addFromNode', () => {
  21. const type = 'test';
  22. const nodeJSON = {
  23. id: 'test',
  24. type,
  25. };
  26. const added = flowOperationService.addFromNode('start_0', nodeJSON);
  27. const node = flowDocument.getNode(added.id) as FlowNodeEntity;
  28. expect(node).toBe(added);
  29. expect(added.id).toEqual(nodeJSON.id);
  30. });
  31. it('deleteNode', () => {
  32. const id = 'dynamicSplit_0';
  33. flowOperationService.deleteNode(id);
  34. const node = flowDocument.getNode(id);
  35. expect(node).toBeUndefined();
  36. });
  37. it('delete block by deleteNode', () => {
  38. const id = 'block_0';
  39. flowOperationService.deleteNode(id);
  40. const node = flowDocument.getNode(id);
  41. expect(node).toBeUndefined();
  42. });
  43. it('addNode', () => {
  44. const nodeJSON = {
  45. id: 'test-node',
  46. type: 'test',
  47. };
  48. const parent = flowDocument.getNode('start_0');
  49. const added = flowOperationService.addNode(nodeJSON, {
  50. parent,
  51. });
  52. const entity = flowDocument.getNode(added.id);
  53. expect(entity).toBe(added);
  54. expect(entity?.parent).toBe(parent);
  55. expect(entity?.originParent).toBeUndefined();
  56. });
  57. it('addBlock', () => {
  58. const target = flowDocument.getNode('dynamicSplit_0') as FlowNodeEntity;
  59. const added = flowOperationService.addBlock(target, {
  60. id: 'test-block',
  61. type: 'test-block',
  62. });
  63. const entity = flowDocument.getNode(added.id);
  64. expect(entity).toBe(added);
  65. expect(entity?.parent?.id).toEqual('$inlineBlocks$dynamicSplit_0');
  66. expect(entity?.originParent).toBe(target);
  67. });
  68. it('deleteNodes', () => {
  69. const parent = flowDocument.getNode('start_0');
  70. const added = flowOperationService.addNode(
  71. {
  72. id: 'delete-node',
  73. type: 'test',
  74. },
  75. {
  76. parent,
  77. },
  78. );
  79. flowOperationService.deleteNodes([added]);
  80. expect(flowDocument.getNode(added.id)).toBeUndefined();
  81. });
  82. it('createGroup ungroup', () => {
  83. const node1 = flowOperationService.addFromNode('start_0', {
  84. id: 'add',
  85. type: 'add',
  86. });
  87. const node2 = flowDocument.getNode('dynamicSplit_0') as FlowNodeEntity;
  88. // TODO 这里需要优化,理论上createGroup不应该依赖渲染,现在createGroup内部有一个index的校验,但index在transformer中被设置对了
  89. flowDocument.transformer.refresh();
  90. const group = flowOperationService.createGroup([node1, node2]) as FlowNodeEntity;
  91. const root = flowDocument.getNode('root');
  92. expect(root?.collapsedChildren.map(c => c.id)).toEqual(['start_0', group.id, 'end_0']);
  93. expect(group.collapsedChildren.map(c => c.id)).toEqual([node1.id, node2.id]);
  94. flowOperationService.ungroup(group);
  95. expect(root?.collapsedChildren.map(c => c.id)).toEqual([
  96. 'start_0',
  97. 'add',
  98. 'dynamicSplit_0',
  99. 'end_0',
  100. ]);
  101. });
  102. it('moveNode', () => {
  103. flowOperationService.moveNode('block_1', {
  104. index: 2,
  105. });
  106. const split = flowDocument.getNode('dynamicSplit_0');
  107. expect(getNodeChildrenIds(split, true)).toEqual(['block_0', 'block_2', 'block_1']);
  108. });
  109. });