Skip to content

Commit

Permalink
build(tests): type check packages/neovim/*.test.ts #411
Browse files Browse the repository at this point in the history
  • Loading branch information
gjf7 authored Oct 10, 2024
1 parent a334f3d commit a7d6c78
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 28 deletions.
10 changes: 6 additions & 4 deletions packages/neovim/src/api/Neovim.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint-env jest */
import * as path from 'node:path';
import assert from 'node:assert';
import * as testUtil from '../testUtil';

describe('Neovim API', () => {
Expand All @@ -25,7 +26,7 @@ describe('Neovim API', () => {

// switch buffers
[nvim.buffer] = buffers;
expect(await nvim.buffer.name).toMatch(/hello\.txt$/);
expect(await (await nvim.buffer).name).toMatch(/hello\.txt$/);
});

it('can list runtimepaths', async () => {
Expand Down Expand Up @@ -218,7 +219,7 @@ describe('Neovim API', () => {
const numWindows = (await nvim.windows).length;
const buffer = await nvim.createBuffer(false, false);
expect(await nvim.buffers).toHaveLength(numBuffers + 1);

assert(typeof buffer !== 'number');
const floatingWindow = await nvim.openWindow(buffer, true, {
relative: 'editor',
row: 5,
Expand All @@ -227,22 +228,23 @@ describe('Neovim API', () => {
height: 50,
});
expect(await nvim.windows).toHaveLength(numWindows + 1);

assert(typeof floatingWindow !== 'number');
await nvim.windowClose(floatingWindow, true);
expect(await nvim.windows).toHaveLength(numWindows);
});

it('resizes a window', async () => {
const numWindows = (await nvim.windows).length;
const buffer = await nvim.createBuffer(false, false);

assert(typeof buffer !== 'number');
const floatingWindow = await nvim.openWindow(buffer, true, {
relative: 'editor',
row: 5,
col: 5,
width: 10,
height: 10,
});
assert(typeof floatingWindow !== 'number');
expect(await nvim.windows).toHaveLength(numWindows + 1);
expect(await floatingWindow.height).toBe(10);
expect(await floatingWindow.width).toBe(10);
Expand Down
6 changes: 3 additions & 3 deletions packages/neovim/src/api/Neovim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ export class Neovim extends BaseApi {
/**
* Sets the current buffer
*/
set buffer(buffer: AsyncBuffer) {
set buffer(buffer: AsyncBuffer | Buffer) {
this.request(`${this.prefix}set_current_buf`, [buffer]);
}

Expand Down Expand Up @@ -232,7 +232,7 @@ export class Neovim extends BaseApi {
/**
* Sets the current tabpage
*/
set tabpage(tabpage: AsyncTabpage) {
set tabpage(tabpage: AsyncTabpage | Tabpage) {
this.request(`${this.prefix}set_current_tabpage`, [tabpage]);
}

Expand All @@ -259,7 +259,7 @@ export class Neovim extends BaseApi {
*
* @param win Window handle
*/
set window(win: AsyncWindow) {
set window(win: AsyncWindow | Window) {
if (win instanceof Window) this.setWindow(win);
else win.then(win => this.setWindow(win));
}
Expand Down
7 changes: 4 additions & 3 deletions packages/neovim/src/api/Tabpage.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint-env jest */
import * as testUtil from '../testUtil';
import type { Tabpage } from './Tabpage';

describe('Tabpage API', () => {
let nvim: ReturnType<typeof testUtil.startNvim>[1];
Expand All @@ -18,7 +19,7 @@ describe('Tabpage API', () => {
});

describe('Normal API calls', () => {
let tabpage;
let tabpage: Tabpage;

beforeEach(async () => {
tabpage = await nvim.tabpage;
Expand Down Expand Up @@ -68,10 +69,10 @@ describe('Tabpage API', () => {
it('logs an error when calling `getOption`', () => {
const spy = jest.spyOn(tabpage.logger, 'error');

tabpage.getOption('option');
tabpage.getOption();
expect(spy.mock.calls.length).toBe(1);

tabpage.setOption('option', 'value');
tabpage.setOption();
expect(spy.mock.calls.length).toBe(2);
spy.mockClear();
});
Expand Down
11 changes: 7 additions & 4 deletions packages/neovim/src/api/Window.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/* eslint-env jest */
import assert from 'node:assert';
import * as testUtil from '../testUtil';
import type { Window } from './Window';

describe('Window API', () => {
let nvim: ReturnType<typeof testUtil.startNvim>[1];
Expand All @@ -24,7 +26,7 @@ describe('Window API', () => {
});

describe('Normal API calls', () => {
let win;
let win: Window;

beforeEach(async () => {
win = await nvim.window;
Expand All @@ -51,7 +53,7 @@ describe('Window API', () => {
});

it('has same cursor position after appending a line to buffer', async () => {
await win.buffer.append(['test']);
await (await win.buffer).append(['test']);
expect(await win.buffer.lines).toEqual(['', 'test']);
expect(await win.cursor).toEqual([1, 0]);
});
Expand Down Expand Up @@ -94,7 +96,7 @@ describe('Window API', () => {
});

it('has the right window positions in display cells', async () => {
let windows;
let windows: Awaited<typeof nvim.windows>;
nvim.command('vsplit');

// XXX If we re-use `win` without a new call to `nvim.window`,
Expand Down Expand Up @@ -126,6 +128,7 @@ describe('Window API', () => {
expect(await win.getOption('list')).toBe(true);
win.setOption('list', false);
expect(await win.getOption('list')).toBe(false);
assert(list !== undefined);
// Restore option
win.setOption('list', list);
expect(await win.getOption('list')).toBe(list);
Expand Down Expand Up @@ -167,7 +170,7 @@ describe('Window API', () => {
});

it.skip('gets current lines in buffer', async () => {
expect(await nvim.window.buffer.lines).toEqual(['test']);
expect(await (await nvim.window.buffer).lines).toEqual(['test']);
});
});
});
3 changes: 2 additions & 1 deletion packages/neovim/src/host/NvimPlugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ describe('NvimPlugin', () => {
() => {},
getFakeNvimClient()
);
// @ts-expect-error Intentionally passing empty array for command arguments.
plugin.registerCommand('MyCommand', [], {});
expect(Object.keys(plugin.commands)).toHaveLength(0);
});
Expand Down Expand Up @@ -175,7 +176,7 @@ describe('NvimPlugin', () => {
() => {},
getFakeNvimClient()
);
const fn = arg => arg;
const fn = (arg: any) => arg;

plugin.registerAutocmd('BufWritePre', fn, { pattern: '*', sync: true });
plugin.registerCommand('MyCommand', fn, { sync: true });
Expand Down
28 changes: 17 additions & 11 deletions packages/neovim/src/plugin/plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { command as Command } from './command';
import { autocmd as Autocmd } from './autocmd';
import { getFakeNvimClient } from '../testUtil';

const instantiateOrRun = (Fn, ...args) => {
const instantiateOrRun = (Fn: ReturnType<typeof Plugin>, ...args: any[]) => {
try {
return new Fn(...args);
} catch (err) {
Expand Down Expand Up @@ -36,13 +36,16 @@ describe('Plugin class decorator', () => {
});

it('decorates class methods', () => {
class MyClass {}
MyClass.prototype.testF = () => {};
MyClass.prototype.testC = () => {};
MyClass.prototype.testA = () => {};
class MyClass {
testF() {}

testC() {}

testA() {}
}

// This is how (closeish) babel applies decorators
FunctionDecorator('TestF', { eval: 'test', range: 'test' })(
FunctionDecorator('TestF', { eval: 'test', range: [1, 10] })(
MyClass.prototype,
'testF'
);
Expand Down Expand Up @@ -85,15 +88,18 @@ describe('Plugin class decorator', () => {
expect(pluginObject.registerFunction).toHaveBeenCalledWith(
'TestF',
[instance, MyClass.prototype.testF],
{ sync: false, eval: 'test', range: 'test' }
{ sync: false, eval: 'test', range: [1, 10] }
);
});

it('generates specs from decorated methods', () => {
class MyClass {}
MyClass.prototype.testF = () => {};
MyClass.prototype.testC = () => {};
MyClass.prototype.testA = () => {};
class MyClass {
testF() {}

testC() {}

testA() {}
}

// This is how (closeish) babel applies decorators
FunctionDecorator('TestF')(MyClass.prototype, 'testF');
Expand Down
4 changes: 2 additions & 2 deletions packages/neovim/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
"compilerOptions": {
"outDir": "./lib"
},
"include": ["src", "**/api/Buffer.test.ts"],
"exclude": ["node_modules", "__tests__", "**/*.test.ts", "examples", "lib"]
"include": ["src"],
"exclude": ["node_modules", "examples", "lib"]
}

0 comments on commit a7d6c78

Please sign in to comment.