diff options
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/fixtures/foo.js | 1 | ||||
| -rw-r--r-- | tests/formatting.test.js | 120 | ||||
| -rw-r--r-- | tests/vimrc | 35 |
3 files changed, 156 insertions, 0 deletions
diff --git a/tests/fixtures/foo.js b/tests/fixtures/foo.js new file mode 100644 index 0000000..82955ab --- /dev/null +++ b/tests/fixtures/foo.js @@ -0,0 +1 @@ +const a = () => { return 2; }; diff --git a/tests/formatting.test.js b/tests/formatting.test.js new file mode 100644 index 0000000..6514fec --- /dev/null +++ b/tests/formatting.test.js @@ -0,0 +1,120 @@ +const fs = require('fs'); +const path = require('path'); +const HeadlessRemoteClient = require('vim-driver/dist/HeadlessRemoteClient'); +const Server = require('vim-driver/dist/Server'); + +const HOST = '127.0.0.1'; +const PORT = 1337; +const FIXTURES_DIR = `${__dirname}/fixtures`; + +let server; +let remote; + +jest.setTimeout(10000); + +const getBufferContents = async remote => + (await remote.call('getline', [1, '$'])).join('\n'); + +const sleep = ms => new Promise(resolve => setTimeout(resolve, ms)); +const waitUntil = (condition, timeout = 2000) => { + return new Promise(resolve => { + let isTimedOut = false; + let timeoutId = null; + + const check = () => { + const promise = condition(); + promise.then(result => { + if (!isTimedOut && result === true) { + clearTimeout(timeoutId); + resolve(); + } else if (!isTimedOut) { + check(); + } + }); + }; + + timeoutId = setTimeout(() => { + isTimedOut = true; + resolve(); + }, timeout); + }); +}; + +const assertFormatting = (file) => { + const filename = path.basename(file); + test(`Prettier formats ${filename} file with :Prettier command`, async () => { + await remote.edit(`${FIXTURES_DIR}/${file}`); + + const lines = await getBufferContents(remote); + + // run sync formatting + await remote.execute('Prettier'); + + const updatedLines = await getBufferContents(remote); + + // we now check that we have indeed formatted the code + expect(updatedLines).not.toBe(lines); + }); + + test(`Prettier formats ${filename} file with :PrettierAsync command`, async () => { + await remote.edit(`${FIXTURES_DIR}/${file}`); + + const lines = await getBufferContents(remote); + + // run async formatting + await remote.execute('PrettierAsync'); + + const unmodifiedLines = await getBufferContents(remote); + + // async should not happen immediatly so content should still remain the same + expect(unmodifiedLines).toBe(lines); + + // we now will wait until prettier has finally updated the content async + await waitUntil(async () => (await getBufferContents(remote)) !== lines); + + const updatedLines = await getBufferContents(remote); + + // we now check that we have indeed formatted the code + expect(lines).not.toBe(updatedLines); + }); +}; + +beforeAll(async () => { + server = new Server(); + await server.listen(HOST, PORT); +}); + +afterAll(async () => { + await server.close(); +}); + +// should ensure that we cache original fixture contents and +// restore it on the afterEach +beforeEach(async () => { + remote = new HeadlessRemoteClient({host: HOST, port: PORT}); + await remote.connect(server); +}); + +afterEach(async () => { + if (remote.isConnected()) { + try { + const filename = await remote.call('expand', ['%:p']); + + if (filename) { + // restore the file + await remote.execute('earlier 1d | noautocmd | write'); + } + } catch (e) { + } finally { + await remote.close(); + } + } +}); + +//test('PrettierVersion returns pluggin version', async () => { +// const result = await remote.execute('PrettierVersion'); +// expect(result).toMatchSnapshot(); +//}); + +// run formatting tests in all fixtures +fs.readdirSync(FIXTURES_DIR).forEach(file => assertFormatting(file)); diff --git a/tests/vimrc b/tests/vimrc new file mode 100644 index 0000000..b33e61e --- /dev/null +++ b/tests/vimrc @@ -0,0 +1,35 @@ +" vint: -ProhibitSetNoCompatible + +source /rtp.vim + +" Load builtin plugins +" We need this because run_vim.sh sets -i NONE +if has('win32') + set runtimepath=$VIM/vimfiles,$VIMRUNTIME,$VIM/vimfiles/after,C:\vader,C:\testplugin +else + set runtimepath=/home/vim,$VIM/vimfiles,$VIMRUNTIME,$VIM/vimfiles/after,/testplugin,/vader +endif + +" The following is just an example +filetype plugin indent on +syntax on + +if !has('win32') + set shell=/bin/sh + set shellcmdflag=-c +endif + +set nocompatible +set tabstop=4 +set softtabstop=4 +set shiftwidth=4 +set expandtab +set backspace=2 +set nofoldenable +set foldmethod=syntax +set foldlevelstart=10 +set foldnestmax=10 +set ttimeoutlen=0 + +" The encoding must be explicitly set for tests for Windows. +execute 'set encoding=utf-8' |
