Files
portal/src/lib/test-utils.ts
T
2026-02-25 13:19:26 -06:00

139 lines
3.2 KiB
TypeScript

/**
* Shared mock data factories for tests.
*
* NOTE: `vi.mock()` calls must live in each test file (hoisting requirement).
* These helpers only build data objects — no mocking side-effects.
*/
/** Build a minimal `App.Locals` object. Pass a user to simulate an authenticated request. */
export function createLocals(user?: { id: string; name: string; email: string }): App.Locals {
return {
session: user
? ({
id: 'session-1',
userId: user.id,
expiresAt: new Date(Date.now() + 86_400_000)
} as App.Locals['session'])
: (undefined as unknown as App.Locals['session']),
user: user as unknown as App.Locals['user']
};
}
/** Build a `Request` whose `.formData()` resolves to the given key/value pairs. */
export function createFormRequest(fields: Record<string, string>): Request {
const fd = new FormData();
for (const [k, v] of Object.entries(fields)) fd.append(k, v);
return new Request('http://localhost', { method: 'POST', body: fd });
}
/** Minimal user object with sensible defaults. */
export function mockUser(overrides?: Record<string, unknown>) {
return {
id: 'user-1',
name: 'Jane Doe',
email: 'jane@example.com',
emailVerified: true,
image: null,
createdAt: new Date(),
updatedAt: new Date(),
roleId: null,
...overrides
};
}
/** User that also has a profile (and optionally a role). */
export function mockUserWithProfile(overrides?: Record<string, unknown>) {
return mockUser({
profile: {
id: 'profile-1',
userId: 'user-1',
firstName: 'Jane',
lastName: 'Doe',
phoneNumber: '555-0100'
},
...overrides
});
}
/** User with profile that includes addresses. */
export function mockUserWithProfileAndAddresses(overrides?: Record<string, unknown>) {
return mockUser({
profile: {
id: 'profile-1',
userId: 'user-1',
firstName: 'Jane',
lastName: 'Doe',
phoneNumber: '5550100',
addresses: [
{
id: 'addr-1',
profileId: 'profile-1',
street: '123 Main St',
city: 'Dallas',
state: 'TX',
postalCode: '75001',
country: 'US'
}
]
},
...overrides
});
}
/** Minimal license key object. */
export function mockLicenseKey(overrides?: Record<string, unknown>) {
return {
id: 'key-1',
key: 'AAAA-BBBB-CCCC-DDDD',
active: true,
level: 'standard',
userId: 'user-1',
...overrides
};
}
/** Minimal proclamation object with relations. */
export function mockProclamation(overrides?: Record<string, unknown>) {
return {
id: 'proc-1',
name: 'Test Proclamation',
content: '<p>Test content</p>',
solfeggioFrequencyId: 1,
categoryId: 1,
price: 0.99,
solfeggioFrequency: {
id: 1,
name: 'UT',
value: 396
},
category: {
id: 1,
name: 'Healing',
parentId: null,
parent: null
},
...overrides
};
}
/** Minimal proclamation category. */
export function mockCategory(overrides?: Record<string, unknown>) {
return {
id: 1,
name: 'Healing',
parentId: null,
...overrides
};
}
/** Build a URL object for use with PageServerLoad. */
export function createUrl(path: string, params?: Record<string, string>): URL {
const url = new URL(path, 'http://localhost');
if (params) {
for (const [k, v] of Object.entries(params)) {
url.searchParams.set(k, v);
}
}
return url;
}