Skip to content

Commit

Permalink
test(facade): test cell hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
Dushusir committed May 20, 2024
1 parent 52d87e8 commit f9d27c7
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 7 deletions.
86 changes: 80 additions & 6 deletions packages/facade/src/apis/sheets/__tests__/f-sheet-hooks.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,35 @@ import type { Injector } from '@wendellhu/redi';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { Subject } from 'rxjs';

import type { IHoverCellPosition } from '@univerjs/sheets-ui';
import { HoverManagerService } from '@univerjs/sheets-ui';
import type { IDragCellPosition, IHoverCellPosition } from '@univerjs/sheets-ui';
import { DragManagerService, HoverManagerService } from '@univerjs/sheets-ui';
import type { FUniver } from '../../facade';
import { createFacadeTestBed } from '../../__tests__/create-test-bed';
import { FSheetHooks } from '../f-sheet-hooks';

class MockDataTransfer implements DataTransfer {
effectAllowed: 'none' | 'copy' | 'link' | 'move' | 'all' | 'copyLink' | 'copyMove' | 'linkMove' | 'uninitialized';
files: FileList;
items: DataTransferItemList;
types: readonly string[];
clearData(format?: string | undefined): void {
throw new Error('Method not implemented.');
}

getData(format: string): string {
throw new Error('Method not implemented.');
}

setData(format: string, data: string): void {
throw new Error('Method not implemented.');
}

dropEffect: 'none' | 'copy' | 'link' | 'move' = 'none';
setDragImage(image: Element, x: number, y: number): void {
throw new Error('Method not implemented.');
}
}

describe('Test FSheetHooks', () => {
let get: Injector['get'];
let injector: Injector;
Expand All @@ -46,7 +69,11 @@ describe('Test FSheetHooks', () => {
endColumn: number
) => Nullable<IStyleData>;
let mockHoverManagerService: Partial<HoverManagerService>;
let currentCell$: Subject<Nullable<IHoverCellPosition>>;
let mockDragManagerService: Partial<DragManagerService>;
let hoverCurrentCell$: Subject<Nullable<IHoverCellPosition>>;
let hoverCurrentPosition$: Subject<Nullable<IHoverCellPosition>>;
let dragCurrentCell$: Subject<Nullable<IDragCellPosition>>;
let dragEndCell$: Subject<Nullable<IDragCellPosition>>;
let sheetHooks: FSheetHooks;
let workbook: Workbook;

Expand All @@ -56,15 +83,26 @@ describe('Test FSheetHooks', () => {

beforeEach(() => {
// Initialize the subject
currentCell$ = new Subject<Nullable<IHoverCellPosition>>();
hoverCurrentCell$ = new Subject<Nullable<IHoverCellPosition>>();
hoverCurrentPosition$ = new Subject<Nullable<IHoverCellPosition>>();
dragCurrentCell$ = new Subject<Nullable<IDragCellPosition>>();
dragEndCell$ = new Subject<Nullable<IDragCellPosition>>();

// Create a mock HoverManagerService with currentCell$
mockHoverManagerService = {
currentCell$: currentCell$.asObservable(),
currentCell$: hoverCurrentCell$.asObservable(),
currentPosition$: hoverCurrentPosition$.asObservable(),
};

// Create a mock DragManagerService with currentCell$
mockDragManagerService = {
currentCell$: dragCurrentCell$.asObservable(),
endCell$: dragEndCell$.asObservable(),
};

const testBed = createFacadeTestBed(undefined, [
[HoverManagerService, { useValue: mockHoverManagerService }],
[DragManagerService, { useValue: mockDragManagerService }],
]);
get = testBed.get;
injector = testBed.injector;
Expand Down Expand Up @@ -109,6 +147,18 @@ describe('Test FSheetHooks', () => {
});

it('Test onCellPointerMove', () => {
sheetHooks.onCellPointerMove((cellPos) => {
expect(cellPos).toEqual({ location: { workbook, worksheet, unitId: workbook.getUnitId(), subUnitId: worksheet.getSheetId(), row: 0, col: 0 }, position: { startX: 0, endX: 1, startY: 0, endY: 1 } });
});

// Trigger the Observable to emit a new value
const worksheet = workbook.getActiveSheet();
const unitId = workbook.getUnitId();
const subUnitId = worksheet.getSheetId();
hoverCurrentPosition$.next({ location: { workbook, worksheet, unitId, subUnitId, row: 0, col: 0 }, position: { startX: 0, endX: 1, startY: 0, endY: 1 } });
});

it('Test onCellPointerOver', () => {
sheetHooks.onCellPointerOver((cellPos) => {
expect(cellPos).toEqual({ location: { workbook, worksheet, unitId: workbook.getUnitId(), subUnitId: worksheet.getSheetId(), row: 0, col: 0 }, position: { startX: 0, endX: 1, startY: 0, endY: 1 } });
});
Expand All @@ -117,6 +167,30 @@ describe('Test FSheetHooks', () => {
const worksheet = workbook.getActiveSheet();
const unitId = workbook.getUnitId();
const subUnitId = worksheet.getSheetId();
currentCell$.next({ location: { workbook, worksheet, unitId, subUnitId, row: 0, col: 0 }, position: { startX: 0, endX: 1, startY: 0, endY: 1 } });
hoverCurrentCell$.next({ location: { workbook, worksheet, unitId, subUnitId, row: 0, col: 0 }, position: { startX: 0, endX: 1, startY: 0, endY: 1 } });
});

it('Test onCellDragOver', () => {
sheetHooks.onCellDragOver((cellPos) => {
expect(cellPos).toEqual({ location: { workbook, worksheet, unitId: workbook.getUnitId(), subUnitId: worksheet.getSheetId(), row: 0, col: 0 }, position: { startX: 0, endX: 1, startY: 0, endY: 1 }, dataTransfer: new MockDataTransfer() });
});

// Trigger the Observable to emit a new value
const worksheet = workbook.getActiveSheet();
const unitId = workbook.getUnitId();
const subUnitId = worksheet.getSheetId();
dragCurrentCell$.next({ location: { workbook, worksheet, unitId, subUnitId, row: 0, col: 0 }, position: { startX: 0, endX: 1, startY: 0, endY: 1 }, dataTransfer: new MockDataTransfer() });
});

it('Test onCellDrop', () => {
sheetHooks.onCellDrop((cellPos) => {
expect(cellPos).toEqual({ location: { workbook, worksheet, unitId: workbook.getUnitId(), subUnitId: worksheet.getSheetId(), row: 0, col: 0 }, position: { startX: 0, endX: 1, startY: 0, endY: 1 }, dataTransfer: new MockDataTransfer() });
});

// Trigger the Observable to emit a new value
const worksheet = workbook.getActiveSheet();
const unitId = workbook.getUnitId();
const subUnitId = worksheet.getSheetId();
dragEndCell$.next({ location: { workbook, worksheet, unitId, subUnitId, row: 0, col: 0 }, position: { startX: 0, endX: 1, startY: 0, endY: 1 }, dataTransfer: new MockDataTransfer() });
});
});
2 changes: 1 addition & 1 deletion packages/facade/src/apis/sheets/f-sheet-hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export class FSheetHooks {
constructor(
@Inject(HoverManagerService) private readonly _hoverManagerService: HoverManagerService,
@Inject(DragManagerService) private readonly _dragManagerService: DragManagerService
) {
) {
// empty
}

Expand Down

0 comments on commit f9d27c7

Please sign in to comment.