Files
framit/app/composables/useDataJson.test.ts
Lucien Cartier-Tilet 70e4ce8b4b test: add comprehensive test suite for components, composables, and pages
Add 16 new test files covering:
- Composables: useBackend, useMeta, useDataJson
- Type classes: QueryResult, ResumeContent
- UI components: BadgeList, BadgeListCard
- Navbar components: LanguageSwitcher, ThemeSwitcher
- App components: AppNavbar, AppFooter
- VocalSynth components: Projects, Tools
- Pages: contact, resume, [...slug]

Tests focus on pure logic, interfaces, and component rendering where
possible, avoiding complex mocking of Nuxt auto-imported composables.

Total: 174 tests across 17 test files (including existing useApi tests).
2026-02-05 13:06:38 +01:00

188 lines
6.2 KiB
TypeScript

import { describe, it, expect } from 'vitest';
import { withLeadingSlash } from 'ufo';
describe('useDataJson', () => {
describe('withLeadingSlash utility', () => {
it('should add leading slash to path without one', () => {
expect(withLeadingSlash('test-page')).toBe('/test-page');
});
it('should preserve leading slash if already present', () => {
expect(withLeadingSlash('/test-page')).toBe('/test-page');
});
it('should handle empty string', () => {
expect(withLeadingSlash('')).toBe('/');
});
it('should handle complex paths', () => {
expect(withLeadingSlash('vocal-synthesis/keine-tashi')).toBe('/vocal-synthesis/keine-tashi');
});
});
describe('slug computation logic', () => {
it('should convert array slug to string with leading slash', () => {
const slugParam = ['vocal-synthesis', 'keine-tashi'];
const slug = withLeadingSlash(String(slugParam));
expect(slug).toBe('/vocal-synthesis,keine-tashi');
});
it('should use route path as fallback when no slug', () => {
const slugParam = '';
const routePath = '/fallback-path';
const slug = withLeadingSlash(String(slugParam || routePath));
expect(slug).toBe('/fallback-path');
});
it('should prefer slug param over route path', () => {
const slugParam = 'my-page';
const routePath = '/different-path';
const slug = withLeadingSlash(String(slugParam || routePath));
expect(slug).toBe('/my-page');
});
});
describe('key computation logic', () => {
it('should create cache key from prefix and slug', () => {
const prefix = 'page';
const slug = '/test-page';
const key = prefix + '-' + slug;
expect(key).toBe('page-/test-page');
});
it('should create unique keys for different prefixes', () => {
const slug = '/resume';
const pageKey = 'page' + '-' + slug;
const dataKey = 'page-data' + '-' + slug;
expect(pageKey).not.toBe(dataKey);
expect(pageKey).toBe('page-/resume');
expect(dataKey).toBe('page-data-/resume');
});
});
describe('collection name construction', () => {
it('should construct collection name from prefix and locale', () => {
const collectionPrefix = 'content_';
const locale = 'en';
const collection = collectionPrefix + locale;
expect(collection).toBe('content_en');
});
it('should handle French locale', () => {
const collectionPrefix = 'content_';
const locale = 'fr';
const collection = collectionPrefix + locale;
expect(collection).toBe('content_fr');
});
it('should handle data collection prefix', () => {
const collectionPrefix = 'content_data_';
const locale = 'en';
const collection = collectionPrefix + locale;
expect(collection).toBe('content_data_en');
});
});
describe('getData options structure', () => {
it('should support useFilter option', () => {
const options = { useFilter: true };
expect(options.useFilter).toBe(true);
});
it('should support fallbackToEnglish option', () => {
const options = { fallbackToEnglish: true };
expect(options.fallbackToEnglish).toBe(true);
});
it('should support extractMeta option', () => {
const options = { extractMeta: true };
expect(options.extractMeta).toBe(true);
});
it('should have sensible defaults', () => {
const options = {
useFilter: false,
fallbackToEnglish: false,
extractMeta: false,
};
expect(options.useFilter).toBe(false);
expect(options.fallbackToEnglish).toBe(false);
expect(options.extractMeta).toBe(false);
});
});
describe('getJsonData configuration', () => {
it('should use useFilter=true for data collections', () => {
// getJsonData calls getData with useFilter=true
const expectedOptions = { useFilter: true, extractMeta: true };
expect(expectedOptions.useFilter).toBe(true);
expect(expectedOptions.extractMeta).toBe(true);
});
it('should have default collection prefix', () => {
const defaultPrefix = 'content_data_';
expect(defaultPrefix).toBe('content_data_');
});
});
describe('getPageContent configuration', () => {
it('should use fallbackToEnglish by default', () => {
const defaultFallback = true;
expect(defaultFallback).toBe(true);
});
it('should have default collection prefix', () => {
const defaultPrefix = 'content_';
expect(defaultPrefix).toBe('content_');
});
});
describe('meta extraction logic', () => {
it('should return meta when extractMeta is true', () => {
const content = {
body: 'some content',
meta: { path: '/test', title: 'Test' },
};
const extractMeta = true;
const result = extractMeta ? content?.meta : content;
expect(result).toEqual({ path: '/test', title: 'Test' });
});
it('should return full content when extractMeta is false', () => {
const content = {
body: 'some content',
meta: { path: '/test', title: 'Test' },
};
const extractMeta = false;
const result = extractMeta ? content?.meta : content;
expect(result).toEqual(content);
});
it('should handle null content gracefully', () => {
const content = null;
const extractMeta = true;
const result = extractMeta ? content?.meta : content;
expect(result).toBeUndefined();
});
});
describe('filter logic for data collections', () => {
it('should filter by meta.path matching slug', () => {
const allData = [
{ meta: { path: '/resume' }, data: 'resume data' },
{ meta: { path: '/other' }, data: 'other data' },
];
const slug = '/resume';
const content = allData.filter((source) => source.meta.path === slug)[0];
expect(content).toEqual({ meta: { path: '/resume' }, data: 'resume data' });
});
it('should return undefined when no match found', () => {
const allData = [{ meta: { path: '/other' }, data: 'other data' }];
const slug = '/nonexistent';
const content = allData.filter((source) => source.meta.path === slug)[0];
expect(content).toBeUndefined();
});
});
});