刘光辉
8 小时以前 0dfe84494048ce27ba8449831782128412d3eb13
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
import { describe, expect, it } from 'vitest';
 
import {
  createRowClickSelectionHandler,
  getDynamicListSelectionConfig,
  isActionColumn,
  isCheckboxSelectionColumn,
  resolveRowClickSelectionAction,
} from '../helpers/rowSelection';
 
interface TestRow {
  id: string;
}
 
function createSelectionHarness(initialSelectedRowKeys: string[]) {
  let selectedRowKeys = [...initialSelectedRowKeys];
  const handleRowSelection = createRowClickSelectionHandler<TestRow, string>({
    deselectRow: (rowKey) => {
      selectedRowKeys = selectedRowKeys.filter((key) => key !== rowKey);
    },
    getRowKey: () => 'id',
    getSelectedRowKeys: () => selectedRowKeys,
    isEnabled: () => true,
    selectRows: (rows) => {
      selectedRowKeys = [...selectedRowKeys, ...rows.map((row) => row.id)];
    },
  });
 
  return {
    getSelectedRowKeys: () => selectedRowKeys,
    handleRowSelection,
  };
}
 
describe('resolveRowClickSelectionAction', () => {
  it('selects an unselected clicked row without replacing other selections', () => {
    expect(resolveRowClickSelectionAction(['row-1', 'row-2'], 'row-3')).toBe('select');
  });
 
  it('deselects a clicked row when it is the only selection', () => {
    expect(resolveRowClickSelectionAction(['row-1'], 'row-1')).toBe('deselect');
  });
 
  it('deselects a clicked selected row while other rows are selected', () => {
    expect(resolveRowClickSelectionAction(['row-1', 'row-2'], 'row-2')).toBe('deselect');
  });
});
 
describe('isCheckboxSelectionColumn', () => {
  it('recognizes checkbox columns and leaves ordinary columns to row-click handling', () => {
    expect(isCheckboxSelectionColumn({ type: 'checkbox' })).toBe(true);
    expect(isCheckboxSelectionColumn({ flag: 'checkbox' })).toBe(true);
    expect(isCheckboxSelectionColumn({ field: 'name' })).toBe(false);
  });
});
 
describe('isActionColumn', () => {
  it('recognizes action columns by field or flag', () => {
    expect(isActionColumn({ field: 'action' })).toBe(true);
    expect(isActionColumn({ flag: 'action' })).toBe(true);
    expect(isActionColumn({ field: 'name' })).toBe(false);
  });
});
 
describe('createRowClickSelectionHandler', () => {
  it('selects an unselected clicked row and preserves previous selections', () => {
    const harness = createSelectionHarness(['row-1', 'row-2']);
 
    harness.handleRowSelection({ column: { field: 'name' }, row: { id: 'row-3' } });
 
    expect(harness.getSelectedRowKeys()).toEqual(['row-1', 'row-2', 'row-3']);
  });
 
  it('deselects the clicked row and preserves other selected rows', () => {
    const harness = createSelectionHarness(['row-1', 'row-2']);
 
    harness.handleRowSelection({ column: { field: 'name' }, row: { id: 'row-2' } });
 
    expect(harness.getSelectedRowKeys()).toEqual(['row-1']);
  });
 
  it('clears selection when the only selected row is clicked', () => {
    const harness = createSelectionHarness(['row-1']);
 
    harness.handleRowSelection({ column: { field: 'name' }, row: { id: 'row-1' } });
 
    expect(harness.getSelectedRowKeys()).toEqual([]);
  });
 
  it('leaves checkbox clicks to the existing checkbox behavior', () => {
    const harness = createSelectionHarness(['row-1']);
 
    harness.handleRowSelection({ column: { type: 'checkbox' }, row: { id: 'row-2' } });
 
    expect(harness.getSelectedRowKeys()).toEqual(['row-1']);
  });
 
  it('does not change selection when an action cell is clicked', () => {
    const harness = createSelectionHarness(['row-1']);
 
    harness.handleRowSelection({ column: { field: 'action', flag: 'action' }, row: { id: 'row-2' } });
 
    expect(harness.getSelectedRowKeys()).toEqual(['row-1']);
  });
});
 
describe('getDynamicListSelectionConfig', () => {
  it('does not add selection config when the list has no batch actions', () => {
    expect(getDynamicListSelectionConfig(false, false)).toEqual({});
  });
 
  it('keeps the existing checkbox config for flat lists', () => {
    expect(getDynamicListSelectionConfig(true, false)).toEqual({ rowSelection: { type: 'checkbox' } });
  });
 
  it('prevents tree checkbox cascades while keeping header select-all visible', () => {
    expect(getDynamicListSelectionConfig(true, true)).toEqual({
      checkboxConfig: { checkStrictly: true, showHeader: true },
      rowSelection: { type: 'checkbox' },
    });
  });
});