您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

profile.spec.js 2.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 Tag = require('../lib/models/tag')
  8. /**
  9. * Route parameters
  10. */
  11. const params = {
  12. profile_id: 1,
  13. target_id: 2,
  14. reinsert: true,
  15. include_profile: false,
  16. }
  17. const mockReturn = {
  18. profile: {
  19. profile_id: 99,
  20. user: {},
  21. responses: [],
  22. tags: [],
  23. },
  24. tags: [],
  25. }
  26. const pathToTest = {
  27. method: 'GET',
  28. url: `/${params.profile_id}`,
  29. }
  30. test('path /<profile_id> should return ok', async t => {
  31. /**
  32. * Create a new server and register services,
  33. * models and routes for testing
  34. * -
  35. * NOTE: We use a mocked registerModel() and register
  36. * models manually. Normally this is handled by
  37. * Schwifty at runtime.
  38. */
  39. const server = Hapi.server()
  40. /**
  41. * Overload so we don't register any models
  42. * using the plugin call (see plugins/profile.js)
  43. * and Manually load the model we need for the test
  44. */
  45. server.registerModel = () => {}
  46. server.models = () => ({ Profile, Tag })
  47. /**
  48. * Register Routes and Services as usual
  49. */
  50. await plugin.register(server)
  51. /**
  52. * Replace Objection model methods with our own mock functions
  53. * !: Janky - might be better to temp knex sqlite instance
  54. */
  55. stub(server.models()['Tag'], 'query').returns(mockReturn.tags)
  56. stub(server.models()['Profile'], 'query').returns({
  57. // Mocked for getProfile()
  58. where: () => ({
  59. first: () => ({
  60. withGraphFetched: () => ({
  61. withGraphFetched: () => ({
  62. withGraphFetched: () => mockReturn.profile,
  63. }),
  64. }),
  65. }),
  66. }),
  67. })
  68. /**
  69. * Test the server with registered models and services
  70. */
  71. const { payload } = await server.inject(pathToTest)
  72. const res = JSON.parse(payload)
  73. t.deepEqual(res.ok, true)
  74. t.deepEqual(res.data.profile_id, mockReturn.profile.profile_id)
  75. t.notDeepEqual(res.data, mockReturn.profile)
  76. t.true(Object.keys(res.data).includes('profile_description'))
  77. t.true(Object.keys(res.data).includes('profile_languages'))
  78. })