2024-07-26 15:13:02 +02:00
|
|
|
import { describe, expect, it } from 'vitest'
|
|
|
|
import { createRedirect } from '../src/createRedirect'
|
2023-09-24 01:57:32 +02:00
|
|
|
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 correct redirects', async () => {
|
|
|
|
const result = await getRedirects(
|
|
|
|
files,
|
|
|
|
srcDir,
|
|
|
|
getSlugFromFilePath,
|
2024-08-12 12:39:00 +02:00
|
|
|
'build',
|
|
|
|
console
|
2023-09-24 01:57:32 +02:00
|
|
|
)
|
|
|
|
expect(result).toBeInstanceOf(Object)
|
|
|
|
expect(result).toStrictEqual({
|
|
|
|
'/hello-astro-old': '/hello-astroooooo',
|
|
|
|
'/hello-astro-old-234837': '/hello-astroooooo',
|
2023-09-24 12:19:46 +02:00
|
|
|
'/hello-world-old': '/posts/hello-world',
|
2024-08-12 12:54:20 +02:00
|
|
|
'/hello-once': '/posts/hello-once',
|
2023-09-24 12:19:46 +02:00
|
|
|
'/hello-world-old-234837': '/posts/hello-world',
|
2023-09-24 01:57:32 +02:00
|
|
|
'/hello-markdown-old': '/hello-markdown',
|
|
|
|
'/hello-markdown-old-234837': '/hello-markdown'
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('createRedirect', () => {
|
|
|
|
it('should merge initialRedirects with newly found ones', () => {
|
|
|
|
const initialRedirects: Record<string, string> = {
|
|
|
|
'/existing-old-url': '/existing-new-url'
|
|
|
|
}
|
|
|
|
const redirectFrom = ['old-url-1', '/old-url-2']
|
|
|
|
const postSlug = 'new-url'
|
|
|
|
|
|
|
|
const result = createRedirect(initialRedirects, redirectFrom, postSlug)
|
|
|
|
|
|
|
|
expect(result).toStrictEqual({
|
|
|
|
'/existing-old-url': '/existing-new-url',
|
|
|
|
'/old-url-1': '/new-url',
|
|
|
|
'/old-url-2': '/new-url'
|
|
|
|
})
|
2023-12-12 22:31:06 +01:00
|
|
|
})
|
2023-09-24 01:57:32 +02:00
|
|
|
|
2023-12-12 22:31:06 +01:00
|
|
|
it('should prepend slash everywhere', () => {
|
|
|
|
const initialRedirects: Record<string, string> = {}
|
|
|
|
const redirectFrom = ['old-url-1']
|
|
|
|
let postSlug = '/new-url'
|
2023-09-24 01:57:32 +02:00
|
|
|
|
2023-12-12 22:31:06 +01:00
|
|
|
let result = createRedirect(initialRedirects, redirectFrom, postSlug)
|
2023-09-24 01:57:32 +02:00
|
|
|
|
2023-12-12 22:31:06 +01:00
|
|
|
expect(result).toStrictEqual({
|
|
|
|
'/old-url-1': '/new-url'
|
|
|
|
})
|
2023-09-24 01:57:32 +02:00
|
|
|
|
2023-12-12 22:31:06 +01:00
|
|
|
postSlug = 'new-url'
|
|
|
|
result = createRedirect(initialRedirects, redirectFrom, postSlug)
|
|
|
|
expect(result).toStrictEqual({
|
|
|
|
'/old-url-1': '/new-url'
|
2023-09-24 01:57:32 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|