NotificationsControllerTest.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * decaffeinate suggestions:
  3. * DS102: Remove unnecessary code created because of implicit returns
  4. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  5. */
  6. const sinon = require('sinon');
  7. const chai = require('chai');
  8. const should = chai.should();
  9. const modulePath = "../../../app/js/NotificationsController.js";
  10. const SandboxedModule = require('sandboxed-module');
  11. const assert = require('assert');
  12. const user_id = "51dc93e6fb625a261300003b";
  13. const notification_id = "fb625a26f09d";
  14. const notification_key = "my-notification-key";
  15. describe('Notifications Controller', function() {
  16. beforeEach(function() {
  17. const self = this;
  18. this.notifications = {};
  19. this.controller = SandboxedModule.require(modulePath, { requires: {
  20. 'logger-sharelatex': { log() {}
  21. },
  22. './Notifications':this.notifications,
  23. 'metrics-sharelatex': {
  24. inc: sinon.stub()
  25. }
  26. }
  27. }
  28. );
  29. return this.stubbedNotification = [{key: notification_key, messageOpts:"some info", templateKey:"template-key"}];});
  30. describe("getUserNotifications", () =>
  31. it('should ask the notifications for the users notifications', function(done){
  32. this.notifications.getUserNotifications = sinon.stub().callsArgWith(1, null, this.stubbedNotification);
  33. const req = {
  34. params: {
  35. user_id
  36. }
  37. };
  38. return this.controller.getUserNotifications(req, { json:result=> {
  39. result.should.equal(this.stubbedNotification);
  40. this.notifications.getUserNotifications.calledWith(user_id).should.equal(true);
  41. return done();
  42. }
  43. }
  44. );
  45. })
  46. );
  47. describe("addNotification", () =>
  48. it("should tell the notifications to add the notification for the user", function(done){
  49. this.notifications.addNotification = sinon.stub().callsArgWith(2);
  50. const req = {
  51. params: {
  52. user_id
  53. },
  54. body: this.stubbedNotification
  55. };
  56. return this.controller.addNotification(req, { send:result=> {
  57. this.notifications.addNotification.calledWith(user_id, this.stubbedNotification).should.equal(true);
  58. return done();
  59. }
  60. }
  61. );
  62. })
  63. );
  64. describe("removeNotificationId", () =>
  65. it("should tell the notifications to mark the notification Id as read", function(done){
  66. this.notifications.removeNotificationId = sinon.stub().callsArgWith(2);
  67. const req = {
  68. params: {
  69. user_id,
  70. notification_id
  71. }
  72. };
  73. return this.controller.removeNotificationId(req, { send:result=> {
  74. this.notifications.removeNotificationId.calledWith(user_id, notification_id).should.equal(true);
  75. return done();
  76. }
  77. }
  78. );
  79. })
  80. );
  81. describe("removeNotificationKey", () =>
  82. it("should tell the notifications to mark the notification Key as read", function(done){
  83. this.notifications.removeNotificationKey = sinon.stub().callsArgWith(2);
  84. const req = {
  85. params: {
  86. user_id
  87. },
  88. body: {key: notification_key}
  89. };
  90. return this.controller.removeNotificationKey(req, { send:result=> {
  91. this.notifications.removeNotificationKey.calledWith(user_id, notification_key).should.equal(true);
  92. return done();
  93. }
  94. }
  95. );
  96. })
  97. );
  98. return describe("removeNotificationByKeyOnly", () =>
  99. it("should tell the notifications to mark the notification Key as read", function(done){
  100. this.notifications.removeNotificationByKeyOnly = sinon.stub().callsArgWith(1);
  101. const req = {
  102. params: {
  103. key: notification_key
  104. }
  105. };
  106. return this.controller.removeNotificationByKeyOnly(req, { send:result=> {
  107. this.notifications.removeNotificationByKeyOnly.calledWith(notification_key).should.equal(true);
  108. return done();
  109. }
  110. }
  111. );
  112. })
  113. );
  114. });