Skip to content

Commit

Permalink
Added tests for ready event
Browse files Browse the repository at this point in the history
  • Loading branch information
salmenus committed May 13, 2024
1 parent de590e5 commit 49e9dc5
Show file tree
Hide file tree
Showing 3 changed files with 189 additions and 0 deletions.
3 changes: 3 additions & 0 deletions specs/specs/aiChat/events/README.md
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.
95 changes: 95 additions & 0 deletions specs/specs/aiChat/events/js/ready.spec.ts
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();
});
});
});
});
91 changes: 91 additions & 0 deletions specs/specs/aiChat/events/react/ready.spec.tsx
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();
});
});
});
});

0 comments on commit 49e9dc5

Please sign in to comment.