Update chat view tests to use new event handlers

This commit is contained in:
2026-05-06 17:21:12 +02:00
parent fd49abcdb9
commit aa8e45912a
+33 -16
View File
@@ -261,7 +261,9 @@ describe('ChatView', () => {
await (view as any).handleUserInput('test'); await (view as any).handleUserInput('test');
expect(chatSpy).toHaveBeenCalled(); expect(chatSpy).toHaveBeenCalled();
expect(consoleSpy).toHaveBeenCalledWith('Error handling user input:', expect.any(Error)); expect(consoleSpy).toHaveBeenCalledWith(
'Ollama Plugin Error [ChatView.handleUserInput]: Network error'
);
}); });
it('should handle empty user input gracefully', async () => { it('should handle empty user input gracefully', async () => {
@@ -383,7 +385,9 @@ describe('ChatView', () => {
await (view as any).handleUserInput('test'); await (view as any).handleUserInput('test');
expect(chatSpy).toHaveBeenCalled(); expect(chatSpy).toHaveBeenCalled();
expect(consoleSpy).toHaveBeenCalledWith('Error handling user input:', expect.any(Error)); expect(consoleSpy).toHaveBeenCalledWith(
'Ollama Plugin Error [ChatView.handleUserInput]: Network error'
);
}); });
describe('event handlers', () => { describe('event handlers', () => {
@@ -391,9 +395,10 @@ describe('ChatView', () => {
view['sendButton'] = document.createElement('button'); view['sendButton'] = document.createElement('button');
view['inputEl'] = document.createElement('textarea'); view['inputEl'] = document.createElement('textarea');
(view['inputEl'] as HTMLTextAreaElement).value = 'test'; (view['inputEl'] as HTMLTextAreaElement).value = 'test';
(view as any).setupEventListeners();
const handleSpy = jest.spyOn(view as any, 'handleUserInput'); const handleSpy = jest.spyOn(view as any, 'handleUserInput');
const handler = view.getSendButtonClickHandler?.bind(view); const handler = view.getSendButtonClickHandler();
if (!handler) throw new Error('Handler not available'); if (!handler) throw new Error('Handler not available');
await handler(); await handler();
@@ -405,11 +410,13 @@ describe('ChatView', () => {
view['sendButton'] = document.createElement('button'); view['sendButton'] = document.createElement('button');
view['inputEl'] = document.createElement('textarea'); view['inputEl'] = document.createElement('textarea');
(view['inputEl'] as HTMLTextAreaElement).value = 'test'; (view['inputEl'] as HTMLTextAreaElement).value = 'test';
(view as any).setupEventListeners();
const handleSpy = jest.spyOn(view as any, 'handleUserInput'); const handleSpy = jest.spyOn(view as any, 'handleUserInput');
const event = new KeyboardEvent('keydown', { key: 'Enter' }) as any; const event = new KeyboardEvent('keydown', { key: 'Enter' }) as any;
const handler = view.getInputKeyDownHandler();
await (view as any).inputKeyDownHandler!(event); if (!handler) throw new Error('Handler not available');
await handler(event);
expect(handleSpy).toHaveBeenCalledWith('test'); expect(handleSpy).toHaveBeenCalledWith('test');
expect((view['inputEl'] as HTMLTextAreaElement).value).toBe(''); expect((view['inputEl'] as HTMLTextAreaElement).value).toBe('');
@@ -419,11 +426,13 @@ describe('ChatView', () => {
view['sendButton'] = document.createElement('button'); view['sendButton'] = document.createElement('button');
view['inputEl'] = document.createElement('textarea'); view['inputEl'] = document.createElement('textarea');
(view['inputEl'] as HTMLTextAreaElement).value = 'test'; (view['inputEl'] as HTMLTextAreaElement).value = 'test';
(view as any).setupEventListeners();
const handleSpy = jest.spyOn(view as any, 'handleUserInput'); const handleSpy = jest.spyOn(view as any, 'handleUserInput');
const event = new KeyboardEvent('keydown', { key: 'Enter', shiftKey: true }) as any; const event = new KeyboardEvent('keydown', { key: 'Enter', shiftKey: true }) as any;
const handler = view.getInputKeyDownHandler();
await (view as any).inputKeyDownHandler!(event); if (!handler) throw new Error('Handler not available');
await handler(event);
expect(handleSpy).not.toHaveBeenCalled(); expect(handleSpy).not.toHaveBeenCalled();
}); });
@@ -432,11 +441,12 @@ describe('ChatView', () => {
view['sendButton'] = document.createElement('button'); view['sendButton'] = document.createElement('button');
view['inputEl'] = document.createElement('textarea'); view['inputEl'] = document.createElement('textarea');
view['newChatButton'] = document.createElement('button'); view['newChatButton'] = document.createElement('button');
(view as any).setupEventListeners();
const clearSpy = jest.spyOn(view as any, 'clearConversation'); const clearSpy = jest.spyOn(view as any, 'clearConversation');
const handler = view.getNewChatButtonClickHandler?.bind(view); const handler = view.getNewChatButtonClickHandler();
if (!handler) throw new Error('Handler not available'); if (!handler) throw new Error('Handler not available');
await handler(); handler();
expect(clearSpy).toHaveBeenCalled(); expect(clearSpy).toHaveBeenCalled();
}); });
@@ -467,9 +477,10 @@ describe('ChatView', () => {
view['sendButton'] = document.createElement('button'); view['sendButton'] = document.createElement('button');
view['inputEl'] = document.createElement('textarea'); view['inputEl'] = document.createElement('textarea');
(view['inputEl'] as HTMLTextAreaElement).value = 'test'; (view['inputEl'] as HTMLTextAreaElement).value = 'test';
(view as any).setupEventListeners();
const handleSpy = jest.spyOn(view as any, 'handleUserInput'); const handleSpy = jest.spyOn(view as any, 'handleUserInput');
const handler = view.getSendButtonClickHandler?.bind(view); const handler = view.getSendButtonClickHandler();
if (!handler) throw new Error('Handler not available'); if (!handler) throw new Error('Handler not available');
await handler(); await handler();
@@ -481,11 +492,13 @@ describe('ChatView', () => {
view['sendButton'] = document.createElement('button'); view['sendButton'] = document.createElement('button');
view['inputEl'] = document.createElement('textarea'); view['inputEl'] = document.createElement('textarea');
(view['inputEl'] as HTMLTextAreaElement).value = 'test'; (view['inputEl'] as HTMLTextAreaElement).value = 'test';
(view as any).setupEventListeners();
const handleSpy = jest.spyOn(view as any, 'handleUserInput'); const handleSpy = jest.spyOn(view as any, 'handleUserInput');
const event = new KeyboardEvent('keydown', { key: 'Enter' }) as any; const event = new KeyboardEvent('keydown', { key: 'Enter' }) as any;
const handler = view.getInputKeyDownHandler();
await (view as any).inputKeyDownHandler!(event); if (!handler) throw new Error('Handler not available');
await handler(event);
expect(handleSpy).toHaveBeenCalledWith('test'); expect(handleSpy).toHaveBeenCalledWith('test');
expect((view['inputEl'] as HTMLTextAreaElement).value).toBe(''); expect((view['inputEl'] as HTMLTextAreaElement).value).toBe('');
@@ -495,11 +508,13 @@ describe('ChatView', () => {
view['sendButton'] = document.createElement('button'); view['sendButton'] = document.createElement('button');
view['inputEl'] = document.createElement('textarea'); view['inputEl'] = document.createElement('textarea');
(view['inputEl'] as HTMLTextAreaElement).value = 'test'; (view['inputEl'] as HTMLTextAreaElement).value = 'test';
(view as any).setupEventListeners();
const handleSpy = jest.spyOn(view as any, 'handleUserInput'); const handleSpy = jest.spyOn(view as any, 'handleUserInput');
const event = new KeyboardEvent('keydown', { key: 'Enter', shiftKey: true }) as any; const event = new KeyboardEvent('keydown', { key: 'Enter', shiftKey: true }) as any;
const handler = view.getInputKeyDownHandler();
await (view as any).inputKeyDownHandler!(event); if (!handler) throw new Error('Handler not available');
await handler(event);
expect(handleSpy).not.toHaveBeenCalled(); expect(handleSpy).not.toHaveBeenCalled();
}); });
@@ -508,10 +523,12 @@ describe('ChatView', () => {
view['sendButton'] = document.createElement('button'); view['sendButton'] = document.createElement('button');
view['inputEl'] = document.createElement('textarea'); view['inputEl'] = document.createElement('textarea');
view['newChatButton'] = document.createElement('button'); view['newChatButton'] = document.createElement('button');
(view as any).setupEventListeners();
const clearSpy = jest.spyOn(view as any, 'clearConversation'); const clearSpy = jest.spyOn(view as any, 'clearConversation');
const handler = view.getNewChatButtonClickHandler();
await (view as any).newChatButtonClickHandler!(); if (!handler) throw new Error('Handler not available');
handler();
expect(clearSpy).toHaveBeenCalled(); expect(clearSpy).toHaveBeenCalled();
}); });