Fixes #6 Add support for updating releases
This commit is contained in:
@@ -5,6 +5,8 @@ import { Releases } from "../src/Releases";
|
||||
import { ArtifactUploader } from "../src/ArtifactUploader";
|
||||
|
||||
const createMock = jest.fn()
|
||||
const getMock = jest.fn()
|
||||
const updateMock = jest.fn()
|
||||
const uploadMock = jest.fn()
|
||||
|
||||
const artifacts = [
|
||||
@@ -14,9 +16,8 @@ const artifacts = [
|
||||
const artifactData = Buffer.from('blob', 'utf-8')
|
||||
const body = 'body'
|
||||
const commit = 'commit'
|
||||
const contentType = "raw"
|
||||
const contentLength = 100
|
||||
const draft = true
|
||||
const id = 100
|
||||
const name = 'name'
|
||||
const tag = 'tag'
|
||||
const token = 'token'
|
||||
@@ -25,11 +26,13 @@ const url = 'http://api.example.com'
|
||||
describe("Action", () => {
|
||||
beforeEach(() => {
|
||||
createMock.mockClear()
|
||||
getMock.mockClear()
|
||||
updateMock.mockClear()
|
||||
uploadMock.mockClear()
|
||||
})
|
||||
|
||||
it('creates release but does not upload if no artifact', async () => {
|
||||
const action = createAction(false)
|
||||
const action = createAction(false, false)
|
||||
|
||||
await action.perform()
|
||||
|
||||
@@ -38,12 +41,7 @@ describe("Action", () => {
|
||||
})
|
||||
|
||||
it('creates release then uploads artifact', async () => {
|
||||
const action = createAction(true)
|
||||
createMock.mockResolvedValue({
|
||||
data: {
|
||||
upload_url: url
|
||||
}
|
||||
})
|
||||
const action = createAction(false, true)
|
||||
|
||||
await action.perform()
|
||||
|
||||
@@ -52,7 +50,7 @@ describe("Action", () => {
|
||||
})
|
||||
|
||||
it('throws error when create fails', async () => {
|
||||
const action = createAction(true)
|
||||
const action = createAction(false, true)
|
||||
createMock.mockRejectedValue("error")
|
||||
|
||||
expect.hasAssertions()
|
||||
@@ -66,13 +64,56 @@ describe("Action", () => {
|
||||
expect(uploadMock).not.toBeCalled()
|
||||
})
|
||||
|
||||
it('throws error when get fails', async () => {
|
||||
const action = createAction(true, false)
|
||||
const error = {
|
||||
errors: [
|
||||
{
|
||||
code: 'already_exists'
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
createMock.mockRejectedValue(error)
|
||||
getMock.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 update fails', async () => {
|
||||
const action = createAction(true, false)
|
||||
const error = {
|
||||
errors: [
|
||||
{
|
||||
code: 'already_exists'
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
createMock.mockRejectedValue(error)
|
||||
updateMock.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: {
|
||||
upload_url: url
|
||||
}
|
||||
})
|
||||
const action = createAction(false, true)
|
||||
uploadMock.mockRejectedValue("error")
|
||||
|
||||
expect.hasAssertions()
|
||||
@@ -86,7 +127,45 @@ describe("Action", () => {
|
||||
expect(uploadMock).toBeCalledWith(artifacts, url)
|
||||
})
|
||||
|
||||
function createAction(hasArtifact: boolean): Action {
|
||||
it('updates release but does not upload if no artifact', async () => {
|
||||
const action = createAction(true, false)
|
||||
const error = {
|
||||
errors: [
|
||||
{
|
||||
code: 'already_exists'
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
createMock.mockRejectedValue(error)
|
||||
|
||||
await action.perform()
|
||||
|
||||
expect(createMock).toBeCalledWith(tag, body, commit, draft, name)
|
||||
expect(uploadMock).not.toBeCalled()
|
||||
|
||||
})
|
||||
|
||||
it('updates release then uploads artifact', async () => {
|
||||
const action = createAction(true, true)
|
||||
const error = {
|
||||
errors: [
|
||||
{
|
||||
code: 'already_exists'
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
createMock.mockRejectedValue(error)
|
||||
|
||||
await action.perform()
|
||||
|
||||
expect(createMock).toBeCalledWith(tag, body, commit, draft, name)
|
||||
expect(uploadMock).toBeCalledWith(artifacts, url)
|
||||
|
||||
})
|
||||
|
||||
function createAction(allowUpdates: boolean, hasArtifact: boolean): Action {
|
||||
let inputArtifact: Artifact[]
|
||||
if (hasArtifact) {
|
||||
inputArtifact = artifacts
|
||||
@@ -96,11 +175,32 @@ describe("Action", () => {
|
||||
const MockReleases = jest.fn<Releases, any>(() => {
|
||||
return {
|
||||
create: createMock,
|
||||
uploadArtifact: jest.fn()
|
||||
getByTag: getMock,
|
||||
update: updateMock,
|
||||
uploadArtifact: uploadMock
|
||||
}
|
||||
})
|
||||
|
||||
createMock.mockResolvedValue({
|
||||
data: {
|
||||
upload_url: url
|
||||
}
|
||||
})
|
||||
getMock.mockResolvedValue({
|
||||
data: {
|
||||
id: id
|
||||
}
|
||||
})
|
||||
updateMock.mockResolvedValue({
|
||||
data: {
|
||||
upload_url: url
|
||||
}
|
||||
})
|
||||
uploadMock.mockResolvedValue({})
|
||||
|
||||
const MockInputs = jest.fn<Inputs, any>(() => {
|
||||
return {
|
||||
allowUpdates: allowUpdates,
|
||||
artifacts: inputArtifact,
|
||||
body: body,
|
||||
commit: commit,
|
||||
|
||||
@@ -1,6 +1,33 @@
|
||||
import { ErrorMessage } from "../src/ErrorMessage"
|
||||
|
||||
describe('ErrorMessage', () => {
|
||||
|
||||
describe('has error with code', () => {
|
||||
const error = {
|
||||
message: 'something bad happened',
|
||||
errors: [
|
||||
{
|
||||
code: 'missing',
|
||||
resource: 'release'
|
||||
},
|
||||
{
|
||||
code: 'already_exists',
|
||||
resource: 'release'
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
it('does not have error', ()=> {
|
||||
const errorMessage = new ErrorMessage(error)
|
||||
expect(errorMessage.hasErrorWithCode('missing_field')).toBeFalsy()
|
||||
})
|
||||
|
||||
it('has error', ()=> {
|
||||
const errorMessage = new ErrorMessage(error)
|
||||
expect(errorMessage.hasErrorWithCode('missing')).toBeTruthy()
|
||||
})
|
||||
})
|
||||
|
||||
it('generates message with errors', () => {
|
||||
const resource = "release"
|
||||
const error = {
|
||||
|
||||
@@ -1,6 +1,17 @@
|
||||
import { GithubError } from "../src/GithubError"
|
||||
|
||||
describe('GithubError', () => {
|
||||
|
||||
it('provides error code', () => {
|
||||
const error = {
|
||||
code: "missing"
|
||||
}
|
||||
|
||||
const githubError = new GithubError(error)
|
||||
|
||||
expect(githubError.code).toBe('missing')
|
||||
})
|
||||
|
||||
it('generates missing resource error message', () => {
|
||||
const resource = "release"
|
||||
const error = {
|
||||
@@ -66,10 +77,10 @@ describe('GithubError', () => {
|
||||
message: "foo",
|
||||
documentation_url: url
|
||||
}
|
||||
|
||||
|
||||
const githubError = new GithubError(error)
|
||||
const message = githubError.toString()
|
||||
|
||||
|
||||
expect(message).toBe(`foo\nPlease see ${url}.`)
|
||||
})
|
||||
|
||||
@@ -78,10 +89,10 @@ describe('GithubError', () => {
|
||||
code: "custom",
|
||||
message: "foo"
|
||||
}
|
||||
|
||||
|
||||
const githubError = new GithubError(error)
|
||||
const message = githubError.toString()
|
||||
|
||||
|
||||
expect(message).toBe('foo')
|
||||
})
|
||||
})
|
||||
|
||||
@@ -43,6 +43,17 @@ describe('Inputs', () => {
|
||||
expect(inputs.token).toBe('42')
|
||||
})
|
||||
|
||||
describe('allowsUpdates', () => {
|
||||
it('returns false', () => {
|
||||
expect(inputs.allowUpdates).toBe(false)
|
||||
})
|
||||
|
||||
it('returns true', () => {
|
||||
mockGetInput.mockReturnValue('true')
|
||||
expect(inputs.allowUpdates).toBe(true)
|
||||
})
|
||||
})
|
||||
|
||||
describe('artifacts', () => {
|
||||
it('returns empty artifacts', () => {
|
||||
mockGetInput.mockReturnValueOnce('')
|
||||
|
||||
Reference in New Issue
Block a user