Skip to content

Commit

Permalink
Added TS test for set/getRestrictions (#166)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcin-cebo authored Jan 30, 2025
1 parent aa0e565 commit eeb86b9
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 8 deletions.
36 changes: 28 additions & 8 deletions js-chat/tests/channel.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
createChatInstance,
sendMessageAndWaitForHistory,
makeid,
generateRandomString,
} from "./utils"

import { jest } from "@jest/globals"
Expand All @@ -22,11 +23,13 @@ describe("Channel test", () => {
jest.retryTimes(3)

let chat: Chat
let chatPamServer: Chat
let channel: Channel
let messageDraft: MessageDraft

beforeAll(async () => {
chat = await createChatInstance()
chatPamServer = await createChatInstance( { shouldCreateNewInstance: true, clientType: 'PamServer' })
})

beforeEach(async () => {
Expand Down Expand Up @@ -1086,57 +1089,74 @@ describe("Channel test", () => {
await publicChannel.delete()
})

test("canGetUserRestriction", async() => {
const userId = generateRandomString(10)
let user = await chatPamServer.createUser(userId, { name: "User123" });
const channel = (await chatPamServer.createDirectConversation({ user: user})).channel;
await channel.setRestrictions(user, {mute: true, reason: "rude"})
await sleep(1250)
let restrictions = await channel.getUserRestrictions(user);

expect(restrictions.ban).toEqual(false);
expect(restrictions.mute).toEqual(true);
expect(restrictions.reason).toEqual("rude");

await user.delete()
await channel.delete()
})

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

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

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

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

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

await chat.setRestrictions(chat.currentUser.id, "some-channel", {})
await chatPamServer.setRestrictions(chat.currentUser.id, channelName, {})
await sleep(250) // Wait for the message to be sent and cached
expect(moderationEventCallback).toHaveBeenCalledWith(
expect.objectContaining({
payload: expect.objectContaining({
channelId: "PUBNUB_INTERNAL_MODERATION_some-channel",
channelId: `PUBNUB_INTERNAL_MODERATION_${channelName}`,
restriction: "lifted",
}),
})
Expand Down
10 changes: 10 additions & 0 deletions js-chat/tests/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,16 @@ export function createRandomUser(prefix?: string) {
})
}

export function generateRandomString(length: number): string {
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
const charactersLength = characters.length;
let result = '';
for (let i = 0; i < length; i++) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result;
}

export const waitForAllMessagesToBeDelivered = async (
textMessages: string[],
messages: string[]
Expand Down

0 comments on commit eeb86b9

Please sign in to comment.