Files
q-blog/tests/utils/fetchPosts.test.ts
greenflame089 0871c31b34 Add new tests
2025-08-21 18:51:12 -04:00

46 lines
1.7 KiB
TypeScript

import { describe, it, expect } from 'vitest';
import { server } from '../msw/server';
import { http, HttpResponse } from 'msw';
import { fetchAndEvaluatePosts } from '@/utils/fetchPosts';
const validPost = (overrides?: Partial<any>) => ({
title: 'Hello',
createdAt: 123,
postContent: [
{ type: 'image', version: 1, id: 'img1', content: { image: 'img-data' } },
{ type: 'editor', version: 1, id: 'ed1', content: [{ text: 'Some text' }] },
],
...overrides,
});
describe('fetchAndEvaluatePosts', () => {
it('returns isValid=false when structure invalid', async () => {
server.use(http.get('/arbitrary/BLOG_POST/:user/:postId', () => HttpResponse.json({})));
const res = await fetchAndEvaluatePosts({ user: 'u', postId: 'p', content: {} });
expect(res.isValid).toBe(false);
});
it('maps fields and extracts description when editor present', async () => {
server.use(
http.get('/arbitrary/BLOG_POST/:user/:postId', () => HttpResponse.json(validPost())),
);
const content = { description: '' };
const res = await fetchAndEvaluatePosts({ user: 'alice', postId: 'post1', content });
expect(res.isValid).toBe(true);
expect(res.user).toBe('alice');
expect(res.id).toBe('post1');
expect(res.title).toBe('Hello');
expect(res.postImage).toBe('img-data');
expect(res.description).toBe('Some text');
});
it('keeps existing description when provided', async () => {
server.use(
http.get('/arbitrary/BLOG_POST/:user/:postId', () => HttpResponse.json(validPost())),
);
const content = { description: 'keep me' };
const res = await fetchAndEvaluatePosts({ user: 'alice', postId: 'post1', content });
expect(res.description).toBe('keep me');
});
});