UNPKG

mockbase

Version:
124 lines 4.95 kB
"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.MockFirestore = void 0; const util_1 = require("../util"); const collection_reference_1 = require("./collection-reference"); const data_converter_1 = require("./data-converter"); const document_reference_1 = require("./document-reference"); const collection_group_1 = require("./collection-group"); const write_batch_1 = require("./write-batch"); const transaction_1 = require("./transaction"); const NEW_COLLECTION_EVENT = "collection:new"; class MockFirestore { constructor(app, initialData = new Map()) { this.app = app; this.state = "not-started"; this.id = 0; this.documentEvents = new Map(); this.collectionDocuments = new Map(); this.collectionEvents = new Map(); this.emitter = new util_1.EventEmitter(); this.documentData = new Map(initialData); } setStateRunning() { if (this.state === "terminated") { throw new Error("Firestore is terminated already"); } this.state = "running"; } nextId() { return "__id" + this.id++; } writeDocument(doc, data) { var _a; const { path, parent } = doc; if (data === undefined) { this.documentData.delete(path); (_a = this.collectionDocuments.get(parent.path)) === null || _a === void 0 ? void 0 : _a.delete(path); } else { this.documentData.set(path, data); let collectionDocs = this.collectionDocuments.get(parent.path); if (!collectionDocs) { collectionDocs = new Set(); this.collectionDocuments.set(parent.path, collectionDocs); this.emitter.emit(NEW_COLLECTION_EVENT, [parent.path]); } collectionDocs.add(path); } } onNewCollection(listener) { this.emitter.on(NEW_COLLECTION_EVENT, listener); } clone() { return new MockFirestore(this.app, this.documentData); } batch() { return new write_batch_1.MockWriteBatch(this); } clearPersistence() { return this.state !== "running" ? Promise.resolve() : Promise.reject(new Error("precondition-failed")); } enablePersistence(settings) { return this.state === "not-started" ? Promise.resolve() : Promise.reject(new Error("precondition-failed")); } enableNetwork() { return Promise.resolve(); } disableNetwork() { return Promise.resolve(); } collection(collectionPath) { this.setStateRunning(); const parts = collectionPath.replace(/^\//, "").split("/"); if (parts.length % 2 === 0) { throw new Error("Not a collection path: " + collectionPath); } return new collection_reference_1.MockCollectionReference(this, parts.pop(), parts.length ? this.doc(parts.join("/")) : null, data_converter_1.DEFAULT_DATA_CONVERTER); } collectionGroup(collectionId) { return new collection_group_1.MockCollectionGroup(this, collectionId, data_converter_1.DEFAULT_DATA_CONVERTER); } doc(documentPath) { this.setStateRunning(); const parts = documentPath.replace(/^\//, "").split("/"); if (parts.length % 2 !== 0) { throw new Error("Not a document path: " + documentPath); } return new document_reference_1.MockDocumentReference(this, parts.pop(), this.collection(parts.join("/")), data_converter_1.DEFAULT_DATA_CONVERTER); } runTransaction(updateFunction) { return __awaiter(this, void 0, void 0, function* () { const transaction = new transaction_1.MockTransaction(this); const result = yield updateFunction(transaction); yield transaction.commit(); return result; }); } onSnapshotsInSync(onSync) { throw new Error("Method not implemented."); } settings(settings) { } terminate() { this.state = "terminated"; return Promise.resolve(); } waitForPendingWrites() { return Promise.resolve(); } } exports.MockFirestore = MockFirestore; //# sourceMappingURL=firestore.js.map