-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8002dc0
commit a9f5ea9
Showing
7 changed files
with
288 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}) | ||
}) |
Oops, something went wrong.
a9f5ea9
There was a problem hiding this comment.
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