From 7578827afe8eaef71561b019deac8ca33d268722 Mon Sep 17 00:00:00 2001 From: Lin Wang Date: Fri, 29 Dec 2023 10:39:11 +0800 Subject: [PATCH] test: add unit tests for chat context Signed-off-by: Lin Wang --- .../contexts/__tests__/chat_context.test.tsx | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 public/contexts/__tests__/chat_context.test.tsx diff --git a/public/contexts/__tests__/chat_context.test.tsx b/public/contexts/__tests__/chat_context.test.tsx new file mode 100644 index 00000000..0616a301 --- /dev/null +++ b/public/contexts/__tests__/chat_context.test.tsx @@ -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 }) => ( + {children} + ), + }); + + 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]`); + }); +});