Skip to content

Commit

Permalink
more unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kremalicious committed Sep 19, 2023
1 parent 8002dc0 commit a9f5ea9
Show file tree
Hide file tree
Showing 7 changed files with 288 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/components/Search/Search.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import Search from './Search'
let portalRoot: HTMLDivElement
let unsubscribe: () => void
let fetchSpy: any
let originalFetch: any
let storeState = false

beforeEach(() => {
Expand All @@ -19,6 +20,7 @@ beforeEach(() => {
})

// Mock fetch API
originalFetch = globalThis.fetch
globalThis.fetch = async () => {
return {
json: () =>
Expand All @@ -30,9 +32,9 @@ beforeEach(() => {
})

afterEach(() => {
// Cleanup
portalRoot.remove()
unsubscribe()
globalThis.fetch = originalFetch
})

test('Search component', async () => {
Expand Down
45 changes: 45 additions & 0 deletions src/lib/astro/getAllPosts.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { test, expect, vi, describe, beforeEach, afterEach } from 'vitest'
import * as loadAndFormatCollectionModule from './loadAndFormatCollection'
import { getAllPosts } from './getAllPosts'
import mockArticles from '@test/__fixtures__/getCollectionArticles.json'
import mockLinks from '@test/__fixtures__/getCollectionLinks.json'
import mockPhotos from '@test/__fixtures__/getCollectionPhotos.json'

let loadAndFormatCollectionSpy: any

beforeEach(() => {
loadAndFormatCollectionSpy = vi.spyOn(
loadAndFormatCollectionModule,
'loadAndFormatCollection'
)
})

afterEach(() => {
loadAndFormatCollectionSpy.mockRestore()
})

describe('getAllPosts', () => {
test('combines and sorts all posts', async () => {
loadAndFormatCollectionSpy.mockImplementation(
async (collectionName: string) => {
switch (collectionName) {
case 'articles':
return mockArticles
case 'links':
return mockLinks
case 'photos':
return mockPhotos
default:
return []
}
}
)

const result = await getAllPosts()

expect(result).toBeDefined()
expect(result.length).toBe(
mockArticles.length + mockLinks.length + mockPhotos.length
)
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ test('sortPosts sorts posts by date in descending order', () => {

getAllPostsSpy.mockImplementationOnce(() => Promise.resolve(posts))

// getAllPostsSpy = vi.spyOn(getAllPosts, async () => posts)
const sortedPosts = sortPosts(posts)
expect(sortedPosts[0].data.date).toStrictEqual('2022-01-03')
expect(sortedPosts[1].data.date).toStrictEqual('2022-01-02')
Expand Down
81 changes: 81 additions & 0 deletions src/lib/astro/loadAndFormatCollection.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { test, expect, vi, beforeEach, afterEach, describe } from 'vitest'
import * as astroContent from 'astro:content'
import * as exifLib from '@lib/exif'
import { loadAndFormatCollection } from './loadAndFormatCollection'
import getCollectionArticles from '@test/__fixtures__/getCollectionArticles.json'
import getCollectionLinks from '@test/__fixtures__/getCollectionLinks.json'
import getCollectionPhotos from '@test/__fixtures__/getCollectionPhotos.json'

let getCollectionSpy: any
let readOutExifSpy: any

beforeEach(() => {
getCollectionSpy = vi.spyOn(astroContent, 'getCollection')

readOutExifSpy = vi.spyOn(exifLib, 'readOutExif')
readOutExifSpy.mockImplementationOnce(async () => ({
exif: 'mocked exif',
iptc: 'mocked iptc'
}))
})

afterEach(() => {
getCollectionSpy.mockRestore()
readOutExifSpy.mockRestore()
})

describe('loadAndFormatCollection', () => {
test('loads articles', async () => {
getCollectionSpy.mockImplementationOnce(async () => getCollectionArticles)
const result = await loadAndFormatCollection('articles')
expect(result).toBeDefined()
expect(result.length).toBeGreaterThan(0)
})

test('loads links', async () => {
getCollectionSpy.mockImplementationOnce(async () => getCollectionLinks)
const result = await loadAndFormatCollection('links')
expect(result).toBeDefined()
expect(result.length).toBeGreaterThan(0)
})

test('loads photos', async () => {
getCollectionSpy.mockImplementationOnce(async () => getCollectionPhotos)
const result = await loadAndFormatCollection('photos')
expect(result).toBeDefined()
expect(result.length).toBeGreaterThan(0)
})

test('loads photos in production', async () => {
const originalEnv = import.meta.env.PROD
import.meta.env.PROD = true

getCollectionSpy.mockImplementationOnce(async () => getCollectionPhotos)
const result = await loadAndFormatCollection('photos')
expect(result).toBeDefined()
expect(result.length).toBeGreaterThan(0)

import.meta.env.PROD = originalEnv
})

test('filters out drafts in production', async () => {
const originalEnv = import.meta.env.PROD
import.meta.env.PROD = true

const mockDrafts = [
{ id: '/what/an/id/and/la', data: { draft: true }, otherFields: '...' },
{ id: '/what/an/id/and/le', data: { draft: false }, otherFields: '...' },
{ id: '/what/an/id/and/lu', data: { draft: true }, otherFields: '...' }
] as any

getCollectionSpy.mockImplementationOnce(async () => mockDrafts)
const result = await loadAndFormatCollection('articles')
expect(result).toBeDefined()
// Only one article should remain after filtering out drafts
expect(result.length).toBe(1)
// The remaining article should not be a draft
expect(result[0].data.draft).toBe(false)

import.meta.env.PROD = originalEnv
})
})
Loading

1 comment on commit a9f5ea9

@vercel
Copy link

@vercel vercel bot commented on a9f5ea9 Sep 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

blog – ./

blog-kremalicious.vercel.app
kremalicious.vercel.app
blog-git-main-kremalicious.vercel.app

Please sign in to comment.