Files
release-action/__tests__/Action.test.ts
Nick Cipollo ef24c2a7b5 Add action
2019-08-26 21:26:25 -04:00

114 lines
3.1 KiB
TypeScript

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", () => {
beforeEach(() => {
createMock.mockClear()
uploadMock.mockClear()
})
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>(() => {
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)
}
})