-
Notifications
You must be signed in to change notification settings - Fork 0
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
Dayana Ilieva
committed
Mar 26, 2024
1 parent
c223e8d
commit a530037
Showing
9 changed files
with
403 additions
and
164 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,219 @@ | ||
/* 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 { setConnected } from '../../src/store/slices/chat'; | ||
import { serverSocket } from '../../__mocks__/socket.io-client'; | ||
import { Events, Roles, chat as getInitialConfig, initialMessage } from '../../src/config'; | ||
import { splitIntoRowChunks } from '../../src/utils/formatting'; | ||
|
||
|
||
const textProbeWithTwoRows = (separator = '\n') => ({ 'type': 'text', 'text': `${faker.lorem.word()}${separator}${faker.lorem.word()}`, 'sequence': 2 }); | ||
|
||
const serverData = { | ||
"region": faker.location.country(), | ||
"history": [], | ||
"errors": [], | ||
} | ||
|
||
jest.useFakeTimers(); | ||
|
||
let root; | ||
describe('Having special symbols \n \r or <br> makes separates items into new rows', () => { | ||
beforeEach(() => { | ||
serverData.history = []; | ||
serverData.errors = []; | ||
serverData.region = faker.location.country(); | ||
}); | ||
|
||
afterEach(localTearDown); | ||
|
||
test('having \n returns multiple elements in last message', async () => { | ||
act(() => { | ||
root = renderWithProviders( | ||
<div id="chatbot-container"> | ||
<AppBase config={getInitialConfig({ id: uuidV4(), purpose: '', close: { visible: true } })} /> | ||
</div> | ||
); | ||
root.store.dispatch(setConnected(true)); | ||
}); | ||
|
||
serverData.history = [{ | ||
id: uuidV4(), | ||
role: Roles.assistant, | ||
time: new Date().getTime(), | ||
content: [textProbeWithTwoRows()], | ||
}]; | ||
|
||
// Act | ||
act(() => { | ||
serverSocket.emit(Events.chatHistory, serverData); | ||
jest.advanceTimersByTime((initialMessage.length * 1000) * initialMessage.length - 1); | ||
}); | ||
|
||
// Assert | ||
expect(root.container.querySelector('[data-e2e="assistant-text"] span').children.length) | ||
.toBe(splitIntoRowChunks(textProbeWithTwoRows().text).length); | ||
expect(root.container.querySelector('[data-e2e="assistant-text"] span').children.length) | ||
.toBe(2); | ||
}); | ||
|
||
test('having \r\n returns multiple elements in last message', async () => { | ||
const separator = '\r\n'; | ||
act(() => { | ||
root = renderWithProviders( | ||
<div id="chatbot-container"> | ||
<AppBase config={getInitialConfig({ id: uuidV4(), purpose: '', close: { visible: true } })} /> | ||
</div> | ||
); | ||
root.store.dispatch(setConnected(true)); | ||
}); | ||
|
||
serverData.history = [{ | ||
id: uuidV4(), | ||
role: Roles.assistant, | ||
time: new Date().getTime(), | ||
content: [textProbeWithTwoRows(separator)], | ||
}]; | ||
|
||
// Act | ||
act(() => { | ||
serverSocket.emit(Events.chatHistory, serverData); | ||
jest.advanceTimersByTime((initialMessage.length * 1000) * initialMessage.length - 1); | ||
}); | ||
|
||
// Assert | ||
expect(root.container.querySelector('[data-e2e="assistant-text"] span').children.length) | ||
.toBe(splitIntoRowChunks(textProbeWithTwoRows(separator).text).length); | ||
expect(root.container.querySelector('[data-e2e="assistant-text"] span').children.length) | ||
.toBe(2); | ||
}); | ||
|
||
test('having <br> returns multiple elements in last message', async () => { | ||
const separator = '<br>'; | ||
act(() => { | ||
root = renderWithProviders( | ||
<div id="chatbot-container"> | ||
<AppBase config={getInitialConfig({ id: uuidV4(), purpose: '', close: { visible: true } })} /> | ||
</div> | ||
); | ||
root.store.dispatch(setConnected(true)); | ||
}); | ||
|
||
serverData.history = [{ | ||
id: uuidV4(), | ||
role: Roles.assistant, | ||
time: new Date().getTime(), | ||
content: [textProbeWithTwoRows(separator)], | ||
}]; | ||
|
||
// Act | ||
act(() => { | ||
serverSocket.emit(Events.chatHistory, serverData); | ||
jest.advanceTimersByTime((initialMessage.length * 1000) * initialMessage.length - 1); | ||
}); | ||
|
||
// Assert | ||
expect(root.container.querySelector('[data-e2e="assistant-text"] span').children.length) | ||
.toBe(splitIntoRowChunks(textProbeWithTwoRows(separator).text).length); | ||
expect(root.container.querySelector('[data-e2e="assistant-text"] span').children.length) | ||
.toBe(2); | ||
}); | ||
|
||
|
||
test('having <br/> returns multiple elements in last message', async () => { | ||
const separator = '<br/>'; | ||
act(() => { | ||
root = renderWithProviders( | ||
<div id="chatbot-container"> | ||
<AppBase config={getInitialConfig({ id: uuidV4(), purpose: '', close: { visible: true } })} /> | ||
</div> | ||
); | ||
root.store.dispatch(setConnected(true)); | ||
}); | ||
|
||
serverData.history = [{ | ||
id: uuidV4(), | ||
role: Roles.assistant, | ||
time: new Date().getTime(), | ||
content: [textProbeWithTwoRows(separator)], | ||
}]; | ||
|
||
// Act | ||
act(() => { | ||
serverSocket.emit(Events.chatHistory, serverData); | ||
jest.advanceTimersByTime((initialMessage.length * 1000) * initialMessage.length - 1); | ||
}); | ||
|
||
// Assert | ||
expect(root.container.querySelector('[data-e2e="assistant-text"] span').children.length) | ||
.toBe(splitIntoRowChunks(textProbeWithTwoRows(separator).text).length); | ||
expect(root.container.querySelector('[data-e2e="assistant-text"] span').children.length) | ||
.toBe(2); | ||
}); | ||
|
||
test('having \\r\\n returns multiple elements in last message', async () => { | ||
const separator = '\\r\\n'; | ||
act(() => { | ||
root = renderWithProviders( | ||
<div id="chatbot-container"> | ||
<AppBase config={getInitialConfig({ id: uuidV4(), purpose: '', close: { visible: true } })} /> | ||
</div> | ||
); | ||
root.store.dispatch(setConnected(true)); | ||
}); | ||
|
||
serverData.history = [{ | ||
id: uuidV4(), | ||
role: Roles.assistant, | ||
time: new Date().getTime(), | ||
content: [textProbeWithTwoRows(separator)], | ||
}]; | ||
|
||
// Act | ||
act(() => { | ||
serverSocket.emit(Events.chatHistory, serverData); | ||
jest.advanceTimersByTime((initialMessage.length * 1000) * initialMessage.length - 1); | ||
}); | ||
|
||
// Assert | ||
expect(root.container.querySelector('[data-e2e="assistant-text"] span').children.length) | ||
.toBe(splitIntoRowChunks(textProbeWithTwoRows(separator).text).length); | ||
expect(root.container.querySelector('[data-e2e="assistant-text"] span').children.length) | ||
.toBe(2); | ||
}); | ||
|
||
test('no separator returns single element in last message', async () => { | ||
const separator = ''; | ||
act(() => { | ||
root = renderWithProviders( | ||
<div id="chatbot-container"> | ||
<AppBase config={getInitialConfig({ id: uuidV4(), purpose: '', close: { visible: true } })} /> | ||
</div> | ||
); | ||
root.store.dispatch(setConnected(true)); | ||
}); | ||
|
||
serverData.history = [{ | ||
id: uuidV4(), | ||
role: Roles.assistant, | ||
time: new Date().getTime(), | ||
content: [textProbeWithTwoRows(separator)], | ||
}]; | ||
|
||
// Act | ||
act(() => { | ||
serverSocket.emit(Events.chatHistory, serverData); | ||
jest.advanceTimersByTime((initialMessage.length * 1000) * initialMessage.length - 1); | ||
}); | ||
|
||
// Assert | ||
expect(root.container.querySelector('[data-e2e="assistant-text"] span').children.length) | ||
.toBe(splitIntoRowChunks(textProbeWithTwoRows(separator).text).length); | ||
expect(root.container.querySelector('[data-e2e="assistant-text"] span').children.length) | ||
.toBe(1); | ||
}); | ||
}); |
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
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
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