Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Nick Cipollo committed Dec 13, 2019
1 parent 734853d commit 5ce7223
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 26 deletions.
8 changes: 2 additions & 6 deletions __tests__/Action.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
Expand Down
23 changes: 16 additions & 7 deletions __tests__/ErrorMessage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})
Expand All @@ -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)
})
})
30 changes: 17 additions & 13 deletions lib/Action.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,23 @@ class Action {
}
createOrUpdateRelease() {
return __awaiter(this, void 0, void 0, function* () {
try {
return yield this.createRelease();
}
catch (error) {
if (this.releaseAlreadyExisted(error) && this.inputs.allowUpdates) {
return this.updateRelease();
if (this.inputs.allowUpdates) {
try {
const getResponse = yield this.releases.getByTag(this.inputs.tag);
return yield this.updateRelease(getResponse.data.id);
}
else {
throw error;
catch (error) {
if (this.noRelease(error)) {
return yield this.createRelease();
}
else {
throw error;
}
}
}
else {
return yield this.createRelease();
}
});
}
createRelease() {
Expand All @@ -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;
});
Expand Down

0 comments on commit 5ce7223

Please sign in to comment.