diff --git a/packages/fake-browser/src/apis/tabs.ts b/packages/fake-browser/src/apis/tabs.ts index 87cf070..4c03f00 100644 --- a/packages/fake-browser/src/apis/tabs.ts +++ b/packages/fake-browser/src/apis/tabs.ts @@ -78,6 +78,7 @@ export const tabs: BrowserOverrides['tabs'] = { pinned: createProperties.pinned ?? false, windowId: window.id, id: getNextTabId(), + url: createProperties.url, }; const fullTab = mapTab(newTab); await onCreated.trigger(fullTab); @@ -155,6 +156,36 @@ export const tabs: BrowserOverrides['tabs'] = { await onHighlighted.trigger({ tabIds, windowId: window!.id! }); return window!; }, + // @ts-expect-error the type expects an overload + async update( + tabIdOrUpdateInfo: number | undefined | Tabs.UpdateUpdatePropertiesType, + optionalUpdateInfo: Tabs.UpdateUpdatePropertiesType | never, + ) { + let updateInfo: Tabs.UpdateUpdatePropertiesType; + if (tabIdOrUpdateInfo !== undefined && typeof tabIdOrUpdateInfo === 'object') { + updateInfo = tabIdOrUpdateInfo; + } else { + updateInfo = optionalUpdateInfo; + } + + let tabId: number; + if (typeof tabIdOrUpdateInfo === 'number') { + tabId = tabIdOrUpdateInfo; + } else { + const currentWindow = await windows.getCurrent(); + tabId = currentWindow.tabs!.find(tab => tab.active)!.id!; + } + + const tab = await tabs.get(tabId); + if (!tab) throw new Error('Tab not found'); + + const updatedTab = { ...tab, ...updateInfo }; + const tabIndex = tabList.findIndex(tab => tab.id === tabId); + tabList[tabIndex] = updatedTab; + const fullTab = mapTab(updatedTab); + await onUpdated.trigger(fullTab.id!, updateInfo, fullTab); + return fullTab; + }, async remove(tabIds) { const ids = Array.isArray(tabIds) ? tabIds : [tabIds]; for (const id of ids) { diff --git a/packages/fake-browser/src/types.ts b/packages/fake-browser/src/types.ts index 2ea2381..d0e820a 100644 --- a/packages/fake-browser/src/types.ts +++ b/packages/fake-browser/src/types.ts @@ -70,7 +70,7 @@ export interface BrowserOverrides { }; tabs: Pick< Tabs.Static, - 'get' | 'getCurrent' | 'create' | 'duplicate' | 'query' | 'highlight' | 'remove' + 'get' | 'getCurrent' | 'create' | 'duplicate' | 'query' | 'highlight' | 'remove' | 'update' > & { resetState(): void; onCreated: EventForTesting<[tab: Tabs.Tab]>;