Skip to content

Commit 5a3ca59

Browse files
committed
Type check for Buffer.test.ts
1 parent 3700a9b commit 5a3ca59

File tree

1 file changed

+73
-52
lines changed

1 file changed

+73
-52
lines changed

packages/neovim/src/api/Buffer.test.ts

Lines changed: 73 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/* eslint-env jest */
22
import * as testUtil from '../testUtil';
3+
import type { Buffer } from './Buffer';
34

45
function wait(ms: number): Promise<void> {
56
return new Promise(resolve => {
@@ -14,14 +15,16 @@ describe('Buffer API', () => {
1415

1516
// utility to allow each test to be run in its
1617
// own buffer
17-
function withBuffer(lines, test) {
18+
function withBuffer(
19+
lines: string[],
20+
test: (buffer: Buffer) => Promise<void>
21+
) {
1822
return async () => {
1923
await nvim.command('new!');
2024

2125
const buffer = await nvim.buffer;
2226

2327
if (lines) {
24-
await buffer;
2528
await buffer.replace(lines, 0);
2629
}
2730

@@ -151,7 +154,7 @@ describe('Buffer API', () => {
151154
it(
152155
'removes last 2 lines',
153156
withBuffer(['test', 'bar', 'foo', 'a', 'b'], async buffer => {
154-
buffer.remove(-3, -1);
157+
buffer.remove(-3, -1, true);
155158
expect(await buffer.lines).toEqual(['test', 'bar', 'foo']);
156159
})
157160
);
@@ -178,7 +181,8 @@ describe('Buffer API', () => {
178181
it('returns -1 for byte offset of unloaded buffer', async () => {
179182
await nvim.command('new');
180183
await nvim.command('bunload!');
181-
expect(await nvim.buffer.getOffset(0)).toEqual(-1);
184+
const buffer = await nvim.buffer;
185+
expect(await buffer.getOffset(0)).toEqual(-1);
182186
});
183187

184188
it(
@@ -199,7 +203,7 @@ describe('Buffer API', () => {
199203
it(
200204
'can clear the buffer',
201205
withBuffer(['foo'], async buffer => {
202-
buffer.remove(0, -1);
206+
buffer.remove(0, -1, true);
203207
// One empty line
204208
expect(await buffer.length).toEqual(1);
205209
expect(await buffer.lines).toEqual(['']);
@@ -216,7 +220,7 @@ describe('Buffer API', () => {
216220
expect(await buffer.getOption('copyindent')).toBe(false);
217221

218222
// Restore option
219-
buffer.setOption('copyindent', initial);
223+
buffer.setOption('copyindent', initial!);
220224
expect(await buffer.getOption('copyindent')).toBe(initial);
221225
})
222226
);
@@ -253,7 +257,7 @@ describe('Buffer API', () => {
253257
'sets virtual text and clears namespace',
254258
withBuffer(['test'], async buffer => {
255259
const ns = await nvim.createNamespace();
256-
await buffer.setVirtualText(ns, 0, [['annotation']]);
260+
await buffer.setVirtualText(ns, 0, [['annotation', '']]);
257261
await buffer.clearNamespace({ nsId: ns });
258262
})
259263
);
@@ -263,35 +267,39 @@ describe('Buffer API', () => {
263267

264268
describe('Chainable API calls', () => {
265269
it('sets/gets the current buffer name using api chaining', async () => {
266-
nvim.buffer.name = 'goodbye.txt';
270+
const buffer = await nvim.buffer;
271+
buffer.name = 'goodbye.txt';
267272
expect(await nvim.buffer.name).toMatch('goodbye.txt');
268273
});
269274

270275
it('can chain calls from Base class i.e. getOption', async () => {
271-
const initial = await nvim.buffer.getOption('copyindent');
272-
nvim.buffer.setOption('copyindent', true);
273-
expect(await nvim.buffer.getOption('copyindent')).toBe(true);
274-
nvim.buffer.setOption('copyindent', false);
275-
expect(await nvim.buffer.getOption('copyindent')).toBe(false);
276+
const buffer = await nvim.buffer;
277+
const initial = await buffer.getOption('copyindent');
278+
buffer.setOption('copyindent', true);
279+
expect(await buffer.getOption('copyindent')).toBe(true);
280+
buffer.setOption('copyindent', false);
281+
expect(await buffer.getOption('copyindent')).toBe(false);
276282

277283
// Restore option
278-
nvim.buffer.setOption('copyindent', initial);
279-
expect(await nvim.buffer.getOption('copyindent')).toBe(initial);
284+
buffer.setOption('copyindent', initial!);
285+
expect(await buffer.getOption('copyindent')).toBe(initial);
280286
});
281287

282288
it('sets current buffer name to "bar.js" using api chaining', async () => {
283-
await (nvim.buffer.name = 'bar.js');
284-
expect(await nvim.buffer.name).toMatch('bar.js');
289+
const buffer = await nvim.buffer;
290+
buffer.name = 'bar.js';
291+
expect(await buffer.name).toMatch('bar.js');
285292

286-
await (nvim.buffer.name = 'test2.js');
287-
expect(await nvim.buffer.name).toMatch('test2.js');
293+
buffer.name = 'test2.js';
294+
expect(await buffer.name).toMatch('test2.js');
288295
});
289296

290297
it(
291298
'can replace first line of nvim.buffer with a string',
292299
withBuffer([], async () => {
293-
await nvim.buffer.replace('test', 0);
294-
expect(await nvim.buffer.lines).toEqual(['test']);
300+
const buffer = await nvim.buffer;
301+
await buffer.replace('test', 0);
302+
expect(await buffer.lines).toEqual(['test']);
295303
})
296304
);
297305

@@ -300,9 +308,10 @@ describe('Buffer API', () => {
300308
withBuffer(
301309
['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'],
302310
async () => {
303-
await nvim.buffer.replace(['a', 'b', 'c'], 2);
311+
const buffer = await nvim.buffer;
312+
await buffer.replace(['a', 'b', 'c'], 2);
304313

305-
expect(await nvim.buffer.lines).toEqual([
314+
expect(await buffer.lines).toEqual([
306315
'0',
307316
'1',
308317
'a',
@@ -321,24 +330,27 @@ describe('Buffer API', () => {
321330
it(
322331
'can insert lines at beginning of buffer',
323332
withBuffer(['test'], async () => {
324-
await nvim.buffer.insert(['test', 'foo'], 0);
325-
expect(await nvim.buffer.lines).toEqual(['test', 'foo', 'test']);
333+
const buffer = await nvim.buffer;
334+
await buffer.insert(['test', 'foo'], 0);
335+
expect(await buffer.lines).toEqual(['test', 'foo', 'test']);
326336
})
327337
);
328338

329339
it(
330340
'can replace nvim.buffer starting at line 1',
331341
withBuffer(['test', 'foo'], async () => {
332-
await nvim.buffer.replace(['bar', 'bar', 'bar'], 1);
333-
expect(await nvim.buffer.lines).toEqual(['test', 'bar', 'bar', 'bar']);
342+
const buffer = await nvim.buffer;
343+
await buffer.replace(['bar', 'bar', 'bar'], 1);
344+
expect(await buffer.lines).toEqual(['test', 'bar', 'bar', 'bar']);
334345
})
335346
);
336347

337348
it(
338349
'inserts line at index 2',
339350
withBuffer(['test', 'bar', 'bar', 'bar'], async () => {
340-
await nvim.buffer.insert(['foo'], 2);
341-
expect(await nvim.buffer.lines).toEqual([
351+
const buffer = await nvim.buffer;
352+
await buffer.insert(['foo'], 2);
353+
expect(await buffer.lines).toEqual([
342354
'test',
343355
'bar',
344356
'foo',
@@ -351,16 +363,18 @@ describe('Buffer API', () => {
351363
it(
352364
'removes last 2 lines',
353365
withBuffer(['test', 'bar', 'foo', 'a', 'b'], async () => {
354-
await nvim.buffer.remove(-3, -1);
355-
expect(await nvim.buffer.lines).toEqual(['test', 'bar', 'foo']);
366+
const buffer = await nvim.buffer;
367+
await buffer.remove(-3, -1, true);
368+
expect(await buffer.lines).toEqual(['test', 'bar', 'foo']);
356369
})
357370
);
358371

359372
it(
360373
'append lines to end of buffer',
361374
withBuffer(['test', 'bar', 'foo'], async () => {
362-
await nvim.buffer.append(['test', 'test']);
363-
expect(await nvim.buffer.lines).toEqual([
375+
const buffer = await nvim.buffer;
376+
await buffer.append(['test', 'test']);
377+
expect(await buffer.lines).toEqual([
364378
'test',
365379
'bar',
366380
'foo',
@@ -373,10 +387,11 @@ describe('Buffer API', () => {
373387
it(
374388
'can clear the buffer',
375389
withBuffer(['foo'], async () => {
376-
await nvim.buffer.remove(0, -1);
390+
const buffer = await nvim.buffer;
391+
await buffer.remove(0, -1, true);
377392
// One empty line
378-
expect(await nvim.buffer.length).toEqual(1);
379-
expect(await nvim.buffer.lines).toEqual(['']);
393+
expect(await buffer.length).toEqual(1);
394+
expect(await buffer.lines).toEqual(['']);
380395
})
381396
);
382397
});
@@ -394,17 +409,17 @@ describe('Buffer event updates', () => {
394409
});
395410

396411
beforeEach(async () => {
397-
await nvim.buffer.remove(0, -1);
412+
await (await nvim.buffer).remove(0, -1, true);
398413
});
399414

400415
it('can listen and unlisten', async () => {
401416
const buffer = await nvim.buffer;
402417
const mock = jest.fn();
403418
const unlisten = buffer.listen('lines', mock);
404-
await nvim.buffer.insert(['bar'], 1);
419+
await buffer.insert(['bar'], 1);
405420
expect(mock).toHaveBeenCalledTimes(1);
406421
unlisten();
407-
await nvim.buffer.insert(['bar'], 1);
422+
await buffer.insert(['bar'], 1);
408423
expect(mock).toHaveBeenCalledTimes(1);
409424
});
410425

@@ -415,7 +430,7 @@ describe('Buffer event updates', () => {
415430
await wait(10);
416431
const mock = jest.fn();
417432
unlisten = buffer.listen('lines', mock);
418-
await nvim.buffer.insert(['bar'], 1);
433+
await buffer.insert(['bar'], 1);
419434
expect(mock).toHaveBeenCalledTimes(1);
420435
unlisten();
421436
});
@@ -437,18 +452,18 @@ describe('Buffer event updates', () => {
437452
const mock = jest.fn();
438453
buffer.listen('lines', mock);
439454
buffer.listen('lines', mock);
440-
await nvim.buffer.insert(['bar'], 1);
455+
await buffer.insert(['bar'], 1);
441456
expect(mock).toHaveBeenCalledTimes(1);
442457
});
443458

444459
it('can use `buffer.unlisten` to unlisten', async () => {
445460
const buffer = await nvim.buffer;
446461
const mock = jest.fn();
447462
buffer.listen('lines', mock);
448-
await nvim.buffer.insert(['bar'], 1);
463+
await buffer.insert(['bar'], 1);
449464
expect(mock).toHaveBeenCalledTimes(1);
450465
buffer.unlisten('lines', mock);
451-
await nvim.buffer.insert(['bar'], 1);
466+
await buffer.insert(['bar'], 1);
452467
expect(mock).toHaveBeenCalledTimes(1);
453468
});
454469

@@ -457,10 +472,16 @@ describe('Buffer event updates', () => {
457472
const bufferName = await buffer.name;
458473
await buffer.insert(['test', 'foo'], 0);
459474

460-
const promise = new Promise(resolve => {
475+
const promise = new Promise<void>(resolve => {
461476
const unlisten = buffer.listen(
462477
'lines',
463-
async (currentBuffer, tick, start, end, data) => {
478+
async (
479+
currentBuffer: Buffer,
480+
tick: number,
481+
start: number,
482+
end: number,
483+
data: string[]
484+
) => {
464485
expect(await currentBuffer.name).toBe(bufferName);
465486
expect(start).toBe(1);
466487
expect(end).toBe(1);
@@ -471,7 +492,7 @@ describe('Buffer event updates', () => {
471492
);
472493
});
473494

474-
await nvim.buffer.insert(['bar'], 1);
495+
await buffer.insert(['bar'], 1);
475496
await promise;
476497
});
477498

@@ -484,12 +505,12 @@ describe('Buffer event updates', () => {
484505
buffers[0].listen('lines', foo);
485506
buffers[1].listen('lines', bar);
486507

487-
await nvim.buffer.insert(['bar'], 1);
508+
await (await nvim.buffer).insert(['bar'], 1);
488509
expect(foo).toHaveBeenCalledTimes(0);
489510
expect(bar).toHaveBeenCalledTimes(1);
490511
await nvim.command('q!');
491512

492-
await nvim.buffer.insert(['foo'], 0);
513+
await (await nvim.buffer).insert(['foo'], 0);
493514
expect(foo).toHaveBeenCalledTimes(1);
494515
expect(bar).toHaveBeenCalledTimes(1);
495516

@@ -507,13 +528,13 @@ describe('Buffer event updates', () => {
507528
const unlisten1 = buffer.listen('lines', foo);
508529
const unlisten2 = buffer.listen('lines', bar);
509530

510-
await nvim.buffer.insert(['bar'], 1);
531+
await buffer.insert(['bar'], 1);
511532
expect(foo).toHaveBeenCalledTimes(1);
512533
expect(bar).toHaveBeenCalledTimes(1);
513534

514535
unlisten2();
515536

516-
await nvim.buffer.insert(['foo'], 0);
537+
await buffer.insert(['foo'], 0);
517538
expect(foo).toHaveBeenCalledTimes(2);
518539
expect(bar).toHaveBeenCalledTimes(1);
519540

@@ -531,13 +552,13 @@ describe('Buffer event updates', () => {
531552
const unlisten1 = buffer.listen('lines', foo);
532553
const unlisten2 = buffer.listen('changedtick', bar);
533554

534-
await nvim.buffer.insert(['bar'], 1);
555+
await buffer.insert(['bar'], 1);
535556
expect(foo).toHaveBeenCalledTimes(1);
536557
expect(bar).toHaveBeenCalledTimes(1);
537558

538559
unlisten2();
539560

540-
await nvim.buffer.insert(['foo'], 0);
561+
await buffer.insert(['foo'], 0);
541562
expect(foo).toHaveBeenCalledTimes(2);
542563
expect(bar).toHaveBeenCalledTimes(1);
543564

0 commit comments

Comments
 (0)