Skip to content

Commit

Permalink
Remove NO_DATA fallback and extend tests around ready
Browse files Browse the repository at this point in the history
  • Loading branch information
0x80 committed Jun 1, 2022
1 parent 649cc9a commit 015da36
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 9 deletions.
49 changes: 47 additions & 2 deletions src/__test/document.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { autorun, configure, toJS } from "mobx";
import { autorun, configure } from "mobx";
import { ObservableDocument } from "../document";
import { first } from "../utils";
import { first, last } from "../utils";
import {
clearDataset,
collectionData,
Expand Down Expand Up @@ -154,6 +154,51 @@ describe("Document", () => {
disposeListeners();
});

it("Should resolve ready after changing documents", async () => {
const snapshot = await db
.collection(collectionName)
.orderBy("count", "asc")
.get();

const document = new ObservableDocument<TestDocumentA>(
db.collection(collectionName),
{ debug: false },
);

expect(document.isLoading).toBe(false);
expect(document.hasData).toBe(false);

/**
* Set up listeners because it changes the behavior for ready()
*/
const disposeListeners = autorun(() => {
console.log("isLoading", document.isLoading);
});

{
document.attachTo(first(snapshot.docs)?.id);

expect(document.isLoading).toBe(true);
expect(document.hasData).toBe(false);

const data = await document.ready();

expect(data).toEqual(first(collectionData));
}
{
document.attachTo(last(snapshot.docs)?.id);

expect(document.isLoading).toBe(true);
expect(document.hasData).toBe(false);

const data = await document.ready();

expect(data).toEqual(last(collectionData));
}

disposeListeners();
});

it("Passes undefined on ready when not found", async () => {
const document = new ObservableDocument<TestDocumentA>(
db.collection(collectionName),
Expand Down
18 changes: 11 additions & 7 deletions src/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ type SourceType =
| FirebaseFirestore.CollectionReference;

export class ObservableDocument<T> {
_data: T | typeof NO_DATA = NO_DATA;
_data?: T;
isLoading = false;

private debugId = createUniqueId();
Expand Down Expand Up @@ -129,14 +129,14 @@ export class ObservableDocument<T> {
}

get data(): T /* | undefined */ {
assert(this.documentRef && this._data !== NO_DATA, "No data available");
assert(this.documentRef && this._data, "No data available");
// if (!this.documentRef || this._data === NO_DATA) return;

return toJS(this._data);
}

get document(): Document<T> {
assert(this.documentRef && this._data !== NO_DATA, "No document available");
assert(this.documentRef && this._data, "No document available");

/**
* For document we return the data as non-observable by converting it to a
Expand Down Expand Up @@ -208,7 +208,7 @@ export class ObservableDocument<T> {
}

get hasData(): boolean {
return this._data !== NO_DATA;
return typeof this._data !== "undefined";
}

private changeReady(isReady: boolean) {
Expand Down Expand Up @@ -254,6 +254,7 @@ export class ObservableDocument<T> {
this.documentRef
.get()
.then((snapshot) => this.handleSnapshot(snapshot))
// .then(() => this.changeReady(true))
.catch((err) =>
console.error(`Fetch initial data failed: ${err.message}`),
);
Expand Down Expand Up @@ -284,7 +285,7 @@ export class ObservableDocument<T> {

private handleSnapshot(snapshot: FirebaseFirestore.DocumentSnapshot) {
runInAction(() => {
this._data = snapshot.exists ? (snapshot.data() as T) : NO_DATA;
this._data = snapshot.exists ? (snapshot.data() as T) : undefined;

this._changeLoadingState(false);
});
Expand Down Expand Up @@ -319,14 +320,16 @@ export class ObservableDocument<T> {

this.initializeReadyPromise();

this._data = undefined;

// @TODO make DRY
if (!hasSource) {
if (this.isObserved) {
this.logDebug("Change document -> clear listeners");
this.updateListeners(false);
}

this._data = NO_DATA;
// this._data = NO_DATA;
this._changeLoadingState(false);
} else {
if (this.isObserved) {
Expand Down Expand Up @@ -370,14 +373,15 @@ export class ObservableDocument<T> {

this.initializeReadyPromise();

this._data = undefined;

// @TODO make DRY
if (!hasSource) {
if (this.isObserved) {
this.logDebug("Change document -> clear listeners");
this.updateListeners(false);
}

this._data = NO_DATA;
this._changeLoadingState(false);
} else {
if (this.isObserved) {
Expand Down
4 changes: 4 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,7 @@ export function createUniqueId() {
export function first<T>(array: T[]): T | undefined {
return array[0];
}

export function last<T>(array: T[]): T | undefined {
return array[array.length - 1];
}

0 comments on commit 015da36

Please sign in to comment.