Admins can now create a Profile or License Key for someone who hasn't registered yet (matched by email). When that person registers, any unattached profile and license keys are linked to their new account. Also remove the Proclamations surface from the user dashboard.
72 lines
2.3 KiB
TypeScript
72 lines
2.3 KiB
TypeScript
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
import { isHttpError } from '@sveltejs/kit';
|
|
import { createLocals, mockUser } from '$lib/test-utils';
|
|
|
|
vi.mock('$lib/db', () => ({
|
|
prisma: {
|
|
profile: { findFirst: vi.fn() },
|
|
licenseKey: { groupBy: vi.fn() }
|
|
}
|
|
}));
|
|
|
|
import { prisma } from '$lib/db';
|
|
import { load } from './+page.server';
|
|
|
|
const profileFindFirst = prisma.profile.findFirst as ReturnType<typeof vi.fn>;
|
|
const licenseKeyGroupBy = prisma.licenseKey.groupBy as ReturnType<typeof vi.fn>;
|
|
|
|
describe('(protected)/dashboard page server load', () => {
|
|
beforeEach(() => {
|
|
profileFindFirst.mockReset();
|
|
licenseKeyGroupBy.mockReset();
|
|
});
|
|
|
|
it('throws 403 when locals.user is undefined', async () => {
|
|
const err = await Promise.resolve(load({ locals: createLocals() } as any)).catch(
|
|
(e: unknown) => e
|
|
);
|
|
expect(isHttpError(err, 403)).toBe(true);
|
|
});
|
|
|
|
it('returns firstName from profile', async () => {
|
|
profileFindFirst.mockResolvedValue({ firstName: 'Jane' });
|
|
licenseKeyGroupBy.mockResolvedValue([]);
|
|
|
|
const locals = createLocals(mockUser() as any);
|
|
const result = (await load({ locals } as any)) as any;
|
|
expect(result.firstName).toBe('Jane');
|
|
});
|
|
|
|
it('defaults firstName to "User" when profile is null', async () => {
|
|
profileFindFirst.mockResolvedValue(null);
|
|
licenseKeyGroupBy.mockResolvedValue([]);
|
|
|
|
const locals = createLocals(mockUser() as any);
|
|
const result = (await load({ locals } as any)) as any;
|
|
expect(result.firstName).toBe('User');
|
|
});
|
|
|
|
it('computes activeKeys and totalKeys from groupBy results', async () => {
|
|
profileFindFirst.mockResolvedValue({ firstName: 'Jane' });
|
|
licenseKeyGroupBy.mockResolvedValue([
|
|
{ active: true, _count: 3 },
|
|
{ active: false, _count: 2 }
|
|
]);
|
|
|
|
const locals = createLocals(mockUser() as any);
|
|
const result = (await load({ locals } as any)) as any;
|
|
expect(result.activeKeys).toBe(3);
|
|
expect(result.totalKeys).toBe(5);
|
|
});
|
|
|
|
it('returns 0 for activeKeys when none are active', async () => {
|
|
profileFindFirst.mockResolvedValue({ firstName: 'Jane' });
|
|
licenseKeyGroupBy.mockResolvedValue([{ active: false, _count: 4 }]);
|
|
|
|
const locals = createLocals(mockUser() as any);
|
|
const result = (await load({ locals } as any)) as any;
|
|
expect(result.activeKeys).toBe(0);
|
|
expect(result.totalKeys).toBe(4);
|
|
});
|
|
});
|