flow-node-registry.spec.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 { createDocumentContainer } from './flow-document-container.mock';
  7. import { FlowDocument, FlowNodeRegistry } from '../src';
  8. function registerNode(doc: FlowDocument, newRegistry: FlowNodeRegistry): FlowNodeRegistry {
  9. doc.registerFlowNodes(newRegistry);
  10. return doc.getNodeRegistry(newRegistry.type);
  11. }
  12. const mockRegistries: FlowNodeRegistry[] = [
  13. {
  14. type: 'dynamicSplit',
  15. meta: {},
  16. onCreate(node, json) {
  17. return node.document.addInlineBlocks(node, json.blocks || []);
  18. },
  19. extendChildRegistries: [
  20. {
  21. type: 'blockIcon',
  22. customKey: 'blockIcon_base',
  23. },
  24. {
  25. type: 'inlineBlocks',
  26. customKey: 'inlineBlocks_base',
  27. },
  28. ],
  29. onAdd: () => {},
  30. },
  31. {
  32. type: 'a',
  33. extend: 'dynamicSplit',
  34. },
  35. {
  36. type: 'b',
  37. extend: 'a',
  38. extendChildRegistries: [
  39. {
  40. type: 'blockIcon',
  41. customKey: 'blockIcon_from_b',
  42. },
  43. ],
  44. },
  45. {
  46. type: 'c',
  47. extend: 'b',
  48. extendChildRegistries: [
  49. {
  50. type: 'blockIcon',
  51. customKey: 'blockIcon_from_c',
  52. },
  53. {
  54. type: 'inlineBlocks',
  55. customKey: 'inlineBlocks_from_c',
  56. },
  57. ],
  58. },
  59. ];
  60. describe('flow-node-registry', () => {
  61. let doc: FlowDocument;
  62. beforeEach(() => {
  63. const container = createDocumentContainer();
  64. doc = container.get<FlowDocument>(FlowDocument);
  65. doc.registerFlowNodes(...mockRegistries);
  66. });
  67. it('extend check', () => {
  68. expect(doc.getNodeRegistry('dynamicSplit').__extends__).toEqual(undefined);
  69. expect(doc.getNodeRegistry('a').__extends__).toEqual(['dynamicSplit']);
  70. expect(doc.getNodeRegistry('b').__extends__).toEqual(['a', 'dynamicSplit']);
  71. expect(doc.getNodeRegistry('c').__extends__).toEqual(['b', 'a', 'dynamicSplit']);
  72. expect(doc.isExtend('dynamicSplit', 'dynamicSplit')).toBeFalsy();
  73. expect(doc.isExtend('a', 'b')).toBeFalsy();
  74. expect(doc.isExtend('a', 'dynamicSplit')).toBeTruthy();
  75. expect(doc.isExtend('b', 'dynamicSplit')).toBeTruthy();
  76. expect(doc.isExtend('b', 'a')).toBeTruthy();
  77. expect(doc.isExtend('c', 'dynamicSplit')).toBeTruthy();
  78. expect(doc.isExtend('c', 'b')).toBeTruthy();
  79. expect(doc.isExtend('c', 'a')).toBeTruthy();
  80. expect(doc.isTypeOrExtendType('dynamicSplit', 'dynamicSplit')).toBeTruthy();
  81. expect(doc.isTypeOrExtendType('c', 'a')).toBeTruthy();
  82. });
  83. it('base extend', () => {
  84. expect(doc.getNodeRegistry('a').onAdd).toBeTypeOf('function');
  85. doc.addNode({
  86. id: 'a',
  87. type: 'a',
  88. parent: doc.root,
  89. });
  90. expect(doc.toString()).toEqual(`root
  91. |-- a
  92. |---- $blockIcon$a
  93. |---- $inlineBlocks$a`);
  94. expect(doc.getNode('$blockIcon$a').getNodeRegistry().customKey).toBe('blockIcon_base');
  95. });
  96. it('extend nested', () => {
  97. expect(doc.getNodeRegistry('b').onAdd).toBeTypeOf('function');
  98. doc.addNode({
  99. id: 'b',
  100. type: 'b',
  101. parent: doc.root,
  102. });
  103. doc.addNode({
  104. id: 'c',
  105. type: 'c',
  106. parent: doc.root,
  107. });
  108. expect(doc.toString()).toEqual(`root
  109. |-- b
  110. |---- $blockIcon$b
  111. |---- $inlineBlocks$b
  112. |-- c
  113. |---- $blockIcon$c
  114. |---- $inlineBlocks$c`);
  115. expect(doc.getNode('$blockIcon$b').getNodeRegistry().customKey).toBe('blockIcon_from_b');
  116. expect(doc.getNode('$inlineBlocks$b').getNodeRegistry().customKey).toBe('inlineBlocks_base');
  117. expect(doc.getNode('$blockIcon$c').getNodeRegistry().customKey).toBe('blockIcon_from_c');
  118. expect(doc.getNode('$inlineBlocks$c').getNodeRegistry().customKey).toBe('inlineBlocks_from_c');
  119. });
  120. });