Skip to content

Commit

Permalink
Add test for two terms history recording
Browse files Browse the repository at this point in the history
  • Loading branch information
Dayana Ilieva committed Apr 16, 2024
1 parent e1e09a7 commit 944610a
Show file tree
Hide file tree
Showing 9 changed files with 122 additions and 31 deletions.
5 changes: 3 additions & 2 deletions __tests__/feature/chat-closing.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ import { uuidV4 } from '../../src/utils';

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
6 changes: 3 additions & 3 deletions __tests__/feature/chat-history.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ let root;
const chatConfig = getInitialConfig({ id: uuidV4(), purpose: '', close: { visible: true } });
describe('Chat-history event works and visualizes items accordingly', () => {
const term = 'default'
const href = 'https://example.com/?utm_chat=' + term;
const search = '?utm_chat=default';
const href = `https://example.com/?utm_chat=${term}}`;
const search = `?utm_chat=${term}`;

beforeEach(async () => {
const mockLocation = {
Expand All @@ -54,6 +54,7 @@ describe('Chat-history event works and visualizes items accordingly', () => {
serverData.history = [];
serverData.errors = [];
serverData.region = faker.location.country();
serverData.term = term;
})
afterEach(localTearDown);

Expand Down Expand Up @@ -94,7 +95,6 @@ describe('Chat-history event works and visualizes items accordingly', () => {
time: new Date().getTime(),
content: contentMock.text,
}];
serverData.term = term;

// Act
act(() => {
Expand Down
5 changes: 2 additions & 3 deletions __tests__/feature/chat-streaming.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@ jest.useFakeTimers();

let root;
describe('AppBase, streaming events execute properly', () => {

const term = 'default'
const href = 'https://example.com/?utm_chat=' + term;
const search = '?utm_chat=' + term;
const href = `https://example.com/?utm_chat=${term}}`;
const search = `?utm_chat=${term}`;

beforeEach(async () => {
const mockLocation = {
Expand Down
10 changes: 5 additions & 5 deletions __tests__/feature/message-sending.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { localTearDown } from '../helpers';
import AppBase from '../../src/components/AppBase';
import renderWithProviders from '../../src/utils/storeMockWrapper';
import { serverSocket } from '../../__mocks__/socket.io-client';
import { Events, Roles, chat as getInitialConfig, initialMessage } from '../../src/config';
import { Events, Roles, chat as getInitialConfig, initialMessage, textInitial, textOnly } from '../../src/config';
import { TYPING_TIMEOUT } from '../../src/config/env';

const serverData = {
Expand All @@ -23,8 +23,8 @@ let root;

describe('Chat-history event works and visualizes items accordingly', () => {
const term = 'default'
const href = 'https://example.com/?utm_chat=' + term;
const search = '?utm_chat=' + term;
const href = `https://example.com/?utm_chat=${term}}`;
const search = `?utm_chat=${term}`;

beforeEach(async () => {
const mockLocation = {
Expand Down Expand Up @@ -53,7 +53,7 @@ describe('Chat-history event works and visualizes items accordingly', () => {
act(() => {
root = renderWithProviders(
<div id="chatbot-container">
<AppBase config={getInitialConfig({ id: uuidV4(), purpose: '', close: { visible: true } })} />
<AppBase config={getInitialConfig({ id: uuidV4(), purpose: '', close: { visible: true }, configuredMessage: textOnly })} />
</div>
);
});
Expand Down Expand Up @@ -95,7 +95,7 @@ describe('Chat-history event works and visualizes items accordingly', () => {
act(() => {
root = renderWithProviders(
<div id="chatbot-container">
<AppBase config={getInitialConfig({ id: uuidV4(), purpose: '', close: { visible: true } })} />
<AppBase config={getInitialConfig({ id: uuidV4(), purpose: '', close: { visible: true }, configuredMessage: textOnly })} />
</div>
);
});
Expand Down
6 changes: 3 additions & 3 deletions __tests__/feature/new-rows.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ jest.useFakeTimers();
let root;
describe('Having special symbols \n \r or <br> makes separates items into new rows', () => {
const term = 'default'
const href = 'https://example.com/?utm_chat=' + term;
const search = '?utm_chat=' + term;

const href = `https://example.com/?utm_chat=${term}}`;
const search = `?utm_chat=${term}`;
beforeEach(async () => {
const mockLocation = {
href,
Expand Down
6 changes: 3 additions & 3 deletions __tests__/feature/special-messaging.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ jest.useFakeTimers();
let root;
describe('Special messages are hidden and elements depending on them are visualized accordingly', () => {
const term = 'default'
const href = 'https://example.com/?utm_chat=' + term;
const search = '?utm_chat=' + term;

const href = `https://example.com/?utm_chat=${term}}`;
const search = `?utm_chat=${term}`;
beforeEach(async () => {
const mockLocation = {
href,
Expand Down
88 changes: 88 additions & 0 deletions __tests__/feature/term-differ.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/* eslint-env jest */
import { act } from 'react-dom/test-utils';

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

const textProbe = () => ({ 'type': 'text', 'text': faker.lorem.text(), 'sequence': 2 });
const contentMock = {
text: [textProbe(), textProbe()],
email: [
textProbe(),
{ 'type': 'email', 'email': faker.lorem.text(), 'sequence': 2 },
],
payment: [
textProbe(),
{ 'type': 'payment', 'payment': faker.lorem.text(), 'sequence': 2 },
],
};

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

jest.useFakeTimers();

let root;

describe('Multiple terms are being saved', () => {
const term = 'default'
const href = `https://example.com/?utm_chat=${term}}`;
const search = `?utm_chat=${term}`;

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;
})
afterEach(localTearDown);

test('on preloaded state with history to save the old one and create new record', async () => {
const previousTerm = faker.lorem.word();
// Arrange
act(() => {
root = renderWithProviders(
<div id="chatbot-container">
<AppBase config={getInitialConfig({ id: uuidV4(), purpose: '', close: { visible: true } })} />
</div>,
{ ...initialState, chat: { ...initialState.chat, record: { [previousTerm]: { historyIds: [], historyData: {} } } } }
);
});

serverData.history = [{
id: uuidV4(),
role: Roles.assistant,
time: new Date().getTime(),
content: contentMock.text,
}];

// Act
act(() => {
serverSocket.emit(Events.chatHistory, serverData);
jest.advanceTimersByTime((initialMessage.length * 1000) * initialMessage.length - 1);
});

// Assert
const { chat } = root.store.getState();
expect(chat.record[previousTerm].historyIds).toBeDefined();
expect(chat.record[term].historyIds).toBeDefined();
});
});
23 changes: 12 additions & 11 deletions src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@ import { SPECIAL_MERCHANT, SPECIAL_SUPPORT_TICKET } from './env';
export { Events, Roles } from './enums';
export { config } from './socket';

export const textOnly = [{
id: uuidV4(),
role: Roles.assistant,
time: new Date().getTime(),
content: [
{ 'type': 'text', 'text': 'Do you want to lose weight?', 'sequence': 2 },
]
}];

export const initialMessage = [
{
id: uuidV4(),
Expand Down Expand Up @@ -35,18 +44,10 @@ export const initialMessage = [
{ 'type': 'buttons', 'sequence': 1, 'buttons': [{ 'value': 'Yes', 'sequence': 1, 'text': 'Yes' }, { 'value': 'No', 'sequence': 2, 'text': 'No' }] }
]
},
{
id: uuidV4(),
role: Roles.assistant,
time: new Date().getTime(),
content: [
{ 'type': 'text', 'text': 'Do you want to lose weight?', 'sequence': 2 },
]
},
];
interface InitiationConfig { id: string, purpose?: string, close: { href?: string, visible?: boolean } }
interface InitiationConfig { id: string, purpose?: string, close: { href?: string, visible?: boolean }, configuredMessage: Array<any> }

export const chat = ({ id, purpose, close }: InitiationConfig) => ({
export const chat = ({ id, purpose, close, configuredMessage }: InitiationConfig) => ({
meta: {
cid: localStorage.getItem('__cid') || id,
systemType: 'test',
Expand All @@ -72,7 +73,7 @@ export const chat = ({ id, purpose, close }: InitiationConfig) => ({
role: 'AI-powered nutritionist',
imgSrc: 'https://storage.1forfit.com/lGbeX4lzNpWGyywHuJxMdegZ6My040jsvtVwZqBv.png',
welcome: 'Welcome to our live support. We\'re here to understand your requirements and suggest the best Keto diet suited for you.',
initialMessage
initialMessage: configuredMessage || initialMessage
},
chatUrl: 'https://chat-ws.test',
purpose: purpose || 'default',
Expand Down
4 changes: 3 additions & 1 deletion src/utils/storeMockWrapper.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import configReducer from '../store/slices/config';
import intentionsReducer from '../store/slices/intentions';
import metaReducer from '../store/slices/meta';
import { uuidV4 } from '.';
import initialState from '../store/initialState';

function renderWithProviders(ui) {
function renderWithProviders(ui, preloadedState=initialState) {
const store = configureStore({
reducer: {
meta: metaReducer,
Expand All @@ -21,6 +22,7 @@ function renderWithProviders(ui) {
middleware:
(getDefaultMiddleware) => getDefaultMiddleware({ serializableCheck: false })
.concat(chatMiddleware, intentionsMiddleware),
preloadedState
});

const Wrapper = ({ children }) => <Provider store={store}>{children}</Provider>;
Expand Down

0 comments on commit 944610a

Please sign in to comment.