view data tests
This commit is contained in:
167
apps/website/lib/builders/view-data/HomeViewDataBuilder.test.ts
Normal file
167
apps/website/lib/builders/view-data/HomeViewDataBuilder.test.ts
Normal file
@@ -0,0 +1,167 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { HomeViewDataBuilder } from './HomeViewDataBuilder';
|
||||
import type { HomeDataDTO } from '@/lib/types/dtos/HomeDataDTO';
|
||||
|
||||
describe('HomeViewDataBuilder', () => {
|
||||
describe('happy paths', () => {
|
||||
it('should transform HomeDataDTO to HomeViewData correctly', () => {
|
||||
const homeDataDto: HomeDataDTO = {
|
||||
isAlpha: true,
|
||||
upcomingRaces: [
|
||||
{
|
||||
id: 'race-1',
|
||||
name: 'Test Race',
|
||||
scheduledAt: '2024-01-01T10:00:00Z',
|
||||
track: 'Test Track',
|
||||
},
|
||||
],
|
||||
topLeagues: [
|
||||
{
|
||||
id: 'league-1',
|
||||
name: 'Test League',
|
||||
description: 'Test Description',
|
||||
},
|
||||
],
|
||||
teams: [
|
||||
{
|
||||
id: 'team-1',
|
||||
name: 'Test Team',
|
||||
tag: 'TT',
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
const result = HomeViewDataBuilder.build(homeDataDto);
|
||||
|
||||
expect(result).toEqual({
|
||||
isAlpha: true,
|
||||
upcomingRaces: [
|
||||
{
|
||||
id: 'race-1',
|
||||
name: 'Test Race',
|
||||
scheduledAt: '2024-01-01T10:00:00Z',
|
||||
track: 'Test Track',
|
||||
},
|
||||
],
|
||||
topLeagues: [
|
||||
{
|
||||
id: 'league-1',
|
||||
name: 'Test League',
|
||||
description: 'Test Description',
|
||||
},
|
||||
],
|
||||
teams: [
|
||||
{
|
||||
id: 'team-1',
|
||||
name: 'Test Team',
|
||||
tag: 'TT',
|
||||
},
|
||||
],
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle empty arrays correctly', () => {
|
||||
const homeDataDto: HomeDataDTO = {
|
||||
isAlpha: false,
|
||||
upcomingRaces: [],
|
||||
topLeagues: [],
|
||||
teams: [],
|
||||
};
|
||||
|
||||
const result = HomeViewDataBuilder.build(homeDataDto);
|
||||
|
||||
expect(result).toEqual({
|
||||
isAlpha: false,
|
||||
upcomingRaces: [],
|
||||
topLeagues: [],
|
||||
teams: [],
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle multiple items in arrays', () => {
|
||||
const homeDataDto: HomeDataDTO = {
|
||||
isAlpha: true,
|
||||
upcomingRaces: [
|
||||
{ id: 'race-1', name: 'Race 1', scheduledAt: '2024-01-01T10:00:00Z', track: 'Track 1' },
|
||||
{ id: 'race-2', name: 'Race 2', scheduledAt: '2024-01-02T10:00:00Z', track: 'Track 2' },
|
||||
],
|
||||
topLeagues: [
|
||||
{ id: 'league-1', name: 'League 1', description: 'Description 1' },
|
||||
{ id: 'league-2', name: 'League 2', description: 'Description 2' },
|
||||
],
|
||||
teams: [
|
||||
{ id: 'team-1', name: 'Team 1', tag: 'T1' },
|
||||
{ id: 'team-2', name: 'Team 2', tag: 'T2' },
|
||||
],
|
||||
};
|
||||
|
||||
const result = HomeViewDataBuilder.build(homeDataDto);
|
||||
|
||||
expect(result.upcomingRaces).toHaveLength(2);
|
||||
expect(result.topLeagues).toHaveLength(2);
|
||||
expect(result.teams).toHaveLength(2);
|
||||
});
|
||||
});
|
||||
|
||||
describe('data transformation', () => {
|
||||
it('should preserve all DTO fields in the output', () => {
|
||||
const homeDataDto: HomeDataDTO = {
|
||||
isAlpha: true,
|
||||
upcomingRaces: [{ id: 'race-1', name: 'Race', scheduledAt: '2024-01-01T10:00:00Z', track: 'Track' }],
|
||||
topLeagues: [{ id: 'league-1', name: 'League', description: 'Description' }],
|
||||
teams: [{ id: 'team-1', name: 'Team', tag: 'T' }],
|
||||
};
|
||||
|
||||
const result = HomeViewDataBuilder.build(homeDataDto);
|
||||
|
||||
expect(result.isAlpha).toBe(homeDataDto.isAlpha);
|
||||
expect(result.upcomingRaces).toEqual(homeDataDto.upcomingRaces);
|
||||
expect(result.topLeagues).toEqual(homeDataDto.topLeagues);
|
||||
expect(result.teams).toEqual(homeDataDto.teams);
|
||||
});
|
||||
|
||||
it('should not modify the input DTO', () => {
|
||||
const homeDataDto: HomeDataDTO = {
|
||||
isAlpha: true,
|
||||
upcomingRaces: [{ id: 'race-1', name: 'Race', scheduledAt: '2024-01-01T10:00:00Z', track: 'Track' }],
|
||||
topLeagues: [{ id: 'league-1', name: 'League', description: 'Description' }],
|
||||
teams: [{ id: 'team-1', name: 'Team', tag: 'T' }],
|
||||
};
|
||||
|
||||
const originalDto = { ...homeDataDto };
|
||||
HomeViewDataBuilder.build(homeDataDto);
|
||||
|
||||
expect(homeDataDto).toEqual(originalDto);
|
||||
});
|
||||
});
|
||||
|
||||
describe('edge cases', () => {
|
||||
it('should handle false isAlpha value', () => {
|
||||
const homeDataDto: HomeDataDTO = {
|
||||
isAlpha: false,
|
||||
upcomingRaces: [],
|
||||
topLeagues: [],
|
||||
teams: [],
|
||||
};
|
||||
|
||||
const result = HomeViewDataBuilder.build(homeDataDto);
|
||||
|
||||
expect(result.isAlpha).toBe(false);
|
||||
});
|
||||
|
||||
it('should handle null/undefined values in arrays', () => {
|
||||
const homeDataDto: HomeDataDTO = {
|
||||
isAlpha: true,
|
||||
upcomingRaces: [{ id: 'race-1', name: 'Race', scheduledAt: '2024-01-01T10:00:00Z', track: 'Track' }],
|
||||
topLeagues: [{ id: 'league-1', name: 'League', description: 'Description' }],
|
||||
teams: [{ id: 'team-1', name: 'Team', tag: 'T' }],
|
||||
};
|
||||
|
||||
const result = HomeViewDataBuilder.build(homeDataDto);
|
||||
|
||||
expect(result.upcomingRaces[0].id).toBe('race-1');
|
||||
expect(result.topLeagues[0].id).toBe('league-1');
|
||||
expect(result.teams[0].id).toBe('team-1');
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user