|
| 1 | +import { render, screen } from '@testing-library/react'; |
| 2 | + |
| 3 | +import { PostCategory, PostMetadata, SnippetMetadata } from '@/lib/types'; |
| 4 | +import IndexPage, { |
| 5 | + getStaticProps as getIndexStaticProps, |
| 6 | +} from '@/pages/index'; |
| 7 | +import SnippetsPage, { |
| 8 | + getStaticProps as getSnippetsStaticProps, |
| 9 | +} from '@/pages/snippets'; |
| 10 | + |
| 11 | +const makePost = (slug: string, featured = false): PostMetadata => ({ |
| 12 | + data: { |
| 13 | + slug, |
| 14 | + title: `${slug} title`, |
| 15 | + heading: `${slug} heading`, |
| 16 | + description: `${slug} description`, |
| 17 | + createDate: 1700000000000, |
| 18 | + keywords: [], |
| 19 | + updateDate: null, |
| 20 | + readTime: '3 min read', |
| 21 | + featured, |
| 22 | + categories: featured ? [PostCategory.JS] : [PostCategory.AdvancedReact], |
| 23 | + }, |
| 24 | +}); |
| 25 | + |
| 26 | +const makeSnippet = (slug: string): SnippetMetadata => ({ |
| 27 | + data: { |
| 28 | + slug, |
| 29 | + title: `${slug} title`, |
| 30 | + heading: `${slug} heading`, |
| 31 | + description: `${slug} description`, |
| 32 | + createDate: 1700000000000, |
| 33 | + keywords: [], |
| 34 | + updateDate: null, |
| 35 | + }, |
| 36 | +}); |
| 37 | + |
| 38 | +describe('list pages static props payload', () => { |
| 39 | + test('homepage props contain post metadata only, no content', async () => { |
| 40 | + const result = await getIndexStaticProps(); |
| 41 | + |
| 42 | + if (!('props' in result)) { |
| 43 | + throw new Error('expected getStaticProps to return props'); |
| 44 | + } |
| 45 | + |
| 46 | + const { featuredPosts, otherPosts, advancedReactPosts } = result.props; |
| 47 | + const allPosts = [...featuredPosts, ...otherPosts, ...advancedReactPosts]; |
| 48 | + |
| 49 | + expect(allPosts.length).toBeGreaterThan(0); |
| 50 | + for (const post of allPosts) { |
| 51 | + expect(post).not.toHaveProperty('content'); |
| 52 | + expect(post.data.slug).toEqual(expect.any(String)); |
| 53 | + expect(post.data.heading).toEqual(expect.any(String)); |
| 54 | + } |
| 55 | + }); |
| 56 | + |
| 57 | + test('snippets page props contain snippet metadata only, no content', async () => { |
| 58 | + const result = await getSnippetsStaticProps(); |
| 59 | + |
| 60 | + if (!('props' in result)) { |
| 61 | + throw new Error('expected getStaticProps to return props'); |
| 62 | + } |
| 63 | + |
| 64 | + const { snippets, jsonLd } = result.props; |
| 65 | + |
| 66 | + expect(snippets.length).toBeGreaterThan(0); |
| 67 | + for (const snippet of snippets) { |
| 68 | + expect(snippet).not.toHaveProperty('content'); |
| 69 | + expect(snippet.data.heading).toEqual(expect.any(String)); |
| 70 | + } |
| 71 | + expect(jsonLd).toHaveProperty('@type', 'CollectionPage'); |
| 72 | + }); |
| 73 | +}); |
| 74 | + |
| 75 | +describe('list pages render from metadata-only props', () => { |
| 76 | + test('homepage renders post headings', () => { |
| 77 | + render( |
| 78 | + <IndexPage |
| 79 | + featuredPosts={[makePost('feat-one', true)]} |
| 80 | + otherPosts={[makePost('other-one')]} |
| 81 | + advancedReactPosts={[makePost('react-one')]} |
| 82 | + />, |
| 83 | + ); |
| 84 | + |
| 85 | + expect( |
| 86 | + screen.getByRole('heading', { name: 'Serhii Shramko' }), |
| 87 | + ).toBeInTheDocument(); |
| 88 | + expect(screen.getByText('feat-one heading')).toBeInTheDocument(); |
| 89 | + expect(screen.getByText('react-one heading')).toBeInTheDocument(); |
| 90 | + expect(screen.getByText('other-one heading')).toBeInTheDocument(); |
| 91 | + }); |
| 92 | + |
| 93 | + test('snippets page renders snippet cards', () => { |
| 94 | + render( |
| 95 | + <SnippetsPage snippets={[makeSnippet('use-debounce')]} jsonLd={{}} />, |
| 96 | + ); |
| 97 | + |
| 98 | + expect( |
| 99 | + screen.getByRole('heading', { name: 'Code Snippets' }), |
| 100 | + ).toBeInTheDocument(); |
| 101 | + expect(screen.getByText('use-debounce heading')).toBeInTheDocument(); |
| 102 | + }); |
| 103 | +}); |
0 commit comments