刘光辉
5 小时以前 cf11e63b659afcc9792ab3e395f1b45d844431a8
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
import { describe, expect, it } from 'vitest';
 
import {
  createInputTableRowClickHandler,
  getInputTableCellInteractionAttrs,
  getInputTableSelectedRows,
  isInputTableCellEditable,
  isInputTableInteractiveTarget,
  toggleInputTableRowSelection,
} from '../helpers/rowSelection';
 
interface TestRow {
  id?: string;
}
 
const row1: TestRow = { id: 'row-1' };
const row2: TestRow = { id: 'row-2' };
 
describe('getInputTableSelectedRows', () => {
  const getRowKey = (row: TestRow) => row.id;
 
  it('returns full-table indexes in row order regardless of selection order or page', () => {
    const rows = Array.from({ length: 25 }, (_, index) => ({ id: `row-${index}` }));
    expect(getInputTableSelectedRows(rows, ['row-23', 'row-1', 'row-12'], getRowKey)).toEqual([
      [1, rows[1]],
      [12, rows[12]],
      [23, rows[23]],
    ]);
  });
 
  it('reads the latest edited data and returns an independent deep snapshot', () => {
    const row = { id: 'row-1', detail: { quantity: 1 } };
    const keys = ['row-1'];
    row.detail.quantity = 2;
    const selected = getInputTableSelectedRows([row], keys, getRowKey);
    expect(selected).toEqual([[0, { id: 'row-1', detail: { quantity: 2 } }]]);
 
    selected[0]![1].detail.quantity = 99;
    expect(row.detail.quantity).toBe(2);
    row.detail.quantity = 3;
    expect(getInputTableSelectedRows([row], keys, getRowKey)[0]![1].detail.quantity).toBe(3);
  });
 
  it('ignores removed rows and recalculates indexes after deletion', () => {
    const rows = [row1, row2];
    const keys = ['row-1', 'row-2'];
    rows.splice(0, 1);
    expect(getInputTableSelectedRows(rows, keys, getRowKey)).toEqual([[0, row2]]);
    expect(getInputTableSelectedRows([], keys, getRowKey)).toEqual([]);
  });
 
  it('follows the existing selection toggle and clearing keys', () => {
    const rows = [row1, row2];
    const selection = toggleInputTableRowSelection(['row-1', 'row-2'], rows, row1, getRowKey);
    expect(getInputTableSelectedRows(rows, selection.selectedRowKeys, getRowKey)).toEqual([[1, row2]]);
    expect(getInputTableSelectedRows(rows, [], getRowKey)).toEqual([]);
  });
 
  it('uses the current row for a retained key after data replacement', () => {
    const currentRow = { id: 'row-1', quantity: 5 };
    expect(getInputTableSelectedRows([currentRow], ['row-1', 'missing', 'row-1'], getRowKey)).toEqual([[0, currentRow]]);
  });
});
 
describe('toggleInputTableRowSelection', () => {
  it('adds an unselected row without replacing other selections', () => {
    expect(toggleInputTableRowSelection(['row-1'], [row1], row2, (row) => row.id as string)).toEqual({
      selectedRowKeys: ['row-1', 'row-2'],
      selectedRows: [row1, row2],
    });
  });
 
  it('removes only the clicked row when it is already selected', () => {
    expect(toggleInputTableRowSelection(['row-1', 'row-2'], [row1, row2], row1, (row) => row.id as string)).toEqual({
      selectedRowKeys: ['row-2'],
      selectedRows: [row2],
    });
  });
 
  it('does not mutate the existing selection arrays', () => {
    const selectedRowKeys = ['row-1'];
    const selectedRows = [row1];
 
    toggleInputTableRowSelection(selectedRowKeys, selectedRows, row2, (row) => row.id as string);
 
    expect(selectedRowKeys).toEqual(['row-1']);
    expect(selectedRows).toEqual([row1]);
  });
});
 
