import MagicString, { SourceMap } from 'magic-string'
import { beforeEach, describe, expect, it } from 'vitest'
import { parse } from 'vue/compiler-sfc'
import { MagicSFC as MagicVueSFC, magicVueSfcOptions } from '../src/vue/sfc'
import { completeComponent, script, scriptSetup, style, styleScoped, template } from './utils'
describe('Magic Vue SFC', () => {
beforeEach(() => {
// Set default parser for MagicVueSFC
magicVueSfcOptions.parser = parse
})
it('Can create the class', async () => {
const sfc = await new MagicVueSFC(scriptSetup).parse()
expect(sfc.toString()).toBe(scriptSetup)
})
it('Cannot create a Magic Vue SFC without a parser function', async () => {
magicVueSfcOptions.parser = undefined
try {
await new MagicVueSFC(scriptSetup).parse()
throw new Error('Code should not reach this point!')
}
catch (e) {
expect(e.message).toBe('You must provide a `parser` function (from vue/compiler-sfc) in options when using MagicVueSFC.')
}
})
it('Can create the class from a MagicString', async () => {
const ms = new MagicString(scriptSetup)
const sfc = await new MagicVueSFC(ms).parse()
const appended = '\nlet secondTest: string'
sfc.scripts[0].append(appended)
expect(sfc.toString()).toBe(``)
})
it('Can get a sourcemap', async () => {
const sfc = await new MagicVueSFC(scriptSetup).parse()
expect(sfc.getSourcemap()).toBeInstanceOf(SourceMap)
})
it('Can parse a '
const expectedScript = ''
const sfc = await new MagicVueSFC(originalScript).parse()
sfc.scripts[0].overwrite(27, 38, 'UpdatedComponent')
expect(sfc.toString()).toBe(expectedScript)
})
it('Can manipulate a '
const expectedScriptSetup = ''
const sfc = await new MagicVueSFC(originalScriptSetup).parse()
sfc.scripts[0].overwrite(21, 26, 'Mars')
expect(sfc.toString()).toBe(expectedScriptSetup)
})
it('Can manipulate a '
const expectedStyle = ''
const sfc = await new MagicVueSFC(originalStyle).parse()
sfc.styles[0].overwrite(18, 21, 'blue')
expect(sfc.toString()).toBe(expectedStyle)
})
it('Can manipulate nested elements in a block', async () => {
const originalNestedTemplate = '\n Hello, world!
\n'
const expectedNestedTemplate = '\n Hello, Mars!
\n'
const sfc = await new MagicVueSFC(originalNestedTemplate).parse()
sfc.templates[0].overwrite(21, 26, 'Mars')
expect(sfc.toString()).toBe(expectedNestedTemplate)
})
it('Can handle empty blocks', async () => {
const emptyScript = ''
const emptyScriptSetup = ''
const emptyTemplate = ''
const emptyStyle = ''
const sfc = await new MagicVueSFC(`${emptyScriptSetup}\n${emptyScript}\n${emptyTemplate}\n${emptyStyle}`).parse()
// Vue SFC parser does sets null for empty script blocks
expect(sfc.scripts.length).toBeFalsy()
expect(sfc.styles.length).toBeFalsy()
// Vue SFC parser does detect
expect(sfc.templates.length).toBeTruthy()
})
it('Can append content to a `
const expectedScript = ``
const sfc = await new MagicVueSFC(originalScript).parse()
sfc.scripts[0].append(appended)
expect(sfc.toString()).toBe(expectedScript)
})
it('Can appendLeft content to a `
const expectedScript = ``
const sfc = await new MagicVueSFC(originalScript).parse()
sfc.scripts[0].appendLeft(0, appended)
expect(sfc.toString()).toBe(expectedScript)
})
it('Can appendRight content to a `
const expectedScript = ``
const sfc = await new MagicVueSFC(originalScript).parse()
sfc.scripts[0].appendRight(baseContent.length, appended)
expect(sfc.toString()).toBe(expectedScript)
})
it('Can prepend content to a `
const expectedScript = ``
const sfc = await new MagicVueSFC(originalScript).parse()
sfc.scripts[0].prepend(prepended)
expect(sfc.toString()).toBe(expectedScript)
})
it('Can prependLeft content to a `
const expectedScript = ``
const sfc = await new MagicVueSFC(originalScript).parse()
sfc.scripts[0].prependLeft(0, prepended)
expect(sfc.toString()).toBe(expectedScript)
})
it('Can prependRight content to a `
const expectedScript = ``
const sfc = await new MagicVueSFC(originalScript).parse()
sfc.scripts[0].prependRight(baseContent.length, prepended)
expect(sfc.toString()).toBe(expectedScript)
})
it('Can manipulate every block of an SFC', async () => {
const originalSFC = `
{{ msg }}
`
const expectedSFC = `
{{ updatedMsg }}
`
const sfc = await new MagicVueSFC(originalSFC).parse()
sfc.templates[0].overwrite(11, 14, 'updatedMsg')
sfc.scripts[0].overwrite(61, 66, 'Mars')
sfc.scripts[1].appendLeft(29, ' updated')
sfc.styles[0].overwrite(18, 21, 'blue')
expect(sfc.toString()).toBe(expectedSFC)
})
it('Uses all methods on different blocks', async () => {
const originalSFC = `
Hello, world!
`
const expectedSFC = `
Hi, Mars!
Hello, world!
`
const sfc = await new MagicVueSFC(originalSFC).parse()
// Manipulate block
sfc.templates[0].prepend('\n Hi, Mars!')
// Manipulate