I have the following code that works however I am having trouble getting the test to work. In my test result is always {}. This particular block of code is necessitated by the requirement that code from MM only run on the server.
// mm.js
const MM = {};
MM.producerDetailsPromise = function(id) {
check(id, Number);
const params = id;
const path = Meteor.settings.marketMakerApi.businessDetails + params;
const opts = {
hostname: Meteor.settings.marketMakerApi.hostname,
port: 80,
path: path,
method: 'GET'
};
return new Promise((resolve, reject) => {
const request = http.get(opts, (response) => {
if (response.statusCode < 200 || response.statusCode > 299) {
reject(new Meteor.Error('Failed to get response, status code: ' + response.statusCode));
}
const jsonBodyChunks = [];
response.on('data', chunk => jsonBodyChunks.push(chunk));
response.on('end', () => resolve(jsonBodyChunks.join('')));
});
request.on('error', err => reject(err));
});
};
export default MM;
// mm.methods.js
import MM from '../mm.js';
Meteor.methods({
'MM.producerDetailsPromise': async function(producerId) {
check(producerId, Number);
const detailedProducer = await MM.producerDetailsPromise(producerId).catch((error) => {
throw error;
});
return detailedProducer;
}
});
// mm.methods.test.js
import MM from '../mm';
import './mm.methods.js';
describe("MM methods", function() {
it("can get producer details", function(done) {
const producerId = 3625405;
Meteor.call('MM.producerDetailsPromise', producerId, (err, result) => {
debugLogger('================================================');
debugLogger(`MM.producerDetailsPromise in the callback`);
// result is always {}
debugLogger(`MM.producerDetailsPromise result: ${JSON.stringify(result)}`);
if (err) {
debugLogger(`MM.producerDetailsPromise err: ${JSON.stringify(err)}`);
done(err);
}
assert.isNotOk(err);
assert.isOk(result);
assert.equal(result.name, 'Two Million Blooms');
assert.proptery(result, 'about_us');
done();
});
})
});