import { beforeEach, describe, expect, it } from 'vitest'
import { parse } from '@astrojs/compiler'
import {
createBlock as createAstroBlock,
createSFC as createAstroSFC,
} from '../src/astro/create'
import { magicAstroSfcOptions } from '../src/astro/sfc'
describe('Create Astro Block', () => {
beforeEach(() => {
// Set default parser for MagicAstroSFC
magicAstroSfcOptions.parser = parse
})
it('Should create a template block correctly', () => {
const block = {
content: '
Hello World
',
}
const result = createAstroBlock(block, 'templates')
expect(result).toBe('Hello World
')
})
it('Should create a script block correctly', () => {
const block = {
content: 'console.log("Hello World");',
}
const result = createAstroBlock(block, 'scripts')
expect(result).toBe('') // Adjusted as per your implementation
})
it('Should create a style block correctly', () => {
const block = {
content: 'body { color: red; }',
}
const result = createAstroBlock(block, 'styles')
expect(result).toBe('')
})
it('Should handle multiple attributes correctly', () => {
const block = {
attrs: { lang: 'scss' },
content: 'body { color: red; }',
} as const
const result = createAstroBlock(block, 'styles')
expect(result).toBe('')
})
it('Should return an empty string if no block is provided', () => {
const result = createAstroBlock(undefined, 'templates')
expect(result).toBe('')
})
it('Should handle missing block.attrs gracefully', () => {
const block = {
content: 'Hello
',
}
const result = createAstroBlock(block, 'templates')
// Ensure that the block creation works without errors and the content is present
expect(result).toBe('Hello
')
})
})
describe('Create Astro SFC', () => {
beforeEach(() => {
// Set default parser for MagicAstroSFC
magicAstroSfcOptions.parser = parse
})
it('Can create an SFC with template, script, and styles', () => {
const sfc = createAstroSFC({
templates: [{
content: '{{ msg }}
',
}],
scripts: [
{
content: `export default {
data() {
return {
msg: "Hello, world!",
};
},
};`,
},
],
styles: [
{
content: `.text {
color: red;
}`,
},
],
})
const expectedSFC = `
{{ msg }}
`
expect(sfc.toString()).toBe(expectedSFC)
})
it('Can create an SFC with custom blocks', () => {
const sfc = createAstroSFC({
templates: [{
content: '{{ msg }}
',
}],
scripts: [{
content: `export default {
data() {
return {
msg: "Hello, world!",
};
},
};`,
}],
})
const expectedSFC = `
{{ msg }}
`
expect(sfc.toString()).toBe(expectedSFC)
})
})