[READ-ONLY] a fast, modern browser for the npm registry
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

fix(ui): prevent compare with non-existing package (#1207)

authored by

Matteo Gabriele and committed by
GitHub
50ed3fb2 eb19ccdd

+30 -5
+11 -2
app/components/Compare/PackageSelector.vue
··· 77 77 } 78 78 79 79 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) { 81 86 e.preventDefault() 82 - addPackage(inputValue.value.trim()) 87 + if (showNoDependencyOption.value) { 88 + addPackage(NO_DEPENDENCY_ID) 89 + } else if (hasMatchInPackages) { 90 + addPackage(inputValueTrim) 91 + } 83 92 } 84 93 } 85 94
+19 -3
test/nuxt/components/compare/PackageSelector.spec.ts
··· 140 140 }) 141 141 142 142 const input = component.find('input') 143 - await input.setValue('my-package') 143 + await input.setValue('lodash') 144 144 await input.trigger('keydown', { key: 'Enter' }) 145 145 146 146 const emitted = component.emitted('update:modelValue') 147 147 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__']) 149 165 }) 150 166 151 167 it('clears input after adding package', async () => { ··· 156 172 }) 157 173 158 174 const input = component.find('input') 159 - await input.setValue('my-package') 175 + await input.setValue('lodash') 160 176 await input.trigger('keydown', { key: 'Enter' }) 161 177 162 178 // Input should be cleared