Skip to content

Commit

Permalink
Update config manipulation test.
Browse files Browse the repository at this point in the history
  • Loading branch information
dev-rb committed May 19, 2024
1 parent f950b51 commit 36b0d4e
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 17 deletions.
7 changes: 7 additions & 0 deletions packages/commands/tests/assets/sample_app_config.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { defineConfig } from "@solidjs/start/config";
// Simulates an app config
export default defineConfig({
vite: {
plugins: [solid()]
}
});
9 changes: 9 additions & 0 deletions packages/commands/tests/assets/sample_unocss_vite_result.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import UnoCss from "unocss/vite";
import { defineConfig } from "vite";
// Simulates an app config
export default defineConfig({
plugins: [
solid(),
UnoCss({})
]
});
6 changes: 2 additions & 4 deletions packages/commands/tests/assets/sample_vite_config.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { defineConfig } from "@solidjs/start/config";
import { defineConfig } from "vite";
// Simulates an app config
export default defineConfig({
vite: {
plugins: [solid()]
}
plugins: [solid()]
});
52 changes: 39 additions & 13 deletions packages/commands/tests/config_manipulation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,21 @@ const readFile = async (path: string): Promise<string> => {
readFile1(path, (_, data) => res(data.toString())),
);
};

const removeWhitespace = (str: string) => str.replace(/\s/g, "");

vi.mock("fs/promises", () => {
return {
readFile: async (name: string) => {
if (name === "app.config.ts") {
const sampleConfig: string = await readFile("./packages/commands/tests/assets/sample_app_config.txt");
return sampleConfig;
}
if (name === "vite.config.ts") {
const sampleConfig: string = await readFile("./packages/commands/tests/assets/sample_vite_config.txt");
return sampleConfig;
}

return "{}";
},
writeFile: async (_, contents: string) => {
Expand Down Expand Up @@ -46,22 +53,41 @@ vi.mock("@solid-cli/utils/updates", async () => {

vi.mock("../src/lib/utils/helpers.ts", async () => {
return {
getConfigFile: async (): Promise<string> => new Promise((r) => r("app.config.ts")),
fileExists: (path: string) => path.includes("app_config") || path.includes("app.tsx") || path.includes("index.tsx"),
getConfigFile: async (file: string): Promise<string> => new Promise((r) => r(`${file}.config.ts`)),
fileExists: (path: string) =>
path.includes("vite_config") ||
path.includes("app_config") ||
path.includes("app.tsx") ||
path.includes("index.tsx"),
getRootFile: async (): Promise<string> => new Promise((r) => r("./src/app.tsx")),
};
});

let testingSolidStart = false;

vi.mock("@solid-cli/utils", async () => {
return {
isSolidStart: async () => new Promise((r) => r(testingSolidStart)),
};
});

describe("Update config", () => {
it(
"Adds a plugin properly to the config",
async () => {
await handleAdd(["unocss"]);
it("Adds a plugin properly to the app config", { timeout: 50000 }, async () => {
testingSolidStart = true;
await handleAdd(["unocss"]);

const expected = await readFile("./packages/commands/tests/assets/sample_unocss_result.txt");
// @ts-ignore
const newConfig = UPDATESQUEUE.find((u) => u.name === "app.config.ts")?.contents;
expect(removeWhitespace(expected)).toBe(removeWhitespace(newConfig));
},
{ timeout: 50000 },
);
const expected = await readFile("./packages/commands/tests/assets/sample_unocss_app_result.txt");
// @ts-ignore
const newConfig = UPDATESQUEUE.find((u) => u.name === "app.config.ts")?.contents;
expect(removeWhitespace(newConfig)).toBe(removeWhitespace(expected));
});
it("Adds a plugin properly to the vite config", { timeout: 50000 }, async () => {
testingSolidStart = false;
await handleAdd(["unocss"]);

const expected = await readFile("./packages/commands/tests/assets/sample_unocss_vite_result.txt");
// @ts-ignore
const newConfig = UPDATESQUEUE.find((u) => u.name === "vite.config.ts")?.contents;
expect(removeWhitespace(newConfig)).toBe(removeWhitespace(expected));
});
});
1 change: 1 addition & 0 deletions packages/utils/tests/plugin_transforms.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ plugins: []

test("Object method config is updated properly", async () => {
const config: Config = {
type: "app",
name: "app.config.ts",
contents: makeExampleConfig(["solid()"], [], "method"),
};
Expand Down

0 comments on commit 36b0d4e

Please sign in to comment.