Files
q-blog/tests/utils/qortalRequestFunctions.test.ts
greenflame089 0871c31b34 Add new tests
2025-08-21 18:51:12 -04:00

46 lines
1.7 KiB
TypeScript

import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import { getAccountNames, getPrimaryAccountName } from '@/utils/qortalRequestFunctions';
const original = (global as any).qortalRequest;
beforeEach(() => {
(global as any).qortalRequest = vi.fn();
});
afterEach(() => {
(global as any).qortalRequest = original;
vi.restoreAllMocks();
});
describe('qortalRequestFunctions', () => {
it('getAccountNames returns mapped list', async () => {
(global as any).qortalRequest = vi.fn().mockResolvedValueOnce([
{ name: 'alice', owner: 'ADDR1' },
{ name: 'bob', owner: 'ADDR2' },
]);
const list = await getAccountNames('ADDR1');
expect(list).toEqual([
{ name: 'alice', owner: 'ADDR1' },
{ name: 'bob', owner: 'ADDR2' },
]);
});
it('getAccountNames falls back on empty/non-array/error', async () => {
(global as any).qortalRequest = vi.fn().mockResolvedValueOnce('not-an-array');
const a = await getAccountNames('X');
expect(a).toEqual([{ name: '', owner: 'X' }]);
(global as any).qortalRequest = vi.fn().mockRejectedValueOnce(new Error('boom'));
const b = await getAccountNames('Y');
expect(b).toEqual([{ name: '', owner: 'Y' }]);
});
it('getPrimaryAccountName returns string or empty', async () => {
(global as any).qortalRequest = vi.fn().mockResolvedValueOnce('alice');
expect(await getPrimaryAccountName('ADDR')).toBe('alice');
(global as any).qortalRequest = vi.fn().mockResolvedValueOnce({});
expect(await getPrimaryAccountName('ADDR')).toBe('');
(global as any).qortalRequest = vi.fn().mockRejectedValueOnce(new Error('x'));
expect(await getPrimaryAccountName('ADDR')).toBe('');
});
});