Skip to content

Commit

Permalink
fix(sheet): custom undefined
Browse files Browse the repository at this point in the history
  • Loading branch information
Dushusir committed Oct 18, 2024
1 parent 80a152e commit 28a81d6
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,56 @@
* limitations under the License.
*/

import type { BooleanNumber, ICommandService, type Injector, IUniverInstanceService, IWorkbookData, LocaleType, RedoCommand, UndoCommand, type Univer, UniverInstanceType, type Workbook } from '@univerjs/core';
import type { ISetColCustomMutationParams } from '../../mutations/set-col-custom.mutation';
import { ICommandService, type Injector, IUniverInstanceService, RedoCommand, UndoCommand, type Univer, UniverInstanceType, type Workbook } from '@univerjs/core';
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
import { SetColCustomMutation } from '../../mutations/set-col-custom.mutation';
import { SetColCustomCommand } from '../set-col-custom.command';
import { createCommandTestBed } from './create-command-test-bed';

const TEST_WORKBOOK_DATA_DEMO = (): IWorkbookData => {
return {
id: 'test',
appVersion: '3.0.0-alpha',
sheets: {
sheet1: {
id: 'sheet1',
name: 'sheet1',
cellData: {
0: {
0: {
v: 'A1',
},
1: {
v: 'A2',
},
},
},
columnData: {
1: {
hd: BooleanNumber.FALSE,
},
5: {
custom: {
key: 'value',
},
},
},
rowData: {
1: {
hd: BooleanNumber.FALSE,
},

},
},
},
locale: LocaleType.ZH_CN,
name: '',
sheetOrder: [],
styles: {},
};
};

