@tomei/sso
Version:
Tomei SSO Package
73 lines (64 loc) • 2.91 kB
text/typescript
import { UserPrivilege } from '../../../../src/components/user-privilege/user-privilege';
import { UserPrivilegeRepository } from '../../../../src/components/user-privilege/user-privilege.repository';
import { ClassError } from '@tomei/general';
describe('UserPrivilege', () => {
let userPrivilege: UserPrivilege;
const userPrivilegeAttr = {
UserPrivilegeId: 1,
UserId: 1,
SystemPrivilegeId: 'privilege1',
Status: 'active',
CreatedById: 1,
CreatedAt: new Date(),
UpdatedById: 1,
UpdatedAt: new Date(),
};
beforeEach(() => {
userPrivilege = new (UserPrivilege as any)(userPrivilegeAttr);
});
afterEach(() => {
jest.clearAllMocks();
});
describe('constructor', () => {
it('should create a new UserPrivilege instance', () => {
expect(userPrivilege).toBeDefined();
expect(userPrivilege).toBeInstanceOf(UserPrivilege);
expect(userPrivilege.UserPrivilegeId).toBe(userPrivilegeAttr.UserPrivilegeId);
expect(userPrivilege.UserId).toBe(userPrivilegeAttr.UserId);
expect(userPrivilege.SystemPrivilegeId).toBe(userPrivilegeAttr.SystemPrivilegeId);
expect(userPrivilege.Status).toBe(userPrivilegeAttr.Status);
expect(userPrivilege.CreatedById).toBe(userPrivilegeAttr.CreatedById);
expect(userPrivilege.CreatedAt).toBe(userPrivilegeAttr.CreatedAt);
expect(userPrivilege.UpdatedById).toBe(userPrivilegeAttr.UpdatedById);
expect(userPrivilege.UpdatedAt).toBe(userPrivilegeAttr.UpdatedAt);
});
});
describe('init', () => {
it('should initialize UserPrivilege with valid UserPrivilegeId', async () => {
const findOneMock = jest
.spyOn(UserPrivilegeRepository.prototype, 'findOne')
.mockResolvedValueOnce(userPrivilegeAttr as any);
const result = await UserPrivilege.init(null, 1);
expect(findOneMock).toHaveBeenCalledTimes(1);
expect(findOneMock).toHaveBeenCalledWith({
where: { UserPrivilegeId: 1 },
transaction: null,
});
expect(result).toBeInstanceOf(UserPrivilege);
expect(result.UserPrivilegeId).toBe(userPrivilegeAttr.UserPrivilegeId);
expect(result.UserId).toBe(userPrivilegeAttr.UserId);
expect(result.SystemPrivilegeId).toBe(userPrivilegeAttr.SystemPrivilegeId);
expect(result.Status).toBe(userPrivilegeAttr.Status);
expect(result.CreatedById).toBe(userPrivilegeAttr.CreatedById);
expect(result.CreatedAt).toBe(userPrivilegeAttr.CreatedAt);
expect(result.UpdatedById).toBe(userPrivilegeAttr.UpdatedById);
expect(result.UpdatedAt).toBe(userPrivilegeAttr.UpdatedAt);
});
it('should throw ClassError when UserPrivilegeId is not found', async () => {
jest
.spyOn(UserPrivilegeRepository.prototype, 'findOne')
.mockResolvedValueOnce(null);
await expect(UserPrivilege.init(null, 1)).rejects.toThrow(ClassError);
});
});
});