history.test.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /**
  2. * Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
  3. * SPDX-License-Identifier: MIT
  4. */
  5. import { describe, it, expect } from 'vitest';
  6. import { WorkflowDocument } from '@flowgram.ai/free-layout-core';
  7. import { HistoryService } from '@flowgram.ai/free-history-plugin';
  8. import { mockJSON } from '../__mocks__/flow.mocks';
  9. import { createEditor } from './create-editor';
  10. describe('free-layout history', () => {
  11. it('line-data-change', async () => {
  12. const editor = createEditor({
  13. history: {
  14. enable: true,
  15. },
  16. });
  17. const document = editor.get(WorkflowDocument);
  18. const history = editor.get(HistoryService);
  19. let historyEvent: any;
  20. history.onApply((e) => {
  21. historyEvent = e;
  22. });
  23. document.fromJSON(mockJSON);
  24. const line = document.linesManager.getLine({
  25. from: 'start_0',
  26. to: 'condition_0',
  27. });
  28. line.lineData = { a: 33 };
  29. expect(historyEvent.type).toEqual('changeLineData');
  30. expect(historyEvent.value).toEqual({
  31. id: 'start_0_-condition_0_',
  32. oldValue: undefined,
  33. newValue: { a: 33 },
  34. });
  35. await history.undo();
  36. expect(historyEvent.value).toEqual({
  37. id: 'start_0_-condition_0_',
  38. oldValue: { a: 33 },
  39. newValue: undefined,
  40. });
  41. expect(line.lineData).toEqual(undefined);
  42. await history.redo();
  43. expect(historyEvent.value).toEqual({
  44. id: 'start_0_-condition_0_',
  45. oldValue: undefined,
  46. newValue: { a: 33 },
  47. });
  48. expect(line.lineData).toEqual({ a: 33 });
  49. // change moreTimes
  50. line.lineData = { a: 44 };
  51. line.lineData = { a: 55 };
  52. line.lineData = { a: 66 };
  53. await history.undo();
  54. expect(line.lineData).toEqual(undefined);
  55. await history.redo();
  56. expect(line.lineData).toEqual({ a: 66 });
  57. });
  58. it('enableChangeLineData to false', () => {
  59. const editor = createEditor({
  60. history: {
  61. enable: true,
  62. enableChangeLineData: false,
  63. },
  64. });
  65. const document = editor.get(WorkflowDocument);
  66. document.fromJSON(mockJSON);
  67. const history = editor.get(HistoryService);
  68. let historyEvent: any;
  69. history.onApply((e) => {
  70. historyEvent = e;
  71. });
  72. const line = document.linesManager.getLine({
  73. from: 'start_0',
  74. to: 'condition_0',
  75. });
  76. line.lineData = { a: 33 };
  77. expect(historyEvent).toEqual(undefined);
  78. });
  79. it('changeNodeForm', async () => {
  80. const editor = createEditor({
  81. history: {
  82. enable: true,
  83. },
  84. nodeEngine: {
  85. enable: true,
  86. },
  87. getNodeDefaultRegistry: (type) => ({
  88. type,
  89. formMeta: {
  90. render: () => null,
  91. },
  92. }),
  93. });
  94. let historyEvent: any;
  95. const history = editor.get(HistoryService);
  96. history.onApply((e) => {
  97. historyEvent = e;
  98. });
  99. const flowDocument = editor.get(WorkflowDocument);
  100. flowDocument.fromJSON(mockJSON);
  101. const node = flowDocument.getNode('start_0');
  102. const form = node.form;
  103. form.setValueIn('title', 'title changed');
  104. expect(historyEvent).toEqual({
  105. type: 'changeFormValues',
  106. value: {
  107. id: 'start_0',
  108. path: 'title',
  109. value: 'title changed',
  110. oldValue: 'Start',
  111. },
  112. });
  113. await history.undo();
  114. expect(form.getValueIn('title')).toEqual('Start');
  115. await history.redo();
  116. expect(form.getValueIn('title')).toEqual('title changed');
  117. });
  118. });