Skip to content

Commit 2d77923

Browse files
authored
fix: prevent updateTab with renderPanel tab (#781)
* fix: prevent updateTab with renderPanel tab * test: update test cases
1 parent a8b608d commit 2d77923

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

src/services/workbench/__tests__/editorService.test.tsx

+62
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { expectFnCalled } from '@test/utils';
77
import { modules } from 'mo/services/builtinService/const';
88
import { editor as MonacoEditor } from 'mo/monaco';
99
import { cloneDeep } from 'lodash';
10+
import { act } from 'react-dom/test-utils';
1011

1112
describe('Test EditorService', () => {
1213
let mockTab: IEditorTab;
@@ -189,6 +190,67 @@ describe('Test EditorService', () => {
189190
}
190191
});
191192

193+
test('Should update editor text via updateTab', () => {
194+
const editor = new EditorService();
195+
editor.open({ ...mockTab, data: { value: 'tabData' } });
196+
197+
const { groups } = editor.getState();
198+
expect(groups?.length).toBe(1);
199+
200+
const setValFn = jest.fn();
201+
const getValFn = jest.fn(() => '');
202+
groups![0].editorInstance = {
203+
getModel: () => ({
204+
getValue: getValFn,
205+
setValue: setValFn,
206+
}),
207+
};
208+
209+
act(() => {
210+
editor.updateTab({
211+
id: mockTab.id,
212+
data: {
213+
value: 'test',
214+
},
215+
});
216+
});
217+
218+
expect(setValFn).toBeCalled();
219+
expect(setValFn.mock.calls[0][0]).toBe('test');
220+
});
221+
222+
test('Should prevent update editor text if current tab with renderPane', () => {
223+
const editor = new EditorService();
224+
editor.open({
225+
...mockTab,
226+
data: { value: 'tabData' },
227+
renderPane: () => <div>test</div>,
228+
});
229+
230+
const { groups } = editor.getState();
231+
expect(groups?.length).toBe(1);
232+
233+
const setValFn = jest.fn();
234+
const getValFn = jest.fn(() => '');
235+
groups![0].editorInstance = {
236+
getModel: () => ({
237+
getValue: getValFn,
238+
setValue: setValFn,
239+
}),
240+
};
241+
242+
act(() => {
243+
editor.updateTab({
244+
id: mockTab.id,
245+
data: {
246+
value: 'test',
247+
},
248+
});
249+
});
250+
251+
expect(setValFn).not.toBeCalled();
252+
});
253+
192254
test('Close a tab', () => {
193255
const editor: any = new EditorService();
194256
editor.disposeModel = jest.fn();

src/services/workbench/editorService.ts

+2
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,7 @@ export class EditorService
326326
}
327327
if (group.activeTab === tab.id) {
328328
isString(editorValue) &&
329+
!tabData?.renderPane &&
329330
this.setGroupEditorValue(group, editorValue);
330331
updatedTab = Object.assign(group.tab, tab);
331332
}
@@ -345,6 +346,7 @@ export class EditorService
345346

346347
if (group.activeTab === tab.id) {
347348
isString(editorValue) &&
349+
!tabData?.renderPane &&
348350
this.setGroupEditorValue(group, editorValue);
349351
updatedTab = Object.assign(group.tab, tab);
350352
}

0 commit comments

Comments
 (0)