decaffeinate: Convert NotificationsControllerTest.coffee and 1 other file to JS

This commit is contained in:
decaffeinate 2020-01-13 20:01:07 +01:00 committed by Tim Alby
parent d4fa6e2672
commit 34a61b9c6b
2 changed files with 309 additions and 217 deletions

View file

@ -1,76 +1,122 @@
sinon = require('sinon') /*
chai = require('chai') * decaffeinate suggestions:
should = chai.should() * DS102: Remove unnecessary code created because of implicit returns
modulePath = "../../../app/js/NotificationsController.js" * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
SandboxedModule = require('sandboxed-module') */
assert = require('assert') const sinon = require('sinon');
const chai = require('chai');
const should = chai.should();
const modulePath = "../../../app/js/NotificationsController.js";
const SandboxedModule = require('sandboxed-module');
const assert = require('assert');
user_id = "51dc93e6fb625a261300003b" const user_id = "51dc93e6fb625a261300003b";
notification_id = "fb625a26f09d" const notification_id = "fb625a26f09d";
notification_key = "my-notification-key" const notification_key = "my-notification-key";
describe 'Notifications Controller', -> describe('Notifications Controller', function() {
beforeEach -> beforeEach(function() {
self = @ const self = this;
@notifications = {} this.notifications = {};
@controller = SandboxedModule.require modulePath, requires: this.controller = SandboxedModule.require(modulePath, { requires: {
'logger-sharelatex': log:-> 'logger-sharelatex': { log() {}
'./Notifications':@notifications },
'metrics-sharelatex': './Notifications':this.notifications,
'metrics-sharelatex': {
inc: sinon.stub() inc: sinon.stub()
}
}
}
);
@stubbedNotification = [{key: notification_key, messageOpts:"some info", templateKey:"template-key"}] return this.stubbedNotification = [{key: notification_key, messageOpts:"some info", templateKey:"template-key"}];});
describe "getUserNotifications", -> describe("getUserNotifications", () =>
it 'should ask the notifications for the users notifications', (done)-> it('should ask the notifications for the users notifications', function(done){
@notifications.getUserNotifications = sinon.stub().callsArgWith(1, null, @stubbedNotification) this.notifications.getUserNotifications = sinon.stub().callsArgWith(1, null, this.stubbedNotification);
req = const req = {
params: params: {
user_id: user_id user_id
@controller.getUserNotifications req, json:(result)=> }
result.should.equal @stubbedNotification };
@notifications.getUserNotifications.calledWith(user_id).should.equal true return this.controller.getUserNotifications(req, { json:result=> {
done() result.should.equal(this.stubbedNotification);
this.notifications.getUserNotifications.calledWith(user_id).should.equal(true);
return done();
}
}
);
})
);
describe "addNotification", -> describe("addNotification", () =>
it "should tell the notifications to add the notification for the user", (done)-> it("should tell the notifications to add the notification for the user", function(done){
@notifications.addNotification = sinon.stub().callsArgWith(2) this.notifications.addNotification = sinon.stub().callsArgWith(2);
req = const req = {
params: params: {
user_id: user_id user_id
body: @stubbedNotification },
@controller.addNotification req, send:(result)=> body: this.stubbedNotification
@notifications.addNotification.calledWith(user_id, @stubbedNotification).should.equal true };
done() return this.controller.addNotification(req, { send:result=> {
this.notifications.addNotification.calledWith(user_id, this.stubbedNotification).should.equal(true);
return done();
}
}
);
})
);
describe "removeNotificationId", -> describe("removeNotificationId", () =>
it "should tell the notifications to mark the notification Id as read", (done)-> it("should tell the notifications to mark the notification Id as read", function(done){
@notifications.removeNotificationId = sinon.stub().callsArgWith(2) this.notifications.removeNotificationId = sinon.stub().callsArgWith(2);
req = const req = {
params: params: {
user_id: user_id user_id,
notification_id: notification_id notification_id
@controller.removeNotificationId req, send:(result)=> }
@notifications.removeNotificationId.calledWith(user_id, notification_id).should.equal true };
done() return this.controller.removeNotificationId(req, { send:result=> {
this.notifications.removeNotificationId.calledWith(user_id, notification_id).should.equal(true);
return done();
}
}
);
})
);
describe "removeNotificationKey", -> describe("removeNotificationKey", () =>
it "should tell the notifications to mark the notification Key as read", (done)-> it("should tell the notifications to mark the notification Key as read", function(done){
@notifications.removeNotificationKey = sinon.stub().callsArgWith(2) this.notifications.removeNotificationKey = sinon.stub().callsArgWith(2);
req = const req = {
params: params: {
user_id: user_id user_id
},
body: {key: notification_key} body: {key: notification_key}
@controller.removeNotificationKey req, send:(result)=> };
@notifications.removeNotificationKey.calledWith(user_id, notification_key).should.equal true return this.controller.removeNotificationKey(req, { send:result=> {
done() this.notifications.removeNotificationKey.calledWith(user_id, notification_key).should.equal(true);
return done();
}
}
);
})
);
describe "removeNotificationByKeyOnly", -> return describe("removeNotificationByKeyOnly", () =>
it "should tell the notifications to mark the notification Key as read", (done)-> it("should tell the notifications to mark the notification Key as read", function(done){
@notifications.removeNotificationByKeyOnly = sinon.stub().callsArgWith(1) this.notifications.removeNotificationByKeyOnly = sinon.stub().callsArgWith(1);
req = const req = {
params: params: {
key: notification_key key: notification_key
@controller.removeNotificationByKeyOnly req, send:(result)=> }
@notifications.removeNotificationByKeyOnly.calledWith(notification_key).should.equal true };
done() return this.controller.removeNotificationByKeyOnly(req, { send:result=> {
this.notifications.removeNotificationByKeyOnly.calledWith(notification_key).should.equal(true);
return done();
}
}
);
})
);
});

