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,13 +155,16 @@ describe('PackageSelector', () => {
142155 const input = component . find ( 'input' )
143156 await input . setValue ( 'my-package' )
144157 await input . trigger ( 'keydown' , { key : 'Enter' } )
158+ await flushPromises ( )
145159
146160 const emitted = component . emitted ( 'update:modelValue' )
147161 expect ( emitted ) . toBeTruthy ( )
148162 expect ( emitted ! [ 0 ] ! [ 0 ] ) . toEqual ( [ 'my-package' ] )
149163 } )
150164
151165 it ( 'clears input after adding package' , async ( ) => {
166+ mockCheckPackageExists . mockResolvedValue ( true )
167+
152168 const component = await mountSuspended ( PackageSelector , {
153169 props : {
154170 modelValue : [ ] ,
@@ -158,11 +174,31 @@ describe('PackageSelector', () => {
158174 const input = component . find ( 'input' )
159175 await input . setValue ( 'my-package' )
160176 await input . trigger ( 'keydown' , { key : 'Enter' } )
177+ await flushPromises ( )
161178
162179 // Input should be cleared
163180 expect ( ( input . element as HTMLInputElement ) . value ) . toBe ( '' )
164181 } )
165182
183+ it ( 'does not add non-existent packages' , async ( ) => {
184+ mockCheckPackageExists . mockResolvedValue ( false )
185+
186+ const component = await mountSuspended ( PackageSelector , {
187+ props : {
188+ modelValue : [ ] ,
189+ } ,
190+ } )
191+
192+ const input = component . find ( 'input' )
193+ await input . setValue ( 'nonexistent-pkg' )
194+ await input . trigger ( 'keydown' , { key : 'Enter' } )
195+ await flushPromises ( )
196+
197+ const emitted = component . emitted ( 'update:modelValue' )
198+ expect ( emitted ) . toBeFalsy ( )
199+ expect ( component . find ( '[role="alert"]' ) . exists ( ) ) . toBe ( true )
200+ } )
201+
166202 it ( 'does not add duplicate packages' , async ( ) => {
167203 const component = await mountSuspended ( PackageSelector , {
168204 props : {
0 commit comments