import { beforeEach, describe, expect, it } from 'vitest'
import { preprocess } from 'svelte/compiler'
import { createSFC as create, createBlock as createSvelteBlock } from '../src/svelte/create'
import { magicSvelteSfcOptions } from '../src/svelte/sfc'
describe('Create Svelte Block', () => {
beforeEach(() => {
// Set default parser for MagicVueSFC
magicSvelteSfcOptions.parser = preprocess
})
it('Should create a template block correctly', () => {
const block = {
content: '
Hello World
',
}
const result = createSvelteBlock(block, 'templates')
expect(result).toBe('Hello World
')
})
it('Should create a script block correctly', () => {
const block = {
content: 'console.log("Hello World");',
}
const result = createSvelteBlock(block, 'scripts')
expect(result).toBe('')
})
it('Should create a style block correctly', () => {
const block = {
content: 'body { color: red; }',
}
const result = createSvelteBlock(block, 'styles')
expect(result).toBe('')
})
it('Should handle multiple attributes correctly', () => {
const block = {
attrs: { lang: 'scss' },
content: 'body { color: red; }',
} as const
const result = createSvelteBlock(block, 'styles')
expect(result).toBe('')
})
it('Should return an empty string if no block is provided', () => {
const result = createSvelteBlock(undefined, 'templates')
expect(result).toBe('')
})
it('Should handle missing block.attrs gracefully', () => {
const block = {
content: 'Hello
',
}
const result = createSvelteBlock(block, 'templates')
// Ensure that the block creation works without errors and the content is present
expect(result).toBe('Hello
')
})
it('Should handle missing templates option gracefully', () => {
const options = {
scripts: [{ content: 'console.log("Hello");' }],
}
const result = create(options)
const sfcContent = result.toString() // Assuming toString method exists
// Assert there is no block but the ')
})
})
describe('Create Svelte SFC', () => {
beforeEach(() => {
// Set default parser for MagicVueSFC
magicSvelteSfcOptions.parser = preprocess
})
it('Can create an SFC with template, script, scriptSetup, and styles', () => {
const sfc = create({
templates: [{
content: '{{ msg }}
',
}],
scripts: [
{
content: `export default {
data() {
return {
msg: "Hello, world!",
};
},
};`,
},
],
styles: [
{
content: `.text {
color: red;
}`,
},
],
})
const expectedSFC = `{{ msg }}
\n
`
expect(sfc.toString()).toBe(expectedSFC)
})
it('Can create an SFC with custom blocks', () => {
const sfc = create({
templates: [{
content: '{{ msg }}
',
}],
scripts: [{
content: `export default {
data() {
return {
msg: "Hello, world!",
};
},
};`,
}],
})
const expectedSFC = `{{ msg }}
`
expect(sfc.toString()).toBe(expectedSFC)
})
it('Can create an SFC with attributes, lang, and src', () => {
const sfc = create({
templates: [{
content: '{{ msg }}
',
}],
scripts: [{
content: 'console.log("Hello");',
}],
styles: [
{
content: `.text {
color: red;
}`,
},
],
})
const expectedSFC = `{{ msg }}
`
expect(sfc.toString()).toBe(expectedSFC)
})
})