emi-challenge-fe/tests/app/shared/models/result.model.spec.ts

63 lines
1.5 KiB
TypeScript
Raw Permalink Normal View History

import { describe, it, expect } from 'vitest';
import { ok, err, isOk, isErr } from '@app/shared/models/result.model';
describe('Result model', () => {
describe('ok', () => {
it('should create a success result with data', () => {
const data = { name: 'test' };
const result = ok(data);
expect(result.success).toBe(true);
expect((result as { success: true; data: typeof data }).data).toEqual(data);
});
});
describe('err', () => {
it('should create an error result with error', () => {
const error = new Error('fail');
const result = err(error);
expect(result.success).toBe(false);
expect((result as { success: false; error: Error }).error).toEqual(error);
});
});
describe('isOk', () => {
it('should return true for success result', () => {
const result = ok('value');
const check = isOk(result);
expect(check).toBe(true);
});
it('should return false for error result', () => {
const result = err(new Error('fail'));
const check = isOk(result);
expect(check).toBe(false);
});
});
describe('isErr', () => {
it('should return true for error result', () => {
const result = err(new Error('fail'));
const check = isErr(result);
expect(check).toBe(true);
});
it('should return false for success result', () => {
const result = ok('value');
const check = isErr(result);
expect(check).toBe(false);
});
});
});