forked from pbugnion/jupyterlab-sql
-
Notifications
You must be signed in to change notification settings - Fork 2
/
events.test.ts
223 lines (198 loc) · 7.82 KB
/
events.test.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
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
import { framePromise } from '@jupyterlab/testutils';
import { DataGrid, DataModel } from '@lumino/datagrid';
import {
addMouseEventListener,
GridMouseEvent
} from '../../../../src/services/dataGridExtensions';
namespace Fixtures {
export function grid(): DataGrid {
const model = new TestDataModel();
const grid = new DataGrid();
grid.dataModel = model;
return grid;
}
export function clickEvent(args: MouseEventInit): MouseEvent {
return new MouseEvent('click', args);
}
export function contextmenuEvent(args: MouseEventInit): MouseEvent {
return new MouseEvent('contextmenu', args);
}
export function dblClickEvent(args: MouseEventInit): MouseEvent {
return new MouseEvent('dblclick', args);
}
class TestDataModel extends DataModel {
rowCount(region: DataModel.RowRegion): number {
return region === 'body' ? 100 : 1;
}
columnCount(region: DataModel.ColumnRegion): number {
return region === 'body' ? 10 : 1;
}
data(region: DataModel.CellRegion, row: number, column: number): any {
if (region === 'row-header') {
return `R: ${row}, ${column}`;
}
if (region === 'column-header') {
return `C: ${row}, ${column}`;
}
if (region === 'corner-header') {
return `N: ${row}, ${column}`;
}
return `(${row}, ${column})`;
}
}
}
describe('addMouseEventListener', () => {
const testEvent = (grid: DataGrid, event: MouseEvent): GridMouseEvent => {
const mockListener = jest.fn();
addMouseEventListener('click', grid, mockListener);
grid.node.dispatchEvent(event);
expect(mockListener.mock.calls.length).toBe(1);
const [args] = mockListener.mock.calls;
expect(args.length).toBe(1);
return args[0];
};
it('register an event listener', () => {
const grid = Fixtures.grid();
const event = Fixtures.clickEvent({ clientX: 10, clientY: 5 });
testEvent(grid, event);
});
it.each([
[5, { section: 'column-header', index: null }],
[20 * 101 + 1, { section: 'outside', index: null }],
[20 * 101, { section: 'outside', index: null }], // boundary with last row
[20 * 50 + 2, { section: 'row', index: 49 }],
[20 * 50, { section: 'row', index: 49 }], // boundary between two rows
[20, { section: 'row', index: 0 }] // boundary with header
])('row position %#: clientY: %i', (clientY: any, expected: any) => {
const grid = Fixtures.grid();
const event = Fixtures.clickEvent({ clientX: 10, clientY });
const { row } = testEvent(grid, event);
expect(row).toEqual(expected);
});
it('row position should be correct when scrolled', () => {
const grid = Fixtures.grid();
grid.scrollBy(0, 100);
const event = Fixtures.clickEvent({ clientX: 10, clientY: 32 });
const { row } = testEvent(grid, event);
expect(row).toEqual({ section: 'row', index: 5 });
});
it('scroll should not affect header', () => {
const grid = Fixtures.grid();
grid.scrollBy(0, 100);
const event = Fixtures.clickEvent({ clientX: 10, clientY: 5 });
const { row } = testEvent(grid, event);
expect(row).toEqual({ section: 'column-header', index: null });
});
it('take row resizes into account', async () => {
const grid = Fixtures.grid();
grid.resizeRow('body', 2, 200);
await framePromise();
const event = Fixtures.clickEvent({
clientX: 5,
clientY: 3 * 20 + 200 + 2
});
const { row } = testEvent(grid, event);
expect(row).toEqual({ section: 'row', index: 3 });
});
it.each([
[5, { section: 'row-header', index: null }],
[64 * 11 + 1, { section: 'outside', index: null }],
[64, { section: 'column', index: 0 }], // on boundary with header
[64 + 2, { section: 'column', index: 0 }],
[64 * 5 + 2, { section: 'column', index: 4 }],
[64 * 5, { section: 'column', index: 4 }], // boundary between two columns
[64 * 10 + 2, { section: 'column', index: 9 }],
[64 * 11 - 2, { section: 'column', index: 9 }],
[64 * 11, { section: 'outside', index: null }] // on boundary of last column
])('column position %#: clientX: %i', (clientX: any, expected: any) => {
const grid = Fixtures.grid();
const event = Fixtures.clickEvent({ clientX, clientY: 100 });
const { column } = testEvent(grid, event);
expect(column).toEqual(expected);
});
it('column position should be correct when scrolled', () => {
const grid = Fixtures.grid();
grid.scrollBy(100, 0);
const event = Fixtures.clickEvent({ clientX: 66, clientY: 5 });
const { column } = testEvent(grid, event);
expect(column).toEqual({ section: 'column', index: 1 });
});
it('should not affect the header', () => {
const grid = Fixtures.grid();
grid.scrollBy(100, 0);
const event = Fixtures.clickEvent({ clientX: 2, clientY: 5 });
const { column } = testEvent(grid, event);
expect(column).toEqual({ section: 'row-header', index: null });
});
it('take column resizes into account', async () => {
const grid = Fixtures.grid();
grid.resizeColumn('body', 2, 200);
await framePromise();
const event = Fixtures.clickEvent({
clientX: 3 * 64 + 200 + 2,
clientY: 5
});
const { column } = testEvent(grid, event);
expect(column).toEqual({ section: 'column', index: 3 });
});
it('pass through the raw event', () => {
const grid = Fixtures.grid();
grid.resizeColumn('body', 2, 200);
const event = Fixtures.clickEvent({ clientX: 100, clientY: 60 });
const { rawEvent } = testEvent(grid, event);
expect(rawEvent).toEqual(event);
});
it('remove the event listener', () => {
const grid = Fixtures.grid();
const mockListener = jest.fn();
const disposable = addMouseEventListener('click', grid, mockListener);
disposable.dispose();
const event = Fixtures.clickEvent({ clientX: 10, clientY: 5 });
grid.node.dispatchEvent(event);
expect(mockListener.mock.calls.length).toBe(0);
});
it('support adding contextmenu listeners', () => {
const grid = Fixtures.grid();
const event = Fixtures.contextmenuEvent({ clientX: 10, clientY: 5 });
const mockListener = jest.fn();
addMouseEventListener('contextmenu', grid, mockListener);
grid.node.dispatchEvent(event);
expect(mockListener.mock.calls.length).toBe(1);
const [args] = mockListener.mock.calls;
expect(args.length).toBe(1);
const { row, column } = args[0];
expect(row).toEqual({ section: 'column-header', index: null });
expect(column).toEqual({ section: 'row-header', index: null });
});
it('support removing contextmenu listeners', () => {
const grid = Fixtures.grid();
const event = Fixtures.contextmenuEvent({ clientX: 10, clientY: 5 });
const mockListener = jest.fn();
const disposable = addMouseEventListener('contextmenu', grid, mockListener);
disposable.dispose();
grid.node.dispatchEvent(event);
expect(mockListener.mock.calls.length).toBe(0);
});
it('support adding dblclick event listeners', () => {
const grid = Fixtures.grid();
const event = Fixtures.dblClickEvent({ clientX: 10, clientY: 5 });
const mockListener = jest.fn();
addMouseEventListener('dblclick', grid, mockListener);
grid.node.dispatchEvent(event);
expect(mockListener.mock.calls.length).toBe(1);
const [args] = mockListener.mock.calls;
expect(args.length).toBe(1);
const { row, column } = args[0];
expect(row).toEqual({ section: 'column-header', index: null });
expect(column).toEqual({ section: 'row-header', index: null });
});
it('support removing dblclick event listeners', () => {
const grid = Fixtures.grid();
const event = Fixtures.dblClickEvent({ clientX: 10, clientY: 5 });
const mockListener = jest.fn();
const disposable = addMouseEventListener('dblclick', grid, mockListener);
disposable.dispose();
grid.node.dispatchEvent(event);
expect(mockListener.mock.calls.length).toBe(0);
});
});