From 5ae6abae24cc485bce3f1e0ba729a54de4c79204 Mon Sep 17 00:00:00 2001 From: Piotr Suwala Date: Mon, 11 Dec 2023 13:46:38 +0100 Subject: [PATCH] feat(lib): add tests to moderation events --- lib/src/entities/chat.ts | 4 +-- lib/src/types.ts | 2 +- lib/tests/channel.test.ts | 59 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+), 3 deletions(-) diff --git a/lib/src/entities/chat.ts b/lib/src/entities/chat.ts index 400f8a9..76cd112 100644 --- a/lib/src/entities/chat.ts +++ b/lib/src/entities/chat.ts @@ -1095,7 +1095,7 @@ export class Chat { channel: userId, payload: { channelId: channel, - action: "restrictionsLifted", + restriction: "lifted", }, }) } else { @@ -1105,7 +1105,7 @@ export class Chat { channel: userId, payload: { channelId: channel, - action: params.ban ? "banned" : "muted", + restriction: params.ban ? "banned" : "muted", }, }) } diff --git a/lib/src/types.ts b/lib/src/types.ts index 52447e6..75984a6 100644 --- a/lib/src/types.ts +++ b/lib/src/types.ts @@ -89,7 +89,7 @@ type InviteEventPayload = { } type ModerationEventPayload = { channelId: string - action: "muted" | "banned" | "restrictionsLifted" + restriction: "muted" | "banned" | "lifted" } type CustomEventPayload = any diff --git a/lib/tests/channel.test.ts b/lib/tests/channel.test.ts index 534bfe4..b30d0a5 100644 --- a/lib/tests/channel.test.ts +++ b/lib/tests/channel.test.ts @@ -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() + }) })