Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions app/components/Compare/PackageSelector.vue
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,18 @@ function removePackage(name: string) {
}

function handleKeydown(e: KeyboardEvent) {
if (e.key === 'Enter' && inputValue.value.trim()) {
const inputValueTrim = inputValue.value.trim()
const hasMatchInPackages = filteredResults.value.find(result => {
return result.name === inputValueTrim
})

if (e.key === 'Enter' && inputValueTrim) {
e.preventDefault()
addPackage(inputValue.value.trim())
if (showNoDependencyOption.value) {
addPackage(NO_DEPENDENCY_ID)
} else if (hasMatchInPackages) {
addPackage(inputValueTrim)
}
}
}

Expand Down
22 changes: 19 additions & 3 deletions test/nuxt/components/compare/PackageSelector.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,28 @@ describe('PackageSelector', () => {
})

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

const emitted = component.emitted('update:modelValue')
expect(emitted).toBeTruthy()
expect(emitted![0]![0]).toEqual(['my-package'])
expect(emitted![0]![0]).toEqual(['lodash'])
})
Comment on lines 146 to +149
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Add an explicit guard before indexing emitted arrays.

Line 146–149 and Line 162–165 index into emitted without a concrete length check. This breaks the codebase’s strict type-safety guideline for array indexing.

Proposed fix
-      const emitted = component.emitted('update:modelValue')
-      expect(emitted).toBeTruthy()
-      expect(emitted![0]![0]).toEqual(['lodash'])
+      const emitted = component.emitted('update:modelValue') ?? []
+      expect(emitted.length).toBe(1)
+      expect(emitted[0]?.[0]).toEqual(['lodash'])
-      const emitted = component.emitted('update:modelValue')
-      expect(emitted).toBeTruthy()
-      expect(emitted![0]![0]).toEqual(['__no_dependency__'])
+      const emitted = component.emitted('update:modelValue') ?? []
+      expect(emitted.length).toBe(1)
+      expect(emitted[0]?.[0]).toEqual(['__no_dependency__'])

As per coding guidelines, "Ensure you write strictly type-safe code, for example by ensuring you always check when accessing an array value by index".

Also applies to: 162-165


it('adds "no dep" entry on Enter key', async () => {
const component = await mountSuspended(PackageSelector, {
props: {
modelValue: [],
},
})

const input = component.find('input')
await input.setValue('no dep')
await input.trigger('keydown', { key: 'Enter' })

const emitted = component.emitted('update:modelValue')
expect(emitted).toBeTruthy()
expect(emitted![0]![0]).toEqual(['__no_dependency__'])
})

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

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

// Input should be cleared
Expand Down
Loading