|
| 1 | + |
| 2 | +import { setupChromeMocks, jest } from './helpers.js'; |
| 3 | + |
| 4 | +jest.mock('../js/background/spacesService.js', () => ({ |
| 5 | + cleanUrl: jest.fn(url => url), |
| 6 | +})); |
| 7 | + |
| 8 | +describe('focusOrLoadTabInWindow', () => { |
| 9 | + let focusOrLoadTabInWindow; |
| 10 | + |
| 11 | + beforeEach(async () => { |
| 12 | + setupChromeMocks(); |
| 13 | + const background = await import('../js/background/background.js'); |
| 14 | + focusOrLoadTabInWindow = background.focusOrLoadTabInWindow; |
| 15 | + }); |
| 16 | + |
| 17 | + afterEach(() => { |
| 18 | + jest.clearAllMocks(); |
| 19 | + }); |
| 20 | + |
| 21 | + test('should focus existing tab when match is found', async () => { |
| 22 | + const window = { |
| 23 | + tabs: [ |
| 24 | + { id: 1, url: 'https://example.com', status: 'complete' }, |
| 25 | + { id: 2, url: 'https://google.com', status: 'complete' }, |
| 26 | + ], |
| 27 | + }; |
| 28 | + const tabUrl = 'https://example.com'; |
| 29 | + |
| 30 | + await focusOrLoadTabInWindow(window, tabUrl); |
| 31 | + |
| 32 | + expect(chrome.tabs.update).toHaveBeenCalledWith(1, { active: true }); |
| 33 | + expect(chrome.tabs.create).not.toHaveBeenCalled(); |
| 34 | + }); |
| 35 | + |
| 36 | + test('should create new tab when no match is found', async () => { |
| 37 | + const window = { |
| 38 | + tabs: [ |
| 39 | + { id: 1, url: 'https://google.com', status: 'complete' }, |
| 40 | + { id: 2, url: 'https://github.com', status: 'complete' }, |
| 41 | + ], |
| 42 | + }; |
| 43 | + const tabUrl = 'https://example.com'; |
| 44 | + |
| 45 | + await focusOrLoadTabInWindow(window, tabUrl); |
| 46 | + |
| 47 | + expect(chrome.tabs.update).not.toHaveBeenCalled(); |
| 48 | + expect(chrome.tabs.create).toHaveBeenCalledWith({ url: tabUrl, active: true }); |
| 49 | + }); |
| 50 | + |
| 51 | + test('should handle window with no tabs gracefully', async () => { |
| 52 | + const window = { tabs: [] }; |
| 53 | + const tabUrl = 'https://example.com'; |
| 54 | + |
| 55 | + await focusOrLoadTabInWindow(window, tabUrl); |
| 56 | + |
| 57 | + expect(chrome.tabs.update).not.toHaveBeenCalled(); |
| 58 | + expect(chrome.tabs.create).toHaveBeenCalledWith({ url: tabUrl, active: true }); |
| 59 | + }); |
| 60 | + |
| 61 | + test('should handle window with missing tabs property', async () => { |
| 62 | + const window = {}; |
| 63 | + const tabUrl = 'https://example.com'; |
| 64 | + |
| 65 | + await focusOrLoadTabInWindow(window, tabUrl); |
| 66 | + |
| 67 | + expect(chrome.tabs.update).not.toHaveBeenCalled(); |
| 68 | + expect(chrome.tabs.create).toHaveBeenCalledWith({ url: tabUrl, active: true }); |
| 69 | + }); |
| 70 | +}); |
0 commit comments