Skip to content

Commit

Permalink
✅ Add tests for function
Browse files Browse the repository at this point in the history
  • Loading branch information
AnandChowdhary committed Apr 15, 2020
1 parent 0d889ae commit 2379c2f
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 14 deletions.
3 changes: 3 additions & 0 deletions examples/screenshot.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
go to example.com
take a screenshot
save to example.png
26 changes: 22 additions & 4 deletions index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,25 @@
import { nodeTs } from "./index";
import { puppet } from "./index";

describe("puppet", () => {
it("works", () => {
expect(nodeTs()).toBeTruthy();
describe("puppet basic run", () => {
it("downloads and runs URL", async () => {
const result = await puppet("https://pastebin.com/raw/bqRmm1z9");
expect(result).toBeUndefined();
});
it("runs array of commands", async () => {
const result = await puppet([
"go to example.com",
"take a screenshot",
"save to example.png",
]);
expect(result).toBeUndefined();
});
it("runs a file path", async () => {
const result = await puppet("examples/screenshot.txt");
expect(result).toBeUndefined();
});
it("throws if no commands", async () => {
expect((puppet as any)()).rejects.toEqual(
new Error("Argument must be a string or an array of strings")
);
});
});
21 changes: 11 additions & 10 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { launch, Page } from "puppeteer";
import { log } from "signale";
import got from "got";
import {} from "fs-extra";
import { readFile } from "fs-extra";
import { join } from "path";

/**
*
Expand All @@ -12,21 +13,21 @@ import {} from "fs-extra";
*/
export const puppet = async (commandsOrFile: string[] | string) => {
if (typeof commandsOrFile === "string") {
return puppetFile(commandsOrFile);
if (
commandsOrFile.startsWith("https://") ||
commandsOrFile.startsWith("http://")
) {
const commands = await got.get(commandsOrFile);
return _puppet(commands.body.split("\n"));
}
const commands = await readFile(join(".", commandsOrFile), "utf8");
return _puppet(commands.split("\n"));
} else if (Array.isArray(commandsOrFile)) {
return _puppet(commandsOrFile);
}
throw new Error("Argument must be a string or an array of strings");
};

const puppetFile = async (file: string) => {
if (file.startsWith("https://") || file.startsWith("http://")) {
const commands = await got.get(file);
return _puppet(commands.body.split("\n"));
}
console.log("Starting Puppet");
};

const _puppet = async (commands: string[]) => {
log("Starting Puppet");
};

0 comments on commit 2379c2f

Please sign in to comment.