Files
linkforty/src/routes/links.test.ts
T
2026-07-31 12:06:08 +09:00

42 lines
1.7 KiB
TypeScript

import { beforeEach, describe, expect, it, vi } from 'vitest';
const { dbQueryMock } = vi.hoisted(() => ({
dbQueryMock: vi.fn(),
}));
vi.mock('../lib/database.js', () => ({
db: {
query: dbQueryMock,
},
}));
import { resolveLinkTemplateId } from './links.js';
describe('resolveLinkTemplateId', () => {
beforeEach(() => {
dbQueryMock.mockReset();
});
it('falls back to the default template when the provided id does not exist', async () => {
dbQueryMock.mockResolvedValueOnce({ rows: [] });
dbQueryMock.mockResolvedValueOnce({ rows: [{ id: '11111111-1111-1111-1111-111111111111' }] });
await expect(resolveLinkTemplateId('00000000-0000-0000-0000-000000000000', null)).resolves.toBe('11111111-1111-1111-1111-111111111111');
expect(dbQueryMock).toHaveBeenNthCalledWith(2, 'SELECT id FROM link_templates WHERE is_default = true LIMIT 1');
});
it('uses the template slug when it can be resolved', async () => {
dbQueryMock.mockResolvedValueOnce({ rows: [{ id: '11111111-1111-1111-1111-111111111111' }] });
await expect(resolveLinkTemplateId(null, 'default')).resolves.toBe('11111111-1111-1111-1111-111111111111');
expect(dbQueryMock).toHaveBeenCalledWith('SELECT id FROM link_templates WHERE slug = $1 LIMIT 1', ['default']);
});
it('falls back to the default template when the provided template id is invalid', async () => {
dbQueryMock.mockResolvedValueOnce({ rows: [{ id: '22222222-2222-2222-2222-222222222222' }] });
await expect(resolveLinkTemplateId('not-a-uuid', null)).resolves.toBe('22222222-2222-2222-2222-222222222222');
expect(dbQueryMock).toHaveBeenCalledWith('SELECT id FROM link_templates WHERE is_default = true LIMIT 1');
});
});