From 0ad01079c7504bb464885736680f4506eb0ff3c2 Mon Sep 17 00:00:00 2001 From: Nicolas Brichet Date: Tue, 19 Mar 2024 12:19:15 +0100 Subject: [PATCH] Add tests on model and widget --- jest.config.js | 4 ++ src/__tests__/jupyter-chat.spec.ts | 9 ---- src/__tests__/model.spec.ts | 84 ++++++++++++++++++++++++++++++ src/__tests__/widgets.spec.ts | 35 +++++++++++++ src/components/chat.tsx | 11 ++-- src/widgets/chat-widget.tsx | 2 +- 6 files changed, 129 insertions(+), 16 deletions(-) delete mode 100644 src/__tests__/jupyter-chat.spec.ts create mode 100644 src/__tests__/model.spec.ts create mode 100644 src/__tests__/widgets.spec.ts diff --git a/jest.config.js b/jest.config.js index b0471e6..37c7c33 100644 --- a/jest.config.js +++ b/jest.config.js @@ -2,8 +2,12 @@ const jestJupyterLab = require('@jupyterlab/testutils/lib/jest-config'); const esModules = [ '@codemirror', + '@microsoft', + '@jupyter/react-components', + '@jupyter/web-components', '@jupyter/ydoc', '@jupyterlab/', + 'exenv-es6', 'lib0', 'nanoid', 'vscode-ws-jsonrpc', diff --git a/src/__tests__/jupyter-chat.spec.ts b/src/__tests__/jupyter-chat.spec.ts deleted file mode 100644 index 3f469ed..0000000 --- a/src/__tests__/jupyter-chat.spec.ts +++ /dev/null @@ -1,9 +0,0 @@ -/** - * Example of [Jest](https://jestjs.io/docs/getting-started) unit tests - */ - -describe('@jupyter/chat', () => { - it('should be tested', () => { - expect(1 + 1).toEqual(2); - }); -}); diff --git a/src/__tests__/model.spec.ts b/src/__tests__/model.spec.ts new file mode 100644 index 0000000..42a2759 --- /dev/null +++ b/src/__tests__/model.spec.ts @@ -0,0 +1,84 @@ +/** + * Example of [Jest](https://jestjs.io/docs/getting-started) unit tests + */ + +import { ChatModel, IChatModel } from '../model'; +import { IChatMessage, IMessage } from '../types'; + +describe('test chat model', () => { + describe('model instantiation', () => { + it('should create a ChatModel', () => { + const model = new ChatModel(); + expect(model).toBeInstanceOf(ChatModel); + }); + + it('should dispose a ChatModel', () => { + const model = new ChatModel(); + model.dispose() + expect(model.isDisposed).toBeTruthy(); + }); + }); + + describe('incoming message', () => { + class TestChat extends ChatModel { + protected formatChatMessage(message: IChatMessage): IChatMessage { + message.body = 'formatted msg'; + return message; + } + }; + + let model: IChatModel; + let messages: IMessage[]; + const msg = { + type: 'msg', + id: 'message1', + time: Date.now() / 1000, + body: 'message test', + sender: { id: 'user'} + } as IChatMessage; + + beforeEach(() => { + messages = []; + }) + + it('should signal incoming message', () => { + model = new ChatModel(); + model.incomingMessage.connect( + (sender: IChatModel, message: IMessage) => { + expect(sender).toBe(model); + messages.push(message); + } + ); + model.onMessage(msg); + expect(messages).toHaveLength(1); + expect(messages[0]).toBe(msg); + }); + + it('should format message', () => { + model = new TestChat(); + model.incomingMessage.connect( + (sender: IChatModel, message: IMessage) => { + expect(sender).toBe(model); + messages.push(message); + } + ); + model.onMessage({...msg} as IChatMessage); + expect(messages).toHaveLength(1); + expect(messages[0]).not.toBe(msg); + expect((messages[0] as IChatMessage).body).toBe('formatted msg'); + }) + + }) + + describe('model config', () => { + it('should have empty config', () => { + const model = new ChatModel(); + expect(model.config.sendWithShiftEnter).toBeUndefined(); + }); + + it('should allow config', () => { + const model = new ChatModel({config: {sendWithShiftEnter: true}}); + expect(model.config.sendWithShiftEnter).toBeTruthy(); + }); + }) +}); diff --git a/src/__tests__/widgets.spec.ts b/src/__tests__/widgets.spec.ts new file mode 100644 index 0000000..31997e7 --- /dev/null +++ b/src/__tests__/widgets.spec.ts @@ -0,0 +1,35 @@ +/** + * Example of [Jest](https://jestjs.io/docs/getting-started) unit tests + */ + +import { IRenderMimeRegistry, RenderMimeRegistry } from "@jupyterlab/rendermime"; +import { ChatModel, IChatModel } from "../model"; +import { ChatWidget } from "../widgets/chat-widget"; + +describe('test chat widget', () => { + let model: IChatModel; + let rmRegistry: IRenderMimeRegistry; + + beforeEach(() => { + model = new ChatModel(); + rmRegistry = new RenderMimeRegistry(); + }) + + describe('model instantiation', () => { + it('should create a ChatModel', () => { + const widget = new ChatWidget({ model, rmRegistry }); + expect(widget).toBeInstanceOf(ChatWidget); + }); + + it('should dispose a ChatModel', () => { + const widget = new ChatWidget({ model, rmRegistry }); + widget.dispose() + expect(widget.isDisposed).toBeTruthy(); + }); + + it('should provides the model', () => { + const widget = new ChatWidget({ model, rmRegistry }); + expect(widget.model).toBe(model); + }); + }); +}); diff --git a/src/components/chat.tsx b/src/components/chat.tsx index b929a9e..9010793 100644 --- a/src/components/chat.tsx +++ b/src/components/chat.tsx @@ -96,9 +96,8 @@ export function Chat(props: Chat.IOptions): JSX.Element { const [view, setView] = useState( props.chatView || Chat.ChatView.Chat ); - console.log('Instantiate a chat'); return ( - +