describe('test set column custom commands', () => {
let univer: Univer;
let get: Injector['get'];
Expand All @@ -35,7 +78,7 @@ describe('test set column custom commands', () => {
}

beforeEach(() => {
const testBed = createCommandTestBed();
const testBed = createCommandTestBed(TEST_WORKBOOK_DATA_DEMO());
univer = testBed.univer;
get = testBed.get;

Expand All @@ -59,25 +102,23 @@ describe('test set column custom commands', () => {
2: {
color: 'green',
},
5: {
color: 'blue',
},
5: undefined,
},
};
await commandService.executeCommand(SetColCustomCommand.id, params);

expect(getColumnCustom(1)).toEqual({ color: 'red' });
expect(getColumnCustom(2)).toEqual({ color: 'green' });
expect(getColumnCustom(5)).toEqual({ color: 'blue' });
expect(getColumnCustom(5)).toEqual({ key: 'value' });

await commandService.executeCommand(UndoCommand.id);
expect(getColumnCustom(1)).toBeUndefined();
expect(getColumnCustom(1)).toBeNull();
expect(getColumnCustom(2)).toBeNull();
expect(getColumnCustom(5)).toBeNull();
expect(getColumnCustom(5)).toEqual({ key: 'value' });

await commandService.executeCommand(RedoCommand.id);
expect(getColumnCustom(1)).toEqual({ color: 'red' });
expect(getColumnCustom(2)).toEqual({ color: 'green' });
expect(getColumnCustom(5)).toEqual({ color: 'blue' });
expect(getColumnCustom(5)).toEqual({ key: 'value' });
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,55 @@
* limitations under the License.
*/

import type { BooleanNumber, ICommandService, type Injector, IUniverInstanceService, IWorkbookData, LocaleType, RedoCommand, UndoCommand, type Univer, UniverInstanceType, type Workbook } from '@univerjs/core';
import type { ISetRowCustomMutationParams } from '../../mutations/set-row-custom.mutation';
import { ICommandService, type Injector, IUniverInstanceService, RedoCommand, UndoCommand, type Univer, UniverInstanceType, type Workbook } from '@univerjs/core';
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
import { SetRowCustomMutation } from '../../mutations/set-row-custom.mutation';
import { SetRowCustomCommand } from '../set-row-custom.command';
import { createCommandTestBed } from './create-command-test-bed';

const TEST_WORKBOOK_DATA_DEMO = (): IWorkbookData => {
return {
id: 'test',
appVersion: '3.0.0-alpha',
sheets: {
sheet1: {
id: 'sheet1',
name: 'sheet1',
cellData: {
0: {
0: {
v: 'A1',
},
1: {
v: 'A2',
},
},
},
columnData: {
1: {
hd: BooleanNumber.FALSE,
},
},
rowData: {
1: {
hd: BooleanNumber.FALSE,
},
5: {
custom: {
key: 'value',
},
},
},
},
},
locale: LocaleType.ZH_CN,
name: '',
sheetOrder: [],
styles: {},
};
};

describe('test set row custom commands', () => {
let univer: Univer;
let get: Injector['get'];
Expand All @@ -35,7 +77,7 @@ describe('test set row custom commands', () => {
}

beforeEach(() => {
const testBed = createCommandTestBed();
const testBed = createCommandTestBed(TEST_WORKBOOK_DATA_DEMO());
univer = testBed.univer;
get = testBed.get;

Expand All @@ -59,25 +101,23 @@ describe('test set row custom commands', () => {
2: {
color: 'green',
},
5: {
color: 'blue',
},
5: undefined,
},
};
await commandService.executeCommand(SetRowCustomCommand.id, params);

expect(getRowCustom(1)).toEqual({ color: 'red' });
expect(getRowCustom(2)).toEqual({ color: 'green' });
expect(getRowCustom(5)).toEqual({ color: 'blue' });
expect(getRowCustom(5)).toEqual({ key: 'value' });

await commandService.executeCommand(UndoCommand.id);
expect(getRowCustom(1)).toBeUndefined();
expect(getRowCustom(1)).toBeNull();
expect(getRowCustom(2)).toBeNull();
expect(getRowCustom(5)).toBeNull();
expect(getRowCustom(5)).toEqual({ key: 'value' });

await commandService.executeCommand(RedoCommand.id);
expect(getRowCustom(1)).toEqual({ color: 'red' });
expect(getRowCustom(2)).toEqual({ color: 'green' });
expect(getRowCustom(5)).toEqual({ color: 'blue' });
expect(getRowCustom(5)).toEqual({ key: 'value' });
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export const SetColCustomMutationFactory = (
for (const colIndex in custom) {
const column = manager.getColumn(Number(colIndex));

if (column === null || column === undefined) {
if (column === null || column === undefined || column.custom == null || column.custom === undefined) {
oldCustom[colIndex] = null;
} else {
oldCustom[colIndex] = column.custom;
Expand Down Expand Up @@ -65,7 +65,9 @@ export const SetColCustomMutation: IMutation<ISetColCustomMutationParams> = {
for (const colIndex in custom) {
const customData = custom[colIndex];
const column = manager.getColumnOrCreate(Number(colIndex));
column.custom = customData;
if (customData !== undefined) {
column.custom = customData;
}
}

return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export const SetRowCustomMutationFactory = (
for (const rowIndex in custom) {
const row = manager.getRow(Number(rowIndex));

if (row === null || row === undefined) {
if (row === null || row === undefined || row.custom === null || row.custom === undefined) {
oldCustom[rowIndex] = null;
} else {
oldCustom[rowIndex] = row.custom;
Expand Down Expand Up @@ -65,7 +65,10 @@ export const SetRowCustomMutation: IMutation<ISetRowCustomMutationParams> = {
for (const rowIndex in custom) {
const customData = custom[rowIndex];
const row = manager.getRowOrCreate(Number(rowIndex));
row.custom = customData;
if (customData !== undefined) {
// Custom will overwrite the original value
row.custom = customData;
}
}

return true;
Expand Down

0 comments on commit 28a81d6

Please sign in to comment.