-
-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
189 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
## Event Specs | ||
|
||
In this folder, we have the specs for the events that are emitted by the `<AiChat />` component. |
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,95 @@ | ||
import {AiChat, createAiChat} from '@nlux-dev/core/src'; | ||
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() + events + ready', () => { | ||
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 the component is rendered', () => { | ||
it('The it should trigger the ready event', async () => { | ||
// Arrange | ||
const readyCallback = vi.fn(); | ||
aiChat = createAiChat() | ||
.withAdapter(adapterController!.adapter) | ||
.on('ready', readyCallback); | ||
|
||
// Act | ||
aiChat.mount(rootElement); | ||
await waitForRenderCycle(); | ||
|
||
// Assert | ||
expect(readyCallback).toHaveBeenCalledOnce(); | ||
}); | ||
|
||
it('The it should trigger the ready event with the correct details', async () => { | ||
// Arrange | ||
const readyCallback = vi.fn(); | ||
aiChat = createAiChat() | ||
.withAdapter(adapterController!.adapter) | ||
.on('ready', readyCallback) | ||
.withClassName('test-class') | ||
.withLayoutOptions({ | ||
width: '100%', | ||
height: 800, | ||
}); | ||
|
||
// Act | ||
aiChat.mount(rootElement); | ||
await waitForRenderCycle(); | ||
|
||
// Assert | ||
expect(readyCallback).toHaveBeenCalledWith({ | ||
aiChatProps: { | ||
adapter: adapterController!.adapter, | ||
className: 'test-class', | ||
layoutOptions: { | ||
width: '100%', | ||
height: 800, | ||
}, | ||
}, | ||
}); | ||
}); | ||
}); | ||
|
||
describe('When the component is rendered without a ready event callback', () => { | ||
describe('When the ready callback is provided after the component is rendered', () => { | ||
it('The it should not trigger the ready event', async () => { | ||
// Arrange | ||
const readyCallback = vi.fn(); | ||
aiChat = createAiChat() | ||
.withAdapter(adapterController!.adapter); | ||
|
||
aiChat.mount(rootElement); | ||
await waitForRenderCycle(); | ||
|
||
// Act | ||
aiChat.on('ready', readyCallback); | ||
|
||
// Assert | ||
expect(readyCallback).not.toHaveBeenCalled(); | ||
}); | ||
}); | ||
}); | ||
}); |
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,91 @@ | ||
import {AiChat} from '@nlux-dev/react/src'; | ||
import {render} from '@testing-library/react'; | ||
import {afterEach, beforeEach, describe, expect, it, vi} from 'vitest'; | ||
import {adapterBuilder} from '../../../../utils/adapterBuilder'; | ||
import {AdapterController} from '../../../../utils/adapters'; | ||
import {waitForReactRenderCycle} from '../../../../utils/wait'; | ||
|
||
describe('<AiChat /> + events + ready', () => { | ||
let adapterController: AdapterController | undefined; | ||
|
||
beforeEach(() => { | ||
adapterController = adapterBuilder().withFetchText().create(); | ||
}); | ||
|
||
afterEach(() => { | ||
adapterController = undefined; | ||
}); | ||
|
||
describe('When the component is rendered', () => { | ||
it('The it should trigger the ready event', async () => { | ||
// Arrange | ||
const readyCallback = vi.fn(); | ||
const aiChat = ( | ||
<AiChat adapter={adapterController!.adapter} events={{ready: readyCallback}}/> | ||
); | ||
|
||
// Act | ||
render(aiChat); | ||
await waitForReactRenderCycle(); | ||
|
||
// Assert | ||
expect(readyCallback).toHaveBeenCalledOnce(); | ||
}); | ||
|
||
it('The it should trigger the ready event with the correct details', async () => { | ||
// Arrange | ||
const readyCallback = vi.fn(); | ||
const aiChat = ( | ||
<AiChat | ||
adapter={adapterController!.adapter} | ||
events={{ready: readyCallback}} | ||
className="test-class" | ||
layoutOptions={{ | ||
width: '100%', | ||
height: 800, | ||
}} | ||
/> | ||
); | ||
|
||
// Act | ||
render(aiChat); | ||
await waitForReactRenderCycle(); | ||
|
||
// Assert | ||
expect(readyCallback).toHaveBeenCalledWith({ | ||
aiChatProps: { | ||
adapter: adapterController!.adapter, | ||
className: 'test-class', | ||
layoutOptions: { | ||
width: '100%', | ||
height: 800, | ||
}, | ||
}, | ||
}); | ||
}); | ||
}); | ||
|
||
describe('When the component is rendered without a ready event callback', () => { | ||
describe('When the ready callback is provided after the component is rendered', () => { | ||
it('The it should not trigger the ready event', async () => { | ||
// Arrange | ||
const readyCallback = vi.fn(); | ||
const aiChat = ( | ||
<AiChat adapter={adapterController!.adapter}/> | ||
); | ||
|
||
const {rerender} = render(aiChat); | ||
await waitForReactRenderCycle(); | ||
|
||
// Act | ||
rerender( | ||
<AiChat adapter={adapterController!.adapter} events={{ready: readyCallback}}/>, | ||
); | ||
await waitForReactRenderCycle(); | ||
|
||
// Assert | ||
expect(readyCallback).not.toHaveBeenCalledOnce(); | ||
}); | ||
}); | ||
}); | ||
}); |