discourse/app/assets/javascripts/asset-processor/add-theme-globals.test.mjs
David Taylor 585ba17f56
DEV: Rename theme-transpiler to asset-processor (#35498)
This is already used for more than just themes, and we plan to extend
its usage even further
2025-10-20 14:16:46 +01:00

74 lines
1.9 KiB
JavaScript
Vendored

/* eslint-disable qunit/require-expect */
import { transformSync } from "@babel/core";
import { expect, test } from "vitest";
import AddThemeGlobals from "./add-theme-globals.js";
function compile(input) {
return transformSync(input, {
configFile: false,
plugins: [AddThemeGlobals],
}).code;
}
test("adds imports automatically", () => {
expect(
compile(`
console.log(settings, themePrefix);
`)
).toMatchInlineSnapshot(`
"import { themePrefix, settings } from "virtual:theme";
console.log(settings, themePrefix);"
`);
});
test("throws error if settings or themePrefix are defined locally", () => {
expect(() =>
compile(`
const settings = {};
console.log(settings);
`)
).toThrowErrorMatchingInlineSnapshot(
`[Error: unknown file: \`settings\` is already defined. Unable to add import.]`
);
expect(() =>
compile(`
const themePrefix = "foo";
console.log(themePrefix);
`)
).toThrowErrorMatchingInlineSnapshot(
`[Error: unknown file: \`themePrefix\` is already defined. Unable to add import.]`
);
});
test("works if settings and themePrefix are already imported", () => {
expect(
compile(`
import { themePrefix, settings } from "virtual:theme";
console.log(settings, themePrefix);
`)
).toMatchInlineSnapshot(`
"import { themePrefix, settings } from "virtual:theme";
console.log(settings, themePrefix);"
`);
});
test("throws error if settings or themePrefix are already imported from the wrong place", () => {
expect(() =>
compile(`
import { themePrefix } from "foo";
console.log(themePrefix);
`)
).toThrowErrorMatchingInlineSnapshot(
`[Error: unknown file: \`themePrefix\` is already imported. Unable to add import from \`virtual:theme\`.]`
);
expect(() =>
compile(`
import { settings } from "foo";
console.log(settings);
`)
).toThrowErrorMatchingInlineSnapshot(
`[Error: unknown file: \`settings\` is already imported. Unable to add import from \`virtual:theme\`.]`
);
});