Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add missing unit tests #90

Merged
merged 3 commits into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions public/contexts/__tests__/chat_context.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

import React from 'react';
import { renderHook } from '@testing-library/react-hooks';
import { useChatContext, ChatContext } from '../chat_context';

describe('useChatContext', () => {
it('should return chat context after useChatContext called', () => {
const chatContextValueMock = {
setSessionId: jest.fn(),
selectedTabId: 'chat' as const,
setSelectedTabId: jest.fn(),
flyoutVisible: true,
flyoutFullScreen: true,
setFlyoutVisible: jest.fn(),
setFlyoutComponent: jest.fn(),
userHasAccess: true,
contentRenderers: {},
actionExecutors: {},
currentAccount: { username: 'foo', tenant: '' },
setTitle: jest.fn(),
setTraceId: jest.fn(),
};
const { result } = renderHook(useChatContext, {
wrapper: ({ children }) => (
<ChatContext.Provider value={chatContextValueMock}>{children}</ChatContext.Provider>
),
});

expect(result.current).toBe(chatContextValueMock);
});

it('should return error if context provider missed', () => {
const { result } = renderHook(useChatContext);

expect(result.error).toMatchInlineSnapshot(`[Error: ChatContext is not set]`);
});
});
19 changes: 18 additions & 1 deletion public/tabs/chat/messages/message_content.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,14 @@ jest.mock('../../../components/core_visualization', () => {

describe('<MessageContent />', () => {
const pplVisualizationRenderMock = jest.fn();
const customizedRenderMock = jest.fn();

beforeEach(() => {
jest.spyOn(chatContextExports, 'useChatContext').mockReturnValue({
contentRenderers: { ppl_visualization: pplVisualizationRenderMock },
contentRenderers: {
ppl_visualization: pplVisualizationRenderMock,
customized_content_type: customizedRenderMock,
},
});
});

Expand Down Expand Up @@ -87,4 +91,17 @@ describe('<MessageContent />', () => {
);
expect(pplVisualizationRenderMock).toHaveBeenCalledWith({ query: 'mock ppl query' });
});

it('should render customized render content', () => {
render(
<MessageContent
message={{
type: 'output',
contentType: 'customized_content_type',
content: 'mock customized content',
}}
/>
);
expect(customizedRenderMock).toHaveBeenCalledWith('mock customized content');
});
});
160 changes: 160 additions & 0 deletions public/utils/tests/notebook.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

import * as uuidExports from 'uuid';
import { convertMessagesToParagraphs } from '../notebook';

jest.mock('uuid', () => ({
...jest.requireActual('uuid'),
__esModule: true,
}));

describe('notebook convertMessagesToParagraphs utils', () => {
beforeAll(() => {
jest.useFakeTimers();
jest.setSystemTime(new Date(2023, 11, 26));
jest.spyOn(uuidExports, 'v4').mockReturnValue('a-fixed-uuid');
});
afterAll(() => {
jest.useRealTimers();
jest.spyOn(uuidExports, 'v4').mockRestore();
});
it('should return consistent paragraphs if contentType is text', () => {
expect(
convertMessagesToParagraphs([{ type: 'input', contentType: 'text', content: 'bar' }], 'foo')
).toMatchInlineSnapshot(`
Array [
Object {
"dateCreated": "2023-12-26T00:00:00.000Z",
"dateModified": "2023-12-26T00:00:00.000Z",
"id": "paragraph_a-fixed-uuid",
"input": Object {
"inputText": "%md
foo: bar",
"inputType": "MARKDOWN",
},
"output": Array [
Object {
"execution_time": "0 ms",
"outputType": "MARKDOWN",
"result": "foo: bar",
},
],
},
]
`);
});

it('should return consistent paragraphs if contentType is markdown', () => {
expect(
convertMessagesToParagraphs(
[{ type: 'output', contentType: 'markdown', content: 'bar' }],
'foo'
)
).toMatchInlineSnapshot(`
Array [
Object {
"dateCreated": "2023-12-26T00:00:00.000Z",
"dateModified": "2023-12-26T00:00:00.000Z",
"id": "paragraph_a-fixed-uuid",
"input": Object {
"inputText": "%md
OpenSearch Assistant: bar",
"inputType": "MARKDOWN",
},
"output": Array [
Object {
"execution_time": "0 ms",
"outputType": "MARKDOWN",
"result": "OpenSearch Assistant: bar",
},
],
},
]
`);
});

it('should return consistent paragraphs if contentType is error', () => {
expect(
convertMessagesToParagraphs([{ type: 'output', contentType: 'error', content: 'bar' }], 'foo')
).toMatchInlineSnapshot(`
Array [
Object {
"dateCreated": "2023-12-26T00:00:00.000Z",
"dateModified": "2023-12-26T00:00:00.000Z",
"id": "paragraph_a-fixed-uuid",
"input": Object {
"inputText": "%md
OpenSearch Assistant: bar",
"inputType": "MARKDOWN",
},
"output": Array [
Object {
"execution_time": "0 ms",
"outputType": "MARKDOWN",
"result": "OpenSearch Assistant: bar",
},
],
},
]
`);
});

it('should return consistent paragraphs if contentType is visualization', () => {
expect(
convertMessagesToParagraphs(
[{ type: 'output', contentType: 'visualization', content: 'bar' }],
'foo'
)
).toMatchInlineSnapshot(`
Array [
Object {
"dateCreated": "2023-12-26T00:00:00.000Z",
"dateModified": "2023-12-26T00:00:00.000Z",
"id": "paragraph_a-fixed-uuid",
"input": Object {
"inputText": "{\\"viewMode\\":\\"view\\",\\"panels\\":{\\"1\\":{\\"gridData\\":{\\"x\\":0,\\"y\\":0,\\"w\\":50,\\"h\\":20,\\"i\\":\\"1\\"},\\"type\\":\\"visualization\\",\\"explicitInput\\":{\\"id\\":\\"1\\",\\"savedObjectId\\":\\"bar\\"}}},\\"isFullScreenMode\\":false,\\"filters\\":[],\\"useMargins\\":false,\\"id\\":\\"random_html_id\\",\\"timeRange\\":{\\"to\\":\\"now\\",\\"from\\":\\"now-15m\\"},\\"title\\":\\"embed_viz_random_html_id\\",\\"query\\":{\\"query\\":\\"\\",\\"language\\":\\"lucene\\"},\\"refreshConfig\\":{\\"pause\\":true,\\"value\\":15}}",
"inputType": "VISUALIZATION",
},
"output": Array [
Object {
"execution_time": "0 ms",
"outputType": "VISUALIZATION",
"result": "",
},
],
},
]
`);
});

it('should return consistent paragraphs if contentType is not supported', () => {
expect(
convertMessagesToParagraphs(
[{ type: 'output', contentType: 'not-supported', content: 'bar' }],
'foo'
)
).toMatchInlineSnapshot(`
Array [
Object {
"dateCreated": "2023-12-26T00:00:00.000Z",
"dateModified": "2023-12-26T00:00:00.000Z",
"id": "paragraph_a-fixed-uuid",
"input": Object {
"inputText": "",
"inputType": "",
},
"output": Array [
Object {
"execution_time": "0 ms",
"outputType": "",
"result": "",
},
],
},
]
`);
});
});
Loading