刘光辉
10 小时以前 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
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
import { describe, expect, it } from 'vitest';
 
import {
  createInputTableRowClickHandler,
  getInputTableCellInteractionAttrs,
  isInputTableCellEditable,
  isInputTableInteractiveTarget,
  toggleInputTableRowSelection,
} from '../helpers/rowSelection';
 
interface TestRow {
  id?: string;
}
 
const row1: TestRow = { id: 'row-1' };
const row2: TestRow = { id: 'row-2' };
 
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({});
  });
});