This commit is contained in:
28
website/tests/unit/api-envelope.test.js
Normal file
28
website/tests/unit/api-envelope.test.js
Normal file
@ -0,0 +1,28 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { unwrap } from '../../js/api-envelope.js';
|
||||
|
||||
describe('unwrap', () => {
|
||||
it('unwraps ok:true object data', () => {
|
||||
const r = unwrap({ ok: true, data: { token: 'abc', role: 'admin' } });
|
||||
expect(r.success).toBe(true);
|
||||
expect(r.token).toBe('abc');
|
||||
expect(r.role).toBe('admin');
|
||||
});
|
||||
|
||||
it('unwraps ok:true array data as items', () => {
|
||||
const r = unwrap({ ok: true, data: [{ id: 1 }] });
|
||||
expect(r.success).toBe(true);
|
||||
expect(r.items).toEqual([{ id: 1 }]);
|
||||
});
|
||||
|
||||
it('unwraps ok:false errors', () => {
|
||||
const r = unwrap({ ok: false, error: { code: 'X', message: 'Failed' } });
|
||||
expect(r.success).toBe(false);
|
||||
expect(r.error).toBe('Failed');
|
||||
});
|
||||
|
||||
it('passes through legacy body', () => {
|
||||
const legacy = { success: true, questionnaires: [] };
|
||||
expect(unwrap(legacy)).toBe(legacy);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user