Skip to content

Commit 50ae2f8

Browse files
committed
feat: add unit tests for BestBlogData class
- added comprehensive unit tests for all methods in BestBlogData class - tests cover various scenarios including edge cases and error handling - improved code coverage and reliability - ensured all tests pass before committing changes
1 parent f4cdd65 commit 50ae2f8

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed

tests/blog-data.test.ts

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import BestBlogData from 'best-blog-data'
2+
import { describe, expect, it } from 'vitest'
3+
4+
const blogData = new BestBlogData()
5+
6+
describe('BestBlogData', () => {
7+
describe('getPosts()', () => {
8+
it('should return 10 posts on the first page', () => {
9+
const { posts, nextPageAvailable } = blogData.getPosts()
10+
expect(posts).toHaveLength(10)
11+
expect(typeof nextPageAvailable).toBe('boolean')
12+
})
13+
14+
it('should return different posts on page 2', () => {
15+
const page1 = blogData.getPosts(1)
16+
const page2 = blogData.getPosts(2)
17+
expect(page1.posts[0].slug).not.toBe(page2.posts[0].slug)
18+
})
19+
})
20+
21+
describe('getFullPostBySlug()', () => {
22+
it('should return a full post by slug', () => {
23+
const firstPost = blogData.getPosts().posts[0]
24+
const post = blogData.getFullPostBySlug(firstPost.slug)
25+
expect(post).toBeDefined()
26+
expect(post?.slug).toBe(firstPost.slug)
27+
expect(post?.content).toBeTruthy()
28+
})
29+
30+
it('should return undefined for an invalid slug', () => {
31+
const post = blogData.getFullPostBySlug('invalid-slug')
32+
expect(post).toBeUndefined()
33+
})
34+
})
35+
36+
describe('getPostsByCategory()', () => {
37+
it('should return posts for a valid category', () => {
38+
const categories = blogData.getAllCategories()
39+
const result = blogData.getPostsByCategory(categories[0].slug)
40+
expect(result.categoryFound).toBe(true)
41+
expect(result.posts.length).toBeGreaterThan(0)
42+
})
43+
44+
it('should return categoryFound false for an invalid category', () => {
45+
const result = blogData.getPostsByCategory('unknown-category')
46+
expect(result.categoryFound).toBe(false)
47+
expect(result.posts).toHaveLength(0)
48+
})
49+
})
50+
51+
describe('getAllCategories()', () => {
52+
it('should return a list of categories', () => {
53+
const categories = blogData.getAllCategories()
54+
expect(categories.length).toBeGreaterThan(0)
55+
expect(categories[0]).toHaveProperty('slug')
56+
expect(categories[0]).toHaveProperty('name')
57+
})
58+
})
59+
60+
describe('getPostsBySearch()', () => {
61+
it('should return posts matching the search query', () => {
62+
const results = blogData.getPostsBySearch('ai')
63+
expect(results.length).toBeGreaterThan(0)
64+
expect(results[0]).toHaveProperty('title')
65+
expect(results[0].slug.includes('ai')).toBeTruthy()
66+
})
67+
68+
it('should return empty array for unmatched search', () => {
69+
const results = blogData.getPostsBySearch('nonsensequerygibberish')
70+
expect(results).toHaveLength(0)
71+
})
72+
})
73+
})

0 commit comments

Comments
 (0)