Fix tests

This commit is contained in:
Nick Cipollo
2019-12-13 15:52:52 -05:00
parent 734853d8c8
commit 5ce722314c
3 changed files with 36 additions and 27 deletions

View File

@@ -43,16 +43,12 @@ describe("Action", () => {
it('creates release if no release exists to update', async () => {
const action = createAction(true, true)
const error = {
errors: [
{
code: 'missing'
}
]
status: 404
}
getMock.mockRejectedValue(error)
await action.perform()
expect(createMock).toBeCalledWith(tag, body, commit, draft, name)
expect(uploadMock).toBeCalledWith(artifacts, url)
})

View File

@@ -14,15 +14,16 @@ describe('ErrorMessage', () => {
code: 'already_exists',
resource: 'release'
}
]
],
status: 422
}
it('does not have error', ()=> {
it('does not have error', () => {
const errorMessage = new ErrorMessage(error)
expect(errorMessage.hasErrorWithCode('missing_field')).toBeFalsy()
})
it('has error', ()=> {
it('has error', () => {
const errorMessage = new ErrorMessage(error)
expect(errorMessage.hasErrorWithCode('missing')).toBeTruthy()
})
@@ -41,22 +42,30 @@ describe('ErrorMessage', () => {
code: 'already_exists',
resource: 'release'
}
]
],
status: 422
}
const errorMessage = new ErrorMessage(error)
const expectedString = "something bad happened\nErrors:\n- release does not exist.\n- release already exists."
const expectedString = "Error 422: something bad happened\nErrors:\n- release does not exist.\n- release already exists."
expect(errorMessage.toString()).toBe(expectedString)
})
it('generates message without errors', () => {
const error = {
message: 'something bad happened'
message: 'something bad happened',
status: 422
}
const errorMessage = new ErrorMessage(error)
expect(errorMessage.toString()).toBe('something bad happened')
expect(errorMessage.toString()).toBe('Error 422: something bad happened')
})
it('provides error status', () => {
const error = { status: 404 }
const errorMessage = new ErrorMessage(error)
expect(errorMessage.status).toBe(404)
})
})

View File

@@ -27,16 +27,22 @@ class Action {
}
createOrUpdateRelease() {
return __awaiter(this, void 0, void 0, function* () {
try {
return yield this.createRelease();
if (this.inputs.allowUpdates) {
try {
const getResponse = yield this.releases.getByTag(this.inputs.tag);
return yield this.updateRelease(getResponse.data.id);
}
catch (error) {
if (this.noRelease(error)) {
return yield this.createRelease();
}
else {
throw error;
}
}
}
catch (error) {
if (this.releaseAlreadyExisted(error) && this.inputs.allowUpdates) {
return this.updateRelease();
}
else {
throw error;
}
else {
return yield this.createRelease();
}
});
}
@@ -46,14 +52,12 @@ class Action {
return response.data.upload_url;
});
}
releaseAlreadyExisted(error) {
noRelease(error) {
const errorMessage = new ErrorMessage_1.ErrorMessage(error);
return errorMessage.hasErrorWithCode('already_exists');
return errorMessage.status == 404;
}
updateRelease() {
updateRelease(id) {
return __awaiter(this, void 0, void 0, function* () {
const getResponse = yield this.releases.getByTag(this.inputs.tag);
const id = getResponse.data.id;
const response = yield this.releases.update(id, this.inputs.tag, this.inputs.body, this.inputs.commit, this.inputs.draft, this.inputs.name);
return response.data.upload_url;
});