feat: Add unit tests for backend validators and configure Jest.
This commit is contained in:
parent
ebcae0b3e7
commit
9bb941b45e
16 changed files with 2071 additions and 381 deletions
|
|
@ -0,0 +1,59 @@
|
|||
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);
|
||||
});
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue