view data tests
This commit is contained in:
@@ -0,0 +1,151 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { OnboardingViewDataBuilder } from './OnboardingViewDataBuilder';
|
||||
import { Result } from '@/lib/contracts/Result';
|
||||
|
||||
describe('OnboardingViewDataBuilder', () => {
|
||||
describe('happy paths', () => {
|
||||
it('should transform successful onboarding check to ViewData correctly', () => {
|
||||
const apiDto: Result<{ isAlreadyOnboarded: boolean }, any> = Result.ok({
|
||||
isAlreadyOnboarded: false,
|
||||
});
|
||||
|
||||
const result = OnboardingViewDataBuilder.build(apiDto);
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(result.unwrap()).toEqual({
|
||||
isAlreadyOnboarded: false,
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle already onboarded user correctly', () => {
|
||||
const apiDto: Result<{ isAlreadyOnboarded: boolean }, any> = Result.ok({
|
||||
isAlreadyOnboarded: true,
|
||||
});
|
||||
|
||||
const result = OnboardingViewDataBuilder.build(apiDto);
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(result.unwrap()).toEqual({
|
||||
isAlreadyOnboarded: true,
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle missing isAlreadyOnboarded field with default false', () => {
|
||||
const apiDto: Result<{ isAlreadyOnboarded?: boolean }, any> = Result.ok({});
|
||||
|
||||
const result = OnboardingViewDataBuilder.build(apiDto);
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(result.unwrap()).toEqual({
|
||||
isAlreadyOnboarded: false,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('error handling', () => {
|
||||
it('should propagate unauthorized error', () => {
|
||||
const apiDto: Result<{ isAlreadyOnboarded: boolean }, any> = Result.err('unauthorized');
|
||||
|
||||
const result = OnboardingViewDataBuilder.build(apiDto);
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
expect(result.getError()).toBe('unauthorized');
|
||||
});
|
||||
|
||||
it('should propagate notFound error', () => {
|
||||
const apiDto: Result<{ isAlreadyOnboarded: boolean }, any> = Result.err('notFound');
|
||||
|
||||
const result = OnboardingViewDataBuilder.build(apiDto);
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
expect(result.getError()).toBe('notFound');
|
||||
});
|
||||
|
||||
it('should propagate serverError', () => {
|
||||
const apiDto: Result<{ isAlreadyOnboarded: boolean }, any> = Result.err('serverError');
|
||||
|
||||
const result = OnboardingViewDataBuilder.build(apiDto);
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
expect(result.getError()).toBe('serverError');
|
||||
});
|
||||
|
||||
it('should propagate networkError', () => {
|
||||
const apiDto: Result<{ isAlreadyOnboarded: boolean }, any> = Result.err('networkError');
|
||||
|
||||
const result = OnboardingViewDataBuilder.build(apiDto);
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
expect(result.getError()).toBe('networkError');
|
||||
});
|
||||
|
||||
it('should propagate validationError', () => {
|
||||
const apiDto: Result<{ isAlreadyOnboarded: boolean }, any> = Result.err('validationError');
|
||||
|
||||
const result = OnboardingViewDataBuilder.build(apiDto);
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
expect(result.getError()).toBe('validationError');
|
||||
});
|
||||
|
||||
it('should propagate unknown error', () => {
|
||||
const apiDto: Result<{ isAlreadyOnboarded: boolean }, any> = Result.err('unknown');
|
||||
|
||||
const result = OnboardingViewDataBuilder.build(apiDto);
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
expect(result.getError()).toBe('unknown');
|
||||
});
|
||||
});
|
||||
|
||||
describe('data transformation', () => {
|
||||
it('should preserve all DTO fields in the output', () => {
|
||||
const apiDto: Result<{ isAlreadyOnboarded: boolean }, any> = Result.ok({
|
||||
isAlreadyOnboarded: false,
|
||||
});
|
||||
|
||||
const result = OnboardingViewDataBuilder.build(apiDto);
|
||||
|
||||
expect(result.unwrap().isAlreadyOnboarded).toBe(false);
|
||||
});
|
||||
|
||||
it('should not modify the input DTO', () => {
|
||||
const apiDto: Result<{ isAlreadyOnboarded: boolean }, any> = Result.ok({
|
||||
isAlreadyOnboarded: false,
|
||||
});
|
||||
|
||||
const originalDto = { ...apiDto.unwrap() };
|
||||
OnboardingViewDataBuilder.build(apiDto);
|
||||
|
||||
expect(apiDto.unwrap()).toEqual(originalDto);
|
||||
});
|
||||
});
|
||||
|
||||
describe('edge cases', () => {
|
||||
it('should handle null isAlreadyOnboarded as false', () => {
|
||||
const apiDto: Result<{ isAlreadyOnboarded: boolean | null }, any> = Result.ok({
|
||||
isAlreadyOnboarded: null,
|
||||
});
|
||||
|
||||
const result = OnboardingViewDataBuilder.build(apiDto);
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(result.unwrap()).toEqual({
|
||||
isAlreadyOnboarded: false,
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle undefined isAlreadyOnboarded as false', () => {
|
||||
const apiDto: Result<{ isAlreadyOnboarded: boolean | undefined }, any> = Result.ok({
|
||||
isAlreadyOnboarded: undefined,
|
||||
});
|
||||
|
||||
const result = OnboardingViewDataBuilder.build(apiDto);
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(result.unwrap()).toEqual({
|
||||
isAlreadyOnboarded: false,
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user