params-tests.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. const sinon = require('sinon');
  2. const proxyquire = require('proxyquire').noCallThru();
  3. const storage = {
  4. setField: sinon.stub()
  5. };
  6. function request(id) {
  7. return {
  8. params: { id },
  9. body: {}
  10. };
  11. }
  12. function response() {
  13. return {
  14. sendStatus: sinon.stub()
  15. };
  16. }
  17. const paramsRoute = proxyquire('../../server/routes/params', {
  18. '../storage': storage
  19. });
  20. describe('/api/params', function() {
  21. afterEach(function() {
  22. storage.setField.reset();
  23. });
  24. it('calls storage.setField with the correct parameter', function() {
  25. const req = request('x');
  26. const dlimit = 2;
  27. req.body.dlimit = dlimit;
  28. const res = response();
  29. paramsRoute(req, res);
  30. sinon.assert.calledWith(storage.setField, 'x', 'dlimit', dlimit);
  31. sinon.assert.calledWith(res.sendStatus, 200);
  32. });
  33. it('sends a 400 if dlimit is too large', function() {
  34. const req = request('x');
  35. const res = response();
  36. req.body.dlimit = 201;
  37. paramsRoute(req, res);
  38. sinon.assert.calledWith(res.sendStatus, 400);
  39. });
  40. it('sends a 404 on failure', function() {
  41. storage.setField.throws(new Error());
  42. const req = request('x');
  43. const res = response();
  44. req.body.dlimit = 2;
  45. paramsRoute(req, res);
  46. sinon.assert.calledWith(res.sendStatus, 404);
  47. });
  48. });