Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 4 additions & 15 deletions functions/firebase/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,14 @@
* Triggered by a change to a Firebase RTDB reference.
*
* @param {!Object} event The Cloud Functions event.
* @param {!Function} The callback function.
*/
exports.helloRTDB = (event, callback) => {
exports.helloRTDB = (event) => {
const triggerResource = event.resource;

console.log(`Function triggered by change to: ${triggerResource}`);
console.log(`Admin?: ${!!event.auth.admin}`);
console.log(`Delta:`);
console.log(JSON.stringify(event.delta, null, 2));

// Don't forget to call the callback.
callback();
};
// [END functions_firebase_rtdb]

Expand All @@ -38,9 +34,8 @@ exports.helloRTDB = (event, callback) => {
* Triggered by a change to a Firestore document.
*
* @param {!Object} event The Cloud Functions event.
* @param {!Function} The callback function.
*/
exports.helloFirestore = (event, callback) => {
exports.helloFirestore = (event) => {
const triggerResource = event.resource;

console.log(`Function triggered by event on: ${triggerResource}`);
Expand All @@ -55,9 +50,6 @@ exports.helloFirestore = (event, callback) => {
console.log(`\nNew value:`);
console.log(JSON.stringify(event.data.value, null, 2));
}

// Don't forget to call the callback.
callback();
};
// [END functions_firebase_firestore]

Expand All @@ -66,9 +58,8 @@ exports.helloFirestore = (event, callback) => {
* Triggered by a change to a Firebase Auth user object.
*
* @param {!Object} event The Cloud Functions event.
* @param {!Function} The callback function.
*/
exports.helloAuth = (event, callback) => {
exports.helloAuth = (event) => {
try {
const data = event.data;
console.log(`Function triggered by change to user: ${data.uid}`);
Expand All @@ -80,8 +71,6 @@ exports.helloAuth = (event, callback) => {
} catch (err) {
console.error(err);
}
// Don't forget to call the callback.
callback();
};
// [END functions_firebase_auth]

Expand All @@ -93,7 +82,7 @@ const firestore = new Firestore({
});

// Converts strings added to /messages/{pushId}/original to uppercase
exports.makeUpperCase = (event, callback) => {
exports.makeUpperCase = (event) => {
const resource = event.resource;
const affectedDoc = firestore.doc(resource.split('/documents/')[1]);

Expand Down
15 changes: 4 additions & 11 deletions functions/firebase/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,12 @@ test(`should listen to RTDB`, t => {
},
delta: delta
};
const cb = sinon.stub();

sample.program.helloRTDB(event, cb);
sample.program.helloRTDB(event);

t.true(console.log.calledWith(`Function triggered by change to: resource`));
t.true(console.log.calledWith(`Admin?: true`));
t.true(console.log.calledWith(JSON.stringify(delta, null, 2)));
t.true(cb.calledOnce);
});

test(`should listen to Firestore`, t => {
Expand All @@ -79,15 +77,13 @@ test(`should listen to Firestore`, t => {
value: value
}
};
const cb = sinon.stub();

sample.program.helloFirestore(event, cb);
sample.program.helloFirestore(event);

t.true(console.log.calledWith(`Function triggered by event on: resource`));
t.true(console.log.calledWith(`Event type: type`));
t.true(console.log.calledWith(JSON.stringify(oldValue, null, 2)));
t.true(console.log.calledWith(JSON.stringify(value, null, 2)));
t.true(cb.calledOnce);
});

test(`should listen to Auth events`, t => {
Expand All @@ -103,14 +99,12 @@ test(`should listen to Auth events`, t => {
}
}
};
const cb = sinon.stub();

sample.program.helloAuth(event, cb);
sample.program.helloAuth(event);

t.true(console.log.calledWith(`Function triggered by change to user: me`));
t.true(console.log.calledWith(`Created at: ${date}`));
t.true(console.log.calledWith(`Email: [email protected]`));
t.true(cb.calledOnce);
});

test(`should update data in response to Firestore events`, t => {
Expand All @@ -133,9 +127,8 @@ test(`should update data in response to Firestore events`, t => {
}
}
};
const cb = sinon.stub();

sample.program.makeUpperCase(event, cb);
sample.program.makeUpperCase(event);

t.true(sample.mocks.firestore.doc.calledWith('some/path'));
t.true(console.log.calledWith(`Replacing value: foobar --> FOOBAR`));
Expand Down