Skip to content

Commit

Permalink
feat(lib): add tests to moderation events
Browse files Browse the repository at this point in the history
  • Loading branch information
piotr-suwala committed Dec 11, 2023
1 parent d35a261 commit 5ae6aba
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 3 deletions.
4 changes: 2 additions & 2 deletions lib/src/entities/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1095,7 +1095,7 @@ export class Chat {
channel: userId,
payload: {
channelId: channel,
action: "restrictionsLifted",
restriction: "lifted",
},
})
} else {
Expand All @@ -1105,7 +1105,7 @@ export class Chat {
channel: userId,
payload: {
channelId: channel,
action: params.ban ? "banned" : "muted",
restriction: params.ban ? "banned" : "muted",
},
})
}
Expand Down
2 changes: 1 addition & 1 deletion lib/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ type InviteEventPayload = {
}
type ModerationEventPayload = {
channelId: string
action: "muted" | "banned" | "restrictionsLifted"
restriction: "muted" | "banned" | "lifted"
}
type CustomEventPayload = any

Expand Down
59 changes: 59 additions & 0 deletions lib/tests/channel.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1069,4 +1069,63 @@ describe("Channel test", () => {

await publicChannel.delete()
})

test("should set (or lift) restrictions on a user", async () => {
const moderationEventCallback = jest.fn()

const removeModerationListener = chat.listenForEvents({
channel: chat.currentUser.id,
type: "moderation",
callback: moderationEventCallback,
})

await chat.setRestrictions(chat.currentUser.id, "some-channel", { mute: true })
await sleep(150) // Wait for the message to be sent and cached
expect(moderationEventCallback).toHaveBeenCalledWith(
expect.objectContaining({
payload: {
channelId: "PUBNUB_INTERNAL_MODERATION_some-channel",
restriction: "muted",
},
})
)
moderationEventCallback.mockReset()

await chat.setRestrictions(chat.currentUser.id, "some-channel", { ban: true })
await sleep(150) // Wait for the message to be sent and cached
expect(moderationEventCallback).toHaveBeenCalledWith(
expect.objectContaining({
payload: {
channelId: "PUBNUB_INTERNAL_MODERATION_some-channel",
restriction: "banned",
},
})
)
moderationEventCallback.mockReset()

await chat.setRestrictions(chat.currentUser.id, "some-channel", { ban: true, mute: true })
await sleep(150) // Wait for the message to be sent and cached
expect(moderationEventCallback).toHaveBeenCalledWith(
expect.objectContaining({
payload: {
channelId: "PUBNUB_INTERNAL_MODERATION_some-channel",
restriction: "banned",
},
})
)
moderationEventCallback.mockReset()

await chat.setRestrictions(chat.currentUser.id, "some-channel", {})
await sleep(150) // Wait for the message to be sent and cached
expect(moderationEventCallback).toHaveBeenCalledWith(
expect.objectContaining({
payload: {
channelId: "PUBNUB_INTERNAL_MODERATION_some-channel",
restriction: "lifted",
},
})
)

removeModerationListener()
})
})

0 comments on commit 5ae6aba

Please sign in to comment.