mockbase
Version:
Firebase v7+ mock.
80 lines • 2.77 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.MockQueryDocumentSnapshot = exports.MockDocumentSnapshot = void 0;
const data_converter_1 = require("./data-converter");
class MockDocumentSnapshot {
constructor(ref, _data) {
this.ref = ref;
this._data = _data;
}
get metadata() {
return {
fromCache: true,
hasPendingWrites: false,
isEqual: () => true,
};
}
get id() {
return this.ref.id;
}
get exists() {
return this._data !== undefined;
}
data(options) {
if (this._data === undefined) {
return undefined;
}
const querySnapshot = new MockQueryDocumentSnapshot(this.ref, this._data);
return querySnapshot.data(options);
}
get(fieldPath, options) {
return fieldPath
.toString()
.split(".")
.reduce((obj, path) => (obj !== undefined ? obj[path] : obj), this._data);
}
isEqual(other) {
return (other instanceof MockDocumentSnapshot &&
other.ref.isEqual(this.ref) &&
MockDocumentSnapshot.isDeepEqual(this._data, other._data));
}
static isDeepEqual(a, b) {
if (a === b) {
// Data is exactly the same
return true;
}
else if (a == null || b == null || typeof a !== "object" || typeof b !== "object") {
// Either is undefined
return false;
}
const aKeys = Object.keys(a);
const bKeys = Object.keys(b);
if (aKeys.length !== bKeys.length) {
// The key set is different
return false;
}
for (const key of aKeys) {
if (!bKeys.includes(key) || !MockDocumentSnapshot.isDeepEqual(a[key], b[key])) {
// Either key doesn't exist or the value is deeply not the same
return false;
}
}
return true;
}
}
exports.MockDocumentSnapshot = MockDocumentSnapshot;
class MockQueryDocumentSnapshot extends MockDocumentSnapshot {
constructor(ref, _data) {
super(ref, _data);
this._data = _data;
}
data(options) {
// Create a new ref with the default converter so that consumeres can use .data() in their
// custom converters without causing an infinite loop of .data() calls.
const plainRef = this.ref.withConverter(data_converter_1.DEFAULT_DATA_CONVERTER);
const plainSnapshot = new MockQueryDocumentSnapshot(plainRef, this._data);
return this.ref.converter.fromFirestore(plainSnapshot, options || {});
}
}
exports.MockQueryDocumentSnapshot = MockQueryDocumentSnapshot;
//# sourceMappingURL=document-snapshot.js.map