mirror of
https://github.com/kremalicious/astro-redirect-from.git
synced 2024-11-22 09:57:03 +01:00
refactor for better testability
This commit is contained in:
parent
d9e967aa31
commit
09dfa97af9
32
src/index.ts
32
src/index.ts
@ -10,24 +10,21 @@ export type PluginOptions = {
|
|||||||
getSlug?: GetSlug
|
getSlug?: GetSlug
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type HookOptions = Parameters<
|
||||||
|
AstroIntegration['hooks']['astro:config:setup'] & { 0: any }
|
||||||
|
>[0]
|
||||||
|
|
||||||
export type Redirects = { [old: string]: string }
|
export type Redirects = { [old: string]: string }
|
||||||
|
|
||||||
export default function astroRedirectFrom(
|
export async function initPlugin(
|
||||||
|
hookOptions: HookOptions,
|
||||||
options?: PluginOptions
|
options?: PluginOptions
|
||||||
): AstroIntegration {
|
) {
|
||||||
const _contentDir = options?.contentDir || 'src/pages/'
|
const _contentDir = options?.contentDir || 'src/pages/'
|
||||||
const _getSlug = options?.getSlug || getSlugFromFilePath
|
const _getSlug = options?.getSlug || getSlugFromFilePath
|
||||||
const _contentDirPath = path.join(process.cwd(), _contentDir)
|
const _contentDirPath = path.join(process.cwd(), _contentDir)
|
||||||
|
const { logger, config, command, updateConfig } = hookOptions
|
||||||
|
|
||||||
return {
|
|
||||||
name: 'redirect-from',
|
|
||||||
hooks: {
|
|
||||||
'astro:config:setup': async ({
|
|
||||||
config,
|
|
||||||
command,
|
|
||||||
updateConfig,
|
|
||||||
logger
|
|
||||||
}) => {
|
|
||||||
try {
|
try {
|
||||||
const markdownFiles = await getMarkdownFiles(_contentDirPath)
|
const markdownFiles = await getMarkdownFiles(_contentDirPath)
|
||||||
if (!markdownFiles?.length) {
|
if (!markdownFiles?.length) {
|
||||||
@ -42,7 +39,7 @@ export default function astroRedirectFrom(
|
|||||||
command
|
command
|
||||||
)
|
)
|
||||||
if (!redirects || !Object.keys(redirects).length) {
|
if (!redirects || !Object.keys(redirects).length) {
|
||||||
logger.info('No redirects found in markdown files')
|
logger.warn('No redirects found in markdown files')
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -60,7 +57,16 @@ export default function astroRedirectFrom(
|
|||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
logger.error((error as Error).message)
|
logger.error((error as Error).message)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export default function astroRedirectFrom(
|
||||||
|
options?: PluginOptions
|
||||||
|
): AstroIntegration {
|
||||||
|
return {
|
||||||
|
name: 'redirect-from',
|
||||||
|
hooks: {
|
||||||
|
'astro:config:setup': async (hookOptions) =>
|
||||||
|
await initPlugin(hookOptions, options)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
75
test/index.test.ts
Normal file
75
test/index.test.ts
Normal file
@ -0,0 +1,75 @@
|
|||||||
|
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'
|
||||||
|
|
||||||
|
describe('initPlugin', () => {
|
||||||
|
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()
|
||||||
|
}
|
||||||
|
|
||||||
|
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()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
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')
|
||||||
|
})
|
||||||
|
})
|
Loading…
Reference in New Issue
Block a user