Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Dayana Ilieva committed Dec 12, 2023
1 parent d912e16 commit 955cf46
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 5 deletions.
48 changes: 46 additions & 2 deletions __tests__/components/AppBase/index.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,51 @@ describe('AppBase, streaming events execute properly', () => {
});
});

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

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

Object.defineProperty(window, 'location', {
value: mockLocation,
writable: true,
enumerable: true,
});
await localSetup();
});

afterEach(localTearDown);

test('chat.closed is true after close button is clicked', async () => {
// Arrange
expect(window.location.search).toContain('?utm_chat=default');

const nodeElem = { remove: jest.fn() };
const querySelectorSpy = jest.spyOn(document, 'querySelector');
const ioCloseSpy = jest.spyOn(io.connect(), 'close');
spies.push(querySelectorSpy, ioCloseSpy);
querySelectorSpy.mockImplementation(() => nodeElem);

// Act
act(dispatchSocketChange);
const closeButton = root.container.querySelector('[data-e2e="chat-close-btn"]');
fireEvent.click(closeButton);

// Assert
const { chat } = root.store.getState();
expect(chat.closed).toBeTruthy();
expect(querySelectorSpy).toHaveBeenCalledWith('#chatbot-container');
expect(nodeElem.remove).toHaveBeenCalled();
expect(window.location.search).not.toContain(search);
expect(ioCloseSpy).toHaveBeenCalled();
});
});

function dispatchSocketChange(history = defaultHistory) {
serverSocket.emit('connect');
serverSocket.emit(events.chatHistory, { history, errors: [], region: 'frankfurt' });
Expand All @@ -236,12 +281,11 @@ function dispatchStreaming(chunks) {
async function localSetup() {
localStorage.setItem('__pd', JSON.stringify(pd));
spies.push(jest.spyOn(intent.core, 'emit'));

const intentOnSpy = jest.spyOn(intent.core, 'on');
spies.push(intentOnSpy);
intentOnSpy.mockReturnValue(() => true);
await waitFor(() => {
root = renderWithProviders(<AppBase config={ initialConfig } />);
root = renderWithProviders(<div id="chatbot-container"><AppBase config={ initialConfig } /></div>);
});
}

Expand Down
2 changes: 1 addition & 1 deletion dist/index.es.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.es.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index.umd.js.map

Large diffs are not rendered by default.

0 comments on commit 955cf46

Please sign in to comment.