test(utils): add unit tests for parseQuery function

- Introduce comprehensive tests for the parseQuery utility.
- Validate handling of single and multi-value query parameters.
- Ensure empty query strings return an empty object.
- Confirm repeated keys are grouped into arrays.

Signed-off-by: Max P. <Mail@MPassarello.de>
This commit is contained in:
2025-05-07 12:28:51 +02:00
parent cc734fa7b1
commit 94525fce52
2 changed files with 79 additions and 0 deletions

View File

@@ -0,0 +1,49 @@
import { assertEquals } from 'https://deno.land/std/assert/mod.ts';
import { parseQuery } from '../parseQuery.ts';
Deno.test('parseQuery: single-value parameters are parsed as strings', () => {
const url = new URL('http://localhost?foo=bar&limit=10');
const result = parseQuery(url.searchParams);
assertEquals(result, {
foo: 'bar',
limit: '10',
});
});
Deno.test('parseQuery: multi-value parameters are parsed as string arrays', () => {
const url = new URL('http://localhost?tag=ts&tag=deno&tag=web');
const result = parseQuery(url.searchParams);
assertEquals(result, {
tag: ['ts', 'deno', 'web'],
});
});
Deno.test('parseQuery: mixed single and multi-value parameters', () => {
const url = new URL(
'http://localhost?sort=asc&filter=active&filter=pending',
);
const result = parseQuery(url.searchParams);
assertEquals(result, {
sort: 'asc',
filter: ['active', 'pending'],
});
});
Deno.test('parseQuery: empty query string returns empty object', () => {
const url = new URL('http://localhost');
const result = parseQuery(url.searchParams);
assertEquals(result, {});
});
Deno.test('parseQuery: repeated single-value keys are grouped', () => {
const url = new URL('http://localhost?a=1&a=2&a=3');
const result = parseQuery(url.searchParams);
assertEquals(result, {
a: ['1', '2', '3'],
});
});