|
| 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 { IRemoveDocDrawingCommandParams } from '../remove-doc-drawing.command'; |
| 18 | +import { DrawingTypeEnum, ICommandService, ImageSourceType } from '@univerjs/core'; |
| 19 | +import { DocHistoryAction, RichTextEditingMutation } from '@univerjs/docs'; |
| 20 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 21 | +import { createFacadeTestBed } from '../../../facade/__tests__/create-test-bed'; |
| 22 | +import { RemoveDocDrawingCommand } from '../remove-doc-drawing.command'; |
| 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('RemoveDocDrawingCommand', () => { |
| 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.each([ |
| 53 | + [DrawingTypeEnum.DRAWING_IMAGE, DocHistoryAction.DeleteImage], |
| 54 | + [DrawingTypeEnum.DRAWING_SHAPE, DocHistoryAction.DeleteShape], |
| 55 | + [DrawingTypeEnum.DRAWING_CHART, DocHistoryAction.DeleteChart], |
| 56 | + ])('records the drawing type %s in history metadata', async (drawingType, historyAction) => { |
| 57 | + const image = await testBed.document.insertImage({ |
| 58 | + source: 'data:image/png;base64,image', |
| 59 | + imageSourceType: ImageSourceType.BASE64, |
| 60 | + width: 160, |
| 61 | + height: 90, |
| 62 | + textRange: { |
| 63 | + startOffset: 3, |
| 64 | + endOffset: 3, |
| 65 | + collapsed: true, |
| 66 | + segmentId: '', |
| 67 | + }, |
| 68 | + }); |
| 69 | + const commandService = testBed.injector.get(ICommandService); |
| 70 | + const mutationSpy = vi.spyOn(commandService, 'syncExecuteCommand'); |
| 71 | + |
| 72 | + const result = commandService.syncExecuteCommand<IRemoveDocDrawingCommandParams>( |
| 73 | + RemoveDocDrawingCommand.id, |
| 74 | + { |
| 75 | + unitId: 'test-doc', |
| 76 | + drawings: [{ |
| 77 | + unitId: 'test-doc', |
| 78 | + subUnitId: 'test-doc', |
| 79 | + drawingId: image!.getId(), |
| 80 | + drawingType, |
| 81 | + }], |
| 82 | + } |
| 83 | + ); |
| 84 | + |
| 85 | + expect(result).toBe(true); |
| 86 | + expect(mutationSpy).toHaveBeenCalledWith( |
| 87 | + RichTextEditingMutation.id, |
| 88 | + expect.objectContaining({ historyActions: [historyAction] }) |
| 89 | + ); |
| 90 | + }); |
| 91 | +}); |
0 commit comments