Files
shd.build/src/lib/server/pagination.test.ts
T

43 lines
1.4 KiB
TypeScript

import { describe, expect, it } from 'vitest';
import { getPagination, getRegistrySort } from './pagination.js';
const options = {
name: { asc: 'name ASC', desc: 'name DESC' },
updated: { asc: 'updated_at ASC', desc: 'updated_at DESC', defaultDirection: 'desc' as const }
};
describe('getRegistrySort', () => {
it('returns only allowlisted sort expressions', () => {
const url = new URL('https://shd.build/isac/attributes?sort=name&direction=desc');
expect(getRegistrySort(url, options, 'name')).toEqual({
key: 'name',
direction: 'desc',
orderBy: 'name DESC'
});
});
it('falls back for unknown keys and directions', () => {
const url = new URL('https://shd.build/isac/attributes?sort=DROP%20TABLE&direction=sideways');
expect(getRegistrySort(url, options, 'updated')).toEqual({
key: 'updated',
direction: 'desc',
orderBy: 'updated_at DESC'
});
});
it('supports namespaced registry parameters', () => {
const url = new URL('https://shd.build/isac/users/1?loginSort=name&loginDirection=asc');
expect(getRegistrySort(url, options, 'updated', 'desc', 'login')).toMatchObject({
key: 'name',
direction: 'asc'
});
});
});
describe('getPagination', () => {
it('keeps supported row counts and clamps the selected page', () => {
const url = new URL('https://shd.build/isac/attributes?perPage=50&page=9');
expect(getPagination(url, 75)).toMatchObject({ perPage: 50, page: 2, pageCount: 2 });
});
});