Skip to content

Commit

Permalink
add integration test for react message update
Browse files Browse the repository at this point in the history
  • Loading branch information
vladvelici committed Oct 24, 2024
1 parent cfd1462 commit 121075e
Showing 1 changed file with 66 additions and 1 deletion.
67 changes: 66 additions & 1 deletion test/react/hooks/use-messages.integration.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,75 @@ describe('useMessages', () => {

// expect a message to be received by the second room
await waitForMessages(deletionsRoomTwo, 1);
expect(deletionsRoomTwo[0]?.isDeleted()).toBe(true);
expect(deletionsRoomTwo[0]?.isDeleted).toBe(true);
expect(deletionsRoomTwo[0]?.deletedBy).toBe(chatClientOne.clientId);
}, 10000);

it('should update messages correctly', async () => {
// create new clients
const chatClientOne = newChatClient() as unknown as ChatClient;
const chatClientTwo = newChatClient() as unknown as ChatClient;

// create a second room and attach it, so we can listen for deletions
const roomId = randomRoomId();
const roomTwo = chatClientTwo.rooms.get(roomId, RoomOptionsDefaults);
await roomTwo.attach();

// start listening for deletions
const updatesRoomTwo: Message[] = [];
roomTwo.messages.subscribe((message) => {
if (message.type === MessageEvents.Updated) {
updatesRoomTwo.push(message.message);
}
});

const TestComponent = () => {
const { send, update, roomStatus } = useMessages();

useEffect(() => {
if (roomStatus === RoomLifecycle.Attached) {
void send({ text: 'hello world' }).then((message) => {
void update(message, {
text: 'hello universe',
metadata: { icon: 'universe' },
headers: { awesome: 'yes' },
}, {
description: "make it better",
metadata: { something: 'else' },
});
});
}
}, [roomStatus]);

return null;
};

const TestProvider = () => (
<ChatClientProvider client={chatClientOne}>
<ChatRoomProvider
id={roomId}
options={RoomOptionsDefaults}
>
<TestComponent />
</ChatRoomProvider>
</ChatClientProvider>
);

render(<TestProvider />);

// expect a message to be received by the second room
await waitForMessages(updatesRoomTwo, 1);
expect(updatesRoomTwo.length).toBe(1);
const update = updatesRoomTwo[0]!;

Check failure on line 186 in test/react/hooks/use-messages.integration.test.tsx

View workflow job for this annotation

GitHub Actions / lint

Forbidden non-null assertion
expect(update.isUpdated).toBe(true);
expect(update.updatedBy).toBe(chatClientOne.clientId);
expect(update.text).toBe('hello universe');
expect(update.metadata).toEqual({ icon: 'universe' });
expect(update.updateDetail?.description).toBe('make it better');
expect(update.updateDetail?.metadata).toEqual({ something: 'else' });
}, 10000);


it('should receive messages on a subscribed listener', async () => {
// create new clients
const chatClientOne = newChatClient() as unknown as ChatClient;
Expand Down

0 comments on commit 121075e

Please sign in to comment.