Skip to content

Commit e4e707e

Browse files
author
Ace Nassri
authored
Firebase: print params (GoogleCloudPlatform#1368)
* Use mocha + add params console.log statement * Update index.js
1 parent 3b1b36e commit e4e707e

5 files changed

Lines changed: 29 additions & 5 deletions

File tree

functions/firebase/index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@
2222
exports.helloRTDB = event => {
2323
const triggerResource = event.resource;
2424

25+
const pathParams = event.params;
26+
if (pathParams) {
27+
console.log(`Path parameters:`);
28+
Object.keys(pathParams).forEach(key => {
29+
console.log(` ${key}: ${pathParams[key]}`);
30+
});
31+
}
32+
2533
console.log(`Function triggered by change to: ${triggerResource}`);
2634
console.log(`Admin?: ${!!event.auth.admin}`);
2735
console.log(`Delta:`);

functions/firebase/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@
1212
"node": ">=8.0.0"
1313
},
1414
"scripts": {
15-
"test": "ava -T 30s test/*.test.js"
15+
"test": "mocha -T 30s test/*.test.js"
1616
},
1717
"devDependencies": {
1818
"@google-cloud/nodejs-repo-tools": "^3.3.0",
19-
"ava": "^2.0.0",
19+
"mocha": "^6.1.4",
2020
"proxyquire": "^2.1.0",
2121
"sinon": "^7.2.7",
2222
"supertest": "^4.0.0",

functions/firebase/test/index.test.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,14 @@ it('should listen to RTDB', () => {
5151
admin: true,
5252
},
5353
delta: delta,
54+
params: {
55+
baz: 'quux',
56+
},
5457
};
5558

5659
sample.program.helloRTDB(event);
5760

61+
assert.strictEqual(console.log.calledWith(' baz: quux'), true);
5862
assert.strictEqual(
5963
console.log.calledWith('Function triggered by change to: resource'),
6064
true

functions/node8/index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,14 @@ exports.helloGCSGeneric = (data, context) => {
161161
exports.helloRTDB = (data, context) => {
162162
const triggerResource = context.resource;
163163

164+
const pathParams = data.params;
165+
if (pathParams) {
166+
console.log(`Parameters:`);
167+
Object.entries(pathParams).forEach(([param, value]) => {
168+
console.log(` ${param}: ${value}`);
169+
});
170+
}
171+
164172
console.log(`Function triggered by change to: ${triggerResource}`);
165173
console.log(`Admin?: ${!!data.admin}`);
166174
console.log(`Delta:`);

functions/node8/test/index.test.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,16 +110,20 @@ it('should monitor Firebase RTDB', () => {
110110
delta: {
111111
id: dataId,
112112
},
113+
params: {
114+
baz: 'quux',
115+
},
113116
};
114117
const context = {
115118
resource: resourceId,
116119
};
117120

118121
sample.program.helloRTDB(data, context);
119122

120-
assert.strictEqual(console.log.firstCall.args[0].includes(resourceId), true);
121-
assert.deepStrictEqual(console.log.secondCall.args, ['Admin?: true']);
122-
assert.strictEqual(console.log.getCall(3).args[0].includes(dataId), true);
123+
assert.deepStrictEqual(console.log.getCall(1).args, [' baz: quux']);
124+
assert.strictEqual(console.log.getCall(2).args[0].includes(resourceId), true);
125+
assert.deepStrictEqual(console.log.getCall(3).args, ['Admin?: true']);
126+
assert.strictEqual(console.log.getCall(5).args[0].includes(dataId), true);
123127
});
124128

125129
it('should monitor Firestore', () => {

0 commit comments

Comments
 (0)