TutorialHandlerTests.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. const SandboxedModule = require('sandboxed-module')
  2. const { expect } = require('chai')
  3. const sinon = require('sinon')
  4. const { ObjectId } = require('mongodb-legacy')
  5. const MODULE_PATH = '../../../../app/src/Features/Tutorial/TutorialHandler'
  6. describe('TutorialHandler', function () {
  7. beforeEach(function () {
  8. this.clock = sinon.useFakeTimers()
  9. const THIRTY_DAYS_AGO = Date.now() - 30 * 24 * 60 * 60 * 1000
  10. const TOMORROW = Date.now() + 24 * 60 * 60 * 1000
  11. const YESTERDAY = Date.now() - 24 * 60 * 60 * 1000
  12. this.user = {
  13. _id: new ObjectId(),
  14. completedTutorials: {
  15. 'legacy-format': new Date(Date.now() - 1000),
  16. completed: {
  17. state: 'completed',
  18. updatedAt: new Date(Date.now() - 1000),
  19. },
  20. 'postponed-recently': {
  21. state: 'postponed',
  22. updatedAt: new Date(Date.now() - 1000),
  23. },
  24. 'postponed-long-ago': {
  25. state: 'postponed',
  26. updatedAt: new Date(THIRTY_DAYS_AGO),
  27. },
  28. 'postponed-until-tomorrow': {
  29. state: 'postponed',
  30. updatedAt: new Date(THIRTY_DAYS_AGO),
  31. postponedUntil: new Date(TOMORROW),
  32. },
  33. 'postponed-until-yesterday': {
  34. state: 'postponed',
  35. updatedAt: new Date(THIRTY_DAYS_AGO),
  36. postponedUntil: new Date(YESTERDAY),
  37. },
  38. },
  39. }
  40. this.UserUpdater = {
  41. promises: {
  42. updateUser: sinon.stub().resolves(),
  43. },
  44. }
  45. this.TutorialHandler = SandboxedModule.require(MODULE_PATH, {
  46. requires: {
  47. '../User/UserUpdater': this.UserUpdater,
  48. },
  49. })
  50. })
  51. afterEach(function () {
  52. this.clock.restore()
  53. })
  54. describe('getInactiveTutorials', function () {
  55. it('returns all recorded tutorials except when they were posponed long ago', function () {
  56. const hiddenTutorials = this.TutorialHandler.getInactiveTutorials(
  57. this.user
  58. )
  59. expect(hiddenTutorials).to.have.members([
  60. 'legacy-format',
  61. 'completed',
  62. 'postponed-recently',
  63. 'postponed-until-tomorrow',
  64. ])
  65. expect(hiddenTutorials).to.have.lengthOf(4)
  66. const shownTutorials = Object.keys(this.user.completedTutorials).filter(
  67. key => !hiddenTutorials.includes(key)
  68. )
  69. expect(shownTutorials).to.have.members([
  70. 'postponed-long-ago',
  71. 'postponed-until-yesterday',
  72. ])
  73. expect(shownTutorials).to.have.lengthOf(2)
  74. })
  75. })
  76. })