Skip to content

Commit

Permalink
Add tests on model and widget
Browse files Browse the repository at this point in the history
  • Loading branch information
brichet committed Mar 19, 2024
1 parent 8afcf83 commit 0ad0107
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 16 deletions.
4 changes: 4 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
9 changes: 0 additions & 9 deletions src/__tests__/jupyter-chat.spec.ts

This file was deleted.

84 changes: 84 additions & 0 deletions src/__tests__/model.spec.ts
Original file line number Diff line number Diff line change
@@ -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();
});
})
});
35 changes: 35 additions & 0 deletions src/__tests__/widgets.spec.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
});
11 changes: 5 additions & 6 deletions src/components/chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,8 @@ export function Chat(props: Chat.IOptions): JSX.Element {
const [view, setView] = useState<Chat.ChatView>(
props.chatView || Chat.ChatView.Chat
);
console.log('Instantiate a chat');
return (
<JlThemeProvider themeManager={props.themeManager}>
<JlThemeProvider themeManager={props.themeManager ?? null}>
<Box
// root box should not include padding as it offsets the vertical
// scrollbar to the left
Expand Down Expand Up @@ -152,14 +151,14 @@ export namespace Chat {
* The chat model.
*/
model: IChatModel;
/**
* The theme manager.
*/
themeManager: IThemeManager | null;
/**
* The rendermime registry.
*/
rmRegistry: IRenderMimeRegistry;
/**
* The theme manager.
*/
themeManager?: IThemeManager | null;
/**
* The view to render.
*/
Expand Down
2 changes: 1 addition & 1 deletion src/widgets/chat-widget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export class ChatWidget extends ReactWidget {
this.title.caption = 'Jupyter Chat'; // TODO: i18n

this._model = options.model;
this._themeManager = options.themeManager;
this._themeManager = options?.themeManager || null;
this._rmRegistry = options.rmRegistry;
}

Expand Down

0 comments on commit 0ad0107

Please sign in to comment.