Skip to content

Commit

Permalink
Changes for QuestionInput test
Browse files Browse the repository at this point in the history
  • Loading branch information
Harmanpreet Kaur committed Sep 24, 2024
1 parent 906dc6b commit 31cbc41
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 20 deletions.
15 changes: 15 additions & 0 deletions ResearchAssistant/App/frontend/jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,21 @@ const config: Config.InitialOptions = {
// globals: {
// IS_REACT_ACT_ENVIRONMENT: true,
// }
// Collect coverage
collectCoverage: true,

// Directory for coverage reports
coverageDirectory: 'coverage',

// Enforce coverage thresholds
coverageThreshold: {
global: {
branches: 80,
functions: 80,
lines: 80,
statements: 80,
}
}
}

export default config
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
import { render, screen, fireEvent } from '@testing-library/react';
import { QuestionInput } from './QuestionInput';
import { renderWithContext, mockDispatch,defaultMockState } from '../../test/test.utils';

const mockOnSend = jest.fn();
const documentSectionData = [
{ title: 'Introduction', content: 'This is the introduction section.', metaPrompt: 'Meta for Introduction' },
{ title: 'Methods', content: 'Methods content here.', metaPrompt: 'Meta for Methods' }
];

jest.mock('../../state/AppProvider', () => ({
AppStateContext: {
state: {
documentSections: [],
researchTopic: '',
showInitialChatMessage: true,
sidebarSelection: null,
},
dispatch: jest.fn(),
},
}));
const renderComponent = (props = {}) => {
return renderWithContext(
<QuestionInput
onSend={mockOnSend}
disabled={false}
{...props}
/>
);
};

describe('QuestionInput Component', () => {
afterEach(() => {
jest.clearAllMocks();
});
afterEach(() => {
jest.clearAllMocks();
});

test('renders correctly with placeholder', () => {
render(<QuestionInput onSend={mockOnSend} disabled={false} placeholder="Ask a question" />);
Expand Down Expand Up @@ -77,9 +80,67 @@ describe('QuestionInput Component', () => {
//expect(screen.getByTestId('send-icon')).toBeInTheDocument()
})

test('send button shows Send SVG when enabled', () => {
render(<QuestionInput onSend={mockOnSend} disabled={false} />)
// expect(screen.getByAltText('Send Button')).toBeInTheDocument()
})
it("should call sendQuestion on Enter key press", () => {
const { getByRole } = renderComponent();

const input = getByRole("textbox");

fireEvent.change(input, { target: { value: "Test question" } });
fireEvent.keyDown(input, { key: "Enter", code: "Enter" });

expect(mockOnSend).toHaveBeenCalledWith("Test question");
});

it("should not call sendQuestion on other key press via onKeyDown", () => {
const { getByRole } = renderComponent();

const input = getByRole("textbox");

fireEvent.change(input, { target: { value: "Test question" } });
fireEvent.keyDown(input, { key: "a", code: "KeyA" });

expect(mockOnSend).not.toHaveBeenCalled();
});


it("should not call sendQuestion if input is empty", () => {
const { getByRole } = renderComponent();

const input = getByRole("textbox");
fireEvent.change(input, { target: { value: "" } });
fireEvent.keyDown(input, { key: "Enter", code: "Enter" });

expect(mockOnSend).not.toHaveBeenCalled();
});

it("should not call sendQuestion if disabled", () => {
const { getByRole } = renderComponent({ disabled: true });

const input = getByRole("textbox");
fireEvent.change(input, { target: { value: "Test question" } });
fireEvent.keyDown(input, { key: "Enter", code: "Enter" });

expect(mockOnSend).not.toHaveBeenCalled();
});
it("should set the initial question and dispatch when showInitialChatMessage is true", () => {
// Mock the initial state with showInitialChatMessage as true and a research topic
const mockState = {
...defaultMockState,
showInitialChatMessage: true,
researchTopic: "Test Research Topic"
};

const { getByRole } = renderWithContext(<QuestionInput onSend={mockOnSend} disabled={false} />, mockState);

// The input box should now contain the lowercased research topic
const input = getByRole("textbox");
expect(input).toHaveValue("test research topic"); // researchTopic.toLowerCase()

// Verify that dispatch was called to reset the showInitialChatMessage flag
expect(mockDispatch).toHaveBeenCalledWith({ type: 'SET_SHOW_INITIAL_CHAT_MESSAGE_FLAG', payload: false });
});




})
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export const QuestionInput = ({ onSend, disabled, placeholder, clearOnSend, conv
tabIndex={0}
aria-label="Ask question button"
onClick={sendQuestion}
onKeyDown={e => e.key === "Enter" || e.key === " " ? sendQuestion() : null}
onKeyDown={e => e.key === "Enter" ? sendQuestion() : null}
>
{ sendQuestionDisabled ?
<SendRegular className={styles.questionInputSendButtonDisabled}/>
Expand Down
3 changes: 2 additions & 1 deletion ResearchAssistant/App/frontend/src/state/AppProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ const initialState: AppState = {
articlesChat: null,
grantsChat: null,
frontendSettings: null,
documentSections: JSON.parse(JSON.stringify(documentSectionData)),
//documentSections: JSON.parse(JSON.stringify(documentSectionData)),
documentSections: documentSectionData,
researchTopic: '',
favoritedCitations: [],
isSidebarExpanded: false,
Expand Down
52 changes: 52 additions & 0 deletions ResearchAssistant/App/frontend/src/test/test.utils.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import React from 'react';
import { render, RenderResult } from '@testing-library/react';
import { AppStateContext } from '../state/AppProvider';
import { Conversation, ChatMessage } from '../api/models';

// Default mock state
const defaultMockState = {
currentChat: null,
articlesChat: null,
grantsChat: null,
frontendSettings: null,
documentSections: null,
researchTopic: "Test topic",
favoritedCitations: [],
isSidebarExpanded: false,
isChatViewOpen: true,
sidebarSelection: null,
showInitialChatMessage: false,
};

const mockDispatch = jest.fn();

// Create a custom render function
const renderWithContext = (
component: React.ReactElement,
contextState = {}
): RenderResult => {
const state = { ...defaultMockState, ...contextState };
return render(
<AppStateContext.Provider value={{ state, dispatch: mockDispatch }}>
{component}
</AppStateContext.Provider>
);
};

// Mocked conversation and chat message
const mockChatMessage: ChatMessage = {
id: 'msg1',
role: 'user',
content: 'Test message content',
date: new Date().toISOString(),
};

const mockConversation: Conversation = {
id: '1',
title: 'Test Conversation',
messages: [mockChatMessage],
date: new Date().toISOString(),
};

export { defaultMockState, renderWithContext, mockDispatch, mockChatMessage, mockConversation };
export * from '@testing-library/react';

0 comments on commit 31cbc41

Please sign in to comment.