Skip to content

Commit

Permalink
Updated message received callback
Browse files Browse the repository at this point in the history
  • Loading branch information
salmenus committed May 13, 2024
1 parent 7ec1457 commit 66c878b
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 0 deletions.
45 changes: 45 additions & 0 deletions specs/specs/aiChat/events/js/messageReceived-fetchAdapter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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!',
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,59 @@ describe('<AiChat /> + 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 = (
<AiChat
adapter={adapterController!.adapter}
events={{messageReceived: messageReceivedCallback1}}
/>
);

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(
<AiChat
adapter={adapterController!.adapter}
events={{messageReceived: messageReceivedCallback2}}
/>,
);

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!',
});
});
});
});

0 comments on commit 66c878b

Please sign in to comment.