|
| 1 | +/** |
| 2 | + * Copyright 2023-present DreamNum Co., Ltd. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import type { IUpdateDrawingDocTransformCommandParams } from '@univerjs/docs-drawing'; |
| 18 | +import { ICommandService, ImageSourceType } from '@univerjs/core'; |
| 19 | +import { DocHistoryAction, RichTextEditingMutation } from '@univerjs/docs'; |
| 20 | +import { UpdateDrawingDocTransformCommand } from '@univerjs/docs-drawing'; |
| 21 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 22 | +import { createFacadeTestBed } from '../../../facade/__tests__/create-test-bed'; |
| 23 | + |
| 24 | +class MockImage { |
| 25 | + width = 800; |
| 26 | + height = 400; |
| 27 | + onload: (() => void) | null = null; |
| 28 | + onerror: (() => void) | null = null; |
| 29 | + |
| 30 | + get src(): string { |
| 31 | + return ''; |
| 32 | + } |
| 33 | + |
| 34 | + set src(_value: string) { |
| 35 | + queueMicrotask(() => this.onload?.()); |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +describe('UpdateDrawingDocTransformCommand', () => { |
| 40 | + let testBed: ReturnType<typeof createFacadeTestBed>; |
| 41 | + |
| 42 | + beforeEach(() => { |
| 43 | + vi.stubGlobal('Image', MockImage); |
| 44 | + testBed = createFacadeTestBed(); |
| 45 | + }); |
| 46 | + |
| 47 | + afterEach(() => { |
| 48 | + testBed.univer.dispose(); |
| 49 | + vi.unstubAllGlobals(); |
| 50 | + }); |
| 51 | + |
| 52 | + it('marks image transforms for history action summaries', async () => { |
| 53 | + const image = await testBed.document.insertImage({ |
| 54 | + source: 'data:image/png;base64,image', |
| 55 | + imageSourceType: ImageSourceType.BASE64, |
| 56 | + width: 160, |
| 57 | + height: 90, |
| 58 | + textRange: { |
| 59 | + startOffset: 3, |
| 60 | + endOffset: 3, |
| 61 | + collapsed: true, |
| 62 | + segmentId: '', |
| 63 | + }, |
| 64 | + }); |
| 65 | + const commandService = testBed.injector.get(ICommandService); |
| 66 | + const mutationSpy = vi.spyOn(commandService, 'syncExecuteCommand'); |
| 67 | + |
| 68 | + const result = commandService.syncExecuteCommand<IUpdateDrawingDocTransformCommandParams>( |
| 69 | + UpdateDrawingDocTransformCommand.id, |
| 70 | + { |
| 71 | + unitId: 'test-doc', |
| 72 | + subUnitId: 'test-doc', |
| 73 | + drawings: [{ drawingId: image!.getId(), key: 'angle', value: 15 }], |
| 74 | + } |
| 75 | + ); |
| 76 | + |
| 77 | + expect(result).toBe(true); |
| 78 | + expect(mutationSpy).toHaveBeenCalledWith( |
| 79 | + RichTextEditingMutation.id, |
| 80 | + expect.objectContaining({ historyAction: DocHistoryAction.UpdateImage }) |
| 81 | + ); |
| 82 | + }); |
| 83 | +}); |
0 commit comments