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).
This commit is contained in:
126
app/pages/slug.test.ts
Normal file
126
app/pages/slug.test.ts
Normal file
@@ -0,0 +1,126 @@
|
||||
import { describe, it, expect, vi } from 'vitest';
|
||||
import { mountSuspended } from '@nuxt/test-utils/runtime';
|
||||
import SlugPage from './[...slug].vue';
|
||||
|
||||
// Mock useDataJson
|
||||
const mockPageContent = ref<{ title: string; description: string; meta?: { layout?: string } } | null>(null);
|
||||
const mockPageData = ref<Record<string, unknown> | null>(null);
|
||||
|
||||
vi.mock('~/composables/useDataJson', () => ({
|
||||
useDataJson: vi.fn((prefix: string) => {
|
||||
if (prefix === 'page') {
|
||||
return {
|
||||
getPageContent: vi.fn(async () => mockPageContent),
|
||||
};
|
||||
}
|
||||
if (prefix === 'page-data') {
|
||||
return {
|
||||
getJsonData: vi.fn(async () => mockPageData),
|
||||
};
|
||||
}
|
||||
return {
|
||||
getPageContent: vi.fn(async () => mockPageContent),
|
||||
getJsonData: vi.fn(async () => mockPageData),
|
||||
};
|
||||
}),
|
||||
}));
|
||||
|
||||
// Mock useMeta
|
||||
vi.mock('~/composables/useMeta', () => ({
|
||||
useMeta: vi.fn(),
|
||||
}));
|
||||
|
||||
describe('Slug Page (Catch-all)', () => {
|
||||
describe('rendering', () => {
|
||||
it('should render the page when content exists', async () => {
|
||||
mockPageContent.value = {
|
||||
title: 'Test Page',
|
||||
description: 'A test page',
|
||||
};
|
||||
|
||||
const wrapper = await mountSuspended(SlugPage);
|
||||
|
||||
expect(wrapper.exists()).toBe(true);
|
||||
});
|
||||
|
||||
it('should show not found message when page is null', async () => {
|
||||
mockPageContent.value = null;
|
||||
|
||||
const wrapper = await mountSuspended(SlugPage);
|
||||
|
||||
expect(wrapper.text()).toContain('Page not found');
|
||||
});
|
||||
});
|
||||
|
||||
describe('layout selection', () => {
|
||||
it('should use default layout when no custom layout specified', async () => {
|
||||
mockPageContent.value = {
|
||||
title: 'Test Page',
|
||||
description: 'A test page',
|
||||
};
|
||||
|
||||
const wrapper = await mountSuspended(SlugPage);
|
||||
|
||||
expect(wrapper.exists()).toBe(true);
|
||||
});
|
||||
|
||||
it('should use custom layout when specified in meta', async () => {
|
||||
mockPageContent.value = {
|
||||
title: 'Centered Page',
|
||||
description: 'A centered page',
|
||||
meta: { layout: 'centered' },
|
||||
};
|
||||
|
||||
const wrapper = await mountSuspended(SlugPage);
|
||||
|
||||
expect(wrapper.exists()).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('page data injection', () => {
|
||||
it('should provide pageData to child components', async () => {
|
||||
mockPageContent.value = {
|
||||
title: 'Vocal Synthesis',
|
||||
description: 'Vocal synthesis projects',
|
||||
};
|
||||
mockPageData.value = {
|
||||
projects: [{ title: 'Project 1' }],
|
||||
tools: [{ name: 'Tool 1' }],
|
||||
};
|
||||
|
||||
const wrapper = await mountSuspended(SlugPage);
|
||||
|
||||
// Page data should be provided for MDC components
|
||||
expect(wrapper.exists()).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('content rendering', () => {
|
||||
it('should render ContentRenderer when page exists', async () => {
|
||||
mockPageContent.value = {
|
||||
title: 'Test Content',
|
||||
description: 'Test description',
|
||||
};
|
||||
|
||||
const wrapper = await mountSuspended(SlugPage);
|
||||
|
||||
expect(wrapper.exists()).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('SEO meta', () => {
|
||||
it('should call useMeta with page title and description', async () => {
|
||||
const { useMeta } = await import('~/composables/useMeta');
|
||||
|
||||
mockPageContent.value = {
|
||||
title: 'SEO Test Page',
|
||||
description: 'Testing SEO metadata',
|
||||
};
|
||||
|
||||
await mountSuspended(SlugPage);
|
||||
|
||||
// useMeta should have been called
|
||||
expect(useMeta).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user