@tomei/sso
Version:
Tomei SSO Package
67 lines (56 loc) • 2.32 kB
text/typescript
import { GroupReportingUser } from '../../../../src/components/group-reporting-user/group-reporting-user';
import { GroupReportingUserRepository } from '../../../../src/components/group-reporting-user/group-reporting-user.repository';
import { ClassError } from '@tomei/general';
describe('GroupReportingUser', () => {
const mockGroupReportingUserAttr = {
GroupReportingUserId: 1,
GroupCode: 'test',
UserId: 1,
Rank: 1,
Status: 'active',
CreatedById: 1,
CreatedAt: new Date(),
UpdatedById: 1,
UpdatedAt: new Date(),
};
beforeEach(async () => {
jest.clearAllMocks();
});
describe('init', () => {
const mockDbTransaction = {};
it('should initialize GroupReportingUser without GroupReportingUserId', async () => {
const initGroupReportingUser = await GroupReportingUser.init(mockDbTransaction);
expect(initGroupReportingUser).toBeDefined();
expect(initGroupReportingUser).toBeInstanceOf(GroupReportingUser);
});
it('should initialize GroupReportingUser with valid GroupReportingUserId', async () => {
const mockFindByPk = jest
.spyOn(GroupReportingUserRepository.prototype, 'findByPk')
.mockResolvedValue({
...mockGroupReportingUserAttr,
get: () => mockGroupReportingUserAttr,
} as any);
const initGroupReportingUser = await GroupReportingUser.init(
mockDbTransaction,
'1'
);
expect(initGroupReportingUser).toBeDefined();
expect(initGroupReportingUser).toBeInstanceOf(GroupReportingUser);
expect(mockFindByPk).toHaveBeenCalledWith('1', {
transaction: mockDbTransaction,
});
});
it('should throw ClassError when GroupReportingUser is not found', async () => {
jest.spyOn(GroupReportingUserRepository.prototype, 'findByPk').mockResolvedValue(null);
await expect(
GroupReportingUser.init(mockDbTransaction, '1')
).rejects.toThrowError(ClassError);
});
it('should throw ClassError when failed to initialize GroupReportingUser', async () => {
jest.spyOn(GroupReportingUserRepository.prototype, 'findByPk').mockRejectedValue(new Error());
await expect(
GroupReportingUser.init(mockDbTransaction, '1')
).rejects.toThrowError(ClassError);
});
});
});