-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Invalidate Queries when changes happen to the group #106
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can the admin also change/would you want to add that to the invalidation logic too?