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