Skip to content

Commit

Permalink
fix failed tests
Browse files Browse the repository at this point in the history
Signed-off-by: Yulong Ruan <[email protected]>
  • Loading branch information
ruanyl committed Dec 5, 2023
1 parent 46e7b23 commit 6bd36e0
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 10 deletions.
17 changes: 11 additions & 6 deletions public/tabs/chat/chat_page.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import { render, screen, fireEvent, waitFor } from '@testing-library/react';
import { coreMock } from '../../../../../src/core/public/mocks';
import { SessionLoadService } from '../../services/session_load_service';
import { ChatPage } from './chat_page';
import * as contextExports from '../../contexts';
import * as hookExports from '../../hooks';
import * as chatContextExports from '../../contexts/chat_context';
import * as coreContextExports from '../../contexts/core_context';
import * as hookExports from '../../hooks/use_chat_state';

jest.mock('./controls/chat_input_controls', () => {
return { ChatInputControls: () => <div /> };
Expand All @@ -32,13 +33,14 @@ describe('<ChatPage />', () => {
createdTimeMs: new Date().getTime(),
updatedTimeMs: new Date().getTime(),
messages: [],
interactions: [],
});
const sessionLoadService = new SessionLoadService(coreMock.createStart().http);

beforeEach(() => {
jest.spyOn(sessionLoadService, 'load').mockImplementation(loadMock);

jest.spyOn(contextExports, 'useChatContext').mockReturnValue({
jest.spyOn(chatContextExports, 'useChatContext').mockReturnValue({
sessionId: 'mocked_session_id',
chatEnabled: true,
});
Expand All @@ -48,7 +50,7 @@ describe('<ChatPage />', () => {
chatState: { messages: [], llmResponding: false },
});

jest.spyOn(contextExports, 'useCore').mockReturnValue({
jest.spyOn(coreContextExports, 'useCore').mockReturnValue({
services: {
sessionLoad: sessionLoadService,
},
Expand All @@ -65,12 +67,15 @@ describe('<ChatPage />', () => {

expect(loadMock).toHaveBeenCalledWith('mocked_session_id');
await waitFor(() => {
expect(dispatchMock).toHaveBeenCalledWith({ type: 'receive', payload: [] });
expect(dispatchMock).toHaveBeenCalledWith({
type: 'receive',
payload: { messages: [], interactions: [] },
});
});
});

it('should NOT call reload if current conversation is not set', async () => {
jest.spyOn(contextExports, 'useChatContext').mockReturnValue({
jest.spyOn(chatContextExports, 'useChatContext').mockReturnValue({
sessionId: undefined,
chatEnabled: true,
});
Expand Down
4 changes: 2 additions & 2 deletions public/tabs/chat/controls/chat_input_controls.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import React from 'react';
import { render, screen, fireEvent, waitFor, getByRole } from '@testing-library/react';

import { ChatInputControls } from './chat_input_controls';
import * as contextExports from '../../../contexts';
import * as hookExports from '../../../hooks';
import * as contextExports from '../../../contexts/chat_context';
import * as hookExports from '../../../hooks/use_chat_actions';

describe('<ChatInputControls />', () => {
const sendMock = jest.fn();
Expand Down
2 changes: 0 additions & 2 deletions test/setup.jest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@

import { configure } from '@testing-library/react';
import { TextDecoder, TextEncoder } from 'util';
import 'web-streams-polyfill';
import '@testing-library/jest-dom';
import '../server/fetch-polyfill';

configure({ testIdAttribute: 'data-test-subj' });

Expand Down

0 comments on commit 6bd36e0

Please sign in to comment.