Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

score.spec.js 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 Profile = require('../lib/models/profile')
  7. const ZipCode = require('../lib/models/zip-code')
  8. const MatchQueue = require('../lib/models/matchqueue')
  9. /**
  10. * Route parameters
  11. */
  12. const params = {
  13. profile_id: 1,
  14. max_distance: 1000,
  15. }
  16. const mockReturn = {
  17. user: [
  18. {
  19. profile_id: 1,
  20. user: { is_poster: 1 },
  21. responses: [
  22. { val: '120' },
  23. { val: '200' },
  24. { response_key_id: 16, val: '90065' },
  25. ],
  26. },
  27. {
  28. profile_id: 2,
  29. user: { is_poster: 0 },
  30. responses: [
  31. { val: '120' },
  32. { val: '200' },
  33. { response_key_id: 16, val: '96741' },
  34. ],
  35. },
  36. {
  37. profile_id: 3,
  38. user: { is_poster: 0 },
  39. responses: [
  40. { val: '180' },
  41. { val: '140' },
  42. { response_key_id: 16, val: '97002' },
  43. ],
  44. },
  45. ],
  46. }
  47. const pathToTest = {
  48. method: 'GET',
  49. url: `/${params.profile_id}/score?max_distance=${params.max_distance}`,
  50. }
  51. test(`path ${pathToTest.path} should return ok on GET`, async t => {
  52. /**
  53. * Create a new server and register services,
  54. * models and routes for testing
  55. * -
  56. * NOTE: We use a mocked registerModel() and register
  57. * models manually. Normally this is handled by
  58. * Schwifty at runtime.
  59. */
  60. const server = Hapi.server()
  61. /**
  62. * Overload so we don't register any models
  63. * using the plugin call (see plugins/profile.js)
  64. * and Manually load the model we need for the test
  65. */
  66. server.registerModel = () => {}
  67. server.models = () => ({ MatchQueue, Profile, ZipCode })
  68. /**
  69. * Register Routes and Services as usual
  70. */
  71. await plugin.register(server)
  72. /**
  73. * Replace Objection model methods with our own mock functions
  74. * !: Janky - might be better to temp knex sqlite instance
  75. */
  76. stub(server.models()['Profile'], 'query').returns({
  77. // Mocked for pathToTest(), scoreProfilesFor()
  78. findOne: () => ({
  79. withGraphFetched: () => ({
  80. withGraphFetched: () => mockReturn.user[0],
  81. }),
  82. }),
  83. // Mocked for scoreProfilesFor()
  84. withGraphFetched: () => ({
  85. withGraphFetched: () => [mockReturn.user[1], mockReturn.user[2]],
  86. }),
  87. // Mocked for _getProfileIdsForUserId()
  88. where: () => ({}),
  89. // Mocked for getCompleteProfilesFor(), getProfilesFor()
  90. whereIn: () => ({
  91. withGraphFetched: () => ({}),
  92. }),
  93. // Mocked for deleteProfile()
  94. delete: () => ({
  95. where: () => ({}),
  96. }),
  97. })
  98. stub(server.models()['ZipCode'], 'query').returns({
  99. // Mocked for _latLonForZip()
  100. findOne: () => ({
  101. latitude: 38.0 + Math.random(),
  102. longitude: -121.0 + Math.random(),
  103. }),
  104. })
  105. stub(server.models()['MatchQueue'], 'query').returns({
  106. patch: () => ({
  107. where: () => ({}),
  108. }),
  109. insert: () => ({}),
  110. where: () => ({
  111. andWhere: () => ({}),
  112. }),
  113. })
  114. /**
  115. * Test the server with registered models and services
  116. */
  117. const { payload } = await server.inject(pathToTest)
  118. const res = JSON.parse(payload)
  119. t.deepEqual(res.ok, true)
  120. t.is(res.data.length, 2)
  121. t.is(res.data[0].profile_id, 3)
  122. })