form-plugins.test.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /**
  2. * Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
  3. * SPDX-License-Identifier: MIT
  4. */
  5. import { beforeEach, describe, expect, it, vi } from 'vitest';
  6. import { FlowNodeEntity } from '@flowgram.ai/document';
  7. import { DataEvent, FormMeta } from '../src/types';
  8. import { defineFormPluginCreator } from '../src/form-plugin';
  9. import { FormModelV2 } from '../src/form-model-v2';
  10. describe('FormModelV2 plugins', () => {
  11. const node = {
  12. getService: vi.fn().mockReturnValue({}),
  13. getData: vi.fn().mockReturnValue({ fireChange: vi.fn() }),
  14. } as unknown as FlowNodeEntity;
  15. let formModelV2 = new FormModelV2(node);
  16. beforeEach(() => {
  17. formModelV2.dispose();
  18. formModelV2 = new FormModelV2(node);
  19. });
  20. it('should call onInit when formModel init', () => {
  21. const mockInit = vi.fn();
  22. const plugin = defineFormPluginCreator({
  23. name: 'test',
  24. onInit: mockInit,
  25. })({ opt1: 1 });
  26. const formMeta = {
  27. render: vi.fn(),
  28. plugins: [plugin],
  29. } as unknown as FormMeta;
  30. formModelV2.init(formMeta);
  31. expect(mockInit).toHaveBeenCalledOnce();
  32. expect(mockInit).toHaveBeenCalledWith(
  33. { formModel: formModelV2, ...formModelV2.nodeContext },
  34. { opt1: 1 }
  35. );
  36. });
  37. it('should call onDispose when formModel dispose', () => {
  38. const mockDispose = vi.fn();
  39. const plugin = defineFormPluginCreator({
  40. name: 'test',
  41. onDispose: mockDispose,
  42. })({ opt1: 1 });
  43. const formMeta = {
  44. render: vi.fn(),
  45. plugins: [plugin],
  46. } as unknown as FormMeta;
  47. formModelV2.init(formMeta);
  48. formModelV2.dispose();
  49. expect(mockDispose).toHaveBeenCalledOnce();
  50. expect(mockDispose).toHaveBeenCalledWith(
  51. { formModel: formModelV2, ...formModelV2.nodeContext },
  52. { opt1: 1 }
  53. );
  54. });
  55. it('should call effects when corresponding events trigger', () => {
  56. const mockEffectPlugin = vi.fn();
  57. const mockEffectOrigin = vi.fn();
  58. const plugin = defineFormPluginCreator({
  59. name: 'test',
  60. onSetupFormMeta(ctx, opts) {
  61. ctx.mergeEffect({
  62. a: [
  63. {
  64. event: DataEvent.onValueInitOrChange,
  65. effect: mockEffectPlugin,
  66. },
  67. ],
  68. });
  69. },
  70. })({ opt1: 1 });
  71. const formMeta = {
  72. render: vi.fn(),
  73. effect: {
  74. a: [
  75. {
  76. event: DataEvent.onValueInitOrChange,
  77. effect: mockEffectOrigin,
  78. },
  79. ],
  80. },
  81. plugins: [plugin],
  82. } as unknown as FormMeta;
  83. formModelV2.init(formMeta, { a: 0 });
  84. expect(mockEffectPlugin).toHaveBeenCalledOnce();
  85. expect(mockEffectOrigin).toHaveBeenCalledOnce();
  86. });
  87. it('should call effects when corresponding events trigger: array case', () => {
  88. const mockEffectPluginArrStar = vi.fn();
  89. const mockEffectOriginArrStar = vi.fn();
  90. const mockEffectPluginOther = vi.fn();
  91. const plugin = defineFormPluginCreator({
  92. name: 'test',
  93. onSetupFormMeta(ctx, opts) {
  94. ctx.mergeEffect({
  95. 'arr.*': [
  96. {
  97. event: DataEvent.onValueChange,
  98. effect: mockEffectPluginArrStar,
  99. },
  100. ],
  101. other: [
  102. {
  103. event: DataEvent.onValueChange,
  104. effect: mockEffectPluginOther,
  105. },
  106. ],
  107. });
  108. },
  109. })({ opt1: 1 });
  110. const formMeta = {
  111. render: vi.fn(),
  112. effect: {
  113. 'arr.*': [
  114. {
  115. event: DataEvent.onValueChange,
  116. effect: mockEffectOriginArrStar,
  117. },
  118. ],
  119. },
  120. plugins: [plugin],
  121. } as unknown as FormMeta;
  122. formModelV2.init(formMeta, { arr: [0], other: 1 });
  123. expect(mockEffectOriginArrStar).not.toHaveBeenCalled();
  124. expect(mockEffectPluginArrStar).not.toHaveBeenCalled();
  125. expect(mockEffectPluginOther).not.toHaveBeenCalled();
  126. formModelV2.setValueIn('arr.0', 2);
  127. formModelV2.setValueIn('other', 2);
  128. expect(mockEffectOriginArrStar).toHaveBeenCalledOnce();
  129. expect(mockEffectPluginArrStar).toHaveBeenCalledOnce();
  130. expect(mockEffectPluginOther).toHaveBeenCalledOnce();
  131. });
  132. });