Skip to content

Commit

Permalink
Added unit tests related to welcome message
Browse files Browse the repository at this point in the history
  • Loading branch information
salmenus committed May 13, 2024
1 parent 5ad4e50 commit 5d03e0e
Show file tree
Hide file tree
Showing 2 changed files with 198 additions and 0 deletions.
95 changes: 95 additions & 0 deletions specs/specs/aiChat/behavior/js/welcomeMessage.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {AiChat, createAiChat} from '@nlux-dev/core/src';
import userEvent from '@testing-library/user-event';
import {afterEach, beforeEach, describe, expect, it} from 'vitest';
import '@testing-library/jest-dom';
import {adapterBuilder} from '../../../../utils/adapterBuilder';
Expand Down Expand Up @@ -47,6 +48,100 @@ describe('createAiChat() + bot persona + welcome message', () => {
expect(welcomeMessage).not.toBeNull();
expect(welcomeMessage.textContent).toContain('Welcome to the chat');
});

describe('When the user submits a message', () => {
it('The welcome message should be removed', async () => {
// Arrange
adapterController = adapterBuilder().withFetchText().create();
aiChat = createAiChat()
.withAdapter(adapterController.adapter)
.withPersonaOptions({
bot: {
name: 'Bot',
picture: 'https://example.com/bot.png',
tagline: 'Welcome to the chat',
},
});

// Act
aiChat.mount(rootElement);
await waitForRenderCycle();

const textArea: HTMLTextAreaElement = rootElement.querySelector('.nlux-comp-prmptBox > textarea')!;
await userEvent.type(textArea, 'Hello{enter}');
await waitForRenderCycle();

// Assert
const welcomeMessage = rootElement.querySelector('.nlux-comp-wlc_msg');
expect(welcomeMessage).toBeNull();
});
});

describe('When the initial message submission fails', () => {
it('The welcome message should be displayed again', async () => {
// Arrange
adapterController = adapterBuilder().withFetchText().create();
aiChat = createAiChat()
.withAdapter(adapterController.adapter)
.withPersonaOptions({
bot: {
name: 'Bot',
picture: 'https://example.com/bot.png',
tagline: 'Welcome to the chat',
},
});

// Act
aiChat.mount(rootElement);
await waitForRenderCycle();

const textArea: HTMLTextAreaElement = rootElement.querySelector('.nlux-comp-prmptBox > textarea')!;
await userEvent.type(textArea, 'Hello{enter}');
await waitForRenderCycle();

adapterController.reject('Error');
await waitForRenderCycle();

// Assert
const welcomeMessage = rootElement.querySelector('.nlux-comp-wlc_msg')!;
expect(welcomeMessage).not.toBeNull();
expect(welcomeMessage.textContent).toContain('Welcome to the chat');
});
});
});

describe('When an initial conversation is provided', () => {
it('The welcome message should not be displayed', async () => {
// Arrange
adapterController = adapterBuilder().withFetchText().create();
aiChat = createAiChat()
.withAdapter(adapterController.adapter)
.withPersonaOptions({
bot: {
name: 'Bot',
picture: 'https://example.com/bot.png',
tagline: 'Welcome to the chat',
},
})
.withInitialConversation([
{
role: 'ai',
message: 'Hello, world!',
},
{
role: 'user',
message: 'Hi, bot!',
},
]);

// Act
aiChat.mount(rootElement);
await waitForRenderCycle();

// Assert
const welcomeMessage = rootElement.querySelector('.nlux-comp-wlc_msg');
expect(welcomeMessage).toBeNull();
});
});
});
});
103 changes: 103 additions & 0 deletions specs/specs/aiChat/behavior/react/welcomeMessage.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {AiChat} from '@nlux-dev/react/src';
import {render} from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import {afterEach, beforeEach, describe, expect, it} from 'vitest';
import {adapterBuilder} from '../../../../utils/adapterBuilder';
import {AdapterController} from '../../../../utils/adapters';
Expand Down Expand Up @@ -40,6 +41,108 @@ describe('<AiChat /> + bot persona + welcome message', () => {
expect(welcomeMessage).not.toBeNull();
expect(welcomeMessage.textContent).toContain('Welcome to the chat');
});

describe('When the user submits a message', () => {
it('The welcome message should be removed', async () => {
// Arrange
const aiChat = (
<AiChat
adapter={adapterController!.adapter}
personaOptions={{
bot: {
name: 'Bot',
picture: 'https://example.com/bot.png',
tagline: 'Welcome to the chat',
},
}}
/>
);
const {container} = render(aiChat);
await waitForReactRenderCycle();

await waitForReactRenderCycle();
const textArea: HTMLTextAreaElement = container.querySelector('.nlux-comp-prmptBox > textarea')!;

// Act
await userEvent.type(textArea, 'Hello{enter}');
await waitForReactRenderCycle();

// Assert
const welcomeMessage = container.querySelector('.nlux-comp-wlc_msg');
expect(welcomeMessage).toBeNull();
});
});

describe('When the initial message submission fails', () => {
it('The welcome message should be displayed again', async () => {
// Arrange
const aiChat = (
<AiChat
adapter={adapterController!.adapter}
personaOptions={{
bot: {
name: 'Bot',
picture: 'https://example.com/bot.png',
tagline: 'Welcome to the chat',
},
}}
/>
);
const {container} = render(aiChat);
await waitForReactRenderCycle();

await waitForReactRenderCycle();
const textArea: HTMLTextAreaElement = container.querySelector('.nlux-comp-prmptBox > textarea')!;

// Act
await userEvent.type(textArea, 'Hello{enter}');
await waitForReactRenderCycle();

adapterController!.reject('Sorry user!');
await waitForReactRenderCycle();

// Assert
const welcomeMessage = container.querySelector('.nlux-comp-wlc_msg')!;
expect(welcomeMessage).not.toBeNull();
expect(welcomeMessage.textContent).toContain('Welcome to the chat');
});
});
});

describe('When an initial conversation is provided', () => {
it('The welcome message should not be displayed', async () => {
// Arrange
const aiChat = (
<AiChat
adapter={adapterController!.adapter}
personaOptions={{
bot: {
name: 'Bot',
picture: 'https://example.com/bot.png',
tagline: 'Welcome to the chat',
},
}}
initialConversation={[
{
role: 'ai',
message: 'Hello, world!',
},
{
role: 'user',
message: 'Hi, bot!',
},
]}
/>
);

// Act
const {container} = render(aiChat);
await waitForReactRenderCycle();

// Assert
const welcomeMessage = container.querySelector('.nlux-comp-wlc_msg');
expect(welcomeMessage).toBeNull();
});
});
});
});

0 comments on commit 5d03e0e

Please sign in to comment.