Skip to content

Commit

Permalink
feat: Implement tabs.update (#68)
Browse files Browse the repository at this point in the history
  • Loading branch information
charlie632 authored Aug 26, 2024
1 parent f174a54 commit 2bc4d7c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
31 changes: 31 additions & 0 deletions packages/fake-browser/src/apis/tabs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion packages/fake-browser/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]>;
Expand Down

0 comments on commit 2bc4d7c

Please sign in to comment.