View file

@ -1,203 +1,249 @@
sinon = require('sinon') /*
chai = require('chai') * decaffeinate suggestions:
expect = chai.should * DS102: Remove unnecessary code created because of implicit returns
should = chai.should() * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
modulePath = "../../../app/js/Notifications.js" */
SandboxedModule = require('sandboxed-module') const sinon = require('sinon');
assert = require('assert') const chai = require('chai');
ObjectId = require("mongojs").ObjectId const expect = chai.should;
const should = chai.should();
const modulePath = "../../../app/js/Notifications.js";
const SandboxedModule = require('sandboxed-module');
const assert = require('assert');
const { ObjectId } = require("mongojs");
user_id = "51dc93e6fb625a261300003b" const user_id = "51dc93e6fb625a261300003b";
notification_id = "fb625a26f09d" const notification_id = "fb625a26f09d";
notification_key = "notification-key" const notification_key = "notification-key";
describe 'Notifications Tests', -> describe('Notifications Tests', function() {
beforeEach -> beforeEach(function() {
self = @ const self = this;
@findStub = sinon.stub() this.findStub = sinon.stub();
@insertStub = sinon.stub() this.insertStub = sinon.stub();
@countStub = sinon.stub() this.countStub = sinon.stub();
@updateStub = sinon.stub() this.updateStub = sinon.stub();
@removeStub = sinon.stub() this.removeStub = sinon.stub();
@mongojs = => this.mongojs = () => {
notifications: return {
update: self.mongojsUpdate notifications: {
find: @findStub update: self.mongojsUpdate,
insert: @insertStub find: this.findStub,
count: @countStub insert: this.insertStub,
update: @updateStub count: this.countStub,
remove: @removeStub update: this.updateStub,
@mongojs.ObjectId = ObjectId remove: this.removeStub
@notifications = SandboxedModule.require modulePath,
requires:
'logger-sharelatex': {
log:()->
error:()->
} }
'settings-sharelatex': {} };
'mongojs':@mongojs };
this.mongojs.ObjectId = ObjectId;
this.notifications = SandboxedModule.require(modulePath, {
requires: {
'logger-sharelatex': {
log(){},
error(){}
},
'settings-sharelatex': {},
'mongojs':this.mongojs,
'metrics-sharelatex': {timeAsyncMethod: sinon.stub()} 'metrics-sharelatex': {timeAsyncMethod: sinon.stub()}
globals: },
console: console globals: {
console
}
}
);
@stubbedNotification = {user_id: ObjectId(user_id), key:"notification-key", messageOpts:"some info", templateKey:"template-key"} this.stubbedNotification = {user_id: ObjectId(user_id), key:"notification-key", messageOpts:"some info", templateKey:"template-key"};
@stubbedNotificationArray = [@stubbedNotification] return this.stubbedNotificationArray = [this.stubbedNotification];});
describe 'getUserNotifications', -> describe('getUserNotifications', () =>
it "should find all notifications and return i", (done)-> it("should find all notifications and return i", function(done){
@findStub.callsArgWith(1, null, @stubbedNotificationArray) this.findStub.callsArgWith(1, null, this.stubbedNotificationArray);
@notifications.getUserNotifications user_id, (err, notifications)=> return this.notifications.getUserNotifications(user_id, (err, notifications)=> {
notifications.should.equal @stubbedNotificationArray notifications.should.equal(this.stubbedNotificationArray);
assert.deepEqual(@findStub.args[0][0], {"user_id" :ObjectId(user_id), "templateKey": {"$exists":true}}) assert.deepEqual(this.findStub.args[0][0], {"user_id" :ObjectId(user_id), "templateKey": {"$exists":true}});
done() return done();
});
})
);
describe 'addNotification', -> describe('addNotification', function() {
beforeEach -> beforeEach(function() {
@stubbedNotification = { this.stubbedNotification = {
user_id: ObjectId(user_id), user_id: ObjectId(user_id),
key:"notification-key", key:"notification-key",
messageOpts:"some info", messageOpts:"some info",
templateKey:"template-key" templateKey:"template-key"
} };
@expectedDocument = { this.expectedDocument = {
user_id: @stubbedNotification.user_id, user_id: this.stubbedNotification.user_id,
key:"notification-key", key:"notification-key",
messageOpts:"some info", messageOpts:"some info",
templateKey:"template-key" templateKey:"template-key"
} };
@expectedQuery = { this.expectedQuery = {
user_id: @stubbedNotification.user_id, user_id: this.stubbedNotification.user_id,
key:"notification-key", key:"notification-key",
} };
@updateStub.yields() this.updateStub.yields();
@countStub.yields(null, 0) return this.countStub.yields(null, 0);
});
it 'should insert the notification into the collection', (done)-> it('should insert the notification into the collection', function(done){
@notifications.addNotification user_id, @stubbedNotification, (err)=> return this.notifications.addNotification(user_id, this.stubbedNotification, err=> {
expect(err).not.exists expect(err).not.exists;
sinon.assert.calledWith(@updateStub, @expectedQuery, @expectedDocument, { upsert: true }) sinon.assert.calledWith(this.updateStub, this.expectedQuery, this.expectedDocument, { upsert: true });
done() return done();
});
});
describe 'when there is an existing notification', (done) -> describe('when there is an existing notification', function(done) {
beforeEach -> beforeEach(function() {
@countStub.yields(null, 1) return this.countStub.yields(null, 1);
});
it 'should fail to insert', (done)-> it('should fail to insert', function(done){
@notifications.addNotification user_id, @stubbedNotification, (err)=> return this.notifications.addNotification(user_id, this.stubbedNotification, err=> {
expect(err).not.exists expect(err).not.exists;
sinon.assert.notCalled(@updateStub) sinon.assert.notCalled(this.updateStub);
done() return done();
});
});
it "should update the key if forceCreate is true", (done) -> return it("should update the key if forceCreate is true", function(done) {
@stubbedNotification.forceCreate = true this.stubbedNotification.forceCreate = true;
@notifications.addNotification user_id, @stubbedNotification, (err)=> return this.notifications.addNotification(user_id, this.stubbedNotification, err=> {
expect(err).not.exists expect(err).not.exists;
sinon.assert.calledWith(@updateStub, @expectedQuery, @expectedDocument, { upsert: true }) sinon.assert.calledWith(this.updateStub, this.expectedQuery, this.expectedDocument, { upsert: true });
done() return done();
});
});
});
describe 'when the notification is set to expire', () -> describe('when the notification is set to expire', function() {
beforeEach -> beforeEach(function() {
@stubbedNotification = { this.stubbedNotification = {
user_id: ObjectId(user_id), user_id: ObjectId(user_id),
key:"notification-key", key:"notification-key",
messageOpts:"some info", messageOpts:"some info",
templateKey:"template-key", templateKey:"template-key",
expires: '2922-02-13T09:32:56.289Z' expires: '2922-02-13T09:32:56.289Z'
} };
@expectedDocument = { this.expectedDocument = {
user_id: @stubbedNotification.user_id, user_id: this.stubbedNotification.user_id,
key:"notification-key", key:"notification-key",
messageOpts:"some info", messageOpts:"some info",
templateKey:"template-key", templateKey:"template-key",
expires: new Date(@stubbedNotification.expires), expires: new Date(this.stubbedNotification.expires),
} };
@expectedQuery = { return this.expectedQuery = {
user_id: @stubbedNotification.user_id, user_id: this.stubbedNotification.user_id,
key:"notification-key", key:"notification-key",
} };});
it 'should add an `expires` Date field to the document', (done)-> return it('should add an `expires` Date field to the document', function(done){
@notifications.addNotification user_id, @stubbedNotification, (err)=> return this.notifications.addNotification(user_id, this.stubbedNotification, err=> {
expect(err).not.exists expect(err).not.exists;
sinon.assert.calledWith(@updateStub, @expectedQuery, @expectedDocument, { upsert: true }) sinon.assert.calledWith(this.updateStub, this.expectedQuery, this.expectedDocument, { upsert: true });
done() return done();
});
});
});
describe 'when the notification has a nonsensical expires field', () -> return describe('when the notification has a nonsensical expires field', function() {
beforeEach -> beforeEach(function() {
@stubbedNotification = { this.stubbedNotification = {
user_id: ObjectId(user_id), user_id: ObjectId(user_id),
key:"notification-key", key:"notification-key",
messageOpts:"some info", messageOpts:"some info",
templateKey:"template-key", templateKey:"template-key",
expires: 'WAT' expires: 'WAT'
} };
@expectedDocument = { return this.expectedDocument = {
user_id: @stubbedNotification.user_id, user_id: this.stubbedNotification.user_id,
key:"notification-key", key:"notification-key",
messageOpts:"some info", messageOpts:"some info",
templateKey:"template-key", templateKey:"template-key",
expires: new Date(@stubbedNotification.expires), expires: new Date(this.stubbedNotification.expires),
} };});
it 'should produce an error', (done)-> return it('should produce an error', function(done){
@notifications.addNotification user_id, @stubbedNotification, (err)=> return this.notifications.addNotification(user_id, this.stubbedNotification, err=> {
(err instanceof Error).should.equal true (err instanceof Error).should.equal(true);
sinon.assert.notCalled(@updateStub) sinon.assert.notCalled(this.updateStub);
done() return done();
});
});
});
});
describe 'removeNotificationId', -> describe('removeNotificationId', () =>
it 'should mark the notification id as read', (done)-> it('should mark the notification id as read', function(done){
@updateStub.callsArgWith(2, null) this.updateStub.callsArgWith(2, null);
@notifications.removeNotificationId user_id, notification_id, (err)=> return this.notifications.removeNotificationId(user_id, notification_id, err=> {
searchOps = const searchOps = {
user_id:ObjectId(user_id) user_id:ObjectId(user_id),
_id:ObjectId(notification_id) _id:ObjectId(notification_id)
updateOperation = };
"$unset": {templateKey:true, messageOpts:true} const updateOperation =
assert.deepEqual(@updateStub.args[0][0], searchOps) {"$unset": {templateKey:true, messageOpts:true}};
assert.deepEqual(@updateStub.args[0][1], updateOperation) assert.deepEqual(this.updateStub.args[0][0], searchOps);
done() assert.deepEqual(this.updateStub.args[0][1], updateOperation);
return done();
});
})
);
describe 'removeNotificationKey', -> describe('removeNotificationKey', () =>
it 'should mark the notification key as read', (done)-> it('should mark the notification key as read', function(done){
@updateStub.callsArgWith(2, null) this.updateStub.callsArgWith(2, null);
@notifications.removeNotificationKey user_id, notification_key, (err)=> return this.notifications.removeNotificationKey(user_id, notification_key, err=> {
searchOps = { const searchOps = {
user_id:ObjectId(user_id) user_id:ObjectId(user_id),
key: notification_key key: notification_key
} };
updateOperation = { const updateOperation = {
"$unset": {templateKey:true} "$unset": {templateKey:true}
} };
assert.deepEqual(@updateStub.args[0][0], searchOps) assert.deepEqual(this.updateStub.args[0][0], searchOps);
assert.deepEqual(@updateStub.args[0][1], updateOperation) assert.deepEqual(this.updateStub.args[0][1], updateOperation);
done() return done();
});
})
);
describe 'removeNotificationByKeyOnly', -> describe('removeNotificationByKeyOnly', () =>
it 'should mark the notification key as read', (done)-> it('should mark the notification key as read', function(done){
@updateStub.callsArgWith(2, null) this.updateStub.callsArgWith(2, null);
@notifications.removeNotificationByKeyOnly notification_key, (err)=> return this.notifications.removeNotificationByKeyOnly(notification_key, err=> {
searchOps = const searchOps =
key: notification_key {key: notification_key};
updateOperation = const updateOperation =
"$unset": {templateKey:true} {"$unset": {templateKey:true}};
assert.deepEqual(@updateStub.args[0][0], searchOps) assert.deepEqual(this.updateStub.args[0][0], searchOps);
assert.deepEqual(@updateStub.args[0][1], updateOperation) assert.deepEqual(this.updateStub.args[0][1], updateOperation);
done() return done();
});
})
);
describe 'deleteNotificationByKeyOnly', -> return describe('deleteNotificationByKeyOnly', () =>
it 'should completely remove the notification', (done)-> it('should completely remove the notification', function(done){
@removeStub.callsArgWith(2, null) this.removeStub.callsArgWith(2, null);
@notifications.deleteNotificationByKeyOnly notification_key, (err)=> return this.notifications.deleteNotificationByKeyOnly(notification_key, err=> {
searchOps = const searchOps =
key: notification_key {key: notification_key};
opts = const opts =
justOne: true {justOne: true};
assert.deepEqual(@removeStub.args[0][0], searchOps) assert.deepEqual(this.removeStub.args[0][0], searchOps);
assert.deepEqual(@removeStub.args[0][1], opts) assert.deepEqual(this.removeStub.args[0][1], opts);
done() return done();
});
})
);
});