|
| 1 | +// @vitest-environment jsdom |
| 2 | +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; |
| 3 | +import { render, screen, waitFor } from '@testing-library/react'; |
| 4 | +import type { Author, Post } from '@/components/blog/BlogFilters'; |
| 5 | +import BlogFilters from '@/components/blog/BlogFilters'; |
| 6 | + |
| 7 | +let searchParams = new URLSearchParams(); |
| 8 | +const replaceMock = vi.fn(); |
| 9 | + |
| 10 | +vi.mock('next/navigation', () => ({ |
| 11 | + useSearchParams: () => searchParams, |
| 12 | +})); |
| 13 | + |
| 14 | +vi.mock('@/i18n/routing', () => ({ |
| 15 | + Link: ({ href, children }: { href: string; children: React.ReactNode }) => ( |
| 16 | + <a href={href}>{children}</a> |
| 17 | + ), |
| 18 | + usePathname: () => '/blog', |
| 19 | + useRouter: () => ({ |
| 20 | + replace: replaceMock, |
| 21 | + }), |
| 22 | +})); |
| 23 | + |
| 24 | +vi.mock('next-intl', () => ({ |
| 25 | + useTranslations: () => (key: string) => { |
| 26 | + const map: Record<string, string> = { |
| 27 | + 'categories.tech': 'Технології', |
| 28 | + 'categories.career': 'Кар\'єра', |
| 29 | + 'categories.insights': 'Інсайти', |
| 30 | + 'categories.news': 'Новини', |
| 31 | + author: 'Автор', |
| 32 | + blog: 'Блог', |
| 33 | + noPosts: 'Статей не знайдено', |
| 34 | + all: 'Усі', |
| 35 | + articlesBy: 'Статті', |
| 36 | + articlesPublished: 'Опубліковано', |
| 37 | + }; |
| 38 | + return map[key] || key; |
| 39 | + }, |
| 40 | + useLocale: () => 'uk', |
| 41 | +})); |
| 42 | + |
| 43 | +vi.mock('@/components/blog/BlogGrid', () => ({ |
| 44 | + default: ({ posts }: { posts: Post[] }) => ( |
| 45 | + <ul data-testid="blog-grid"> |
| 46 | + {posts.map(post => ( |
| 47 | + <li key={post._id}>{post.title}</li> |
| 48 | + ))} |
| 49 | + </ul> |
| 50 | + ), |
| 51 | +})); |
| 52 | + |
| 53 | +afterEach(() => { |
| 54 | + replaceMock.mockReset(); |
| 55 | + searchParams = new URLSearchParams(); |
| 56 | + vi.restoreAllMocks(); |
| 57 | +}); |
| 58 | + |
| 59 | +describe('BlogFilters', () => { |
| 60 | + it('filters posts by search query in title or body', () => { |
| 61 | + searchParams = new URLSearchParams({ search: 'співбесіди' }); |
| 62 | + const posts: Post[] = [ |
| 63 | + { |
| 64 | + _id: '1', |
| 65 | + title: 'Як підготуватися до співбесіди', |
| 66 | + slug: { current: 'interview' }, |
| 67 | + body: [ |
| 68 | + { |
| 69 | + _type: 'block', |
| 70 | + children: [{ _type: 'span', text: 'Поради для співбесіди' }], |
| 71 | + }, |
| 72 | + ], |
| 73 | + }, |
| 74 | + { |
| 75 | + _id: '2', |
| 76 | + title: 'CSS підказки', |
| 77 | + slug: { current: 'css-tips' }, |
| 78 | + body: [ |
| 79 | + { |
| 80 | + _type: 'block', |
| 81 | + children: [{ _type: 'span', text: 'Про Flexbox' }], |
| 82 | + }, |
| 83 | + ], |
| 84 | + }, |
| 85 | + ]; |
| 86 | + |
| 87 | + render(<BlogFilters posts={posts} categories={[]} />); |
| 88 | + |
| 89 | + const grid = screen.getByTestId('blog-grid'); |
| 90 | + expect(grid).toHaveTextContent('Як підготуватися до співбесіди'); |
| 91 | + expect(grid).not.toHaveTextContent('CSS підказки'); |
| 92 | + }); |
| 93 | + |
| 94 | + it('filters posts by author and renders author heading', async () => { |
| 95 | + searchParams = new URLSearchParams({ author: 'Анна' }); |
| 96 | + const posts: Post[] = [ |
| 97 | + { |
| 98 | + _id: '1', |
| 99 | + title: 'Пост Анни', |
| 100 | + slug: { current: 'anna-post' }, |
| 101 | + author: { name: 'Анна' }, |
| 102 | + }, |
| 103 | + { |
| 104 | + _id: '2', |
| 105 | + title: 'Пост Віктора', |
| 106 | + slug: { current: 'viktor-post' }, |
| 107 | + author: { name: 'Віктор' }, |
| 108 | + }, |
| 109 | + ]; |
| 110 | + |
| 111 | + const authorPayload: Author = { |
| 112 | + name: 'Анна', |
| 113 | + jobTitle: 'QA', |
| 114 | + company: 'DevLovers', |
| 115 | + }; |
| 116 | + |
| 117 | + const fetchMock = vi.fn().mockResolvedValue({ |
| 118 | + ok: true, |
| 119 | + json: async () => authorPayload, |
| 120 | + }); |
| 121 | + vi.stubGlobal('fetch', fetchMock as typeof fetch); |
| 122 | + |
| 123 | + render(<BlogFilters posts={posts} categories={[]} />); |
| 124 | + |
| 125 | + await waitFor(() => { |
| 126 | + expect(fetchMock).toHaveBeenCalled(); |
| 127 | + }); |
| 128 | + |
| 129 | + expect( |
| 130 | + screen.getByRole('heading', { name: 'Анна' }) |
| 131 | + ).toBeInTheDocument(); |
| 132 | + |
| 133 | + const grid = screen.getByTestId('blog-grid'); |
| 134 | + expect(grid).toHaveTextContent('Пост Анни'); |
| 135 | + expect(grid).not.toHaveTextContent('Пост Віктора'); |
| 136 | + }); |
| 137 | +}); |
0 commit comments