diff --git a/__tests__/Action.test.ts b/__tests__/Action.test.ts index f3a3f4f..3822436 100644 --- a/__tests__/Action.test.ts +++ b/__tests__/Action.test.ts @@ -2,35 +2,113 @@ import { Action } from "../src/Action"; import { Inputs } from "../src/Inputs"; import { Releases } from "../src/Releases"; +const createMock = jest.fn() +const uploadMock = jest.fn() + +const artifactPath = 'a/path' +const body = 'body' +const commit = 'commit' +const contentType = "raw" +const contentLength = 100 +const draft = true +const name = 'name' +const tag = 'tag' +const token = 'token' +const url = 'http://api.example.com' describe("Action", () => { - - it('verifies my sanity', () => { - + beforeEach(() => { + createMock.mockClear() + uploadMock.mockClear() }) -}) -function createAction(): Action { - const MockReleases = jest.fn(() => { - return { - create: jest.fn(), - uploadArtifact: jest.fn() + it('creates release but does not upload if no artifact', async () => { + const action = createAction(false) + + await action.perform() + + expect(createMock).toBeCalledWith(tag, body, commit, draft, name) + expect(uploadMock).not.toBeCalled() + }) + + it('creates release then uploads artifact', async () => { + const action = createAction(true) + createMock.mockResolvedValue({ + data: { + assets_url: url + } + }) + + await action.perform() + + expect(createMock).toBeCalledWith(tag, body, commit, draft, name) + expect(uploadMock).toBeCalledWith(url, contentLength, contentType, artifactPath, 'path') + }) + + it('throws error when create fails', async () => { + const action = createAction(true) + createMock.mockRejectedValue("error") + + expect.hasAssertions() + try { + await action.perform() + } catch (error) { + expect(error).toEqual("error") } - }) - const MockInputs = jest.fn(() => { - return { - artifact: "foo", - artifactContentType: "foo", - artifactContentLength: 22, - body: "bar", - commit: "foo", - name: "name", - tag: "foo", - token: "foo" - } - }) - const inputs = new MockInputs() - const releases = new MockReleases() - return new Action(inputs, releases) -} \ No newline at end of file + expect(createMock).toBeCalledWith(tag, body, commit, draft, name) + expect(uploadMock).not.toBeCalled() + }) + + it('throws error when upload fails', async () => { + const action = createAction(true) + createMock.mockResolvedValue({ + data: { + assets_url: url + } + }) + uploadMock.mockRejectedValue("error") + + expect.hasAssertions() + try { + await action.perform() + } catch (error) { + expect(error).toEqual("error") + } + + expect(createMock).toBeCalledWith(tag, body, commit, draft, name) + expect(uploadMock).toBeCalledWith(url, contentLength, contentType, artifactPath, 'path') + }) + + function createAction(hasArtifact: boolean): Action { + let artifact: string + if (hasArtifact) { + artifact = artifactPath + } else { + artifact = '' + } + const MockReleases = jest.fn(() => { + return { + create: createMock, + uploadArtifact: uploadMock + } + }) + const MockInputs = jest.fn(() => { + return { + artifact: artifact, + artifactContentType: contentType, + artifactContentLength: contentLength, + body: body, + commit: commit, + draft: draft, + name: name, + tag: tag, + token: token + } + }) + const inputs = new MockInputs() + const releases = new MockReleases() + + return new Action(inputs, releases) + } +}) \ No newline at end of file diff --git a/src/Action.ts b/src/Action.ts index 8fa47de..f3d18ef 100644 --- a/src/Action.ts +++ b/src/Action.ts @@ -17,7 +17,8 @@ export class Action { this.inputs.body, this.inputs.commit, this.inputs.draft, - this.inputs.name) + this.inputs.name + ) if (this.inputs.artifact) { await this.releases.uploadArtifact( @@ -25,7 +26,8 @@ export class Action { this.inputs.artifactContentLength, this.inputs.artifactContentType, this.inputs.artifact, - basename(this.inputs.artifact)) + basename(this.inputs.artifact) + ) } } } \ No newline at end of file