UNPKG

@tomei/sso

Version:
84 lines (75 loc) 3.83 kB
import { SystemPrivilege } from '../../../../src/components/system-privilege/system-privilege'; import { SystemPrivilegeRepository } from '../../../../src/components/system-privilege/system-privilege.repository'; import { ClassError } from '@tomei/general'; describe('SystemPrivilege', () => { let systemPrivilege: SystemPrivilege; const systemPrivilegeAttr = { PrivilegeCode: 'test', SystemCode: 'test', Name: 'test', Description: 'test', Status: 'test', CreatedById: 1, CreatedAt: new Date(), UpdatedById: 1, UpdatedAt: new Date(), }; beforeEach(() => { jest.resetAllMocks(); }); describe('constructor', () => { it('should create a new SystemPrivilege instance', () => { systemPrivilege = new (SystemPrivilege as any)(systemPrivilegeAttr); expect(systemPrivilege).toBeDefined(); expect(systemPrivilege).toBeInstanceOf(SystemPrivilege); expect(systemPrivilege.PrivilegeCode).toBe(systemPrivilegeAttr.PrivilegeCode); expect(systemPrivilege.SystemCode).toBe(systemPrivilegeAttr.SystemCode); expect(systemPrivilege.Description).toBe(systemPrivilegeAttr.Description); expect(systemPrivilege.Status).toBe(systemPrivilegeAttr.Status); expect(systemPrivilege.CreatedById).toBe(systemPrivilegeAttr.CreatedById); expect(systemPrivilege.CreatedAt).toBe(systemPrivilegeAttr.CreatedAt); expect(systemPrivilege.UpdatedById).toBe(systemPrivilegeAttr.UpdatedById); expect(systemPrivilege.UpdatedAt).toBe(systemPrivilegeAttr.UpdatedAt); }); }); describe('init', () => { it('should initialize SystemPrivilege without PrivilegeCode', async () => { const systemPrivilege = await SystemPrivilege.init(null); expect(systemPrivilege).toBeDefined(); expect(systemPrivilege).toBeInstanceOf(SystemPrivilege); expect(systemPrivilege.PrivilegeCode).toBeUndefined(); expect(systemPrivilege.SystemCode).toBeUndefined(); expect(systemPrivilege.Description).toBeUndefined(); expect(systemPrivilege.Status).toBeUndefined(); expect(systemPrivilege.CreatedById).toBeUndefined(); expect(systemPrivilege.CreatedAt).toBeUndefined(); expect(systemPrivilege.UpdatedById).toBeUndefined(); expect(systemPrivilege.UpdatedAt).toBeUndefined(); }); it('should initialize SystemPrivilege with PrivilegeCode', async () => { const findByPkSpy = jest .spyOn(SystemPrivilegeRepository.prototype, 'findByPk') .mockResolvedValue(systemPrivilegeAttr as any); const systemPrivilege = await SystemPrivilege.init(null, 'test'); expect(findByPkSpy).toHaveBeenCalledWith('test', { transaction: null, }); expect(systemPrivilege).toBeDefined(); expect(systemPrivilege).toBeInstanceOf(SystemPrivilege); expect(systemPrivilege.PrivilegeCode).toBe(systemPrivilegeAttr.PrivilegeCode); expect(systemPrivilege.SystemCode).toBe(systemPrivilegeAttr.SystemCode); expect(systemPrivilege.Description).toBe(systemPrivilegeAttr.Description); expect(systemPrivilege.Status).toBe(systemPrivilegeAttr.Status); expect(systemPrivilege.CreatedById).toBe(systemPrivilegeAttr.CreatedById); expect(systemPrivilege.CreatedAt).toBe(systemPrivilegeAttr.CreatedAt); expect(systemPrivilege.UpdatedById).toBe(systemPrivilegeAttr.UpdatedById); expect(systemPrivilege.UpdatedAt).toBe(systemPrivilegeAttr.UpdatedAt); }); it('should throw an error if PrivilegeCode is not found', async () => { jest.spyOn(SystemPrivilegeRepository.prototype, 'findByPk').mockResolvedValue(null); await expect(SystemPrivilege.init(null, 'test')).rejects.toThrow( new ClassError('SystemPrivilege', 'SystemPrivilegeErrMsg00', 'System Privilege Not Found'), ); }); }); });