-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #106 from Unshut-Labs/ar/invalidate-members
feat: Invalidate Queries when changes happen to the group
- Loading branch information
Showing
13 changed files
with
237 additions
and
162 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
data/helpers/messages/handleGroupUpdatedMessage.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
import { invalidateGroupMembersQuery } from "../../../queries/useGroupMembersQuery"; | ||
import { invalidateGroupNameQuery } from "../../../queries/useGroupNameQuery"; | ||
import { invalidateGroupPhotoQuery } from "../../../queries/useGroupPhotoQuery"; | ||
import { DecodedMessageWithCodecsType } from "../../../utils/xmtpRN/client"; | ||
import { handleGroupUpdatedMessage } from "./handleGroupUpdatedMessage"; | ||
|
||
jest.mock("../../../queries/useGroupMembersQuery", () => ({ | ||
invalidateGroupMembersQuery: jest.fn(), | ||
})); | ||
|
||
jest.mock("../../../queries/useGroupNameQuery", () => ({ | ||
invalidateGroupNameQuery: jest.fn(), | ||
})); | ||
|
||
jest.mock("../../../queries/useGroupPhotoQuery", () => ({ | ||
invalidateGroupPhotoQuery: jest.fn(), | ||
})); | ||
|
||
describe("handleGroupUpdatedMessage", () => { | ||
const account = "testAccount"; | ||
const topic = "testTopic"; | ||
|
||
const createMessage = (contentTypeId: string, content: any) => | ||
({ | ||
contentTypeId, | ||
content: () => content, | ||
}) as unknown as DecodedMessageWithCodecsType; | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should not proceed if contentTypeId does not include "group_updated"', () => { | ||
const message = createMessage("text", {}); | ||
|
||
handleGroupUpdatedMessage(account, topic, message); | ||
|
||
expect(invalidateGroupMembersQuery).not.toHaveBeenCalled(); | ||
expect(invalidateGroupNameQuery).not.toHaveBeenCalled(); | ||
expect(invalidateGroupPhotoQuery).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it("should invalidate group members query if members are added or removed", () => { | ||
const content = { | ||
membersAdded: ["member1"], | ||
membersRemoved: [], | ||
metadataFieldsChanged: [], | ||
}; | ||
const message = createMessage("group_updated", content); | ||
|
||
handleGroupUpdatedMessage(account, topic, message); | ||
|
||
expect(invalidateGroupMembersQuery).toHaveBeenCalledWith(account, topic); | ||
}); | ||
|
||
it("should invalidate group name query if group name is changed", () => { | ||
const content = { | ||
membersAdded: [], | ||
membersRemoved: [], | ||
metadataFieldsChanged: [ | ||
{ fieldName: "group_name", newValue: "New Group Name" }, | ||
], | ||
}; | ||
const message = createMessage("group_updated", content); | ||
|
||
handleGroupUpdatedMessage(account, topic, message); | ||
|
||
expect(invalidateGroupNameQuery).toHaveBeenCalledWith(account, topic); | ||
}); | ||
|
||
it("should invalidate group photo query if group photo is changed", () => { | ||
const content = { | ||
membersAdded: [], | ||
membersRemoved: [], | ||
metadataFieldsChanged: [ | ||
{ fieldName: "group_image_url_square", newValue: "New Photo URL" }, | ||
], | ||
}; | ||
const message = createMessage("group_updated", content); | ||
|
||
handleGroupUpdatedMessage(account, topic, message); | ||
|
||
expect(invalidateGroupPhotoQuery).toHaveBeenCalledWith(account, topic); | ||
}); | ||
|
||
it("should invalidate all relevant queries if multiple changes occur", () => { | ||
const content = { | ||
membersAdded: ["member1"], | ||
membersRemoved: ["member2"], | ||
metadataFieldsChanged: [ | ||
{ fieldName: "group_name", newValue: "New Group Name" }, | ||
{ fieldName: "group_image_url_square", newValue: "New Photo URL" }, | ||
], | ||
}; | ||
const message = createMessage("group_updated", content); | ||
|
||
handleGroupUpdatedMessage(account, topic, message); | ||
|
||
expect(invalidateGroupMembersQuery).toHaveBeenCalledWith(account, topic); | ||
expect(invalidateGroupNameQuery).toHaveBeenCalledWith(account, topic); | ||
expect(invalidateGroupPhotoQuery).toHaveBeenCalledWith(account, topic); | ||
}); | ||
|
||
it("should handle empty metadataFieldsChanged array", () => { | ||
const content = { | ||
membersAdded: [], | ||
membersRemoved: [], | ||
metadataFieldsChanged: [], | ||
}; | ||
const message = createMessage("group_updated", content); | ||
|
||
handleGroupUpdatedMessage(account, topic, message); | ||
|
||
expect(invalidateGroupMembersQuery).toHaveBeenCalled(); | ||
expect(invalidateGroupNameQuery).not.toHaveBeenCalled(); | ||
expect(invalidateGroupPhotoQuery).not.toHaveBeenCalled(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { GroupUpdatedContent } from "@xmtp/react-native-sdk"; | ||
|
||
import { invalidateGroupMembersQuery } from "../../../queries/useGroupMembersQuery"; | ||
import { invalidateGroupNameQuery } from "../../../queries/useGroupNameQuery"; | ||
import { invalidateGroupPhotoQuery } from "../../../queries/useGroupPhotoQuery"; | ||
import { DecodedMessageWithCodecsType } from "../../../utils/xmtpRN/client"; | ||
|
||
export const handleGroupUpdatedMessage = ( | ||
account: string, | ||
topic: string, | ||
message: DecodedMessageWithCodecsType | ||
) => { | ||
if (!message.contentTypeId.includes("group_updated")) return; | ||
const content = message.content() as GroupUpdatedContent; | ||
if (content.membersAdded.length > 0 || content.membersRemoved.length > 0) { | ||
invalidateGroupMembersQuery(account, topic); | ||
} | ||
if (content.metadataFieldsChanged.length > 0) { | ||
let groupNameChanged = false; | ||
let groupPhotoChanged = false; | ||
for (const field of content.metadataFieldsChanged) { | ||
if (field.fieldName === "group_name") { | ||
groupNameChanged = true; | ||
} else if (field.fieldName === "group_image_url_square") { | ||
groupPhotoChanged = true; | ||
} | ||
} | ||
if (groupNameChanged) { | ||
invalidateGroupNameQuery(account, topic); | ||
} | ||
if (groupPhotoChanged) { | ||
invalidateGroupPhotoQuery(account, topic); | ||
} | ||
} | ||
// Admin Update | ||
if ( | ||
content.membersAdded.length === 0 && | ||
content.membersRemoved.length === 0 && | ||
content.metadataFieldsChanged.length === 0 | ||
) { | ||
invalidateGroupMembersQuery(account, topic); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.