1
0
mirror of https://github.com/kremalicious/astro-redirect-from.git synced 2024-11-29 07:16:37 +01:00
astro-redirect-from/test/index.test.ts

109 lines
3.5 KiB
TypeScript
Raw Normal View History

2023-09-24 13:49:10 +02:00
import { describe, it, expect, vi } from 'vitest'
import astroRedirectFrom, { initPlugin } from '../src/index'
import * as utils from '../src/utils'
import * as redirects from '../src/getRedirects'
2023-09-24 14:15:23 +02:00
const mockLogger = {
warn: vi.fn(),
info: vi.fn(),
error: vi.fn()
}
const hookOptionsMock = {
logger: mockLogger,
config: {
cacheDir: { pathname: './node_modules/.astro/' }
},
command: 'dev',
updateConfig: vi.fn()
}
2023-09-24 13:49:10 +02:00
2023-09-24 14:15:23 +02:00
describe('initPlugin', () => {
2023-09-24 13:49:10 +02:00
it('should handle no markdown files scenario', async () => {
const getMarkdownFilesSpy = vi.spyOn(utils, 'getMarkdownFiles')
getMarkdownFilesSpy.mockResolvedValue([])
await initPlugin(hookOptionsMock as any)
expect(mockLogger.warn).toBeCalledWith('No markdown files found')
expect(hookOptionsMock.updateConfig).not.toBeCalled()
})
it('should handle no redirects found scenario', async () => {
const getMarkdownFilesSpy = vi.spyOn(utils, 'getMarkdownFiles')
getMarkdownFilesSpy.mockResolvedValue(['test.md', 'test2.md'])
const getRedirectsSpy = vi.spyOn(redirects, 'getRedirects')
getRedirectsSpy.mockResolvedValue({})
await initPlugin(hookOptionsMock as any)
expect(mockLogger.warn).toBeCalledWith(
'No redirects found in markdown files'
)
expect(hookOptionsMock.updateConfig).not.toBeCalled()
})
it('should handle redirects found scenario', async () => {
const getMarkdownFilesSpy = vi.spyOn(utils, 'getMarkdownFiles')
getMarkdownFilesSpy.mockResolvedValue(['test.md', 'test2.md'])
const getRedirectsSpy = vi.spyOn(redirects, 'getRedirects')
getRedirectsSpy.mockResolvedValue({ '/old': '/new' })
const writeJsonSpy = vi.spyOn(utils, 'writeJson')
writeJsonSpy.mockImplementation(() => Promise.resolve())
await initPlugin(hookOptionsMock as any)
expect(hookOptionsMock.updateConfig).toBeCalledWith({
redirects: { '/old': '/new' }
})
expect(mockLogger.info).toBeCalledWith('Added 1 redirects to Astro config')
expect(writeJsonSpy).toBeCalled()
})
2023-09-24 14:15:23 +02:00
it('should handle errors and log them', async () => {
// Make getMarkdownFiles throw an error
vi.spyOn(utils, 'getMarkdownFiles')
const getMarkdownFilesSpy = vi.spyOn(utils, 'getMarkdownFiles')
getMarkdownFilesSpy.mockImplementation(async () => {
throw new Error('Mocked error')
})
await initPlugin({ ...hookOptionsMock, logger: mockLogger } as any)
expect(mockLogger.error).toHaveBeenCalledWith('Mocked error')
})
2023-09-24 13:49:10 +02:00
})
describe('astroRedirectFrom', () => {
it('should return an AstroIntegration object', () => {
const result = astroRedirectFrom()
expect(result).toHaveProperty('name', 'redirect-from')
expect(result).toHaveProperty('hooks')
expect(result.hooks).toHaveProperty('astro:config:setup')
})
2023-09-24 14:15:23 +02:00
it('should call initPlugin when astro:config:setup hook is invoked', async () => {
const getMarkdownFilesSpy = vi.spyOn(utils, 'getMarkdownFiles')
getMarkdownFilesSpy.mockResolvedValue(['test.md', 'test2.md'])
const getRedirectsSpy = vi.spyOn(redirects, 'getRedirects')
getRedirectsSpy.mockResolvedValue({ '/old': '/new' })
const writeJsonSpy = vi.spyOn(utils, 'writeJson')
writeJsonSpy.mockImplementation(() => Promise.resolve())
const integration = astroRedirectFrom()
expect(integration.hooks).toHaveProperty('astro:config:setup')
// Invoke the hook
if (integration.hooks['astro:config:setup']) {
await integration.hooks['astro:config:setup'](hookOptionsMock as any)
}
})
2023-09-24 13:49:10 +02:00
})