89 lines
3.4 KiB
TypeScript
89 lines
3.4 KiB
TypeScript
import { CertificationService } from './certification.service';
|
|
|
|
function createPrismaMock() {
|
|
return {
|
|
tenant: {
|
|
findUnique: jest.fn().mockResolvedValue({ id: 'tenant-1' }),
|
|
update: jest.fn().mockResolvedValue({ id: 'tenant-1', certificationStatus: 'pending' }),
|
|
},
|
|
enterpriseCertification: {
|
|
create: jest.fn().mockImplementation(({ data }) => Promise.resolve({ id: 'cert-1', ...data })),
|
|
findMany: jest.fn().mockResolvedValue([]),
|
|
findUnique: jest.fn().mockResolvedValue({ id: 'cert-1', tenantId: 'tenant-1', status: 'pending' }),
|
|
update: jest.fn().mockImplementation(({ data }) => Promise.resolve({ id: 'cert-1', ...data })),
|
|
},
|
|
user: {
|
|
findUnique: jest.fn().mockResolvedValue({ id: 'reviewer-1', username: 'reviewer', displayName: '审核员' }),
|
|
findMany: jest.fn().mockResolvedValue([]),
|
|
},
|
|
operationLog: {
|
|
create: jest.fn(),
|
|
},
|
|
};
|
|
}
|
|
|
|
describe('CertificationService', () => {
|
|
it('returns the reviewer username with enterprise certification records', async () => {
|
|
const prisma = createPrismaMock();
|
|
prisma.enterpriseCertification.findMany.mockResolvedValue([
|
|
{ id: 'cert-1', tenantId: 'tenant-1', reviewerId: 'reviewer-1' },
|
|
]);
|
|
prisma.user.findMany.mockResolvedValue([
|
|
{ id: 'reviewer-1', username: 'reviewer', displayName: '审核员' },
|
|
]);
|
|
const service = new CertificationService(prisma as never);
|
|
|
|
await expect(service.list()).resolves.toEqual([
|
|
expect.objectContaining({ reviewer: expect.objectContaining({ username: 'reviewer' }) }),
|
|
]);
|
|
});
|
|
|
|
it('filters enterprise certification submissions by Shanghai date range', async () => {
|
|
const prisma = createPrismaMock();
|
|
const service = new CertificationService(prisma as never);
|
|
|
|
await service.list(undefined, 'pending', undefined, '2026-08-01', '2026-08-03');
|
|
|
|
expect(prisma.enterpriseCertification.findMany).toHaveBeenCalledWith(expect.objectContaining({
|
|
where: expect.objectContaining({
|
|
submittedAt: {
|
|
gte: new Date('2026-08-01T00:00:00+08:00'),
|
|
lte: new Date('2026-08-03T23:59:59.999+08:00'),
|
|
},
|
|
}),
|
|
}));
|
|
});
|
|
|
|
it('submits certification and marks tenant pending', async () => {
|
|
const prisma = createPrismaMock();
|
|
const service = new CertificationService(prisma as never);
|
|
|
|
await service.submit({ tenantId: 'tenant-1', companyName: '测试企业', licenseNo: 'LIC-1' });
|
|
|
|
expect(prisma.enterpriseCertification.create).toHaveBeenCalledWith({
|
|
data: expect.objectContaining({ tenantId: 'tenant-1', companyName: '测试企业', status: 'pending' }),
|
|
});
|
|
expect(prisma.tenant.update).toHaveBeenCalledWith({
|
|
where: { id: 'tenant-1' },
|
|
data: { certificationStatus: 'pending' },
|
|
});
|
|
});
|
|
|
|
it('approves and rejects certification while syncing tenant status', async () => {
|
|
const prisma = createPrismaMock();
|
|
const service = new CertificationService(prisma as never);
|
|
|
|
await service.approve('cert-1', { reviewerId: 'reviewer-1' });
|
|
expect(prisma.tenant.update).toHaveBeenLastCalledWith({
|
|
where: { id: 'tenant-1' },
|
|
data: { certificationStatus: 'approved' },
|
|
});
|
|
|
|
await service.reject('cert-1', { reviewerId: 'reviewer-1', reason: '资料不清晰' });
|
|
expect(prisma.tenant.update).toHaveBeenLastCalledWith({
|
|
where: { id: 'tenant-1' },
|
|
data: { certificationStatus: 'rejected' },
|
|
});
|
|
});
|
|
});
|