Skip to content

Commit

Permalink
fix(lib): take membership related events into account
Browse files Browse the repository at this point in the history
  • Loading branch information
piotr-suwala committed Jun 18, 2024
1 parent c5778cc commit d29b3a4
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 4 deletions.
4 changes: 3 additions & 1 deletion lib/src/entities/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ export class User {
const listener = {
objects: (event: PubNub.SetUUIDMetadataEvent<PubNub.ObjectCustom>) => {
if (event.message.type !== "uuid") return
const user = users.find((c) => c.id === event.channel)
const user =
users.find((c) => c.id === event.channel) ||
users.find((c) => c.id === event.message.data.id)
if (!user) return
const newUser = User.fromDTO(user.chat, event.message.data)
const newUsers = users.map((user) => (user.id === newUser.id ? newUser : user))
Expand Down
12 changes: 9 additions & 3 deletions lib/tests/channel.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,13 +131,14 @@ describe("Channel test", () => {
})

test("should edit membership metadata", async () => {
const { membership } = await channel.join(() => null)
const { membership, disconnect } = await channel.join(() => null)
const updatedMembership = await membership.update({
custom: { role: "admin" },
})
expect(updatedMembership.custom?.role).toBe("admin")

await channel.leave()
disconnect()
})

test("should create a direct, group and public chats with a predefined ID", async () => {
Expand Down Expand Up @@ -365,7 +366,7 @@ describe("Channel test", () => {
})

test("should stream membership updates and invoke the callback", async () => {
const { membership } = await channel.join(() => null)
const { membership, disconnect } = await channel.join(() => null)
expect(membership).toBeDefined()

let updatedMembership
Expand All @@ -382,6 +383,7 @@ describe("Channel test", () => {

await channel.leave()
stopUpdates()
disconnect()
})

test("should get unread messages count", async () => {
Expand All @@ -392,7 +394,9 @@ describe("Channel test", () => {
await channel.sendText(messageText2)
await sleep(150) // history calls have around 130ms of cache time

let { membership } = await channel.join(() => null)
const channelJoinData = await channel.join(() => null)
let { membership } = channelJoinData
const { disconnect } = channelJoinData
let unreadCount = await membership.getUnreadMessagesCount()
expect(unreadCount).toBe(0)

Expand All @@ -402,6 +406,7 @@ describe("Channel test", () => {
expect(unreadCount).toBe(1)

await channel.leave()
disconnect()
})

test("should mention users in a message and validate mentioned users", async () => {
Expand Down Expand Up @@ -873,6 +878,7 @@ describe("Channel test", () => {
const nonExistentChannel = await chat.getChannel(nonExistentChannelId)
const membership = await nonExistentChannel.join(() => null)
await membership.update({ custom: { role: "admin" } })
membership.disconnect()
fail("Editing membership metadata of a non-existent channel should fail")
} catch (error) {
expect(error.message).toContain("Cannot read properties of null (reading 'join')")
Expand Down
58 changes: 58 additions & 0 deletions lib/tests/user.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,64 @@ describe("User test", () => {
stopUpdates()
})

test("should update the user even if they're a member of a particular channel", async () => {
let someUser = await chat.getUser("test-user-chatsdk0")
if (!someUser) {
someUser = await chat.createUser("test-user-chatsdk0", { name: "Chat SDK user 0" })
}
let someChannel = await chat.getChannel("some-public-channel")
if (!someChannel) {
someChannel = await chat.createPublicConversation({
channelId: "some-public-channel",
channelData: { name: "Public channel test" },
})
}
await chat.sdk.objects.setChannelMembers({
channel: someChannel.id,
uuids: [someUser.id],
})

const stopUpdates = User.streamUpdatesOn([someUser], (updatedUsers) => {
someUser = updatedUsers[0]
})
await someUser.update({ name: "update number 1" })
await sleep(1000)
expect(someUser.name).toBe("update number 1")
await someUser.update({ name: "update number 2" })
await sleep(1000)
expect(someUser.name).toBe("update number 2")

stopUpdates()
})

test("should update the user even if they're not a member of a particular channel", async () => {
let someUser = await chat.getUser("test-user-chatsdk1")
if (!someUser) {
someUser = await chat.createUser("test-user-chatsdk1", { name: "Chat SDK user 1" })
}
let someChannel = await chat.getChannel("some-public-channel-2")
if (!someChannel) {
someChannel = await chat.createPublicConversation({
channelId: "some-public-channel-2",
channelData: { name: "Public channel test 2" },
})
}
const { members } = await someChannel.getMembers()

expect(members.length).toBe(0)
const stopUpdates = User.streamUpdatesOn([someUser], (updatedUsers) => {
someUser = updatedUsers[0]
})
await someUser.update({ name: "update number 1" })
await sleep(1000)
expect(someUser.name).toBe("update number 1")
await someUser.update({ name: "update number 2" })
await sleep(1000)
expect(someUser.name).toBe("update number 2")

stopUpdates()
})

test("should report a user", async () => {
const reportReason = "Inappropriate behavior"
await user.report(reportReason)
Expand Down

0 comments on commit d29b3a4

Please sign in to comment.