Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions components/dashboard/tooltipUtils.mock-integrations.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { describe, expect, it, vi } from 'vitest';
import {
formatTooltipDate,
getActivityInsight,
getContributionLabel,
getLocalActiveStreak,
getStreakLabel,
} from './tooltipUtils';

describe('tooltipUtils mock integrations', () => {
it('mocks async contribution fetch successfully', async () => {
const mockFetch = vi.fn().mockResolvedValue({
count: 5,
});

const result = await mockFetch();

expect(result.count).toBe(5);
expect(mockFetch).toHaveBeenCalled();
});

it('uses cached tooltip label when available', () => {
const cache = new Map();

cache.set('label', getContributionLabel(3));

expect(cache.get('label')).toBe('3 contributions');
});

it('formats cached tooltip dates correctly', () => {
const cacheDate = formatTooltipDate('2024-01-10');

expect(cacheDate).toBe('Jan 10, 2024');
});

it('falls back safely during async timeout simulation', async () => {
const mockTimeout = vi.fn().mockRejectedValue(new Error('Timeout'));

await expect(mockTimeout()).rejects.toThrow('Timeout');
});

it('syncs streak cache correctly after async update', async () => {
const mockData = [
{ date: '2024-01-01', count: 1, intensity: 1 },
{ date: '2024-01-02', count: 2, intensity: 2 },
];

const mockSync = vi.fn().mockResolvedValue(mockData);

const result = await mockSync();

expect(getLocalActiveStreak(result, 0)).toBe(2);
expect(getActivityInsight(2, 2)).toBe('Steady contribution day');
expect(getStreakLabel(2)).toBe('2-day active streak');
});
});
34 changes: 34 additions & 0 deletions components/dashboard/tooltipUtils.timezone-boundaries.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { describe, expect, it } from 'vitest';
import { formatTooltipDate, getLocalActiveStreak, getStreakLabel } from './tooltipUtils';

describe('tooltipUtils timezone boundaries', () => {
it('keeps UTC date stable across timezone boundaries', () => {
expect(formatTooltipDate('2024-01-01')).toBe('Jan 1, 2024');
});

it('correctly formats leap year boundary dates', () => {
expect(formatTooltipDate('2024-02-29')).toBe('Feb 29, 2024');
});

it('correctly formats year-end boundary dates', () => {
expect(formatTooltipDate('2024-12-31')).toBe('Dec 31, 2024');
});

it('preserves active streak across consecutive calendar days', () => {
const data: {
date: string;
count: number;
intensity: 1 | 2 | 3 | 4;
}[] = [
{ date: '2024-01-01', count: 1, intensity: 1 },
{ date: '2024-01-02', count: 2, intensity: 2 },
{ date: '2024-01-03', count: 3, intensity: 3 },
];

expect(getLocalActiveStreak(data, 1)).toBe(3);
});

it('returns correct streak label after boundary calculations', () => {
expect(getStreakLabel(3)).toBe('3-day active streak');
});
});
58 changes: 58 additions & 0 deletions components/dashboard/tooltipUtils.type-compiler.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { describe, expectTypeOf, it } from 'vitest';
import type { ActivityData } from '@/types/dashboard';

describe('tooltipUtils type compiler tests', () => {
it('validates ActivityData structure correctly', () => {
const validData: ActivityData = {
date: '2024-01-01',
count: 5,
intensity: 2,
};

expectTypeOf(validData.date).toBeString();
expectTypeOf(validData.count).toBeNumber();
expectTypeOf(validData.intensity).toBeNumber();
});

it('ensures ActivityData array typing works', () => {
const data: ActivityData[] = [
{
date: '2024-01-01',
count: 1,
intensity: 1,
},
];

expectTypeOf(data).toBeArray();
});

it('accepts optional compatible values safely', () => {
const data: ActivityData = {
date: '2024-01-02',
count: 0,
intensity: 0,
};

expectTypeOf(data.count).toEqualTypeOf<number>();
});
it('rejects invalid property types during compilation', () => {
expectTypeOf<ActivityData>().toBeObject();

const invalidCount: ActivityData = {
date: '2024-01-01',
// @ts-expect-error count must be number
count: 'five',
intensity: 2,
};
});

it('rejects missing required properties', () => {
expectTypeOf<ActivityData>().toBeObject();

// @ts-expect-error intensity is required
const invalidData: ActivityData = {
date: '2024-01-01',
count: 5,
};
});
});
1 change: 0 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading