find-selected-nodes.test.ts 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /**
  2. * Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
  3. * SPDX-License-Identifier: MIT
  4. */
  5. import { type FlowDocument } from '@flowgram.ai/document';
  6. import { findSelectedNodes } from '../../src/utils/find-selected-nodes';
  7. import { FLOW_SELECTED_NODES } from '../../__mocks__/flow-selected-nodes.mock';
  8. import { createDocument } from '../../__mocks__/flow-document-container.mock';
  9. function selectNodes(document: FlowDocument, nodeIds: string[]): string[] {
  10. const nodes = nodeIds.map(n => document.getNode(n));
  11. return findSelectedNodes(nodes).map(n => n.id);
  12. }
  13. describe('find selected nodes', () => {
  14. let document: FlowDocument;
  15. beforeEach(() => {
  16. document = createDocument();
  17. document.fromJSON(FLOW_SELECTED_NODES);
  18. });
  19. /**
  20. * 同分支选择
  21. */
  22. it('some branch', () => {
  23. const res = selectNodes(document, [
  24. 'createRecord_47e8fe1dfc3',
  25. 'createRecord_32dcdd10274',
  26. 'exclusiveSplit_a5579b3997d',
  27. ]);
  28. expect(res).toEqual([
  29. 'createRecord_47e8fe1dfc3',
  30. 'createRecord_32dcdd10274',
  31. 'exclusiveSplit_a5579b3997d',
  32. ]);
  33. });
  34. /**
  35. * 同分支下再选择子节点
  36. */
  37. it('some branch with sub branch', () => {
  38. const res = selectNodes(document, [
  39. 'createRecord_47e8fe1dfc3',
  40. 'createRecord_32dcdd10274',
  41. 'createRecord_b57b00eee94', // 这个属于 "exclusiveSplit_a5579b3997d" 的子节点
  42. ]);
  43. expect(res).toEqual([
  44. 'createRecord_47e8fe1dfc3',
  45. 'createRecord_32dcdd10274',
  46. 'exclusiveSplit_a5579b3997d',
  47. ]);
  48. });
  49. /**
  50. * 跨分支选择
  51. */
  52. it('different branch', () => {
  53. const res = selectNodes(document, ['createRecord_897b61c55f3', 'createRecord_b57b00eee94']);
  54. expect(res).toEqual(['exclusiveSplit_30baf8b1da0']);
  55. const res2 = selectNodes(document, ['createRecord_897b61c55f3', 'createRecord_47e8fe1dfc3']);
  56. expect(res2).toEqual(['exclusiveSplit_30baf8b1da0']);
  57. });
  58. });