Skip to content

Commit

Permalink
refactor(test): avoid test failure
Browse files Browse the repository at this point in the history
  • Loading branch information
chemzqm committed Jan 14, 2022
1 parent 01e5dcd commit 05a1d23
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 30 deletions.
12 changes: 8 additions & 4 deletions src/__tests__/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,13 @@ export class Helper extends EventEmitter {
for (let arg of args) {
let event = arg[0]
this.emit(event, arg.slice(1))
// if (event == 'put') {
// let arr = arg.slice(1).map(o => o[0])
// console.log(arr.join(''))
// }
if (event == 'put') {
let arr = arg.slice(1).map(o => o[0])
let line = arr.join('').trim()
if (line.length > 3) {
// console.log(line)
}
}
}
}
})
Expand Down Expand Up @@ -106,6 +109,7 @@ export class Helper extends EventEmitter {
await this.nvim.command('stopinsert')
await this.nvim.call('feedkeys', [String.fromCharCode(27), 'in'])
}
await this.nvim.call('coc#float#close_all')
await this.nvim.command('silent! %bwipeout!')
await this.wait(80)
}
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/list/commandTask.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ describe('Command task', () => {
it('should not show error', async () => {
disposables.push(manager.registerList(new ErrorTask(nvim)))
await manager.start(['error'])
await helper.wait(200)
await helper.wait(500)
let res = await helper.getCmdline()
expect(res).toMatch('NOT_EXISTS ENOENT')
})
Expand Down
46 changes: 26 additions & 20 deletions src/__tests__/list/mappings.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,13 @@ afterAll(async () => {
await helper.shutdown()
})

beforeEach(async () => {
let m = await nvim.mode
if (m.blocking) {
console.error('nvim blocking', m)
}
})

afterEach(async () => {
manager.reset()
await helper.reset()
Expand Down Expand Up @@ -388,20 +395,20 @@ describe('list insert mappings', () => {
it('should move cursor by <left> and <right>', async () => {
await manager.start(['location'])
await manager.session.ui.ready
await nvim.eval('feedkeys("f", "in")')
await helper.wait(10)
await nvim.eval('feedkeys("\\<left>", "in")')
await nvim.input('f')
await helper.wait(10)
await nvim.eval('feedkeys("\\<left>", "in")')
await nvim.input('<Left>')
await helper.wait(10)
await nvim.eval('feedkeys("a", "in")')
await nvim.input('<Left>')
await helper.wait(10)
await nvim.eval('feedkeys("\\<right>", "in")')
await nvim.input('a')
await helper.wait(10)
await nvim.eval('feedkeys("\\<right>", "in")')
await nvim.input('<Right>')
await helper.wait(10)
await nvim.eval('feedkeys("c", "in")')
await nvim.input('<Right>')
await helper.wait(10)
await nvim.input('c')
await helper.wait(100)
let input = manager.prompt.input
expect(input).toBe('afc')
})
Expand Down Expand Up @@ -754,50 +761,49 @@ describe('User mappings', () => {
})
await manager.start(['location'])
await manager.session.ui.ready
await nvim.eval('feedkeys("\\<C-r>", "in")')
await nvim.input('<C-r>')
await helper.wait(30)
expect(manager.isActivated).toBe(true)
await nvim.eval('feedkeys("\\<C-a>", "in")')
await nvim.input('<C-a>')
await helper.wait(30)
expect(manager.session?.ui.selectedItems.length).toBe(locations.length)
await nvim.eval('feedkeys("\\<C-s>", "in")')
await nvim.input('<C-s>')
await helper.wait(30)
expect(manager.session?.listOptions.matcher).toBe('strict')
await nvim.eval('feedkeys("\\<C-n>", "in")')
await nvim.input('<C-n>')
await helper.wait(30)
let item = await manager.session?.ui.item
expect(item.label).toMatch(locations[1].text)
await nvim.eval('feedkeys("\\<C-p>", "in")')
await nvim.input('<C-p>')
await helper.wait(30)
item = await manager.session?.ui.item
expect(item.label).toMatch(locations[0].text)
await nvim.eval('feedkeys("\\<C-x>", "in")')
await nvim.input('<C-x>')
await helper.wait(30)
expect(manager.isActivated).toBe(false)
await manager.start(['location'])
await manager.session.ui.ready
await nvim.eval('feedkeys("\\<C-l>", "in")')
await helper.wait(50)
await nvim.input('<C-l>')
await helper.wait(30)
let res = await nvim.call('coc#prompt#activated')
expect(res).toBe(0)
await manager.session.hide()
await manager.start(['location'])
await manager.session.ui.ready
await nvim.eval('feedkeys("?", "in")')
await nvim.input('?')
await helper.wait(30)
await nvim.input('<CR>')
await manager.cancel()
await manager.start(['location'])
await manager.session.ui.ready
await nvim.eval('feedkeys("\\<C-d>", "in")')
await nvim.input('<C-d>')
await helper.wait(100)
expect(manager.isActivated).toBe(false)
await manager.start(['location'])
await manager.session.ui.ready
await nvim.eval('feedkeys("\\<C-b>", "in")')
await nvim.input('<C-b>')
await helper.wait(100)
expect(manager.isActivated).toBe(true)
await nvim.call('coc#prompt#stop_prompt', ['list'])
}, 20000)

it('should execute prompt mappings', async () => {
Expand Down
7 changes: 5 additions & 2 deletions src/__tests__/modules/completion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,7 @@ describe('completion TextChangedP', () => {
})

it('should fix cursor position with plain text snippet on additionalTextEdits', async () => {
await helper.createDocument()
let provider: CompletionItemProvider = {
provideCompletionItems: async (): Promise<CompletionItem[]> => [{
label: 'if',
Expand All @@ -448,8 +449,10 @@ describe('completion TextChangedP', () => {
disposables.push(languages.registerCompletionItemProvider('edits', 'edit', null, provider))
await nvim.input('iif')
await helper.waitPopup()
await helper.selectCompleteItem(0)
await helper.wait(200)
let items = await helper.getItems()
let idx = items.findIndex(o => o.word == 'do' && o.menu == '[edit]')
await helper.selectCompleteItem(idx)
await helper.wait(500)
let line = await nvim.line
let [, lnum, col] = await nvim.call('getcurpos')
expect(line).toBe('bar do')
Expand Down
4 changes: 3 additions & 1 deletion src/__tests__/modules/picker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ describe('Picker key mappings', () => {
await helper.createDocument()
picker = new Picker(nvim, { title: 'title', items })
let winid = await picker.show({ pickerButtons: true })
await helper.waitFloat()
expect(winid).toBeDefined()
await nvim.input('j')
await helper.wait(100)
Expand All @@ -125,11 +126,12 @@ describe('Picker key mappings', () => {
await helper.createDocument()
picker = new Picker(nvim, { title: 'title', items })
let winid = await picker.show({ pickerButtons: true })
await helper.waitFloat()
expect(winid).toBeDefined()
let fn = jest.fn()
picker.onDidClose(fn)
await nvim.input('<space>')
await helper.wait(200)
await helper.wait(300)
await nvim.command('redraw')
let lines = await nvim.call('getbufline', [picker.buffer.id, 1])
expect(lines[0]).toMatch('[x]')
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/tree/treeView.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,7 @@ describe('TreeView', () => {
await helper.wait(50)
await nvim.call('cursor', [2, 3])
await nvim.input('<tab>')
await helper.wait(50)
await helper.waitFloat()
await nvim.input('<cr>')
await helper.wait(50)
expect(called).toBe(true)
Expand Down
3 changes: 2 additions & 1 deletion src/__tests__/vimrc
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ set shiftwidth=2
set updatetime=300
set expandtab
set noshowmode
set shortmess=at
set shortmess=aFtW
set noruler

let s:dir = expand('<sfile>:h')
let s:root = expand('<sfile>:h:h:h')
Expand Down

0 comments on commit 05a1d23

Please sign in to comment.