From 121075e5147a6836065a856b648b26094420dd2f Mon Sep 17 00:00:00 2001 From: Vlad Velici Date: Thu, 24 Oct 2024 10:52:34 +0100 Subject: [PATCH] add integration test for react message update --- .../hooks/use-messages.integration.test.tsx | 67 ++++++++++++++++++- 1 file changed, 66 insertions(+), 1 deletion(-) diff --git a/test/react/hooks/use-messages.integration.test.tsx b/test/react/hooks/use-messages.integration.test.tsx index 7e85023c..abfbbe2f 100644 --- a/test/react/hooks/use-messages.integration.test.tsx +++ b/test/react/hooks/use-messages.integration.test.tsx @@ -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 = () => ( + + + + + + ); + + render(); + + // expect a message to be received by the second room + await waitForMessages(updatesRoomTwo, 1); + expect(updatesRoomTwo.length).toBe(1); + const update = updatesRoomTwo[0]!; + 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;