1
0
mirror of https://github.com/kremalicious/astro-redirect-from.git synced 2024-10-22 11:22:44 +02:00
astro-redirect-from/test/redirects.test.ts

69 lines
2.0 KiB
TypeScript
Raw Normal View History

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'
2023-09-24 01:57:32 +02:00
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,
'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',
"/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
})
})
})