Skip to content

Commit ec71c77

Browse files
authored
fix(fake-browser): Push new tabs to tabList on creation (#106)
1 parent 27195e9 commit ec71c77

File tree

3 files changed

+58
-4
lines changed

3 files changed

+58
-4
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { describe, it, expect, vi, beforeEach } from 'vitest';
2+
import { fakeBrowser } from '../index';
3+
4+
describe('tabs', () => {
5+
beforeEach(fakeBrowser.reset);
6+
7+
describe('create', () => {
8+
it('should create a new tab and add it to tabList', async () => {
9+
const newTab = await fakeBrowser.tabs.create({ url: 'https://example.com' });
10+
11+
expect(newTab).toBeDefined();
12+
expect(newTab.id).toBe(1);
13+
expect(newTab.url).toBe('https://example.com');
14+
15+
const tabs = await fakeBrowser.tabs.query({});
16+
expect(tabs).toHaveLength(2); // default tab + new tab
17+
});
18+
19+
it('should trigger onCreated event', async () => {
20+
const listener = vi.fn();
21+
fakeBrowser.tabs.onCreated.addListener(listener);
22+
23+
const newTab = await fakeBrowser.tabs.create({ url: 'https://example.com' });
24+
25+
expect(listener).toHaveBeenCalledWith(
26+
expect.objectContaining({
27+
id: newTab.id,
28+
url: 'https://example.com',
29+
}),
30+
);
31+
});
32+
});
33+
34+
describe('query', () => {
35+
it('should filter tabs by windowId', async () => {
36+
const window2 = await fakeBrowser.windows.create();
37+
const tab1 = await fakeBrowser.tabs.create({ url: 'https://window1.com' });
38+
const tab2 = await fakeBrowser.tabs.create({
39+
url: 'https://window2.com',
40+
windowId: window2.id,
41+
});
42+
43+
const window1Tabs = await fakeBrowser.tabs.query({ windowId: 0 });
44+
expect(window1Tabs).toHaveLength(2); // default + tab1
45+
46+
const window2Tabs = await fakeBrowser.tabs.query({ windowId: window2.id });
47+
expect(window2Tabs).toHaveLength(1);
48+
expect(window2Tabs[0].url).toBe('https://window2.com');
49+
});
50+
});
51+
});

packages/fake-browser/src/apis/tabs.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Tabs, Windows } from 'webextension-polyfill';
22
import { BrowserOverrides } from '../types';
3-
import { windows } from './windows';
3+
import { windows, DEFAULT_WINDOW } from './windows';
44
import { defineEventWithTrigger } from '../utils/defineEventWithTrigger';
55

66
type InMemoryTab = Omit<Tabs.Tab, 'active'>;
@@ -23,6 +23,7 @@ const DEFAULT_TAB: InMemoryTab = {
2323
highlighted: false,
2424
incognito: false,
2525
pinned: false,
26+
windowId: DEFAULT_WINDOW.id,
2627
};
2728
const DEFAULT_NEXT_TAB_ID = 1;
2829

@@ -74,12 +75,13 @@ export const tabs: BrowserOverrides['tabs'] = {
7475
const newTab: InMemoryTab = {
7576
highlighted: false,
7677
incognito: false,
77-
index: window.tabs?.length ?? 0,
78+
index: window?.tabs?.length ?? 0,
7879
pinned: createProperties.pinned ?? false,
79-
windowId: window.id,
80+
windowId: window?.id ?? 0,
8081
id: getNextTabId(),
8182
url: createProperties.url,
8283
};
84+
tabList.push(newTab);
8385
const fullTab = mapTab(newTab);
8486
await onCreated.trigger(fullTab);
8587
return fullTab;
@@ -122,6 +124,7 @@ export const tabs: BrowserOverrides['tabs'] = {
122124
res = res && currentWindow.id === tab.windowId;
123125
if (queryInfo.currentWindow != null && !queryInfo.currentWindow)
124126
res = res && currentWindow.id !== tab.windowId;
127+
if (queryInfo.windowId != null) res = res && tab.windowId === queryInfo.windowId;
125128
if (queryInfo.discarded != null) res = res && tab.discarded === queryInfo.discarded;
126129
if (queryInfo.hidden != null) res = res && tab.hidden === queryInfo.hidden;
127130
if (queryInfo.highlighted != null) res = res && tab.highlighted === queryInfo.highlighted;

packages/fake-browser/src/apis/windows.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const onCreated = defineEventWithTrigger<(window: Windows.Window) => void>();
99
const onRemoved = defineEventWithTrigger<(windowId: number) => void>();
1010
const onFocusChanged = defineEventWithTrigger<(windowId: number) => void>();
1111

12-
const DEFAULT_WINDOW: InMemoryWindow = {
12+
export const DEFAULT_WINDOW: InMemoryWindow = {
1313
id: 0,
1414
alwaysOnTop: false,
1515
incognito: false,

0 commit comments

Comments
 (0)