Skip to content

Commit 03c06cb

Browse files
committed
Updated functions/background.
1 parent fefe5fd commit 03c06cb

File tree

4 files changed

+93
-87
lines changed

4 files changed

+93
-87
lines changed

functions/background/index.js

+16-14
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1-
// Copyright 2016, Google, Inc.
2-
// Licensed under the Apache License, Version 2.0 (the "License");
3-
// you may not use this file except in compliance with the License.
4-
// You may obtain a copy of the License at
5-
//
6-
// http://www.apache.org/licenses/LICENSE-2.0
7-
//
8-
// Unless required by applicable law or agreed to in writing, software
9-
// distributed under the License is distributed on an "AS IS" BASIS,
10-
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11-
// See the License for the specific language governing permissions and
12-
// limitations under the License.
1+
/**
2+
* Copyright 2016, Google, Inc.
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
1315

1416
'use strict';
1517

@@ -34,8 +36,6 @@ exports.helloWorld = function helloWorld (event, callback) {
3436
// [END helloworld]
3537

3638
// [START helloPromise]
37-
var request = require('request-promise');
38-
3939
/**
4040
* Background Cloud Function that returns a Promise. Note that we don't pass
4141
* a "context" argument to the function.
@@ -45,6 +45,8 @@ var request = require('request-promise');
4545
* @returns {Promise}
4646
*/
4747
exports.helloPromise = function helloPromise (event) {
48+
const request = require('request-promise');
49+
4850
return request({
4951
uri: event.payload.endpoint
5052
});

functions/background/package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@
99
"test": "mocha -R spec -t 120000 --require intelli-espower-loader ../../test/_setup.js test/*.test.js"
1010
},
1111
"dependencies": {
12-
"request-promise": "^3.0.0"
12+
"request": "^2.75.0",
13+
"request-promise": "^4.1.1"
1314
},
1415
"devDependencies": {
15-
"mocha": "^2.5.3"
16+
"mocha": "^3.1.2"
1617
}
1718
}
+68-65
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,27 @@
1-
// Copyright 2016, Google, Inc.
2-
// Licensed under the Apache License, Version 2.0 (the "License");
3-
// you may not use this file except in compliance with the License.
4-
// You may obtain a copy of the License at
5-
//
6-
// http://www.apache.org/licenses/LICENSE-2.0
7-
//
8-
// Unless required by applicable law or agreed to in writing, software
9-
// distributed under the License is distributed on an "AS IS" BASIS,
10-
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11-
// See the License for the specific language governing permissions and
12-
// limitations under the License.
1+
/**
2+
* Copyright 2016, Google, Inc.
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
1315

1416
'use strict';
1517

16-
var proxyquire = require('proxyquire').noCallThru();
18+
const proxyquire = require(`proxyquire`).noCallThru();
1719

1820
function getSample () {
19-
var requestPromise = sinon.stub().returns(new Promise(function (resolve) {
20-
resolve('test');
21-
}));
21+
const requestPromise = sinon.stub().returns(Promise.resolve(`test`));
22+
2223
return {
23-
sample: proxyquire('../', {
24+
program: proxyquire(`../`, {
2425
'request-promise': requestPromise
2526
}),
2627
mocks: {
@@ -29,62 +30,64 @@ function getSample () {
2930
};
3031
}
3132

32-
function getMockContext () {
33-
return {
34-
success: sinon.stub(),
35-
failure: sinon.stub()
36-
};
37-
}
33+
describe(`functions:background`, () => {
34+
it(`should echo message`, () => {
35+
const event = {
36+
payload: {
37+
myMessage: `hi`
38+
}
39+
};
40+
const sample = getSample();
41+
const callback = sinon.stub();
3842

39-
describe('functions:background', function () {
40-
it('should echo message', function () {
41-
var expectedMsg = 'hi';
42-
var context = getMockContext();
43-
var backgroundSample = getSample();
44-
backgroundSample.sample.helloWorld(context, {
45-
message: expectedMsg
46-
});
43+
sample.program.helloWorld(event, callback);
4744

48-
assert(context.success.calledOnce);
49-
assert.equal(context.failure.called, false);
50-
assert(console.log.calledWith(expectedMsg));
45+
assert.equal(console.log.callCount, 1);
46+
assert.deepEqual(console.log.firstCall.args, [event.payload.myMessage]);
47+
assert.equal(callback.callCount, 1);
48+
assert.deepEqual(callback.firstCall.args, []);
5149
});
52-
it('should say no message was provided', function () {
53-
var expectedMsg = 'No message defined!';
54-
var context = getMockContext();
55-
var backgroundSample = getSample();
56-
backgroundSample.sample.helloWorld(context, {});
5750

58-
assert(context.failure.calledOnce);
59-
assert(context.failure.firstCall.args[0] === expectedMsg);
60-
assert.equal(context.success.called, false);
51+
it(`should say no message was provided`, () => {
52+
const error = new Error(`No message defined!`);
53+
const callback = sinon.stub();
54+
const sample = getSample();
55+
sample.program.helloWorld({ payload: {} }, callback);
56+
57+
assert.equal(callback.callCount, 1);
58+
assert.deepEqual(callback.firstCall.args, [error]);
6159
});
62-
it('should make a promise request', function (done) {
63-
var backgroundSample = getSample();
64-
backgroundSample.sample.helloPromise({
65-
endpoint: 'foo.com'
66-
}).then(function (result) {
67-
assert.deepEqual(backgroundSample.mocks.requestPromise.firstCall.args[0], {
68-
uri: 'foo.com'
60+
61+
it(`should make a promise request`, () => {
62+
const sample = getSample();
63+
const event = {
64+
payload: {
65+
endpoint: `foo.com`
66+
}
67+
};
68+
69+
return sample.program.helloPromise(event)
70+
.then((result) => {
71+
assert.deepEqual(sample.mocks.requestPromise.firstCall.args, [{ uri: `foo.com` }]);
72+
assert.equal(result, `test`);
6973
});
70-
assert.equal(result, 'test');
71-
done();
72-
}, function () {
73-
assert.fail();
74-
});
7574
});
76-
it('should return synchronously', function () {
77-
var backgroundSample = getSample();
78-
assert(backgroundSample.sample.helloSynchronous({
79-
something: true
80-
}) === 'Something is true!');
75+
76+
it(`should return synchronously`, () => {
77+
assert.equal(getSample().program.helloSynchronous({
78+
payload: {
79+
something: true
80+
}
81+
}), `Something is true!`);
8182
});
82-
it('should throw an error', function () {
83-
var backgroundSample = getSample();
84-
assert.throws(function () {
85-
backgroundSample.sample.helloSynchronous({
86-
something: false
83+
84+
it(`should throw an error`, () => {
85+
assert.throws(() => {
86+
getSample().program.helloSynchronous({
87+
payload: {
88+
something: false
89+
}
8790
});
88-
}, Error, 'Something was not true!');
91+
}, Error, `Something was not true!`);
8992
});
9093
});

functions/pubsub/test/index.test.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ function getSample () {
5151
};
5252
}
5353

54-
describe('functions:pubsub', () => {
55-
it('Publish fails without a topic', () => {
54+
describe(`functions:pubsub`, () => {
55+
it(`Publish fails without a topic`, () => {
5656
const expectedMsg = `Topic not provided. Make sure you have a "topic" property in your request`;
5757
const sample = getSample();
5858

@@ -65,7 +65,7 @@ describe('functions:pubsub', () => {
6565
assert.deepEqual(sample.mocks.res.send.firstCall.args[0].message, expectedMsg);
6666
});
6767

68-
it('Publish fails without a message', () => {
68+
it(`Publish fails without a message`, () => {
6969
const expectedMsg = `Message not provided. Make sure you have a "message" property in your request`;
7070
const sample = getSample();
7171

@@ -78,8 +78,8 @@ describe('functions:pubsub', () => {
7878
assert.deepEqual(sample.mocks.res.send.firstCall.args[0].message, expectedMsg);
7979
});
8080

81-
it('Publishes the message to the topic and calls success', () => {
82-
const expectedMsg = 'Message published.';
81+
it(`Publishes the message to the topic and calls success`, () => {
82+
const expectedMsg = `Message published.`;
8383
const sample = getSample();
8484

8585
return sample.program.publish(sample.mocks.req, sample.mocks.res)
@@ -97,7 +97,7 @@ describe('functions:pubsub', () => {
9797
});
9898
});
9999

100-
it('Fails to publish the message and calls failure', () => {
100+
it(`Fails to publish the message and calls failure`, () => {
101101
const error = new Error(`error`);
102102
const sample = getSample();
103103
sample.mocks.topic.publish.returns(Promise.reject(error));

0 commit comments

Comments
 (0)