forked from invertase/react-native-firebase
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers.js
More file actions
186 lines (163 loc) · 7.54 KB
/
Copy pathhelpers.js
File metadata and controls
186 lines (163 loc) · 7.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/* eslint-disable no-console */
const { getE2eTestProject, getE2eEmulatorHost } = require('../../app/e2e/helpers');
/*
* Copyright (c) 2016-present Invertase Limited & Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this library except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
exports.wipe = async function wipe(debug = false, databaseId = '(default)', retries = 3) {
const host = getE2eEmulatorHost();
const url =
`http://${host}:8080/emulator/v1/projects/` +
getE2eTestProject() +
`/databases/${databaseId}/documents`;
let lastError;
for (let attempt = 1; attempt <= retries; attempt++) {
try {
if (debug) {
console.time('wipe');
}
const response = await fetch(url, {
method: 'DELETE',
headers: {
// Undocumented, but necessary - from Emulator UI network requests
Authorization: 'Bearer owner',
},
});
if (!response.ok) {
const body = await response.text();
throw new Error(`Firestore wipe failed: HTTP ${response.status} ${body.slice(0, 200)}`);
}
if (debug) {
console.timeEnd('wipe');
}
return;
} catch (e) {
lastError = e;
const message = e?.message || String(e);
console.error(`Unable to wipe firestore (attempt ${attempt}/${retries}): ${message}`);
if (attempt < retries) {
await new Promise(resolve => setTimeout(resolve, 250 * attempt));
}
}
}
throw lastError;
};
function toFirestoreValue(value) {
if (value === null) {
return { nullValue: null };
}
if (Array.isArray(value)) {
return { arrayValue: { values: value.map(toFirestoreValue) } };
}
if (typeof value === 'boolean') {
return { booleanValue: value };
}
if (typeof value === 'number') {
if (Number.isInteger(value)) {
return { integerValue: String(value) };
}
return { doubleValue: value };
}
if (typeof value === 'string') {
return { stringValue: value };
}
if (typeof value === 'object') {
const fields = {};
for (const [key, nestedValue] of Object.entries(value)) {
fields[key] = toFirestoreValue(nestedValue);
}
return { mapValue: { fields } };
}
throw new Error(`Unsupported Firestore REST value type: ${typeof value}`);
}
exports.setDocumentOutOfBand = async function setDocumentOutOfBand(
path,
data,
databaseId = '(default)',
) {
const url =
`http://${getE2eEmulatorHost()}:8080/v1/projects/` +
getE2eTestProject() +
`/databases/${databaseId}/documents/${path}`;
const fields = {};
for (const [key, value] of Object.entries(data)) {
fields[key] = toFirestoreValue(value);
}
const response = await fetch(url, {
method: 'PATCH',
headers: {
Authorization: 'Bearer owner',
'Content-Type': 'application/json',
},
body: JSON.stringify({ fields }),
});
if (!response.ok) {
const body = await response.text();
throw new Error(`Unable to set out-of-band firestore document: ${response.status} ${body}`);
}
return response.json();
};
exports.BUNDLE_QUERY_NAME = 'named-bundle-test';
exports.BUNDLE_COLLECTION = 'firestore-bundle-tests';
exports.getBundle = function getBundle() {
// Original source: http://api.rnfirebase.io/firestore/bundle
const content = `
151{"metadata":{"id":"firestore-bundle-tests","createTime":{"seconds":"1621252690","n
anos":143267000},"version":1,"totalDocuments":6,"totalBytes":"3500"}}265{"namedQuery"
:{"name":"named-bundle-test","bundledQuery":{"parent":"projects/react-native-firebase
-testing/databases/(default)/documents","structuredQuery":{"from":[{"collectionId":"f
irestore-bundle-tests"}]}},"readTime":{"seconds":"1621252690","nanos":143267000}}}244
{"documentMetadata":{"name":"projects/react-native-firebase-testing/databases/(defaul
t)/documents/firestore-bundle-tests/8I4LbBaQBgOQpbiw3BA6","readTime":{"seconds":"16
21252690","nanos":143267000},"exists":true,"queries":["named-bundle-test"]}}287{"do
cument":{"name":"projects/react-native-firebase-testing/databases/(default)/documen
ts/firestore-bundle-tests/8I4LbBaQBgOQpbiw3BA6","fields":{"number":{"integerValue":
"1"}},"createTime":{"seconds":"1621252690","nanos":90568000},"updateTime":{"seconds
":"1621252690","nanos":90568000}}}244{"documentMetadata":{"name":"projects/react-na
tive-firebase-testing/databases/(default)/documents/firestore-bundle-tests/IGQgUS2i
OfxbiEjPwU79","readTime":{"seconds":"1621252690","nanos":143267000},"exists":true,"
queries":["named-bundle-test"]}}289{"document":{"name":"projects/react-native-fireb
ase-testing/databases/(default)/documents/firestore-bundle-tests/IGQgUS2iOfxbiEjPwU
79","fields":{"number":{"integerValue":"2"}},"createTime":{"seconds":"1621252690","
nanos":103781000},"updateTime":{"seconds":"1621252690","nanos":103781000}}}244{"doc
umentMetadata":{"name":"projects/react-native-firebase-testing/databases/(default)/
documents/firestore-bundle-tests/IfIlAnixFCuxKJfjgU8R","readTime":{"seconds":"16212
52690","nanos":143267000},"exists":true,"queries":["named-bundle-test"]}}289{"docum
ent":{"name":"projects/react-native-firebase-testing/databases/(default)/documents/
firestore-bundle-tests/IfIlAnixFCuxKJfjgU8R","fields":{"number":{"integerValue":"3"
}},"createTime":{"seconds":"1621252690","nanos":107789000},"updateTime":{"seconds":"1
621252690","nanos":107789000}}}244{"documentMetadata":{"name":"projects/react-native-
firebase-testing/databases/(default)/documents/firestore-bundle-tests/2QXGhtR75xCpN3k
p1Uup","readTime":{"seconds":"1621252690","nanos":143267000},"exists":true,"queries":
["named-bundle-test"]}}289{"document":{"name":"projects/react-native-firebase-testing
/databases/(default)/documents/firestore-bundle-tests/2QXGhtR75xCpN3kp1Uup","fields":
{"number":{"integerValue":"2"}},"createTime":{"seconds":"1621250323","nanos":16629300
0},"updateTime":{"seconds":"1621250323","nanos":166293000}}}244{"documentMetadata":{"
name":"projects/react-native-firebase-testing/databases/(default)/documents/firestore
-bundle-tests/gwuvIm5uXGRk1jZGdsvP","readTime":{"seconds":"1621252690","nanos":143267
000},"exists":true,"queries":["named-bundle-test"]}}289{"document":{"name":"projects/
react-native-firebase-testing/databases/(default)/documents/firestore-bundle-tests/gw
uvIm5uXGRk1jZGdsvP","fields":{"number":{"integerValue":"1"}},"createTime":{"seconds":
"1621250323","nanos":125981000},"updateTime":{"seconds":"1621250323","nanos":12598100
0}}}244{"documentMetadata":{"name":"projects/react-native-firebase-testing/databases/
(default)/documents/firestore-bundle-tests/yDtWms0n3845bs5CAyEu","readTime":{"seconds
":"1621252690","nanos":143267000},"exists":true,"queries":["named-bundle-test"]}}289{
"document":{"name":"projects/react-native-firebase-testing/databases/(default)/docu
ments/firestore-bundle-tests/yDtWms0n3845bs5CAyEu","fields":{"number":{"integerValu
e":"3"}},"createTime":{"seconds":"1621250323","nanos":170451000},"updateTime":{"sec
onds":"1621250323","nanos":170451000}}}
`;
return content.replace(/(\r\n|\n|\r|\s)/gm, '');
};