Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Records with different terms and tabs #62

Merged
merged 6 commits into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion __mocks__/socket.io-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ function emit(event, ...args) {
return;
}

EVENTS[event].forEach(func => func(...args));
if (EVENTS[event]?.length) {
EVENTS[event].forEach((func) => func(...args));
}
}

export const socket = {
Expand Down
27 changes: 14 additions & 13 deletions __tests__/feature/chat-closing.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,20 @@ import { fireEvent } from '@testing-library/react';
import { act } from 'react-dom/test-utils';
import { io } from 'socket.io-client';

import { localTearDown } from '../helpers';
import { SCROLL_STOP_CLASS } from '../../src/config/env';
import renderWithProviders from '../../src/utils/storeMockWrapper';
import { serverSocket } from '../../__mocks__/socket.io-client';
import AppBase from '../../src/components/AppBase';
import { chat as getInitialConfig, Events, initialMessage } from '../../src/config';
import { SCROLL_STOP_CLASS } from '../../src/config/env';
import { setConnected } from '../../src/store/slices/chat';
import { serverSocket } from '../../__mocks__/socket.io-client';
import { Events, chat as getInitialConfig, initialMessage } from '../../src/config';
import { uuidV4 } from '../../src/utils';
import renderWithProviders from '../../src/utils/storeMockWrapper';
import { localTearDown } from '../helpers';

let root;
describe('Close button closes chat', () => {
const href = 'https://example.com/?utm_chat=default';
const search = '?utm_chat=default';
const term = 'default'
const href = `https://example.com/?utm_chat=${term}}`;
const search = `?utm_chat=${term}`;

beforeEach(async () => {
const mockLocation = {
Expand Down Expand Up @@ -45,9 +46,9 @@ describe('Close button closes chat', () => {
});

const serverData = {
"region": faker.location.country(),
"history": [],
"errors": []
'region': faker.location.country(),
'history': [],
'errors': []
}
act(() => {
serverSocket.emit(Events.chatHistory, serverData);
Expand Down Expand Up @@ -90,9 +91,9 @@ describe('Close button closes chat', () => {
});

const serverData = {
"region": faker.location.country(),
"history": [],
"errors": []
'region': faker.location.country(),
'history': [],
'errors': []
}
act(() => {
serverSocket.emit(Events.chatHistory, serverData);
Expand Down
55 changes: 30 additions & 25 deletions __tests__/feature/chat-history.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@ import { fireEvent } from '@testing-library/react';
import { act } from 'react-dom/test-utils';
import { io } from 'socket.io-client';

import { uuidV4 } from '../../src/utils';
import { intent } from '../../src/main';
import { localTearDown } from '../helpers';
import AppBase from '../../src/components/AppBase';
import renderWithProviders from '../../src/utils/storeMockWrapper';
import { setConnected } from '../../src/store/slices/chat';
import { serverSocket } from '../../__mocks__/socket.io-client';
import { Events, Roles, chat as getInitialConfig, initialMessage } from '../../src/config';
import AppBase from '../../src/components/AppBase';
import { chat as getInitialConfig, Events, initialMessage,Roles } from '../../src/config';
import { Intentions } from '../../src/config/enums';
import { intent } from '../../src/main';
import { uuidV4 } from '../../src/utils';
import renderWithProviders from '../../src/utils/storeMockWrapper';
import { localTearDown } from '../helpers';

const textProbe = () => ({ 'type': 'text', 'text': faker.lorem.text(), 'sequence': 2 });
const contentMock = {
Expand All @@ -27,20 +26,37 @@ const contentMock = {
};

const serverData = {
"region": faker.location.country(),
"history": [],
"errors": [],
'region': faker.location.country(),
'history': [],
'errors': [],
}

jest.useFakeTimers();

let root;
const chatConfig = getInitialConfig({ id: uuidV4(), purpose: '', close: { visible: true } });
describe('Chat-history event works and visualizes items accordingly', () => {
beforeEach(() => {
const term = 'default'
const href = `https://example.com/?utm_chat=${term}}`;
const search = `?utm_chat=${term}`;
const threadId = uuidV4();

beforeEach(async () => {
const mockLocation = {
href,
search,
};

Object.defineProperty(window, 'location', {
value: mockLocation,
writable: true,
enumerable: true,
});
serverData.history = [];
serverData.errors = [];
serverData.region = faker.location.country();
serverData.term = term;
serverData.threadId = threadId;
})
afterEach(localTearDown);

Expand All @@ -50,12 +66,11 @@ describe('Chat-history event works and visualizes items accordingly', () => {
root = renderWithProviders(
<div id="chatbot-container">
<AppBase config={getInitialConfig({ id: uuidV4(), purpose: '', close: {} })} />
</div>
</div>,
);
});

// Assert
const historyElements = root.container.querySelectorAll('[data-e2e="history-item"]');
const userFormElement = root.container.querySelector('[data-e2e="user-form"]');
const emailFormElement = root.container.querySelector('[data-e2e="email-form"]');
expect(root.container.querySelector('[data-e2e="stream-response-loader"]')).toBeVisible();
Expand All @@ -64,7 +79,6 @@ describe('Chat-history event works and visualizes items accordingly', () => {

const { chat } = root.store.getState();
expect(chat.isLoading).toBe(true);
expect(historyElements.length).toEqual(chat.historyIds.length);
});

test('on chat-history event state is shown and saved properly', async () => {
Expand All @@ -75,7 +89,6 @@ describe('Chat-history event works and visualizes items accordingly', () => {
<AppBase config={getInitialConfig({ id: uuidV4(), purpose: '', close: { visible: true } })} />
</div>
);
root.store.dispatch(setConnected(true));
});

serverData.history = [{
Expand All @@ -102,7 +115,7 @@ describe('Chat-history event works and visualizes items accordingly', () => {
expect(emailFormElement).toBeNull();

const { meta, config, chat } = root.store.getState();
expect(historyElements.length).toEqual(chat.historyIds.length);
expect(historyElements.length).toEqual(chat.record[threadId].historyIds.length);
expect(meta.region).toStrictEqual(serverData.region);
expect(config.aiProfile).toStrictEqual(chatConfig.app.aiProfile);
expect(config.translations).toStrictEqual(chatConfig.app.translations);
Expand All @@ -114,7 +127,6 @@ describe('Chat-history event works and visualizes items accordingly', () => {
root = renderWithProviders(<div id="chatbot-container">
<AppBase config={getInitialConfig({ id: uuidV4(), purpose: '', close: { visible: true } })} />
</div>);
root.store.dispatch(setConnected(true));
});

// Act
Expand Down Expand Up @@ -145,7 +157,6 @@ describe('Chat-history event works and visualizes items accordingly', () => {
root = renderWithProviders(<div id="chatbot-container">
<AppBase config={getInitialConfig({ id: uuidV4(), purpose: '', close: { visible: true } })} />
</div>);
root.store.dispatch(setConnected(true));
});

// Act
Expand Down Expand Up @@ -176,7 +187,6 @@ describe('Chat-history event works and visualizes items accordingly', () => {

const { chat } = root.store.getState();
expect(chat.isLoading).toBe(false);
expect(chat.isStreaming).toBe(false);
});

test('email form submitted does not submit the email when the field is empty', async () => {
Expand All @@ -185,7 +195,6 @@ describe('Chat-history event works and visualizes items accordingly', () => {
root = renderWithProviders(<div id="chatbot-container">
<AppBase config={getInitialConfig({ id: uuidV4(), purpose: '', close: { visible: true } })} />
</div>);
root.store.dispatch(setConnected(true));
});

// Act
Expand Down Expand Up @@ -216,7 +225,6 @@ describe('Chat-history event works and visualizes items accordingly', () => {
root = renderWithProviders(<div id="chatbot-container">
<AppBase config={getInitialConfig({ id: uuidV4(), purpose: '', close: { visible: true } })} />
</div>);
root.store.dispatch(setConnected(true));
});

// Act
Expand Down Expand Up @@ -253,8 +261,8 @@ describe('Chat-history event works and visualizes items accordingly', () => {
<AppBase config={getInitialConfig({ id: uuidV4(), purpose: '', close: { visible: true } })} />
</div>
);
root.store.dispatch(setConnected(true));
});

serverData.history = [
{
id: uuidV4(),
Expand Down Expand Up @@ -284,7 +292,6 @@ describe('Chat-history event works and visualizes items accordingly', () => {

const { chat } = root.store.getState();
expect(chat.isLoading).toBe(false);
expect(chat.isStreaming).toBe(false);
});

test('on chat-history event with errors in the callback visualizes the error', async () => {
Expand All @@ -294,7 +301,6 @@ describe('Chat-history event works and visualizes items accordingly', () => {
<AppBase config={getInitialConfig({ id: uuidV4(), purpose: '', close: { visible: true } })} />
</div>
);
root.store.dispatch(setConnected(true));
});

// Act
Expand Down Expand Up @@ -331,7 +337,6 @@ describe('Chat-history event works and visualizes items accordingly', () => {
<AppBase config={getInitialConfig({ id: uuidV4(), purpose: '', close: { visible: true } })} />
</div>
);
root.store.dispatch(setConnected(true));
});

// Act
Expand Down
Loading