forked from Qortal/q-blog
46 lines
1.7 KiB
TypeScript
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('');
|
|
});
|
|
});
|