elearning/Backend/tests/unit/validators/usermanagement.validator.test.ts

59 lines
2.1 KiB
TypeScript

import {
getUserByIdValidator,
updateUserRoleValidator,
} from '@/validators/usermanagement.validator';
describe('getUserByIdValidator', () => {
it('should pass with valid id', () => {
const { error } = getUserByIdValidator.validate({ id: 1 });
expect(error).toBeUndefined();
});
it('should fail without id', () => {
const { error } = getUserByIdValidator.validate({});
expect(error).toBeDefined();
});
it('should fail with non-numeric id', () => {
const { error } = getUserByIdValidator.validate({ id: 'abc' });
expect(error).toBeDefined();
expect(error?.details[0].message).toMatch(/ID must be a number/i);
});
it('should pass with id = 0', () => {
// Joi number() allows 0 by default unless positive() is specified
const { error } = getUserByIdValidator.validate({ id: 0 });
expect(error).toBeUndefined();
});
});
describe('updateUserRoleValidator', () => {
it('should pass with valid id and role_id', () => {
const { error } = updateUserRoleValidator.validate({ id: 1, role_id: 2 });
expect(error).toBeUndefined();
});
it('should fail without id', () => {
const { error } = updateUserRoleValidator.validate({ role_id: 2 });
expect(error).toBeDefined();
});
it('should fail without role_id', () => {
const { error } = updateUserRoleValidator.validate({ id: 1 });
expect(error).toBeDefined();
// Joi uses field name in message when custom messages don't match
expect(error?.details[0].message).toContain('role_id');
});
it('should fail with non-numeric role_id', () => {
const { error } = updateUserRoleValidator.validate({ id: 1, role_id: 'admin' });
expect(error).toBeDefined();
expect(error?.details[0].message).toMatch(/Role ID must be a number/i);
});
it('should fail with non-numeric id', () => {
const { error } = updateUserRoleValidator.validate({ id: 'abc', role_id: 1 });
expect(error).toBeDefined();
expect(error?.details[0].message).toMatch(/ID must be a number/i);
});
});