mirror of
https://github.com/rgrgogu/new_starr.git
synced 2026-09-27 00:12:54 +08:00
@@ -0,0 +1,92 @@
|
||||
'use strict';
|
||||
|
||||
const R = require('../../utils/response.util');
|
||||
|
||||
function makeRes() {
|
||||
const res = {
|
||||
_status: null,
|
||||
_body: null,
|
||||
status(code) { this._status = code; return this; },
|
||||
json(body) { this._body = body; return this; },
|
||||
};
|
||||
return res;
|
||||
}
|
||||
|
||||
// ── R.success ─────────────────────────────────────────────────────────────────
|
||||
|
||||
describe('R.success()', () => {
|
||||
test('default status is 200', () => {
|
||||
const res = makeRes();
|
||||
R.success(res, 'OK');
|
||||
expect(res._status).toBe(200);
|
||||
});
|
||||
|
||||
test('envelope shape: status=success, message, data', () => {
|
||||
const res = makeRes();
|
||||
R.success(res, 'Created', { id: 1 }, 201);
|
||||
expect(res._body).toEqual({ status: 'success', message: 'Created', data: { id: 1 } });
|
||||
});
|
||||
|
||||
test('custom status code is used', () => {
|
||||
const res = makeRes();
|
||||
R.success(res, 'Created', null, 201);
|
||||
expect(res._status).toBe(201);
|
||||
});
|
||||
|
||||
test('data is null when omitted', () => {
|
||||
const res = makeRes();
|
||||
R.success(res, 'OK');
|
||||
expect(res._body.data).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
// ── R.error ───────────────────────────────────────────────────────────────────
|
||||
|
||||
describe('R.error()', () => {
|
||||
test('default status is 500', () => {
|
||||
const res = makeRes();
|
||||
R.error(res, 'Something broke');
|
||||
expect(res._status).toBe(500);
|
||||
});
|
||||
|
||||
test('envelope shape: status=error, message', () => {
|
||||
const res = makeRes();
|
||||
R.error(res, 'Not found', 404);
|
||||
expect(res._body).toMatchObject({ status: 'error', message: 'Not found' });
|
||||
expect(res._status).toBe(404);
|
||||
});
|
||||
|
||||
test('errors field is absent when not provided', () => {
|
||||
const res = makeRes();
|
||||
R.error(res, 'Bad request', 400);
|
||||
expect(res._body).not.toHaveProperty('errors');
|
||||
});
|
||||
|
||||
test('errors field is included when provided', () => {
|
||||
const res = makeRes();
|
||||
const errs = [{ field: 'email', msg: 'Invalid' }];
|
||||
R.error(res, 'Validation failed', 422, errs);
|
||||
expect(res._body.errors).toEqual(errs);
|
||||
});
|
||||
});
|
||||
|
||||
// ── R.validationError ─────────────────────────────────────────────────────────
|
||||
|
||||
describe('R.validationError()', () => {
|
||||
test('status is always 422', () => {
|
||||
const res = makeRes();
|
||||
R.validationError(res, []);
|
||||
expect(res._status).toBe(422);
|
||||
});
|
||||
|
||||
test('envelope shape: status=error, message=Validation failed, errors', () => {
|
||||
const res = makeRes();
|
||||
const errs = [{ field: 'name', msg: 'Required' }];
|
||||
R.validationError(res, errs);
|
||||
expect(res._body).toEqual({
|
||||
status: 'error',
|
||||
message: 'Validation failed',
|
||||
errors: errs,
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user