import { describe, it, expect } from 'vitest'
import transformCodePlugin, { VIEWS_PATH } from '.'
const SCRIPT_TAG_REGEX = /'
const result2 = fn(code2, `${VIEWS_PATH}/file.svelte`)!
expect(result2.code).toBe(code2)
})
describe('When a reactive function is found', () => {
it('replaces the function call with a variable', () => {
const code = '
{param("param_name")}
'
const result = fn(code, `${VIEWS_PATH}/file.svelte`)!
const varRegex = /__param_\d+/
expect(result.code).not.toBe(code)
expect(result.code).toMatch(varRegex)
})
it('creates a main script tag with the declaration if it does not exist', () => {
const code = '{param("param_name")}
'
const result = fn(code, `${VIEWS_PATH}/file.svelte`)!
const scriptTag = result.code.match(SCRIPT_TAG_REGEX)
expect(scriptTag).not.toBeNull()
})
it('does not create a new main script tag if one already exists', () => {
const code = `
{param("param_name")}
`
const result = fn(code, `${VIEWS_PATH}/file.svelte`)!
const scriptTags = result.code.match(SCRIPT_TAG_REGEX)
expect(scriptTags).toHaveLength(1)
})
it('creates a main script tag even if there are other script tags inside a ', () => {
const code = `
{param("param_name")}
`
const result = fn(code, `${VIEWS_PATH}/file.svelte`)!
const scriptTag = result.code.match(SCRIPT_TAG_REGEX)
expect(scriptTag?.length).toBe(2)
})
it('creates a main script tag even if there is another script tag with type="module"', () => {
const code = `
{param("param_name")}
`
const result = fn(code, `${VIEWS_PATH}/file.svelte`)!
const scriptTag = result.code.match(SCRIPT_TAG_REGEX)
expect(scriptTag).toHaveLength(2)
})
it('correctly replaces each reactive function call with a different variable when there is no main script', () => {
const functions = [
'param("param_name")',
'param("another_param", "some_other_param")',
`runQuery("query_name", "another_param", {
multiple_params: "value"
})`,
]
const code = functions.map((fn) => `{${fn}}
`).join('\n')
const result = fn(code, `${VIEWS_PATH}/file.svelte`)!
expect(result.code).not.toBe(code)
const scriptTags = result.code.match(SCRIPT_TAG_REGEX)
expect(scriptTags).toHaveLength(1)
const scriptTag = scriptTags![0]
functions.forEach((fn) => expect(scriptTag).toContain(fn))
// Expressions have been replaced with the variable.
// This ensures the position is valid because it must respect the start and end '{}' characters.
const varRegex = /{\$__\w+_\d+}/g
expect(result.code.match(varRegex)).toHaveLength(3)
})
it('correctly replaces the reactive function call with the variable in the main script tag without removing other code', () => {
const functions = [
'param("param_name")',
'param("another_param", "some_other_param")',
`runQuery("query_name", "another_param", {
multiple_params: "value"
})`,
]
const code = `
${functions.map((fn) => `{${fn}}
`).join('\n')}
`
const result = fn(code, `${VIEWS_PATH}/file.svelte`)!
expect(result.code).not.toBe(code)
const scriptTags = result.code.match(SCRIPT_TAG_REGEX)
expect(scriptTags).toHaveLength(1)
const scriptTag = scriptTags![0]
expect(scriptTag).toContain('console.log("Hello")') // Original code is still there
functions.forEach((fn) => expect(scriptTag).toContain(fn))
// Expressions have been replaced with the variable.
// This ensures the position is valid because it must respect the start and end '{}' characters.
const varRegex = /{\$__\w+_\d+}/g
expect(result.code).toMatch(varRegex)
})
})
})