Skip to content

Commit

Permalink
Fix unstable quickPickUserOrderedValues test in windows (#4900)
Browse files Browse the repository at this point in the history
  • Loading branch information
julieg18 committed Oct 26, 2023
1 parent d65c8b6 commit 9dc008c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 7 deletions.
31 changes: 28 additions & 3 deletions extension/src/test/suite/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
commands,
ConfigurationChangeEvent,
EventEmitter,
QuickPick,
TextEditor,
Uri,
window,
Expand Down Expand Up @@ -38,6 +39,7 @@ import { Toast } from '../../vscode/toast'
import { GitExecutor } from '../../cli/git/executor'
import { DvcConfig } from '../../cli/dvc/config'
import { ExpShowOutput, PlotsOutput } from '../../cli/dvc/contract'
import { QuickPickItemWithValue } from '../../vscode/quickPick'

export const mockDisposable = {
dispose: stub()
Expand Down Expand Up @@ -85,23 +87,46 @@ export const selectQuickPickItem = async (number: number) => {
return commands.executeCommand('workbench.action.acceptSelectedQuickOpenItem')
}

const toggleQuickPickItem = async (number: number, itemsLength: number) => {
type NumberQuickPick = QuickPick<QuickPickItemWithValue<number>>

const getQuickPickSelectionEvent = (
quickPick: NumberQuickPick,
numberInd: number
) =>
new Promise(resolve =>
quickPick.onDidChangeSelection(items => {
const isItemSelected = items[numberInd]
if (isItemSelected) {
resolve(undefined)
}
})
)

const toggleQuickPickItem = async (
number: number,
numberInd: number,
itemsLength: number,
quickPick: NumberQuickPick
) => {
for (let itemInd = 1; itemInd <= itemsLength; itemInd++) {
await commands.executeCommand('workbench.action.quickOpenSelectNext')

if (itemInd === number) {
const selectionEvent = getQuickPickSelectionEvent(quickPick, numberInd)
await commands.executeCommand('workbench.action.quickPickManyToggle')
await selectionEvent
}
}
}

export const selectMultipleQuickPickItems = async (
numbers: number[],
itemsLength: number,
quickPick: NumberQuickPick,
acceptItems = true
) => {
for (const number of numbers) {
await toggleQuickPickItem(number, itemsLength)
for (const [ind, num] of numbers.entries()) {
await toggleQuickPickItem(num, ind, itemsLength, quickPick)
}
if (acceptItems) {
return commands.executeCommand(
Expand Down
17 changes: 13 additions & 4 deletions extension/src/test/suite/vscode/quickPick.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@ suite('Quick Pick Test Suite', () => {

describe('quickPickUserOrderedValues', () => {
it('should return selected values in the order that the user selected', async () => {
const quickPick = disposable.track(
window.createQuickPick<QuickPickItemWithValue<number>>()
)
stub(window, 'createQuickPick').returns(quickPick)
const items = [
{ label: 'J', value: 1 },
{ label: 'K', value: 2 },
Expand All @@ -156,12 +160,12 @@ suite('Quick Pick Test Suite', () => {
title: 'Select some values' as Title
})

await selectMultipleQuickPickItems([5, 2, 1], items.length)
await selectMultipleQuickPickItems([5, 2, 1], items.length, quickPick)

const result = await resultPromise

expect(result).to.deep.equal([5, 2, 1])
}).timeout(8000)
}).timeout(10000)

it('should return undefined if user cancels the quick pick', async () => {
const quickPick = disposable.track(
Expand Down Expand Up @@ -209,7 +213,12 @@ suite('Quick Pick Test Suite', () => {
maxSelectedItems
)

await selectMultipleQuickPickItems([5, 2, 1], items.length, false)
await selectMultipleQuickPickItems(
[5, 2, 1],
items.length,
quickPick,
false
)

expect(
quickPick.selectedItems,
Expand All @@ -219,6 +228,6 @@ suite('Quick Pick Test Suite', () => {
quickPick.items,
'all items which could be selected are hidden'
).to.have.lengthOf(maxSelectedItems)
}).timeout(8000)
}).timeout(10000)
})
})

0 comments on commit 9dc008c

Please sign in to comment.