flow-operation-service.test.ts 3.7 KB

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