11import { beforeEach , describe , expect , it , vi } from 'vitest'
22import { ref } from 'vue'
3+ import { flushPromises } from '@vue/test-utils'
34import { mountSuspended } from '@nuxt/test-utils/runtime'
45import PackageSelector from '~/components/Compare/PackageSelector.vue'
56
7+ // Mock checkPackageExists
8+ vi . mock ( '~/utils/package-name' , ( ) => ( {
9+ checkPackageExists : vi . fn ( ) ,
10+ } ) )
11+
12+ import { checkPackageExists } from '~/utils/package-name'
13+ const mockCheckPackageExists = vi . mocked ( checkPackageExists )
14+
615// Mock $fetch for useNpmSearch
716const mockFetch = vi . fn ( )
817vi . stubGlobal ( '$fetch' , mockFetch )
918
1019describe ( 'PackageSelector' , ( ) => {
1120 beforeEach ( ( ) => {
1221 mockFetch . mockReset ( )
22+ mockCheckPackageExists . mockReset ( )
1323 mockFetch . mockResolvedValue ( {
1424 objects : [
1525 { package : { name : 'lodash' , description : 'Lodash modular utilities' } } ,
@@ -18,6 +28,7 @@ describe('PackageSelector', () => {
1828 total : 2 ,
1929 time : new Date ( ) . toISOString ( ) ,
2030 } )
31+ mockCheckPackageExists . mockResolvedValue ( true )
2132 } )
2233
2334 describe ( 'selected packages display' , ( ) => {
@@ -132,7 +143,9 @@ describe('PackageSelector', () => {
132143 } )
133144
134145 describe ( 'adding packages' , ( ) => {
135- it ( 'adds package on Enter key' , async ( ) => {
146+ it ( 'adds package on Enter key when package exists' , async ( ) => {
147+ mockCheckPackageExists . mockResolvedValue ( true )
148+
136149 const component = await mountSuspended ( PackageSelector , {
137150 props : {
138151 modelValue : [ ] ,
@@ -142,6 +155,7 @@ describe('PackageSelector', () => {
142155 const input = component . find ( 'input' )
143156 await input . setValue ( 'lodash' )
144157 await input . trigger ( 'keydown' , { key : 'Enter' } )
158+ await flushPromises ( )
145159
146160 const emitted = component . emitted ( 'update:modelValue' )
147161 expect ( emitted ) . toBeTruthy ( )
@@ -165,6 +179,8 @@ describe('PackageSelector', () => {
165179 } )
166180
167181 it ( 'clears input after adding package' , async ( ) => {
182+ mockCheckPackageExists . mockResolvedValue ( true )
183+
168184 const component = await mountSuspended ( PackageSelector , {
169185 props : {
170186 modelValue : [ ] ,
@@ -174,11 +190,31 @@ describe('PackageSelector', () => {
174190 const input = component . find ( 'input' )
175191 await input . setValue ( 'lodash' )
176192 await input . trigger ( 'keydown' , { key : 'Enter' } )
193+ await flushPromises ( )
177194
178195 // Input should be cleared
179196 expect ( ( input . element as HTMLInputElement ) . value ) . toBe ( '' )
180197 } )
181198
199+ it ( 'does not add non-existent packages' , async ( ) => {
200+ mockCheckPackageExists . mockResolvedValue ( false )
201+
202+ const component = await mountSuspended ( PackageSelector , {
203+ props : {
204+ modelValue : [ ] ,
205+ } ,
206+ } )
207+
208+ const input = component . find ( 'input' )
209+ await input . setValue ( 'nonexistent-pkg' )
210+ await input . trigger ( 'keydown' , { key : 'Enter' } )
211+ await flushPromises ( )
212+
213+ const emitted = component . emitted ( 'update:modelValue' )
214+ expect ( emitted ) . toBeFalsy ( )
215+ expect ( component . find ( '[role="alert"]' ) . exists ( ) ) . toBe ( true )
216+ } )
217+
182218 it ( 'does not add duplicate packages' , async ( ) => {
183219 const component = await mountSuspended ( PackageSelector , {
184220 props : {
0 commit comments