Skip to content

Commit 50ed3fb

Browse files
fix(ui): prevent compare with non-existing package (#1207)
1 parent eb19ccd commit 50ed3fb

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
lines changed

app/components/Compare/PackageSelector.vue

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,18 @@ function removePackage(name: string) {
7777
}
7878
7979
function handleKeydown(e: KeyboardEvent) {
80-
if (e.key === 'Enter' && inputValue.value.trim()) {
80+
const inputValueTrim = inputValue.value.trim()
81+
const hasMatchInPackages = filteredResults.value.find(result => {
82+
return result.name === inputValueTrim
83+
})
84+
85+
if (e.key === 'Enter' && inputValueTrim) {
8186
e.preventDefault()
82-
addPackage(inputValue.value.trim())
87+
if (showNoDependencyOption.value) {
88+
addPackage(NO_DEPENDENCY_ID)
89+
} else if (hasMatchInPackages) {
90+
addPackage(inputValueTrim)
91+
}
8392
}
8493
}
8594

test/nuxt/components/compare/PackageSelector.spec.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,12 +140,28 @@ describe('PackageSelector', () => {
140140
})
141141

142142
const input = component.find('input')
143-
await input.setValue('my-package')
143+
await input.setValue('lodash')
144144
await input.trigger('keydown', { key: 'Enter' })
145145

146146
const emitted = component.emitted('update:modelValue')
147147
expect(emitted).toBeTruthy()
148-
expect(emitted![0]![0]).toEqual(['my-package'])
148+
expect(emitted![0]![0]).toEqual(['lodash'])
149+
})
150+
151+
it('adds "no dep" entry on Enter key', async () => {
152+
const component = await mountSuspended(PackageSelector, {
153+
props: {
154+
modelValue: [],
155+
},
156+
})
157+
158+
const input = component.find('input')
159+
await input.setValue('no dep')
160+
await input.trigger('keydown', { key: 'Enter' })
161+
162+
const emitted = component.emitted('update:modelValue')
163+
expect(emitted).toBeTruthy()
164+
expect(emitted![0]![0]).toEqual(['__no_dependency__'])
149165
})
150166

151167
it('clears input after adding package', async () => {
@@ -156,7 +172,7 @@ describe('PackageSelector', () => {
156172
})
157173

158174
const input = component.find('input')
159-
await input.setValue('my-package')
175+
await input.setValue('lodash')
160176
await input.trigger('keydown', { key: 'Enter' })
161177

162178
// Input should be cleared

0 commit comments

Comments
 (0)