This commit is contained in:
85
app/composables/__tests__/usePocketbase.test.ts
Normal file
85
app/composables/__tests__/usePocketbase.test.ts
Normal file
@@ -0,0 +1,85 @@
|
||||
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
||||
import { usePocketbase } from '../usePocketbase';
|
||||
import PocketBase from 'pocketbase';
|
||||
|
||||
/**
|
||||
* Tests for usePocketbase composable
|
||||
*
|
||||
* This composable provides a singleton PocketBase client instance.
|
||||
*/
|
||||
|
||||
describe('usePocketbase', () => {
|
||||
beforeEach(() => {
|
||||
// Reset modules to clear singleton between test suites
|
||||
vi.resetModules();
|
||||
});
|
||||
|
||||
describe('Instance Creation', () => {
|
||||
it('should return a PocketBase instance', () => {
|
||||
const pb = usePocketbase();
|
||||
expect(pb).toBeInstanceOf(PocketBase);
|
||||
});
|
||||
|
||||
it('should return the same instance on multiple calls (singleton)', () => {
|
||||
const pb1 = usePocketbase();
|
||||
const pb2 = usePocketbase();
|
||||
expect(pb1).toBe(pb2);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Configuration', () => {
|
||||
it('should initialize with URL from runtime config or fallback to default', () => {
|
||||
const pb = usePocketbase();
|
||||
|
||||
// Should have a baseURL set (either from config or default)
|
||||
expect(pb.baseURL).toBeDefined();
|
||||
expect(typeof pb.baseURL).toBe('string');
|
||||
expect(pb.baseURL).toMatch(/^https?:\/\//); // Valid URL format
|
||||
});
|
||||
|
||||
it('should use a valid URL format', () => {
|
||||
const pb = usePocketbase();
|
||||
|
||||
// URL should be a valid HTTP/HTTPS URL
|
||||
expect(pb.baseURL).toMatch(/^https?:\/\/[\w\d.:]+/);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Singleton Behavior', () => {
|
||||
it('should maintain singleton across multiple imports', () => {
|
||||
const pb1 = usePocketbase();
|
||||
const pb2 = usePocketbase();
|
||||
const pb3 = usePocketbase();
|
||||
|
||||
expect(pb1).toBe(pb2);
|
||||
expect(pb2).toBe(pb3);
|
||||
});
|
||||
|
||||
it('should share auth state across all consumers', () => {
|
||||
const pb1 = usePocketbase();
|
||||
const pb2 = usePocketbase();
|
||||
|
||||
// Both should share the same authStore
|
||||
expect(pb1.authStore).toBe(pb2.authStore);
|
||||
});
|
||||
});
|
||||
|
||||
describe('PocketBase Features', () => {
|
||||
it('should have authStore available', () => {
|
||||
const pb = usePocketbase();
|
||||
expect(pb.authStore).toBeDefined();
|
||||
});
|
||||
|
||||
it('should have collection method available', () => {
|
||||
const pb = usePocketbase();
|
||||
expect(pb.collection).toBeDefined();
|
||||
expect(typeof pb.collection).toBe('function');
|
||||
});
|
||||
|
||||
it('should be able to access collections', () => {
|
||||
const pb = usePocketbase();
|
||||
const usersCollection = pb.collection('users');
|
||||
expect(usersCollection).toBeDefined();
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user