1
0
mirror of https://github.com/kremalicious/astro-redirect-from.git synced 2024-11-22 09:57:03 +01:00

getRedirects test

This commit is contained in:
Matthias Kretschmann 2023-09-24 00:36:44 +01:00
parent cc0ba7d390
commit 23b8f01601
Signed by: m
GPG Key ID: 606EEEF3C479A91F
5 changed files with 46 additions and 7 deletions

View File

@ -25,9 +25,9 @@ export function getSlugFromFilePath(filePath: string) {
// - folder name if file name is index.md, or // - folder name if file name is index.md, or
// - file name // - file name
if (parsedPath.base === 'index.md' || parsedPath.base === 'index.mdx') { if (parsedPath.base === 'index.md' || parsedPath.base === 'index.mdx') {
slug = `/${parsedPath.dir}` slug = `${parsedPath.dir}`
} else { } else {
slug = `/${parsedPath.dir}/${parsedPath.name}` slug = `${parsedPath.dir}/${parsedPath.name}`
} }
return slug return slug

View File

@ -0,0 +1,6 @@
---
slug: hello-astroooooo
redirect_from:
- /hello-astro-old
- hello-astro-old-234837
---

View File

@ -0,0 +1,6 @@
---
draft: true
redirect_from:
- /hello-draft-old
---

27
test/getRedirects.test.ts Normal file
View File

@ -0,0 +1,27 @@
import { expect, it, describe } from 'vitest'
import { getRedirects } from '../src/getRedirects'
import { getMarkdownFiles, getSlugFromFilePath } from '../src/utils'
describe('getRedirects', async () => {
// handling this more as an integration test
const srcDir = './test/__fixtures__/markdown'
const files = await getMarkdownFiles(srcDir)
it('should return redirects', async () => {
const result = await getRedirects(
files,
srcDir,
getSlugFromFilePath,
'build'
)
expect(result).toBeInstanceOf(Object)
expect(result).toStrictEqual({
'/hello-astro-old': '/hello-astroooooo',
'/hello-astro-old-234837': '/hello-astroooooo',
'/hello-world-old': '/hello-world',
'/hello-world-old-234837': '/hello-world',
'/hello-markdown-old': '/hello-markdown',
'/hello-markdown-old-234837': '/hello-markdown'
})
})
})

View File

@ -11,7 +11,7 @@ describe('getMarkdownFiles', () => {
it('should return an array of markdown files from the given directory', async () => { it('should return an array of markdown files from the given directory', async () => {
const files = await getMarkdownFiles('./test/__fixtures__/markdown') const files = await getMarkdownFiles('./test/__fixtures__/markdown')
expect(files).toBeInstanceOf(Array) expect(files).toBeInstanceOf(Array)
expect(files).toHaveLength(2) expect(files).toHaveLength(4)
}) })
}) })
@ -28,17 +28,17 @@ describe('getMarkdownFrontmatter', () => {
describe('getSlugFromFilePath', () => { describe('getSlugFromFilePath', () => {
it('should return slug for testDir/testFile.md', () => { it('should return slug for testDir/testFile.md', () => {
const slug = getSlugFromFilePath('testDir/testFile.md') const slug = getSlugFromFilePath('testDir/testFile.md')
expect(slug).toBe('/testDir/testFile') expect(slug).toBe('testDir/testFile')
}) })
it('should return slug for dir/testDir/index.md', () => { it('should return slug for dir/testDir/index.md', () => {
const slug = getSlugFromFilePath('dir/testDir/index.md') const slug = getSlugFromFilePath('dir/testDir/index.md')
expect(slug).toBe('/dir/testDir') expect(slug).toBe('dir/testDir')
}) })
it('should return slug for dir/dir2/testDir/index.md', () => { it('should return slug for dir/dir2/testDir/index.md', () => {
const slug = getSlugFromFilePath('dir/dir2/testDir/index.md') const slug = getSlugFromFilePath('dir/dir2/testDir/index.md')
expect(slug).toBe('/dir/dir2/testDir') expect(slug).toBe('dir/dir2/testDir')
}) })
}) })
@ -49,7 +49,7 @@ describe('writeJson', () => {
try { try {
await fs.unlink(testFilePath) await fs.unlink(testFilePath)
} catch (error) { } catch (error) {
console.error('Error cleaning up test file:', error) return
} }
}) })