@@ -185,3 +185,43 @@ describe('hasSearchOperators', () => {
185185 expect ( hasSearchOperators ( { name : [ ] , keywords : [ ] } ) ) . toBe ( false )
186186 } )
187187} )
188+
189+ describe ( 'keyword clearing scenarios' , ( ) => {
190+ it ( 'returns keywords when kw: operator is present' , ( ) => {
191+ const result = parseSearchOperators ( 'test kw:react' )
192+ expect ( result . keywords ) . toEqual ( [ 'react' ] )
193+ expect ( result . text ) . toBe ( 'test' )
194+ } )
195+
196+ it ( 'returns undefined keywords when kw: operator is removed' , ( ) => {
197+ const result = parseSearchOperators ( 'test' )
198+ expect ( result . keywords ) . toBeUndefined ( )
199+ expect ( result . text ) . toBe ( 'test' )
200+ } )
201+
202+ it ( 'handles transition from keyword to no keyword' , ( ) => {
203+ // Simulate the state transition when user removes keyword from search
204+ const withKeyword = parseSearchOperators ( 'test kw:react' )
205+ expect ( withKeyword . keywords ) . toEqual ( [ 'react' ] )
206+
207+ const withoutKeyword = parseSearchOperators ( 'test' )
208+ expect ( withoutKeyword . keywords ) . toBeUndefined ( )
209+
210+ // This is what useStructuredFilters does in the watcher:
211+ // filters.value.keywords = [...(parsed.keywords ?? [])]
212+ const updatedKeywords = [ ...( withoutKeyword . keywords ?? [ ] ) ]
213+ expect ( updatedKeywords ) . toEqual ( [ ] )
214+ } )
215+
216+ it ( 'returns empty keywords array after nullish coalescing' , ( ) => {
217+ // Verify the exact logic used in useStructuredFilters watcher
218+ const testCases = [ '' , 'test' , 'some search query' , 'name:package' , 'desc:something' ]
219+
220+ for ( const query of testCases ) {
221+ const parsed = parseSearchOperators ( query )
222+ // This is the exact line from useStructuredFilters.ts:
223+ const keywords = [ ...( parsed . keywords ?? [ ] ) ]
224+ expect ( keywords ) . toEqual ( [ ] )
225+ }
226+ } )
227+ } )
0 commit comments