diff --git a/specs/specs/aiChat/events/js/messageReceived-fetchAdapter.spec.ts b/specs/specs/aiChat/events/js/messageReceived-fetchAdapter.spec.ts index 7b898e5b..86660d4d 100644 --- a/specs/specs/aiChat/events/js/messageReceived-fetchAdapter.spec.ts +++ b/specs/specs/aiChat/events/js/messageReceived-fetchAdapter.spec.ts @@ -50,4 +50,49 @@ describe('createAiChat() + fetch adapter + events + messageReceived', () => { expect(messageReceivedCallback).toHaveBeenCalledOnce(); }); }); + + describe('When the callback updates between messages', () => { + it('It should trigger the messageReceived event with the new callback', async () => { + // Arrange + const messageReceivedCallback1 = vi.fn(); + const messageReceivedCallback2 = vi.fn(); + aiChat = createAiChat() + .withAdapter(adapterController!.adapter) + .on('messageReceived', messageReceivedCallback1); + + aiChat.mount(rootElement); + await waitForRenderCycle(); + + const textArea: HTMLTextAreaElement = rootElement.querySelector('.nlux-comp-prmptBox > textarea')!; + await userEvent.type(textArea, 'Hello{enter}'); + + // Act + adapterController!.resolve('Yo!'); + await waitForRenderCycle(); + + // Assert + expect(messageReceivedCallback1).toHaveBeenCalledWith({ + uid: expect.any(String), + message: 'Yo!', + }); + + // Arrange + aiChat.removeEventListener('messageReceived', messageReceivedCallback1); + aiChat.on('messageReceived', messageReceivedCallback2); + + // Act + await userEvent.type(textArea, 'Bonjour{enter}'); + await waitForRenderCycle(); + + adapterController!.resolve('Salut!'); + await waitForRenderCycle(); + + // Assert + expect(messageReceivedCallback1).toHaveBeenCalledOnce(); + expect(messageReceivedCallback2).toHaveBeenCalledWith({ + uid: expect.any(String), + message: 'Salut!', + }); + }); + }); }); diff --git a/specs/specs/aiChat/events/react/messageReceived-fetchAdapter.spec.tsx b/specs/specs/aiChat/events/react/messageReceived-fetchAdapter.spec.tsx index 2d016ae6..051c9c82 100644 --- a/specs/specs/aiChat/events/react/messageReceived-fetchAdapter.spec.tsx +++ b/specs/specs/aiChat/events/react/messageReceived-fetchAdapter.spec.tsx @@ -71,4 +71,59 @@ describe(' + fetch adapter + events + messageReceived', () => { }); }); }); + + describe('When the callback updates between messages', () => { + it('It should trigger the messageReceived event with the new callback', async () => { + // Arrange + const messageReceivedCallback1 = vi.fn(); + const messageReceivedCallback2 = vi.fn(); + const aiChat = ( + + ); + + const {container, rerender} = render(aiChat); + await waitForReactRenderCycle(); + + const textArea: HTMLTextAreaElement = container.querySelector('.nlux-comp-prmptBox > textarea')!; + await userEvent.type(textArea, 'Hello{enter}'); + await waitForReactRenderCycle(); + + // Act + adapterController!.resolve('Yo!'); + await waitForReactRenderCycle(); + + // Assert + expect(messageReceivedCallback1).toHaveBeenCalledWith({ + uid: expect.any(String), + message: 'Yo!', + }); + + // Arrange + rerender( + , + ); + + await waitForReactRenderCycle(); + const textArea2: HTMLTextAreaElement = container.querySelector('.nlux-comp-prmptBox > textarea')!; + await userEvent.type(textArea2, 'Bonjour{enter}'); + await waitForReactRenderCycle(); + + // Act + adapterController!.resolve('Salut!'); + await waitForReactRenderCycle(); + + // Assert + expect(messageReceivedCallback1).toHaveBeenCalledOnce(); + expect(messageReceivedCallback2).toHaveBeenCalledWith({ + uid: expect.any(String), + message: 'Salut!', + }); + }); + }); });