-
-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added handling for messageReceived event
- Loading branch information
Showing
12 changed files
with
348 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
specs/specs/aiChat/events/js/messageReceived-fetchAdapter.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import {AiChat, createAiChat} from '@nlux-dev/core/src'; | ||
import userEvent from '@testing-library/user-event'; | ||
import {afterEach, beforeEach, describe, expect, it, vi} from 'vitest'; | ||
import '@testing-library/jest-dom'; | ||
import {adapterBuilder} from '../../../../utils/adapterBuilder'; | ||
import {AdapterController} from '../../../../utils/adapters'; | ||
import {waitForRenderCycle} from '../../../../utils/wait'; | ||
|
||
describe('createAiChat() + fetch adapter + events + messageReceived', () => { | ||
let adapterController: AdapterController | undefined = undefined; | ||
let rootElement: HTMLElement; | ||
let aiChat: AiChat | undefined; | ||
|
||
beforeEach(() => { | ||
adapterController = adapterBuilder() | ||
.withFetchText(true) | ||
.withStreamText(false) | ||
.create(); | ||
|
||
rootElement = document.createElement('div'); | ||
document.body.append(rootElement); | ||
}); | ||
|
||
afterEach(() => { | ||
adapterController = undefined; | ||
aiChat?.unmount(); | ||
rootElement?.remove(); | ||
aiChat = undefined; | ||
}); | ||
|
||
describe('When a message is received', () => { | ||
it('It should trigger the messageReceived event', async () => { | ||
// Arrange | ||
const messageReceivedCallback = vi.fn(); | ||
aiChat = createAiChat() | ||
.withAdapter(adapterController!.adapter) | ||
.on('messageReceived', messageReceivedCallback); | ||
|
||
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(messageReceivedCallback).toHaveBeenCalledOnce(); | ||
}); | ||
}); | ||
}); |
54 changes: 54 additions & 0 deletions
54
specs/specs/aiChat/events/js/messageReceived-streamAdapter.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import {AiChat, createAiChat} from '@nlux-dev/core/src'; | ||
import userEvent from '@testing-library/user-event'; | ||
import {afterEach, beforeEach, describe, expect, it, vi} from 'vitest'; | ||
import '@testing-library/jest-dom'; | ||
import {adapterBuilder} from '../../../../utils/adapterBuilder'; | ||
import {AdapterController} from '../../../../utils/adapters'; | ||
import {waitForRenderCycle} from '../../../../utils/wait'; | ||
|
||
describe('createAiChat() + stream adapter + events + messageReceived', () => { | ||
let adapterController: AdapterController | undefined = undefined; | ||
let rootElement: HTMLElement; | ||
let aiChat: AiChat | undefined; | ||
|
||
beforeEach(() => { | ||
adapterController = adapterBuilder() | ||
.withFetchText(false) | ||
.withStreamText(true) | ||
.create(); | ||
|
||
rootElement = document.createElement('div'); | ||
document.body.append(rootElement); | ||
}); | ||
|
||
afterEach(() => { | ||
adapterController = undefined; | ||
aiChat?.unmount(); | ||
rootElement?.remove(); | ||
aiChat = undefined; | ||
}); | ||
|
||
describe('When a message is received', () => { | ||
it('It should trigger the messageReceived event', async () => { | ||
// Arrange | ||
const messageReceivedCallback = vi.fn(); | ||
aiChat = createAiChat() | ||
.withAdapter(adapterController!.adapter) | ||
.on('messageReceived', messageReceivedCallback); | ||
|
||
aiChat.mount(rootElement); | ||
await waitForRenderCycle(); | ||
|
||
const textArea: HTMLTextAreaElement = rootElement.querySelector('.nlux-comp-prmptBox > textarea')!; | ||
await userEvent.type(textArea, 'Hello{enter}'); | ||
|
||
// Act | ||
adapterController!.next('Yo!'); | ||
adapterController!.complete(); | ||
await waitForRenderCycle(); | ||
|
||
// Assert | ||
expect(messageReceivedCallback).toHaveBeenCalledOnce(); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.