ModelTests.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. const { expect } = require('chai')
  2. const { User } = require('../../../app/src/models/User')
  3. const { Subscription } = require('../../../app/src/models/Subscription')
  4. describe('mongoose', function () {
  5. describe('User', function () {
  6. const email = 'wombat@potato.net'
  7. it('allows the creation of a user', async function () {
  8. await expect(User.create({ email })).to.be.fulfilled
  9. await expect(User.findOne({ email })).to.eventually.exist
  10. })
  11. it('does not allow the creation of multiple users with the same email', async function () {
  12. await expect(User.create({ email })).to.be.fulfilled
  13. await expect(User.create({ email })).to.be.rejected
  14. await expect(User.countDocuments({ email })).to.eventually.equal(1)
  15. })
  16. it('formats assignedAt as Date', async function () {
  17. await expect(
  18. User.create({
  19. email,
  20. splitTests: {
  21. 'some-test': [
  22. {
  23. variantName: 'control',
  24. versionNumber: 1,
  25. phase: 'release',
  26. assignedAt: '2021-09-24T11:53:18.313Z',
  27. },
  28. {
  29. variantName: 'control',
  30. versionNumber: 2,
  31. phase: 'release',
  32. assignedAt: new Date(),
  33. },
  34. ],
  35. },
  36. })
  37. ).to.be.fulfilled
  38. const user = await User.findOne({ email })
  39. expect(user.splitTests['some-test'][0].assignedAt).to.be.a('date')
  40. expect(user.splitTests['some-test'][1].assignedAt).to.be.a('date')
  41. })
  42. })
  43. describe('Subsription', function () {
  44. let user
  45. beforeEach(async function () {
  46. user = await User.create({ email: 'wombat@potato.net' })
  47. })
  48. it('allows the creation of a subscription', async function () {
  49. await expect(
  50. Subscription.create({ admin_id: user._id, manager_ids: [user._id] })
  51. ).to.be.fulfilled
  52. await expect(Subscription.findOne({ admin_id: user._id })).to.eventually
  53. .exist
  54. })
  55. it('does not allow the creation of a subscription without a manager', async function () {
  56. await expect(Subscription.create({ admin_id: user._id })).to.be.rejected
  57. })
  58. })
  59. })