mockbase
Version:
Firebase v7+ mock.
62 lines • 2.82 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.MockTransaction = void 0;
class MockTransaction {
constructor(firestore) {
this.firestore = firestore;
this.operations = [];
this.affectedDocs = new Set();
this.surrogateFirestore = firestore.clone();
}
get(documentRef) {
return this.surrogateFirestore.doc(documentRef.path).withConverter(documentRef.converter).get();
}
set(documentRef, data, options) {
const doc = this.surrogateFirestore.doc(documentRef.path);
this.operations.push(doc.set(data, options));
this.affectedDocs.add(doc.path);
return this;
}
update(documentRef, data, value, ...rest) {
const doc = this.surrogateFirestore.doc(documentRef.path);
this.operations.push(doc.update(data, value, ...rest));
this.affectedDocs.add(doc.path);
return this;
}
delete(documentRef) {
const doc = this.surrogateFirestore.doc(documentRef.path);
this.operations.push(doc.delete());
this.affectedDocs.add(doc.path);
return this;
}
commit() {
return __awaiter(this, void 0, void 0, function* () {
yield Promise.all(this.operations);
const changedDocs = new Set();
for (const path of this.affectedDocs) {
const beforeWrite = yield this.firestore.doc(path).get();
this.firestore.writeDocument(this.surrogateFirestore.doc(path), this.surrogateFirestore.documentData.get(path));
const afterWrite = yield this.firestore.doc(path).get();
// If state before and after changed, then it's a path which should have events emitted
// after all operations are completed
if (!beforeWrite.isEqual(afterWrite)) {
changedDocs.add(path);
}
}
for (const path of changedDocs) {
this.firestore.doc(path).emitChange();
}
});
}
}
exports.MockTransaction = MockTransaction;
//# sourceMappingURL=transaction.js.map