|
1 | 1 | import { Filter } from '../query' |
2 | 2 | import { |
3 | 3 | encodeURIComponentPermissive, |
| 4 | + getSearchWithEnforcedSegment, |
4 | 5 | isSearchEntryDefined, |
5 | | - getRedirectTarget, |
| 6 | + maybeGetLatestReadableSearch, |
6 | 7 | parseFilter, |
7 | 8 | parseLabelsEntry, |
8 | 9 | parseSearch, |
@@ -206,57 +207,45 @@ describe(`${stringifySearch.name}`, () => { |
206 | 207 | }) |
207 | 208 | }) |
208 | 209 |
|
209 | | -describe(`${getRedirectTarget.name}`, () => { |
| 210 | +describe(`${maybeGetLatestReadableSearch.name}`, () => { |
210 | 211 | it.each([ |
211 | 212 | [''], |
212 | 213 | ['?auth=_Y6YOjUl2beUJF_XzG1hk&theme=light&background=%23ee00ee'], |
213 | 214 | ['?keybindHint=Escape&with_imported=true'], |
214 | 215 | ['?f=is,page,/blog/:category/:article-name&date=2024-10-10&period=day'], |
215 | 216 | ['?f=is,country,US&l=US,United%20States'] |
216 | | - ])('for modern search %p returns null', (search) => { |
217 | | - expect( |
218 | | - getRedirectTarget({ |
219 | | - pathname: '/example.com%2Fdeep%2Fpath', |
220 | | - search |
221 | | - } as Location) |
222 | | - ).toBeNull() |
| 217 | + ])('for modern search string %p returns null', (search) => { |
| 218 | + expect(maybeGetLatestReadableSearch(search)).toBeNull() |
223 | 219 | }) |
224 | 220 |
|
225 | | - it('returns updated URL for jsonurl style filters (v2), and running the updated value through the function again returns null (no redirect loop)', () => { |
226 | | - const pathname = '/' |
| 221 | + it('returns updated search string for jsonurl style filters (v2), and running the updated value through the function again returns null (no redirect loop)', () => { |
227 | 222 | const search = |
228 | 223 | '?filters=((is,exit_page,(/plausible.io)),(is,source,(Brave)),(is,city,(993800)))&labels=(993800:Johannesburg)' |
229 | 224 | const expectedUpdatedSearch = |
230 | 225 | '?f=is,exit_page,/plausible.io&f=is,source,Brave&f=is,city,993800&l=993800,Johannesburg&r=v2' |
231 | | - expect( |
232 | | - getRedirectTarget({ |
233 | | - pathname, |
234 | | - search |
235 | | - } as Location) |
236 | | - ).toEqual(`${pathname}${expectedUpdatedSearch}`) |
237 | | - expect( |
238 | | - getRedirectTarget({ |
239 | | - pathname, |
240 | | - search: expectedUpdatedSearch |
241 | | - } as Location) |
242 | | - ).toBeNull() |
| 226 | + expect(maybeGetLatestReadableSearch(search)).toEqual(expectedUpdatedSearch) |
| 227 | + expect(maybeGetLatestReadableSearch(expectedUpdatedSearch)).toBeNull() |
243 | 228 | }) |
244 | 229 |
|
245 | | - it('returns updated URL for page=... style filters (v1), and running the updated value through the function again returns null (no redirect loop)', () => { |
246 | | - const pathname = '/' |
| 230 | + it('returns updated search string for page=... style filters (v1), and running the updated value through the function again returns null (no redirect loop)', () => { |
247 | 231 | const search = '?page=/docs' |
248 | 232 | const expectedUpdatedSearch = '?f=is,page,/docs&r=v1' |
| 233 | + expect(maybeGetLatestReadableSearch(search)).toEqual(expectedUpdatedSearch) |
| 234 | + expect(maybeGetLatestReadableSearch(expectedUpdatedSearch)).toBeNull() |
| 235 | + }) |
| 236 | +}) |
| 237 | + |
| 238 | +describe(`${getSearchWithEnforcedSegment.name}`, () => { |
| 239 | + it('adds enforced segment appropriately, and running the updated value through the function again returns the same value', () => { |
| 240 | + const segment = { id: 100, name: 'Eastern Europe' } |
| 241 | + const search = '?auth=foo&embed=true' |
| 242 | + const expectedUpdatedSearch = |
| 243 | + '?f=is,segment,100&l=s-100,Eastern%20Europe&auth=foo&embed=true' |
| 244 | + expect(getSearchWithEnforcedSegment(search, segment)).toEqual( |
| 245 | + expectedUpdatedSearch |
| 246 | + ) |
249 | 247 | expect( |
250 | | - getRedirectTarget({ |
251 | | - pathname, |
252 | | - search |
253 | | - } as Location) |
254 | | - ).toEqual(`${pathname}${expectedUpdatedSearch}`) |
255 | | - expect( |
256 | | - getRedirectTarget({ |
257 | | - pathname, |
258 | | - search: expectedUpdatedSearch |
259 | | - } as Location) |
260 | | - ).toBeNull() |
| 248 | + getSearchWithEnforcedSegment(expectedUpdatedSearch, segment) |
| 249 | + ).toEqual(expectedUpdatedSearch) |
261 | 250 | }) |
262 | 251 | }) |
0 commit comments