Files
framit/app/types/query-result.test.ts

99 lines
3.0 KiB
TypeScript
Raw Normal View History

import { describe, it, expect } from 'vitest';
import { QueryResult } from './query-result';
import type { ApiError } from './api/error';
describe('QueryResult', () => {
describe('initialization', () => {
it('should initialize with null data', () => {
const result = new QueryResult<string, void>();
expect(result.data.value).toBeNull();
});
it('should initialize with null error', () => {
const result = new QueryResult<string, void>();
expect(result.error.value).toBeNull();
});
it('should initialize with loading as false', () => {
const result = new QueryResult<string, void>();
expect(result.loading.value).toBe(false);
});
it('should have run property (initially undefined)', () => {
const result = new QueryResult<string, void>();
expect(result).toHaveProperty('run');
});
});
describe('reactive properties', () => {
it('should have reactive data ref', () => {
const result = new QueryResult<{ id: number }, void>();
result.data.value = { id: 1 };
expect(result.data.value).toEqual({ id: 1 });
});
it('should have reactive error ref', () => {
const result = new QueryResult<string, void>();
const error: ApiError = { message: 'Test error', success: false };
result.error.value = error;
expect(result.error.value).toEqual(error);
});
it('should have reactive loading ref', () => {
const result = new QueryResult<string, void>();
result.loading.value = true;
expect(result.loading.value).toBe(true);
});
});
describe('type safety', () => {
it('should accept generic type for data', () => {
interface TestData {
name: string;
count: number;
}
const result = new QueryResult<TestData, void>();
result.data.value = { name: 'test', count: 42 };
expect(result.data.value.name).toBe('test');
expect(result.data.value.count).toBe(42);
});
it('should accept generic type for payload', () => {
interface ResponseData {
success: boolean;
}
interface PayloadData {
input: string;
}
const result = new QueryResult<ResponseData, PayloadData>();
// PayloadT is used by the run function signature
expect(result).toHaveProperty('run');
});
});
describe('run method assignment', () => {
it('should allow run method to be assigned', async () => {
const result = new QueryResult<string, void>();
let called = false;
result.run = async () => {
called = true;
};
await result.run();
expect(called).toBe(true);
});
it('should allow run method to accept payload parameter', async () => {
const result = new QueryResult<string, { data: string }>();
let receivedPayload: { data: string } | undefined;
result.run = async (payload) => {
receivedPayload = payload;
};
await result.run({ data: 'test' });
expect(receivedPayload).toEqual({ data: 'test' });
});
});
});