Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 14 additions & 13 deletions src/hooks/accounts/accounts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1316,10 +1316,11 @@ describe("accounts", () => {
test("useEditedComment has edited comment", async () => {
const rendered2 = renderHook<any, any>(() => {
const comment = useComment({ commentCid: "Qm..." });
const editedComment = useEditedComment({ comment });
return editedComment;
return { comment, ...useEditedComment({ comment }) };
});
await waitFor(() => rendered2.result.current.editedComment);
const waitForEditedComment = testUtils.createWaitFor(rendered2);
await waitForEditedComment(() => rendered2.result.current.comment.cid === "Qm...");
await waitForEditedComment(() => rendered2.result.current.editedComment);
expect(rendered2.result.current.editedComment).not.toBe(undefined);
expect(
rendered2.result.current.pendingEdits.spoiler ||
Expand Down Expand Up @@ -2292,10 +2293,10 @@ describe("accounts", () => {

beforeEach(async () => {
rendered = renderHook<any, any>(() => {
const { accountCommunities } = useAccountCommunities();
const { accountCommunities, state, error, errors } = useAccountCommunities();
const account = useAccount();
const { setAccount } = accountsActions;
return { accountCommunities, setAccount, account };
return { accountCommunities, state, error, errors, setAccount, account };
});
waitFor = testUtils.createWaitFor(rendered);

Expand Down Expand Up @@ -2382,19 +2383,19 @@ describe("accounts", () => {
test("useAccountCommunities reflects in-flight community fetches", async () => {
await waitFor(() => rendered.result.current.accountCommunities["community address 1"]);
const { account, setAccount } = rendered.result.current;
const createCommunityOrig = account.plebbit.createCommunity;
const createCommunityOrig = Plebbit.prototype.createCommunity;
const slowCommunity = await createCommunityOrig.call(account.plebbit, {
address: "slow community address",
});
let resolveSlowCommunity: ((community: any) => void) | undefined;
const slowCommunityPromise = new Promise((resolve) => {
resolveSlowCommunity = resolve;
});
account.plebbit.createCommunity = vi.fn(async (options: any) => {
Plebbit.prototype.createCommunity = vi.fn(async function (options: any) {
if (options.address === "slow community address") {
return slowCommunityPromise;
}
return createCommunityOrig.call(account.plebbit, options);
return createCommunityOrig.call(this, options);
});

try {
Expand All @@ -2418,19 +2419,19 @@ describe("accounts", () => {
resolveSlowCommunity?.(slowCommunity);
await waitFor(() => rendered.result.current.state === "succeeded");
} finally {
account.plebbit.createCommunity = createCommunityOrig;
Plebbit.prototype.createCommunity = createCommunityOrig;
}
});

test("useAccountCommunities propagates failed community fetches", async () => {
await waitFor(() => rendered.result.current.accountCommunities["community address 1"]);
const { account, setAccount } = rendered.result.current;
const createCommunityOrig = account.plebbit.createCommunity;
account.plebbit.createCommunity = vi.fn(async (options: any) => {
const createCommunityOrig = Plebbit.prototype.createCommunity;
Plebbit.prototype.createCommunity = vi.fn(async function (options: any) {
if (options.address === "failing community address") {
throw new Error("community fetch failed");
}
return createCommunityOrig.call(account.plebbit, options);
return createCommunityOrig.call(this, options);
});

try {
Expand All @@ -2450,7 +2451,7 @@ describe("accounts", () => {
"community fetch failed",
);
} finally {
account.plebbit.createCommunity = createCommunityOrig;
Plebbit.prototype.createCommunity = createCommunityOrig;
}
});
});
Expand Down
1 change: 1 addition & 0 deletions src/hooks/accounts/accounts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,7 @@ export function useEditedComment(options?: UseEditedCommentOptions): UseEditedCo
"signer",
"commentCid",
"communityAddress",
"subplebbitAddress",
"timestamp",
]);

Expand Down
42 changes: 28 additions & 14 deletions src/stores/communities/communities-store.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,22 +137,36 @@ describe("communities store", () => {
const address = "owner-retry-address";
const createOrig = mockAccount.plebbit.createCommunity;
const communitiesOrig = mockAccount.plebbit.communities;
const ownCommunitiesDescriptor = Object.getOwnPropertyDescriptor(
mockAccount.plebbit,
"communities",
);
const resolvedCommunity = await createOrig.call(mockAccount.plebbit, { address });
mockAccount.plebbit.createCommunity = vi
.fn()
.mockRejectedValueOnce(new Error("owner create failed"))
.mockRejectedValueOnce(new Error("fetch create failed"))
.mockResolvedValueOnce(resolvedCommunity);
mockAccount.plebbit.communities = [...communitiesOrig, address];

await expect(
communitiesStore.getState().addCommunityToStore(address, mockAccount),
).rejects.toThrow("fetch create failed");
await communitiesStore.getState().addCommunityToStore(address, mockAccount);
try {
mockAccount.plebbit.createCommunity = vi
.fn()
.mockRejectedValueOnce(new Error("owner create failed"))
.mockRejectedValueOnce(new Error("fetch create failed"))
.mockResolvedValueOnce(resolvedCommunity);
Object.defineProperty(mockAccount.plebbit, "communities", {
configurable: true,
get: () => [...communitiesOrig, address],
});

await expect(
communitiesStore.getState().addCommunityToStore(address, mockAccount),
).rejects.toThrow("fetch create failed");
await communitiesStore.getState().addCommunityToStore(address, mockAccount);

expect(communitiesStore.getState().communities[address]).toBeDefined();
mockAccount.plebbit.createCommunity = createOrig;
mockAccount.plebbit.communities = communitiesOrig;
expect(communitiesStore.getState().communities[address]).toBeDefined();
} finally {
mockAccount.plebbit.createCommunity = createOrig;
if (ownCommunitiesDescriptor) {
Object.defineProperty(mockAccount.plebbit, "communities", ownCommunitiesDescriptor);
} else {
delete (mockAccount.plebbit as any).communities;
}
}
});

test("addCommunityToStore throws generic Error when community is undefined without thrown error", async () => {
Expand Down
2 changes: 1 addition & 1 deletion test/test-server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ const plebbitDataPath = getTmpFolderPath();
const comment = await plebbit2.createComment({
title: "comment title",
content: "comment content",
communityAddress: signer.address,
subplebbitAddress: signer.address,
signer,
author: { address: signer.address },
});
Expand Down
Loading