UserMembershipViewModelTests.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /* eslint-disable
  2. 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 chai = require('chai')
  14. const should = chai.should()
  15. const { expect } = require('chai')
  16. const sinon = require('sinon')
  17. const assertCalledWith = sinon.assert.calledWith
  18. const assertNotCalled = sinon.assert.notCalled
  19. const mongojs = require('mongojs')
  20. const { ObjectId } = mongojs
  21. const modulePath =
  22. '../../../../app/src/Features/UserMembership/UserMembershipViewModel'
  23. const SandboxedModule = require('sandboxed-module')
  24. describe('UserMembershipViewModel', function() {
  25. beforeEach(function() {
  26. this.UserGetter = { getUser: sinon.stub() }
  27. this.UserMembershipViewModel = SandboxedModule.require(modulePath, {
  28. globals: {
  29. console: console
  30. },
  31. requires: {
  32. mongojs: mongojs,
  33. '../User/UserGetter': this.UserGetter
  34. }
  35. })
  36. this.email = 'mock-email@bar.com'
  37. this.user = {
  38. _id: 'mock-user-id',
  39. email: 'mock-email@baz.com',
  40. first_name: 'Name'
  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. first_name: null,
  50. last_name: null,
  51. _id: null
  52. })
  53. })
  54. it('build user', function() {
  55. const viewModel = this.UserMembershipViewModel.build(this.user)
  56. expect(viewModel._id).to.equal(this.user._id)
  57. expect(viewModel.email).to.equal(this.user.email)
  58. return expect(viewModel.invite).to.equal(false)
  59. })
  60. })
  61. describe('build async', function() {
  62. beforeEach(function() {
  63. return (this.UserMembershipViewModel.build = sinon.stub())
  64. })
  65. it('build email', function(done) {
  66. return this.UserMembershipViewModel.buildAsync(
  67. this.email,
  68. (error, viewModel) => {
  69. assertCalledWith(this.UserMembershipViewModel.build, this.email)
  70. return done()
  71. }
  72. )
  73. })
  74. it('build user', function(done) {
  75. return this.UserMembershipViewModel.buildAsync(
  76. this.user,
  77. (error, viewModel) => {
  78. assertCalledWith(this.UserMembershipViewModel.build, this.user)
  79. return done()
  80. }
  81. )
  82. })
  83. it('build user id', function(done) {
  84. this.UserGetter.getUser.yields(null, this.user)
  85. return this.UserMembershipViewModel.buildAsync(
  86. ObjectId(),
  87. (error, viewModel) => {
  88. should.not.exist(error)
  89. assertNotCalled(this.UserMembershipViewModel.build)
  90. expect(viewModel._id).to.equal(this.user._id)
  91. expect(viewModel.email).to.equal(this.user.email)
  92. expect(viewModel.first_name).to.equal(this.user.first_name)
  93. expect(viewModel.invite).to.equal(false)
  94. should.exist(viewModel.email)
  95. return done()
  96. }
  97. )
  98. })
  99. it('build user id with error', function(done) {
  100. this.UserGetter.getUser.yields(new Error('nope'))
  101. const userId = ObjectId()
  102. return this.UserMembershipViewModel.buildAsync(
  103. userId,
  104. (error, viewModel) => {
  105. should.not.exist(error)
  106. assertNotCalled(this.UserMembershipViewModel.build)
  107. expect(viewModel._id).to.equal(userId.toString())
  108. should.not.exist(viewModel.email)
  109. return done()
  110. }
  111. )
  112. })
  113. })
  114. })