-
-
Notifications
You must be signed in to change notification settings - Fork 492
/
sheets-reorder.command.spec.ts
127 lines (119 loc) · 4.92 KB
/
sheets-reorder.command.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/**
* Copyright 2023-present DreamNum Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http:https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import type { ICellData, Nullable, Univer, Workbook } from '@univerjs/core';
import { ICommandService, IUniverInstanceService } from '@univerjs/core';
import type { Injector } from '@wendellhu/redi';
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
import { ReorderRangeCommand, ReorderRangeMutation, SetSelectionsOperation } from '@univerjs/sheets';
import type { ISortRangeCommandParams } from '../sheets-sort.command';
import { SortRangeCommand } from '../sheets-sort.command';
import { createCommandTestBed } from './create-command-test-bed';
describe('Test "Sort Range Commands"', () => {
let univer: Univer;
let get: Injector['get'];
let commandService: ICommandService;
let getData: (row: number, col: number) => Nullable<ICellData>;
beforeEach(() => {
const testBed = createCommandTestBed();
univer = testBed.univer;
get = testBed.get;
commandService = get(ICommandService);
commandService.registerCommand(SetSelectionsOperation);
commandService.registerCommand(ReorderRangeMutation);
commandService.registerCommand(ReorderRangeCommand);
const univerInstanceService = get(IUniverInstanceService);
const workbook = univerInstanceService.getUnit('test') as Workbook;
const worksheet = workbook?.getSheetBySheetId('sheet1')!;
getData = (row: number, col: number) => {
const data = worksheet.getCellMatrix().getValue(row, col);
if (data) {
return data;
}
return undefined;
};
});
afterEach(() => univer.dispose());
describe('sort with single col', () => {
it('asc case: ', async () => {
const params = {
unitId: 'test',
subUnitId: 'sheet1',
hasTitle: false,
orderRules: [{ colIndex: 1, type: 'asc' }],
range: {
startRow: 0,
endRow: 5,
startColumn: 0,
endColumn: 2,
},
} as ISortRangeCommandParams;
const result = await commandService.executeCommand(SortRangeCommand.id, params);
expect(result).toBeTruthy();
expect(getData(0, 0)?.v).toBe(6);
expect(getData(0, 1)?.v).toBe(15);
expect(getData(0, 2)?.v).toBe(200);
expect(getData(5, 0)?.v).toBe(1);
expect(getData(5, 1)?.v).toBe(20);
expect(getData(5, 2)?.v).toBe(100);
});
it('desc case: ', async () => {
const params = {
unitId: 'test',
subUnitId: 'sheet1',
hasTitle: false,
orderRules: [{ colIndex: 0, type: 'desc' }],
range: {
startRow: 0,
endRow: 5,
startColumn: 0,
endColumn: 2,
},
} as ISortRangeCommandParams;
const result = await commandService.executeCommand(SortRangeCommand.id, params);
expect(result).toBeTruthy();
expect(getData(0, 0)?.v).toBe(6);
expect(getData(0, 1)?.v).toBe(15);
expect(getData(0, 2)?.v).toBe(200);
expect(getData(5, 0)?.v).toBe(1);
expect(getData(5, 1)?.v).toBe(20);
expect(getData(5, 2)?.v).toBe(100);
});
});
describe('sort with multiple cols', () => {
it('asc + desc combine case', async () => {
const params = {
unitId: 'test',
subUnitId: 'sheet1',
hasTitle: false,
orderRules: [{ colIndex: 2, type: 'desc' }, { colIndex: 1, type: 'asc' }],
range: {
startRow: 0,
endRow: 5,
startColumn: 0,
endColumn: 2,
},
} as ISortRangeCommandParams;
const result = await commandService.executeCommand(SortRangeCommand.id, params);
expect(result).toBeTruthy();
expect(getData(0, 0)?.v).toBe(6);
expect(getData(0, 1)?.v).toBe(15);
expect(getData(0, 2)?.v).toBe(200);
expect(getData(5, 0)?.v).toBe(1);
expect(getData(5, 1)?.v).toBe(20);
expect(getData(5, 2)?.v).toBe(100);
});
});
});