Skip to content

Commit

Permalink
Add Node 8 Firestore reactive sample (#798)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ace Nassri committed Nov 5, 2018
1 parent a0c11fb commit 6b3d1db
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 5 deletions.
22 changes: 22 additions & 0 deletions functions/node8/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,3 +207,25 @@ exports.helloAnalytics = (data, context) => {
console.log(`Location: ${userObj.geoInfo.city}, ${userObj.geoInfo.country}`);
};
// [END functions_firebase_analytics_node8]

// [START functions_firebase_reactive_node8]
const Firestore = require('@google-cloud/firestore');

const firestore = new Firestore({
projectId: process.env.GCP_PROJECT
});

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

const curValue = data.value.fields.original.stringValue;
const newValue = curValue.toUpperCase();
console.log(`Replacing value: ${curValue} --> ${newValue}`);

return affectedDoc.set({
'original': newValue
});
};
// [END functions_firebase_reactive_node8]
5 changes: 5 additions & 0 deletions functions/node8/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@
"devDependencies": {
"@google-cloud/nodejs-repo-tools": "^2.3.3",
"ava": "^0.25.0",
"proxyquire": "^2.1.0",
"semistandard": "^12.0.1",
"sinon": "^7.0.0",
"uuid": "^3.3.2"
},
"dependencies": {
"@google-cloud/firestore": "^0.18.0"
}
}
64 changes: 59 additions & 5 deletions functions/node8/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,34 @@

'use strict';

const sinon = require('sinon');
const uuid = require('uuid');
const test = require('ava');
const utils = require('@google-cloud/nodejs-repo-tools');
const proxyquire = require(`proxyquire`).noCallThru();

const program = require('../');
function getSample () {
const firestoreMock = {
doc: sinon.stub().returnsThis(),
set: sinon.stub()
};

return {
program: proxyquire(`../`, {
'@google-cloud/firestore': sinon.stub().returns(firestoreMock)
}),
mocks: {
firestore: firestoreMock
}
};
}

test.beforeEach(utils.stubConsole);
test.afterEach.always(utils.restoreConsole);

test.serial('should monitor Firebase RTDB', t => {
const sample = getSample();

const dataId = uuid.v4();
const resourceId = uuid.v4();

Expand All @@ -38,14 +56,16 @@ test.serial('should monitor Firebase RTDB', t => {
resource: resourceId
};

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

t.true(console.log.firstCall.args[0].includes(resourceId));
t.deepEqual(console.log.secondCall.args, ['Admin?: true']);
t.true(console.log.getCall(3).args[0].includes(dataId));
});

test.serial('should monitor Firestore', t => {
const sample = getSample();

const resourceId = uuid.v4();

const context = {
Expand All @@ -56,14 +76,16 @@ test.serial('should monitor Firestore', t => {
value: { uuid: uuid.v4() }
};

program.helloFirestore(data, context);
sample.program.helloFirestore(data, context);

t.true(console.log.firstCall.args[0].includes(resourceId));
t.true(console.log.calledWith(JSON.stringify(data.oldValue, null, 2)));
t.true(console.log.calledWith(JSON.stringify(data.value, null, 2)));
});

test.serial('should monitor Auth', t => {
const sample = getSample();

const userId = uuid.v4();
const dateString = (new Date()).toISOString();
const emailString = `${uuid.v4()}@${uuid.v4()}.com`;
Expand All @@ -76,14 +98,16 @@ test.serial('should monitor Auth', t => {
email: emailString
};

program.helloAuth(data, null);
sample.program.helloAuth(data, null);

t.true(console.log.firstCall.args[0].includes(userId));
t.true(console.log.secondCall.args[0].includes(dateString));
t.true(console.log.thirdCall.args[0].includes(emailString));
});

test.serial('should monitor Analytics', t => {
const sample = getSample();

const date = new Date();
const data = {
eventDim: [{
Expand All @@ -105,10 +129,40 @@ test.serial('should monitor Analytics', t => {
resource: 'my-resource'
};

program.helloAnalytics(data, context);
sample.program.helloAnalytics(data, context);

t.is(console.log.args[0][0], `Function triggered by the following event: my-resource`);
t.is(console.log.args[1][0], `Name: my-event`);
t.is(console.log.args[2][0], `Timestamp: ${date}`);
t.is(console.log.args[3][0], `Device Model: Pixel`);
t.is(console.log.args[4][0], `Location: London, UK`);
});

test(`should update data in response to Firestore events`, t => {
const sample = getSample();