-
-
Notifications
You must be signed in to change notification settings - Fork 424
Expand file tree
/
Copy pathuseStructuredFilters.spec.ts
More file actions
187 lines (157 loc) · 5.86 KB
/
useStructuredFilters.spec.ts
File metadata and controls
187 lines (157 loc) · 5.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import { describe, expect, it } from 'vitest'
import { hasSearchOperators, parseSearchOperators } from '~/composables/useStructuredFilters'
describe('parseSearchOperators', () => {
describe('basic operator parsing', () => {
it('parses name: operator', () => {
const result = parseSearchOperators('name:react')
expect(result).toEqual({ name: ['react'] })
})
it('parses desc: operator', () => {
const result = parseSearchOperators('desc:framework')
expect(result).toEqual({ description: ['framework'] })
})
it('parses description: operator (long form)', () => {
const result = parseSearchOperators('description:framework')
expect(result).toEqual({ description: ['framework'] })
})
it('parses kw: operator', () => {
const result = parseSearchOperators('kw:typescript')
expect(result).toEqual({ keywords: ['typescript'] })
})
it('parses keyword: operator (long form)', () => {
const result = parseSearchOperators('keyword:typescript')
expect(result).toEqual({ keywords: ['typescript'] })
})
})
describe('comma-separated values', () => {
it('parses multiple keywords with comma', () => {
const result = parseSearchOperators('kw:typescript,react,hooks')
expect(result).toEqual({ keywords: ['typescript', 'react', 'hooks'] })
})
it('parses multiple names with comma', () => {
const result = parseSearchOperators('name:react,vue,angular')
expect(result).toEqual({ name: ['react', 'vue', 'angular'] })
})
it('handles empty values between commas', () => {
const result = parseSearchOperators('kw:foo,,bar')
expect(result).toEqual({ keywords: ['foo', 'bar'] })
})
})
describe('multiple operators', () => {
it('parses name and kw operators together', () => {
const result = parseSearchOperators('name:react kw:typescript')
expect(result).toEqual({
name: ['react'],
keywords: ['typescript'],
})
})
it('parses all three operator types', () => {
const result = parseSearchOperators('name:react desc:framework kw:typescript')
expect(result).toEqual({
name: ['react'],
description: ['framework'],
keywords: ['typescript'],
})
})
it('merges multiple instances of same operator', () => {
const result = parseSearchOperators('kw:react kw:typescript')
expect(result).toEqual({
keywords: ['react', 'typescript'],
})
})
})
describe('remaining text', () => {
it('captures text without operators', () => {
const result = parseSearchOperators('some search text')
expect(result).toEqual({ text: 'some search text' })
})
it('captures remaining text after operators', () => {
const result = parseSearchOperators('name:react some text')
expect(result).toEqual({
name: ['react'],
text: 'some text',
})
})
it('captures remaining text before operators', () => {
const result = parseSearchOperators('some text name:react')
expect(result).toEqual({
name: ['react'],
text: 'some text',
})
})
it('captures text mixed with operators', () => {
const result = parseSearchOperators('hello name:react world kw:hooks foo')
expect(result).toEqual({
name: ['react'],
keywords: ['hooks'],
text: 'hello world foo',
})
})
it('collapses multiple spaces in remaining text', () => {
const result = parseSearchOperators('name:react lots of spaces')
expect(result).toEqual({
name: ['react'],
text: 'lots of spaces',
})
})
})
describe('case insensitivity', () => {
it('handles uppercase operator names', () => {
const result = parseSearchOperators('NAME:react')
expect(result).toEqual({ name: ['react'] })
})
it('handles mixed case operator names', () => {
const result = parseSearchOperators('NaMe:react KW:typescript')
expect(result).toEqual({
name: ['react'],
keywords: ['typescript'],
})
})
})
describe('edge cases', () => {
it('returns empty object for empty string', () => {
const result = parseSearchOperators('')
expect(result).toEqual({})
})
it('returns empty object for whitespace only', () => {
const result = parseSearchOperators(' ')
expect(result).toEqual({})
})
it('handles operator with no value', () => {
// "name:" followed by space - the regex won't match empty values
const result = parseSearchOperators('name: react')
expect(result).toEqual({ text: 'name: react' })
})
it('handles special characters in values', () => {
const result = parseSearchOperators('name:@scope/package')
expect(result).toEqual({ name: ['@scope/package'] })
})
it('handles hyphenated values', () => {
const result = parseSearchOperators('kw:gatsby-plugin')
expect(result).toEqual({ keywords: ['gatsby-plugin'] })
})
})
})
describe('hasSearchOperators', () => {
it('returns true when name is present', () => {
expect(hasSearchOperators({ name: ['react'] })).toBe(true)
})
it('returns true when description is present', () => {
expect(hasSearchOperators({ description: ['framework'] })).toBe(true)
})
it('returns true when keywords is present', () => {
expect(hasSearchOperators({ keywords: ['typescript'] })).toBe(true)
})
it('returns false when only text is present', () => {
expect(hasSearchOperators({ text: 'search query' })).toBe(false)
})
it('returns false for empty object', () => {
expect(hasSearchOperators({})).toBe(false)
})
it('returns true when operators and text are present', () => {
expect(hasSearchOperators({ name: ['react'], text: 'query' })).toBe(true)
})
it('returns false for empty arrays', () => {
expect(hasSearchOperators({ name: [], keywords: [] })).toBe(false)
})
})