|
1 | 1 | import { describe, expect, it } from 'vitest' |
2 | | -import { hasSearchOperators, parseSearchOperators } from '~/composables/useStructuredFilters' |
| 2 | +import { |
| 3 | + hasSearchOperators, |
| 4 | + parseSearchOperators, |
| 5 | + removeKeywordFromQuery, |
| 6 | +} from '~/composables/useStructuredFilters' |
3 | 7 |
|
4 | 8 | describe('parseSearchOperators', () => { |
5 | 9 | describe('basic operator parsing', () => { |
@@ -247,3 +251,104 @@ describe('keyword clearing scenarios', () => { |
247 | 251 | } |
248 | 252 | }) |
249 | 253 | }) |
| 254 | + |
| 255 | +describe('removeKeywordFromQuery', () => { |
| 256 | + describe('standalone keyword removal', () => { |
| 257 | + it('removes standalone kw:value', () => { |
| 258 | + expect(removeKeywordFromQuery('test kw:react', 'react')).toBe('test') |
| 259 | + }) |
| 260 | + |
| 261 | + it('removes standalone keyword:value', () => { |
| 262 | + expect(removeKeywordFromQuery('test keyword:react', 'react')).toBe('test') |
| 263 | + }) |
| 264 | + |
| 265 | + it('removes keyword at start of query', () => { |
| 266 | + expect(removeKeywordFromQuery('kw:react test', 'react')).toBe('test') |
| 267 | + }) |
| 268 | + |
| 269 | + it('removes keyword when it is the entire query', () => { |
| 270 | + expect(removeKeywordFromQuery('kw:react', 'react')).toBe('') |
| 271 | + }) |
| 272 | + |
| 273 | + it('is case-insensitive', () => { |
| 274 | + expect(removeKeywordFromQuery('kw:React', 'react')).toBe('') |
| 275 | + expect(removeKeywordFromQuery('kw:react', 'React')).toBe('') |
| 276 | + expect(removeKeywordFromQuery('kw:REACT', 'react')).toBe('') |
| 277 | + }) |
| 278 | + }) |
| 279 | + |
| 280 | + describe('comma-separated keyword removal', () => { |
| 281 | + it('removes keyword from middle of comma list', () => { |
| 282 | + expect(removeKeywordFromQuery('kw:foo,bar,baz', 'bar')).toBe('kw:foo,baz') |
| 283 | + }) |
| 284 | + |
| 285 | + it('removes keyword from start of comma list', () => { |
| 286 | + expect(removeKeywordFromQuery('kw:foo,bar,baz', 'foo')).toBe('kw:bar,baz') |
| 287 | + }) |
| 288 | + |
| 289 | + it('removes keyword from end of comma list', () => { |
| 290 | + expect(removeKeywordFromQuery('kw:foo,bar,baz', 'baz')).toBe('kw:foo,bar') |
| 291 | + }) |
| 292 | + |
| 293 | + it('removes only keyword in comma list (drops operator)', () => { |
| 294 | + expect(removeKeywordFromQuery('test kw:react', 'react')).toBe('test') |
| 295 | + }) |
| 296 | + |
| 297 | + it('removes keyword from two-item list', () => { |
| 298 | + expect(removeKeywordFromQuery('kw:foo,bar', 'foo')).toBe('kw:bar') |
| 299 | + expect(removeKeywordFromQuery('kw:foo,bar', 'bar')).toBe('kw:foo') |
| 300 | + }) |
| 301 | + |
| 302 | + it('is case-insensitive within comma list', () => { |
| 303 | + expect(removeKeywordFromQuery('kw:Foo,Bar,Baz', 'bar')).toBe('kw:Foo,Baz') |
| 304 | + }) |
| 305 | + }) |
| 306 | + |
| 307 | + describe('duplicate keyword removal', () => { |
| 308 | + it('removes all occurrences across multiple operators', () => { |
| 309 | + expect(removeKeywordFromQuery('kw:react keyword:react', 'react')).toBe('') |
| 310 | + }) |
| 311 | + |
| 312 | + it('removes from both standalone and comma-separated', () => { |
| 313 | + expect(removeKeywordFromQuery('kw:react,vue keyword:react', 'react')).toBe('kw:vue') |
| 314 | + }) |
| 315 | + |
| 316 | + it('removes duplicate within same comma list', () => { |
| 317 | + expect(removeKeywordFromQuery('kw:react,vue,react', 'react')).toBe('kw:vue') |
| 318 | + }) |
| 319 | + }) |
| 320 | + |
| 321 | + describe('preserves unrelated content', () => { |
| 322 | + it('preserves other operators', () => { |
| 323 | + expect(removeKeywordFromQuery('name:foo kw:react desc:bar', 'react')).toBe( |
| 324 | + 'name:foo desc:bar', |
| 325 | + ) |
| 326 | + }) |
| 327 | + |
| 328 | + it('preserves free text', () => { |
| 329 | + expect(removeKeywordFromQuery('hello world kw:react', 'react')).toBe('hello world') |
| 330 | + }) |
| 331 | + |
| 332 | + it('does not remove substring matches', () => { |
| 333 | + expect(removeKeywordFromQuery('kw:react-hooks', 'react')).toBe('kw:react-hooks') |
| 334 | + }) |
| 335 | + |
| 336 | + it('does not remove keyword that is a prefix of another in comma list', () => { |
| 337 | + expect(removeKeywordFromQuery('kw:react,react-hooks', 'react')).toBe('kw:react-hooks') |
| 338 | + }) |
| 339 | + |
| 340 | + it('does not modify query when keyword is not present', () => { |
| 341 | + expect(removeKeywordFromQuery('kw:vue,angular test', 'react')).toBe('kw:vue,angular test') |
| 342 | + }) |
| 343 | + }) |
| 344 | + |
| 345 | + describe('whitespace handling', () => { |
| 346 | + it('collapses multiple spaces after removal', () => { |
| 347 | + expect(removeKeywordFromQuery('test kw:react more', 'react')).toBe('test more') |
| 348 | + }) |
| 349 | + |
| 350 | + it('trims leading and trailing spaces', () => { |
| 351 | + expect(removeKeywordFromQuery(' kw:react ', 'react')).toBe('') |
| 352 | + }) |
| 353 | + }) |
| 354 | +}) |
0 commit comments