describe('createInputTableRowClickHandler', () => {
  function createHarness(options: { enabled?: boolean; readonly?: boolean } = {}) {
    let selection = { selectedRowKeys: ['row-1'], selectedRows: [row1] };
    let updateCount = 0;
    const customRow = createInputTableRowClickHandler<TestRow, string>({
      getRowKey: (row) => row.id,
      getSelectedRowKeys: () => selection.selectedRowKeys,
      getSelectedRows: () => selection.selectedRows,
      isEnabled: () => options.enabled !== false,
      isRowReadonly: () => options.readonly === true,
      updateSelection: (nextSelection) => {
        selection = nextSelection;
        updateCount++;
      },
    });
 
    return {
      click: (row: TestRow, target: EventTarget | null) => customRow(row).onClick({ target }),
      getSelection: () => selection,
      getUpdateCount: () => updateCount,
    };
  }
 
  it('publishes a toggled selection for an ordinary row click', () => {
    const harness = createHarness();
    const target = document.createElement('td');
 
    harness.click(row2, target);
 
    expect(harness.getSelection()).toEqual({
      selectedRowKeys: ['row-1', 'row-2'],
      selectedRows: [row1, row2],
    });
    expect(harness.getUpdateCount()).toBe(1);
  });
 
  it('ignores clicks from interactive row content', () => {
    const harness = createHarness();
    const control = document.createElement('div');
    const target = document.createElement('input');
    control.dataset.inputTableInteractive = '';
    control.append(target);
 
    harness.click(row2, target);
 
    expect(harness.getSelection()).toEqual({ selectedRowKeys: ['row-1'], selectedRows: [row1] });
    expect(harness.getUpdateCount()).toBe(0);
  });
 
  it.each([
    ['batch selection is disabled', { enabled: false }],
    ['the clicked row is read-only', { readonly: true }],
  ])('ignores the click when %s', (_label, options) => {
    const harness = createHarness(options);
 
    harness.click(row2, document.createElement('td'));
 
    expect(harness.getUpdateCount()).toBe(0);
  });
 
  it('ignores rows without a key', () => {
    const harness = createHarness();
 
    harness.click({}, document.createElement('td'));
 
    expect(harness.getUpdateCount()).toBe(0);
  });
});
 
describe('isInputTableInteractiveTarget', () => {
  it.each(['a', 'button'])('recognizes row action %s elements', (tagName) => {
    expect(isInputTableInteractiveTarget(document.createElement(tagName))).toBe(true);
  });
 
  it('recognizes nested content inside a field marked editable', () => {
    const control = document.createElement('div');
    const target = document.createElement('span');
    control.dataset.inputTableInteractive = '';
    control.append(target);
 
    expect(isInputTableInteractiveTarget(target)).toBe(true);
  });
 
  it.each(['input', 'select', 'textarea'])('allows row selection from unmarked %s content', (tagName) => {
    expect(isInputTableInteractiveTarget(document.createElement(tagName))).toBe(false);
  });
 
  it('recognizes the complete Ant Design selection cell', () => {
    const selectionCell = document.createElement('td');
    const target = document.createElement('span');
    selectionCell.className = 'ant-table-selection-column';
    selectionCell.append(target);
 
    expect(isInputTableInteractiveTarget(target)).toBe(true);
  });
 
  it('allows clicks on ordinary table-cell content', () => {
    const cell = document.createElement('td');
    const target = document.createElement('span');
    cell.append(target);
 
    expect(isInputTableInteractiveTarget(target)).toBe(false);
    expect(isInputTableInteractiveTarget(null)).toBe(false);
  });
});
 
describe('isInputTableCellEditable', () => {
  it('marks an enabled generated field as editable', () => {
    expect(isInputTableCellEditable({})).toBe(true);
  });
 
  it.each([
    ['disabled', { disabled: true }],
    ['readonly', { readonly: true }],
    ['detailed', { detailed: true }],
  ])('treats a %s generated field as row-clickable content', (_label, controlProps) => {
    expect(isInputTableCellEditable(controlProps)).toBe(false);
  });
});
 
describe('getInputTableCellInteractionAttrs', () => {
  it('marks an enabled field as interactive', () => {
    expect(getInputTableCellInteractionAttrs({})).toEqual({ 'data-input-table-interactive': '' });
  });
 
  it('makes a disabled field pass pointer events through to the table row', () => {
    expect(getInputTableCellInteractionAttrs({ disabled: true })).toEqual({ 'data-input-table-click-through': '' });
  });
 
  it('keeps a disabled detail field interactive', () => {
    expect(getInputTableCellInteractionAttrs({ disabled: true, disabledButClickable: true })).toEqual({ 'data-input-table-interactive': '' });
  });
 
  it.each([
    ['readonly', { readonly: true }],
    ['detailed', { detailed: true }],
  ])('leaves a %s field unmarked so its click bubbles normally', (_label, controlProps) => {
    expect(getInputTableCellInteractionAttrs(controlProps)).toEqual({});
  });
});