Skip to content

Commit 26dfbe9

Browse files
author
Ace Nassri
authored
chore(functions/firebase): fix samples + tests not running (GoogleCloudPlatform#1967)
* Fix potential infinite loop in Firestore sample * Fix broken tests D: * Add kokoro file for Firebase * Fix nit
1 parent ec8a862 commit 26dfbe9

3 files changed

Lines changed: 59 additions & 21 deletions

File tree

.kokoro/functions/firebase.cfg

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Format: //devtools/kokoro/config/proto/build.proto
2+
3+
# Set the folder in which the tests are run
4+
env_vars: {
5+
key: "PROJECT"
6+
value: "functions/firebase"
7+
}
8+
9+
# Tell the trampoline which build file to use.
10+
env_vars: {
11+
key: "TRAMPOLINE_BUILD_FILE"
12+
value: "github/nodejs-docs-samples/.kokoro/build.sh"
13+
}

functions/firebase/index.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,16 +89,23 @@ const firestore = new Firestore({
8989

9090
// Converts strings added to /messages/{pushId}/original to uppercase
9191
exports.makeUpperCase = (event) => {
92-
const {resource} = event;
92+
const resource = event.value.name;
9393
const affectedDoc = firestore.doc(resource.split('/documents/')[1]);
9494

9595
const curValue = event.value.fields.original.stringValue;
9696
const newValue = curValue.toUpperCase();
97-
console.log(`Replacing value: ${curValue} --> ${newValue}`);
9897

99-
return affectedDoc.set({
100-
original: newValue,
101-
});
98+
if (curValue !== newValue) {
99+
console.log(`Replacing value: ${curValue} --> ${newValue}`);
100+
101+
return affectedDoc.set({
102+
original: newValue,
103+
});
104+
} else {
105+
// Value is already upper-case
106+
// Don't perform a(nother) write to avoid infinite loops
107+
console.log('Value is already upper-case.')
108+
}
102109
};
103110
// [END functions_firebase_reactive]
104111

functions/firebase/test/index.test.js

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -121,12 +121,10 @@ describe('functions_firebase_auth', () => {
121121
const date = Date.now();
122122
const event = {
123123
resource: 'resource',
124-
data: {
125-
uid: 'me',
126-
email: 'me@example.com',
127-
metadata: {
128-
createdAt: date,
129-
},
124+
uid: 'me',
125+
email: 'me@example.com',
126+
metadata: {
127+
createdAt: date,
130128
},
131129
};
132130

@@ -183,11 +181,9 @@ describe('functions_firebase_remote_config', () => {
183181
const sample = getSample();
184182

185183
const event = {
186-
data: {
187-
updateOrigin: 'CONSOLE',
188-
updateType: 'INCREMENTAL_UPDATE',
189-
versionNumber: '1',
190-
},
184+
updateOrigin: 'CONSOLE',
185+
updateType: 'INCREMENTAL_UPDATE',
186+
versionNumber: '1',
191187
};
192188

193189
sample.program.helloRemoteConfig(event);
@@ -206,6 +202,7 @@ describe('functions_firebase_reactive', () => {
206202
const sample = getSample();
207203

208204
const value = {
205+
name: 'foo/documents/bar',
209206
fields: {
210207
original: {
211208
stringValue: 'abc'
@@ -214,16 +211,37 @@ describe('functions_firebase_reactive', () => {
214211
};
215212

216213
const event = {
217-
resource: 'foo/documents/bar',
218214
eventType: 'type',
219-
data: {
220-
value: value,
221-
},
215+
value: value,
222216
};
223217

224-
sample.program.makeUpperCase(event, context);
218+
sample.program.makeUpperCase(event);
225219

226220
assert.strictEqual(console.log.calledWith('Replacing value: abc --> ABC'), true);
227221
assert.strictEqual(sample.mocks.firestore.doc.calledWith('bar'), true);
222+
assert.strictEqual(sample.mocks.firestore.set.callCount, 1);
223+
});
224+
225+
it('should do nothing if value is already capitalized', () => {
226+
const sample = getSample();
227+
228+
const value = {
229+
name: 'foo/documents/bar',
230+
fields: {
231+
original: {
232+
stringValue: 'ABC'
233+
}
234+
}
235+
};
236+
237+
const event = {
238+
eventType: 'type',
239+
value: value,
240+
};
241+
242+
sample.program.makeUpperCase(event);
243+
244+
assert.strictEqual(console.log.calledWith('Value is already upper-case.'), true);
245+
assert.strictEqual(sample.mocks.firestore.set.callCount, 0);
228246
});
229247
});

0 commit comments

Comments
 (0)