You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. 'use strict'
  2. const test = require('ava')
  3. const { stub } = require('sinon')
  4. const Hapi = require('@hapi/hapi')
  5. const plugin = require('../lib/plugins/profile')
  6. const Response = require('../lib/models/response')
  7. const ResponseKey = require('../lib/models/response-key')
  8. /**
  9. * Route parameters
  10. */
  11. const params = {
  12. profile_id: 38,
  13. }
  14. const mockReturn = {
  15. responses: [
  16. {
  17. response_id: 1,
  18. profile_id: 38,
  19. response_key_id: 6,
  20. val: '2056',
  21. },
  22. {
  23. response_id: 2,
  24. profile_id: 38,
  25. response_key_id: 7,
  26. val: '90012',
  27. },
  28. ],
  29. }
  30. const pathToTest = {
  31. method: 'POST',
  32. url: `/${params.profile_id}/respond?response_key_id=5&val=2053`,
  33. }
  34. test('path /<profile_id>/respond should return ok on POST', async t => {
  35. /**
  36. * Create a new server and register services,
  37. * models and routes for testing
  38. * -
  39. * NOTE: We use a mocked registerModel() and register
  40. * models manually. Normally this is handled by
  41. * Schwifty at runtime.
  42. */
  43. const server = Hapi.server()
  44. /**
  45. * Overload so we don't register any models
  46. * using the plugin call (see plugins/profile.js)
  47. * and Manually load the model we need for the test
  48. */
  49. server.registerModel = () => {}
  50. server.models = () => ({ Response, ResponseKey })
  51. /**
  52. * Register Routes and Services as usual
  53. */
  54. await plugin.register(server)
  55. /**
  56. * Replace Objection model methods with our own mock functions
  57. * !: Janky - might be better to temp knex sqlite instance
  58. */
  59. stub(server.models()['Response'], 'query').returns({
  60. where: () => mockReturn.responses,
  61. insert: response => {
  62. response.response_id = 3
  63. mockReturn.responses.push(response)
  64. },
  65. })
  66. /**
  67. * Test the server with registered models and services
  68. */
  69. const { payload } = await server.inject(pathToTest)
  70. const res = JSON.parse(payload)
  71. t.deepEqual(res.ok, true)
  72. t.deepEqual(res.data, mockReturn.responses)
  73. server.stop()
  74. })