UserMembershipViewModelTests.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /* eslint-disable
  2. n/handle-callback-err,
  3. max-len,
  4. no-return-assign,
  5. */
  6. // TODO: This file was created by bulk-decaffeinate.
  7. // Fix any style issues and re-enable lint.
  8. /*
  9. * decaffeinate suggestions:
  10. * DS102: Remove unnecessary code created because of implicit returns
  11. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  12. */
  13. const { expect } = require('chai')
  14. const sinon = require('sinon')
  15. const assertCalledWith = sinon.assert.calledWith
  16. const assertNotCalled = sinon.assert.notCalled
  17. const { ObjectId } = require('mongodb')
  18. const modulePath =
  19. '../../../../app/src/Features/UserMembership/UserMembershipViewModel'
  20. const SandboxedModule = require('sandboxed-module')
  21. const {
  22. isObjectIdInstance,
  23. normalizeQuery,
  24. } = require('../../../../app/src/Features/Helpers/Mongo')
  25. describe('UserMembershipViewModel', function () {
  26. beforeEach(function () {
  27. this.UserGetter = { getUser: sinon.stub() }
  28. this.UserMembershipViewModel = SandboxedModule.require(modulePath, {
  29. requires: {
  30. mongodb: { ObjectId },
  31. '../Helpers/Mongo': { isObjectIdInstance, normalizeQuery },
  32. '../User/UserGetter': this.UserGetter,
  33. },
  34. })
  35. this.email = 'mock-email@bar.com'
  36. this.user = {
  37. _id: 'mock-user-id',
  38. email: 'mock-email@baz.com',
  39. first_name: 'Name',
  40. lastLoggedIn: '2020-05-20T10:41:11.407Z',
  41. }
  42. })
  43. describe('build', function () {
  44. it('build email', function () {
  45. const viewModel = this.UserMembershipViewModel.build(this.email)
  46. return expect(viewModel).to.deep.equal({
  47. email: this.email,
  48. invite: true,
  49. last_active_at: null,
  50. last_logged_in_at: null,
  51. first_name: null,
  52. last_name: null,
  53. _id: null,
  54. })
  55. })
  56. it('build user', function () {
  57. const viewModel = this.UserMembershipViewModel.build(this.user)
  58. expect(viewModel).to.deep.equal({
  59. email: this.user.email,
  60. invite: false,
  61. last_active_at: this.user.lastLoggedIn,
  62. last_logged_in_at: this.user.lastLoggedIn,
  63. first_name: this.user.first_name,
  64. last_name: null,
  65. _id: this.user._id,
  66. })
  67. })
  68. })
  69. describe('build async', function () {
  70. beforeEach(function () {
  71. return (this.UserMembershipViewModel.build = sinon.stub())
  72. })
  73. it('build email', function (done) {
  74. return this.UserMembershipViewModel.buildAsync(
  75. this.email,
  76. (error, viewModel) => {
  77. assertCalledWith(this.UserMembershipViewModel.build, this.email)
  78. return done()
  79. }
  80. )
  81. })
  82. it('build user', function (done) {
  83. return this.UserMembershipViewModel.buildAsync(
  84. this.user,
  85. (error, viewModel) => {
  86. assertCalledWith(this.UserMembershipViewModel.build, this.user)
  87. return done()
  88. }
  89. )
  90. })
  91. it('build user id', function (done) {
  92. this.UserGetter.getUser.yields(null, this.user)
  93. return this.UserMembershipViewModel.buildAsync(
  94. ObjectId(),
  95. (error, viewModel) => {
  96. expect(error).not.to.exist
  97. assertNotCalled(this.UserMembershipViewModel.build)
  98. expect(viewModel._id).to.equal(this.user._id)
  99. expect(viewModel.email).to.equal(this.user.email)
  100. expect(viewModel.first_name).to.equal(this.user.first_name)
  101. expect(viewModel.invite).to.equal(false)
  102. expect(viewModel.email).to.exist
  103. return done()
  104. }
  105. )
  106. })
  107. it('build user id with error', function (done) {
  108. this.UserGetter.getUser.yields(new Error('nope'))
  109. const userId = ObjectId()
  110. return this.UserMembershipViewModel.buildAsync(
  111. userId,
  112. (error, viewModel) => {
  113. expect(error).not.to.exist
  114. assertNotCalled(this.UserMembershipViewModel.build)
  115. expect(viewModel._id).to.equal(userId.toString())
  116. expect(viewModel.email).not.to.exist
  117. return done()
  118. }
  119. )
  120. })
  121. })
  122. })