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

This commit is contained in:
decaffeinate 2020-02-17 07:38:18 +00:00 committed by Simon Detheridge
parent 2c3d8739ed
commit 01e42e2b1c
2 changed files with 116 additions and 79 deletions

View file

@ -1,20 +1,38 @@
app = require('../../../app')
require("logger-sharelatex").logger.level("error")
/*
* decaffeinate suggestions:
* DS101: Remove unnecessary use of Array.from
* DS102: Remove unnecessary code created because of implicit returns
* DS205: Consider reworking code to avoid use of IIFEs
* DS207: Consider shorter variations of null checks
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
const app = require('../../../app');
require("logger-sharelatex").logger.level("error");
module.exports =
running: false
initing: false
callbacks: []
ensureRunning: (callback = (error) ->) ->
if @running
return callback()
else if @initing
@callbacks.push callback
else
@initing = true
@callbacks.push callback
app.listen 3036, "localhost", (error) =>
throw error if error?
@running = true
for callback in @callbacks
callback()
module.exports = {
running: false,
initing: false,
callbacks: [],
ensureRunning(callback) {
if (callback == null) { callback = function(error) {}; }
if (this.running) {
return callback();
} else if (this.initing) {
return this.callbacks.push(callback);
} else {
this.initing = true;
this.callbacks.push(callback);
return app.listen(3036, "localhost", error => {
if (error != null) { throw error; }
this.running = true;
return (() => {
const result = [];
for (callback of Array.from(this.callbacks)) {
result.push(callback());
}
return result;
})();
});
}
}
};

View file

@ -1,71 +1,90 @@
sinon = require "sinon"
chai = require("chai")
chai.should()
expect = chai.expect
ObjectId = require("mongojs").ObjectId
request = require "request"
async = require "async"
ContactsApp = require "./ContactsApp"
HOST = "http://localhost:3036"
/*
* decaffeinate suggestions:
* DS102: Remove unnecessary code created because of implicit returns
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
const sinon = require("sinon");
const chai = require("chai");
chai.should();
const {
expect
} = chai;
const {
ObjectId
} = require("mongojs");
const request = require("request");
const async = require("async");
const ContactsApp = require("./ContactsApp");
const HOST = "http://localhost:3036";
describe "Getting Contacts", ->
describe "with no contacts", ->
beforeEach (done)->
@user_id = ObjectId().toString()
ContactsApp.ensureRunning done
describe("Getting Contacts", function() {
describe("with no contacts", function() {
beforeEach(function(done){
this.user_id = ObjectId().toString();
return ContactsApp.ensureRunning(done);
});
it "should return an empty array", (done) ->
request {
method: "GET"
url: "#{HOST}/user/#{@user_id}/contacts"
return it("should return an empty array", function(done) {
return request({
method: "GET",
url: `${HOST}/user/${this.user_id}/contacts`,
json: true
}, (error, response, body) ->
response.statusCode.should.equal 200
body.contact_ids.should.deep.equal []
done()
}, function(error, response, body) {
response.statusCode.should.equal(200);
body.contact_ids.should.deep.equal([]);
return done();
});
});
});
describe "with contacts", ->
beforeEach (done) ->
@user_id = ObjectId().toString()
@contact_id_1 = ObjectId().toString()
@contact_id_2 = ObjectId().toString()
@contact_id_3 = ObjectId().toString()
return describe("with contacts", function() {
beforeEach(function(done) {
this.user_id = ObjectId().toString();
this.contact_id_1 = ObjectId().toString();
this.contact_id_2 = ObjectId().toString();
this.contact_id_3 = ObjectId().toString();
touchContact = (user_id, contact_id, cb) ->
request({
method: "POST"
url: "#{HOST}/user/#{user_id}/contacts"
json: {
contact_id: contact_id
}
}, cb)
const touchContact = (user_id, contact_id, cb) => request({
method: "POST",
url: `${HOST}/user/${user_id}/contacts`,
json: {
contact_id
}
}, cb);
async.series [
# 2 is preferred since touched twice, then 3 since most recent, then 1
(cb) => ContactsApp.ensureRunning cb
(cb) => touchContact @user_id, @contact_id_1, cb
(cb) => touchContact @user_id, @contact_id_2, cb
(cb) => touchContact @user_id, @contact_id_2, cb
(cb) => touchContact @user_id, @contact_id_3, cb
], done
return async.series([
// 2 is preferred since touched twice, then 3 since most recent, then 1
cb => ContactsApp.ensureRunning(cb),
cb => touchContact(this.user_id, this.contact_id_1, cb),
cb => touchContact(this.user_id, this.contact_id_2, cb),
cb => touchContact(this.user_id, this.contact_id_2, cb),
cb => touchContact(this.user_id, this.contact_id_3, cb)
], done);
});
it "should return a sorted list of contacts", (done) ->
request {
method: "GET"
url: "#{HOST}/user/#{@user_id}/contacts"
it("should return a sorted list of contacts", function(done) {
return request({
method: "GET",
url: `${HOST}/user/${this.user_id}/contacts`,
json: true
}, (error, response, body) =>
response.statusCode.should.equal 200
body.contact_ids.should.deep.equal [@contact_id_2, @contact_id_3, @contact_id_1]
done()
}, (error, response, body) => {
response.statusCode.should.equal(200);
body.contact_ids.should.deep.equal([this.contact_id_2, this.contact_id_3, this.contact_id_1]);
return done();
});
});
it "should respect a limit and only return top X contacts", ->
request {
method: "GET"
url: "#{HOST}/user/#{@user_id}/contacts?limit=2"
return it("should respect a limit and only return top X contacts", function() {
return request({
method: "GET",
url: `${HOST}/user/${this.user_id}/contacts?limit=2`,
json: true
}, (error, response, body) =>
response.statusCode.should.equal 200
body.contact_ids.should.deep.equal [@contact_id_2, @contact_id_3]
done()
}, (error, response, body) => {
response.statusCode.should.equal(200);
body.contact_ids.should.deep.equal([this.contact_id_2, this.contact_id_3]);
return done();
});
});
});
});