Skip to content

Commit

Permalink
Remove console.logs, add testing
Browse files Browse the repository at this point in the history
  • Loading branch information
bgoldowsky committed Sep 24, 2024
1 parent e23d568 commit 9cb9ecb
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
27 changes: 26 additions & 1 deletion src/hooks/use-document-sync-to-firebase.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ jest.mock("firebase/app", () => {

const mockUpdate = jest.fn();
const mockRef = jest.fn();
const mockSetLastEditedOnDisconnect = jest.fn();

const specUser = (overrides?: Partial<SnapshotIn<typeof UserModel>>) => {
return UserModel.create({ id: "1", ...overrides });
Expand All @@ -94,7 +95,8 @@ const specFirebase = (type: string, key: string) => {
typedMetadata: `${user.id}/${type}/${key}`
};
},
ref: (path: string) => mockRef(path)
ref: (path: string) => mockRef(path),
setLastEditedOnDisconnect: mockSetLastEditedOnDisconnect,
} as unknown as Firebase;
};

Expand Down Expand Up @@ -128,6 +130,7 @@ describe("useDocumentSyncToFirebase hook", () => {
beforeEach(() => {
mockUpdate.mockReset();
mockRef.mockReset();
mockSetLastEditedOnDisconnect.mockReset();
mockRef.mockImplementation((path: string) => {
return {
update: (value: any) => Promise.resolve(mockUpdate(value))
Expand Down Expand Up @@ -204,123 +207,145 @@ describe("useDocumentSyncToFirebase hook", () => {
document.setProperty("foo", "bar");
expect(mockRef).toHaveBeenCalledTimes(0);
expect(mockUpdate).toHaveBeenCalledTimes(0);

expect(mockSetLastEditedOnDisconnect).toHaveBeenCalledTimes(0);
});

it("monitors problem documents", async () => {
const { user, fb, firestore, document } = specArgs(ProblemDocument, "xyz");
renderHook(() => useDocumentSyncToFirebase(user, fb, firestore, document));
expect(mockRef).toHaveBeenCalledTimes(0);
expect(mockUpdate).toHaveBeenCalledTimes(0);
expect(mockSetLastEditedOnDisconnect).toHaveBeenCalledTimes(0);

// updates public/private status on visibility change
document.setVisibility("public");
expect(mockRef).toBeCalledTimes(1);
expect(mockRef).toHaveBeenCalledWith(`${user.id}/problem/${document.key}`);
expect(mockUpdate).toHaveBeenCalledTimes(1);
expect(mockSetLastEditedOnDisconnect).toHaveBeenCalledTimes(0);

// saves when content changes
document.content?.addTile("text");
expect(mockRef).toHaveBeenCalledTimes(2);
expect(mockRef).toHaveBeenCalledWith(`${user.id}/content/${document.key}`);
expect(mockUpdate).toHaveBeenCalledTimes(2);
expect(mockSetLastEditedOnDisconnect).toHaveBeenCalledTimes(1);

// doesn't respond to title change (problem documents don't have user-settable titles)
document.setTitle("New Title");
expect(mockRef).toHaveBeenCalledTimes(2);
expect(mockUpdate).toHaveBeenCalledTimes(2);
expect(mockSetLastEditedOnDisconnect).toHaveBeenCalledTimes(1);

// doesn't respond to properties change (problem documents don't have user-settable properties)
document.setProperty("foo", "bar");
expect(mockRef).toHaveBeenCalledTimes(3);
expect(mockUpdate).toHaveBeenCalledTimes(3);
expect(mockSetLastEditedOnDisconnect).toHaveBeenCalledTimes(1);
});

it("monitors planning documents", () => {
const { user, fb, firestore, document } = specArgs(PlanningDocument, "xyz");
renderHook(() => useDocumentSyncToFirebase(user, fb, firestore, document));
expect(mockRef).toHaveBeenCalledTimes(0);
expect(mockUpdate).toHaveBeenCalledTimes(0);
expect(mockSetLastEditedOnDisconnect).toHaveBeenCalledTimes(0);

// doesn't respond to visibility change (planning documents are always private)
document.setVisibility("public");
expect(mockRef).toHaveBeenCalledTimes(0);
expect(mockUpdate).toHaveBeenCalledTimes(0);
expect(mockSetLastEditedOnDisconnect).toHaveBeenCalledTimes(0);

// saves when content changes
document.content?.addTile("text");
expect(mockRef).toHaveBeenCalledTimes(1);
expect(mockRef).toHaveBeenCalledWith(`${user.id}/content/${document.key}`);
expect(mockUpdate).toHaveBeenCalledTimes(1);
expect(mockSetLastEditedOnDisconnect).toHaveBeenCalledTimes(1);

// doesn't respond to title change (planning documents don't have user-settable titles)
document.setTitle("New Title");
expect(mockRef).toHaveBeenCalledTimes(1);
expect(mockUpdate).toHaveBeenCalledTimes(1);
expect(mockSetLastEditedOnDisconnect).toHaveBeenCalledTimes(1);

// doesn't respond to properties change (planning documents don't have user-settable properties)
document.setProperty("foo", "bar");
expect(mockRef).toHaveBeenCalledTimes(1);
expect(mockUpdate).toHaveBeenCalledTimes(1);
expect(mockSetLastEditedOnDisconnect).toHaveBeenCalledTimes(1);
});

it("monitors personal documents", () => {
const { user, fb, firestore, document } = specArgs(PersonalDocument, "xyz");
renderHook(() => useDocumentSyncToFirebase(user, fb, firestore, document));
expect(mockRef).toHaveBeenCalledTimes(0);
expect(mockUpdate).toHaveBeenCalledTimes(0);
expect(mockSetLastEditedOnDisconnect).toHaveBeenCalledTimes(0);

// responds to visibility change
document.setVisibility("public");
expect(mockRef).toHaveBeenCalledTimes(1);
expect(mockUpdate).toHaveBeenCalledTimes(1);
expect(mockSetLastEditedOnDisconnect).toHaveBeenCalledTimes(0);

// saves when content changes
document.content?.addTile("text");
expect(mockRef).toHaveBeenCalledTimes(2);
expect(mockRef).toHaveBeenCalledWith(`${user.id}/content/${document.key}`);
expect(mockUpdate).toHaveBeenCalledTimes(2);
expect(mockSetLastEditedOnDisconnect).toHaveBeenCalledTimes(1);

// updates title when it changes
document.setTitle("New Title");
expect(mockRef).toHaveBeenCalledTimes(3);
expect(mockRef).toHaveBeenCalledWith(`${user.id}/personal/${document.key}`);
expect(mockUpdate).toHaveBeenCalledTimes(3);
expect(mockSetLastEditedOnDisconnect).toHaveBeenCalledTimes(1);

// updates properties when they change
document.setProperty("foo", "bar");
expect(mockRef).toHaveBeenCalledTimes(4);
expect(mockRef).toHaveBeenCalledWith(`${user.id}/personal/${document.key}`);
expect(mockUpdate).toHaveBeenCalledTimes(4);
expect(mockSetLastEditedOnDisconnect).toHaveBeenCalledTimes(1);
});

it("monitors learning log documents", () => {
const { user, fb, firestore, document } = specArgs(LearningLogDocument, "xyz");
renderHook(() => useDocumentSyncToFirebase(user, fb, firestore, document));
expect(mockRef).toHaveBeenCalledTimes(0);
expect(mockUpdate).toHaveBeenCalledTimes(0);
expect(mockSetLastEditedOnDisconnect).toHaveBeenCalledTimes(0);

// responds to visibility change
document.setVisibility("public");
expect(mockRef).toHaveBeenCalledTimes(1);
expect(mockUpdate).toHaveBeenCalledTimes(1);
expect(mockSetLastEditedOnDisconnect).toHaveBeenCalledTimes(0);

// saves when content changes
document.content?.addTile("text");
expect(mockRef).toHaveBeenCalledTimes(2);
expect(mockRef).toHaveBeenCalledWith(`${user.id}/content/${document.key}`);
expect(mockUpdate).toHaveBeenCalledTimes(2);
expect(mockSetLastEditedOnDisconnect).toHaveBeenCalledTimes(1);

// updates title when it changes
document.setTitle("New Title");
expect(mockRef).toHaveBeenCalledTimes(3);
expect(mockRef).toHaveBeenCalledWith(`${user.id}/learningLog/${document.key}`);
expect(mockUpdate).toHaveBeenCalledTimes(3);
expect(mockSetLastEditedOnDisconnect).toHaveBeenCalledTimes(1);

// updates properties when they change
document.setProperty("foo", "bar");
expect(mockRef).toHaveBeenCalledTimes(4);
expect(mockRef).toHaveBeenCalledWith(`${user.id}/learningLog/${document.key}`);
expect(mockUpdate).toHaveBeenCalledTimes(4);
expect(mockSetLastEditedOnDisconnect).toHaveBeenCalledTimes(1);
});

it("monitors problem documents with additional logging when DEBUG_SAVE == true", async () => {
Expand Down
2 changes: 0 additions & 2 deletions src/hooks/use-document-sync-to-firebase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ export function useDocumentSyncToFirebase(
// If an onDisconnect was set, remove it and set updated timestamp to now.
if (disconnectHandler.current) {
firebase.setLastEditedNow(user, key, uid, disconnectHandler.current);
console.log("Doc closed, setting last modified", key);
}
};
}
Expand Down Expand Up @@ -205,7 +204,6 @@ export function useDocumentSyncToFirebase(
const mutation = useMutation((snapshot: DocumentContentSnapshotType) => {
if (!disconnectHandler.current) {
disconnectHandler.current = firebase.setLastEditedOnDisconnect(user, key, uid);
console.log("Doc modified, tracking disconnect", key);
}

const tileMap = snapshot.tileMap || {};
Expand Down

0 comments on commit 9cb9ecb

Please sign in to comment.