Skip to content

Commit

Permalink
Add tests for PATCH requests
Browse files Browse the repository at this point in the history
  • Loading branch information
nonword committed Nov 17, 2021
1 parent 3a2af73 commit 7342e20
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
language: node_js
install: npm install
script: npm test
24 changes: 24 additions & 0 deletions test/data/hold-requests%2F1234-patch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"data": {
"id": 1234,
"jobId": "1106896193cb7216d96",
"createdDate": "2021-11-16T10:17:06-05:00",
"updatedDate": "2021-11-16T10:17:12-05:00",
"success": false,
"processed": true,
"patron": "987654321",
"nyplSource": "sierra-nypl",
"requestType": "hold",
"recordType": "i",
"record": "15371503",
"pickupLocation": "",
"deliveryLocation": "NH",
"neededBy": "2022-11-16T00:00:00-05:00",
"numberOfCopies": 1,
"docDeliveryData": null,
"error": null
},
"count": 1,
"statusCode": 200,
"debugInfo": []
}
54 changes: 54 additions & 0 deletions test/patch-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
let client = null

describe('Client PATCH method', function () {
beforeEach(() => {
client = require('./make-test-client')()
})

afterEach(() => {
client = null
})

// This is a common patch that the
// [HoldRequestResultConsumer makes](https://github.com/NYPL/hold-request-result-consumer/blob/e7bd5b04afe25cf65ed2a5ce2de0576973e592cb/src/OAuthClient/HoldRequestClient.php#L123)
// makes when it detects a hold-request has been fulfilled
var holdRequestPatch = {
success: true,
processed: true
}

describe('when config.json=true (default)', function () {
it('should accept a new schema object via PATCH and return an object', function () {
return client.patch('hold-requests/1234', holdRequestPatch)
.then((resp) => {
expect(resp).to.be.a('object')
})
})

it('should fail if supplied body is plaintext', function () {
let call = client.patch('hold-requests/1234', JSON.stringify(holdRequestPatch))
return expect(call).to.be.rejected
})

// A null/empty body should be accepted as valid if options.json===true
it('should succeed if supplied body is empty', function () {
let call = client.patch('hold-requests/1234')
return expect(call).to.be.fulfilled
})
})

describe('when config.json=false', function () {
it('should accept a plaintext body and return plain text', function () {
let call = client.patch('hold-requests/1234', JSON.stringify(holdRequestPatch), { json: false })
return Promise.all([
expect(call).to.be.fulfilled,
expect(call).to.eventually.be.a('string')
])
})

it('should fail if supplied body is not plaintext', function () {
let call = client.patch('hold-requests/1234', holdRequestPatch, { json: false })
return expect(call).to.be.rejected
})
})
})

0 comments on commit 7342e20

Please sign in to comment.