0
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2026-08-06 01:10:43 +08:00
discourse/frontend/asset-processor/add-theme-globals.test.mjs
David Taylor d1e1c02fcb DEV: Rename app/assets/javascripts/ -> frontend/
Commit created with `mv app/assets/javascripts frontend && git add .`
2025-10-22 16:24:11 +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\`.]`
);
});