|
| 1 | +/** |
| 2 | + * Unit tests for popup.js functionality |
| 3 | + * Tests the popup menu item click handlers that send messages to background script |
| 4 | + */ |
| 5 | + |
| 6 | +import { jest, setupTestMocks } from './helpers.js'; |
| 7 | +import { handlePopupMenuClick } from '../js/popup.js'; |
| 8 | + |
| 9 | +// Setup all test mocks |
| 10 | +setupTestMocks(); |
| 11 | + |
| 12 | +describe('Popup Menu Click Handlers', () => { |
| 13 | + beforeEach(() => { |
| 14 | + jest.clearAllMocks(); |
| 15 | + |
| 16 | + // Reset window location |
| 17 | + window.location.hash = ''; |
| 18 | + window.location.reload.mockClear(); |
| 19 | + chrome.runtime.sendMessage.mockClear(); |
| 20 | + }); |
| 21 | + |
| 22 | + test('handlePopupMenuClick with switch action sends correct message and reloads popup', async () => { |
| 23 | + // Setup: Mock successful response from background script |
| 24 | + const mockParams = 'action=switch&windowId=123&sessionName=TestSpace&tabId=456'; |
| 25 | + chrome.runtime.sendMessage.mockResolvedValue(mockParams); |
| 26 | + |
| 27 | + // Execute the click handler with switch action |
| 28 | + await handlePopupMenuClick('switch'); |
| 29 | + |
| 30 | + // Verify the message was sent with correct parameters |
| 31 | + expect(chrome.runtime.sendMessage).toHaveBeenCalledWith({ |
| 32 | + 'action': 'generatePopupParams', |
| 33 | + 'popupAction': 'switch' |
| 34 | + }); |
| 35 | + |
| 36 | + // Verify popup was reloaded with correct parameters |
| 37 | + expect(window.location.hash).toBe(mockParams); |
| 38 | + expect(window.location.reload).toHaveBeenCalled(); |
| 39 | + }); |
| 40 | + |
| 41 | + test('handlePopupMenuClick with move action sends correct message and reloads popup', async () => { |
| 42 | + // Setup: Mock successful response from background script |
| 43 | + const mockParams = 'action=move&windowId=123&sessionName=TestSpace&tabId=456'; |
| 44 | + chrome.runtime.sendMessage.mockResolvedValue(mockParams); |
| 45 | + |
| 46 | + // Execute the click handler with move action |
| 47 | + await handlePopupMenuClick('move'); |
| 48 | + |
| 49 | + // Verify the message was sent with correct parameters |
| 50 | + expect(chrome.runtime.sendMessage).toHaveBeenCalledWith({ |
| 51 | + 'action': 'generatePopupParams', |
| 52 | + 'popupAction': 'move' |
| 53 | + }); |
| 54 | + |
| 55 | + // Verify popup was reloaded with correct parameters |
| 56 | + expect(window.location.hash).toBe(mockParams); |
| 57 | + expect(window.location.reload).toHaveBeenCalled(); |
| 58 | + }); |
| 59 | + |
| 60 | + test('handlePopupMenuClick handles empty response gracefully', async () => { |
| 61 | + // Setup: Mock empty response from background script |
| 62 | + chrome.runtime.sendMessage.mockResolvedValue(''); |
| 63 | + |
| 64 | + // Execute the click handler |
| 65 | + await handlePopupMenuClick('test'); |
| 66 | + |
| 67 | + // Verify the message was sent |
| 68 | + expect(chrome.runtime.sendMessage).toHaveBeenCalledWith({ |
| 69 | + 'action': 'generatePopupParams', |
| 70 | + 'popupAction': 'test' |
| 71 | + }); |
| 72 | + |
| 73 | + // Verify popup was NOT reloaded due to empty response |
| 74 | + expect(window.location.hash).toBe(''); |
| 75 | + expect(window.location.reload).not.toHaveBeenCalled(); |
| 76 | + }); |
| 77 | + |
| 78 | + test('handlePopupMenuClick handles null response gracefully', async () => { |
| 79 | + // Setup: Mock null response from background script |
| 80 | + chrome.runtime.sendMessage.mockResolvedValue(null); |
| 81 | + |
| 82 | + // Execute the click handler |
| 83 | + await handlePopupMenuClick('move'); |
| 84 | + |
| 85 | + // Verify the message was sent |
| 86 | + expect(chrome.runtime.sendMessage).toHaveBeenCalledWith({ |
| 87 | + 'action': 'generatePopupParams', |
| 88 | + 'popupAction': 'move' |
| 89 | + }); |
| 90 | + |
| 91 | + // Verify popup was NOT reloaded due to null response |
| 92 | + expect(window.location.hash).toBe(''); |
| 93 | + expect(window.location.reload).not.toHaveBeenCalled(); |
| 94 | + }); |
| 95 | + |
| 96 | + test('handlePopupMenuClick handles sendMessage rejection', async () => { |
| 97 | + // Setup: Mock sendMessage rejection |
| 98 | + const mockError = new Error('Background script error'); |
| 99 | + chrome.runtime.sendMessage.mockRejectedValue(mockError); |
| 100 | + |
| 101 | + // Execute the click handler - it should throw since there's no error handling |
| 102 | + await expect(handlePopupMenuClick('switch')).rejects.toThrow('Background script error'); |
| 103 | + |
| 104 | + // Verify the message was sent |
| 105 | + expect(chrome.runtime.sendMessage).toHaveBeenCalledWith({ |
| 106 | + 'action': 'generatePopupParams', |
| 107 | + 'popupAction': 'switch' |
| 108 | + }); |
| 109 | + |
| 110 | + // Verify popup was NOT reloaded due to error |
| 111 | + expect(window.location.hash).toBe(''); |
| 112 | + expect(window.location.reload).not.toHaveBeenCalled(); |
| 113 | + }); |
| 114 | +}); |
0 commit comments