diff --git a/create-or-update-files.test.ts b/create-or-update-files.test.ts index aee121a..cb27a76 100644 --- a/create-or-update-files.test.ts +++ b/create-or-update-files.test.ts @@ -1,4 +1,5 @@ -const plugin = require("./create-or-update-files"); +import CreateOrUpdateMultipleFiles from "."; +import CreateOrUpdateMultipleFilesHandler from "./create-or-update-files"; const { Octokit } = require("@octokit/rest"); const nock = require("nock"); @@ -6,10 +7,22 @@ nock.disableNetConnect(); const octokit = new Octokit(); function run(body: any) { - return plugin(octokit, body); + return CreateOrUpdateMultipleFilesHandler(octokit, body); } -const validRequest = { +type RequestStructure = { + owner: string; + repo: string; + branch: string; + createBranch: boolean; + base?: string; // Marked as optional + changes?: Array<{ + message: string; + files: Record; + }>; +}; + +const validRequest: RequestStructure = { owner: "mheap", repo: "test-repo", branch: "new-branch-name", @@ -43,8 +56,8 @@ const mockSubmoduleCommitList = { }; for (let req of ["owner", "repo", "branch"]) { - const body = { ...validRequest }; - delete body[req]; + const body: RequestStructure = { ...validRequest }; + delete body[req as keyof RequestStructure]; test(`missing parameter (${req})`, () => { expect(run(body)).rejects.toEqual(`'${req}' is a required parameter`); }); @@ -499,18 +512,18 @@ test("failure (fileToDelete is missing)", async () => { }); test("Loads plugin", () => { - const TestOctokit = Octokit.plugin(require(".")); + const TestOctokit = Octokit.plugin(CreateOrUpdateMultipleFiles); const testOctokit = new TestOctokit(); expect(testOctokit).toHaveProperty("createOrUpdateFiles"); }); test("Does not overwrite other methods", () => { - const TestOctokit = Octokit.plugin(require(".")); + const TestOctokit = Octokit.plugin(CreateOrUpdateMultipleFiles); const testOctokit = new TestOctokit(); expect(testOctokit).toHaveProperty("rest.repos.acceptInvitation"); }); -function mockGetRef(branch: string, sha: string, success: boolean) { +function mockGetRef(branch: string | undefined, sha: string, success: boolean) { const m = nock("https://api.github.com").get( `/repos/${owner}/${repo}/git/ref/heads%2F${branch}`, ); diff --git a/create-or-update-files.ts b/create-or-update-files.ts index 9394322..4dd69af 100644 --- a/create-or-update-files.ts +++ b/create-or-update-files.ts @@ -56,7 +56,7 @@ function isBase64(str: string | Buffer): boolean { ); } -module.exports = function ( +export default function ( octokit: Octokit, opts: Options, ): Promise { @@ -292,7 +292,7 @@ module.exports = function ( return reject(e); } }); -}; +} async function fileExistsInRepo( octokit: Octokit, diff --git a/index.ts b/index.ts index bce8b16..7c16a29 100644 --- a/index.ts +++ b/index.ts @@ -1,9 +1,9 @@ import { Octokit } from "@octokit/rest"; -const createOrUpdateFilesPlugin = require("./create-or-update-files"); +import createOrUpdateFilesPlugin from "./create-or-update-files"; -module.exports = function (octokit: Octokit) { +export default function (octokit: Octokit) { return { createOrUpdateFiles: createOrUpdateFilesPlugin.bind(null, octokit), }; -}; +}