Add action

This commit is contained in:
Nick Cipollo
2019-08-26 21:26:25 -04:00
parent 028c2af361
commit ef24c2a7b5
2 changed files with 108 additions and 28 deletions

View File

@@ -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<Releases, any>(() => {
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<Inputs, any>(() => {
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)
}
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<Releases, any>(() => {
return {
create: createMock,
uploadArtifact: uploadMock
}
})
const MockInputs = jest.fn<Inputs, any>(() => {
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)
}
})

View File

@@ -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)
)
}
}
}