Add action
This commit is contained in:
@@ -2,35 +2,113 @@ import { Action } from "../src/Action";
|
|||||||
import { Inputs } from "../src/Inputs";
|
import { Inputs } from "../src/Inputs";
|
||||||
import { Releases } from "../src/Releases";
|
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", () => {
|
describe("Action", () => {
|
||||||
|
beforeEach(() => {
|
||||||
it('verifies my sanity', () => {
|
createMock.mockClear()
|
||||||
|
uploadMock.mockClear()
|
||||||
})
|
})
|
||||||
})
|
|
||||||
|
|
||||||
function createAction(): Action {
|
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")
|
||||||
|
}
|
||||||
|
|
||||||
|
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>(() => {
|
const MockReleases = jest.fn<Releases, any>(() => {
|
||||||
return {
|
return {
|
||||||
create: jest.fn(),
|
create: createMock,
|
||||||
uploadArtifact: jest.fn()
|
uploadArtifact: uploadMock
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
const MockInputs = jest.fn<Inputs, any>(() => {
|
const MockInputs = jest.fn<Inputs, any>(() => {
|
||||||
return {
|
return {
|
||||||
artifact: "foo",
|
artifact: artifact,
|
||||||
artifactContentType: "foo",
|
artifactContentType: contentType,
|
||||||
artifactContentLength: 22,
|
artifactContentLength: contentLength,
|
||||||
body: "bar",
|
body: body,
|
||||||
commit: "foo",
|
commit: commit,
|
||||||
name: "name",
|
draft: draft,
|
||||||
tag: "foo",
|
name: name,
|
||||||
token: "foo"
|
tag: tag,
|
||||||
|
token: token
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
const inputs = new MockInputs()
|
const inputs = new MockInputs()
|
||||||
const releases = new MockReleases()
|
const releases = new MockReleases()
|
||||||
|
|
||||||
return new Action(inputs, releases)
|
return new Action(inputs, releases)
|
||||||
}
|
}
|
||||||
|
})
|
||||||
@@ -17,7 +17,8 @@ export class Action {
|
|||||||
this.inputs.body,
|
this.inputs.body,
|
||||||
this.inputs.commit,
|
this.inputs.commit,
|
||||||
this.inputs.draft,
|
this.inputs.draft,
|
||||||
this.inputs.name)
|
this.inputs.name
|
||||||
|
)
|
||||||
|
|
||||||
if (this.inputs.artifact) {
|
if (this.inputs.artifact) {
|
||||||
await this.releases.uploadArtifact(
|
await this.releases.uploadArtifact(
|
||||||
@@ -25,7 +26,8 @@ export class Action {
|
|||||||
this.inputs.artifactContentLength,
|
this.inputs.artifactContentLength,
|
||||||
this.inputs.artifactContentType,
|
this.inputs.artifactContentType,
|
||||||
this.inputs.artifact,
|
this.inputs.artifact,
|
||||||
basename(this.inputs.artifact))
|
basename(this.inputs.artifact)
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user