@tomei/sso
Version:
Tomei SSO Package
80 lines (67 loc) • 3.04 kB
text/typescript
import { Group } from '../../../../src/components/group/group';
import { GroupRepository } from '../../../../src/components/group/group.repository';
import { ClassError } from '@tomei/general';
import { GroupTypeEnum } from '../../../../src/enum/group-type.enum';
describe('Group', () => {
afterEach(() => {
jest.clearAllMocks();
});
it('should initialize a group with valid GroupCode', async () => {
const groupAttr = {
GroupCode: 'group1',
Name: 'Group 1',
Description: 'This is Group 1',
Type: GroupTypeEnum.ROLE,
ParentGroupCode: 'parentGroup1',
InheritParentPrivilegeYN: 'Y',
InheritParentSystemAccessYN: 'N',
Status: 'Active',
CreatedById: 1,
CreatedAt: new Date(),
UpdatedById: 2,
UpdatedAt: new Date(),
};
const groupRepositoryMock = jest.spyOn(GroupRepository.prototype, 'findByPk').mockResolvedValueOnce({
...groupAttr,
get: () => groupAttr,
} as any);
const result = await Group.init(null, 'group1');
expect(result).toBeInstanceOf(Group);
expect(result.GroupCode).toBe(groupAttr.GroupCode);
expect(result.Name).toBe(groupAttr.Name);
expect(result.Description).toBe(groupAttr.Description);
expect(result.Type).toBe(groupAttr.Type);
expect(result.ParentGroupCode).toBe(groupAttr.ParentGroupCode);
expect(result.InheritParentPrivilegeYN).toBe(groupAttr.InheritParentPrivilegeYN);
expect(result.InheritParentSystemAccessYN).toBe(groupAttr.InheritParentSystemAccessYN);
expect(result.Status).toBe(groupAttr.Status);
expect(result.CreatedById).toBe(groupAttr.CreatedById);
expect(result.CreatedAt).toBe(groupAttr.CreatedAt);
expect(result.UpdatedById).toBe(groupAttr.UpdatedById);
expect(result.UpdatedAt).toBe(groupAttr.UpdatedAt);
expect(groupRepositoryMock).toHaveBeenCalledTimes(1);
expect(groupRepositoryMock).toHaveBeenCalledWith('group1', {
transaction: null,
});
});
it('should throw an error when initializing a group with invalid GroupCode', async () => {
const groupRepositoryMock = jest.spyOn(GroupRepository.prototype, 'findByPk').mockResolvedValueOnce(null);
await expect(Group.init(null, 'invalidGroupCode')).rejects.toThrow(
new ClassError('Group', 'GroupErrMsg01', 'Failed To Initialize Group')
);
expect(groupRepositoryMock).toHaveBeenCalledTimes(1);
expect(groupRepositoryMock).toHaveBeenCalledWith('invalidGroupCode', {
transaction: null,
});
});
it('should throw an error when initializing a group with an error', async () => {
const groupRepositoryMock = jest.spyOn(GroupRepository.prototype, 'findByPk').mockRejectedValueOnce(new Error('Database error'));
await expect(Group.init(null, 'group1')).rejects.toThrow(
new ClassError('Group', 'GroupErrMsg01', 'Failed To Initialize Group')
);
expect(groupRepositoryMock).toHaveBeenCalledTimes(1);
expect(groupRepositoryMock).toHaveBeenCalledWith('group1', {
transaction: null,
});
});
});