Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[maint] test cleanup #50

Merged
merged 1 commit into from
Sep 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions src/tabViews/__tests__/WindowsTab.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ import WindowsTab from "../WindowsTab";
describe("Windows Tab", () => {
test("should render initial tab content", async () => {
const win1 = mockWindow({ id: 1, state: "normal" });
const group1 = mockTabGroup({ id: 11, windowId: win1.id });
const tab1 = mockTab({ id: 2, windowId: win1.id, groupId: group1.id });
const tab2 = mockTab({ id: 3, windowId: win1.id, groupId: group1.id });
const tab3 = mockTab({ id: 4, windowId: win1.id });
const group1 = mockTabGroup({ id: 111, windowId: win1.id });
const tab1 = mockTab({ id: 10, windowId: win1.id, groupId: group1.id });
const tab2 = mockTab({ id: 11, windowId: win1.id, groupId: group1.id });
const tab3 = mockTab({ id: 12, windowId: win1.id });

const win2 = mockWindow({ id: 5, state: "normal" });
const tab4 = mockTab({ id: 6, windowId: win2.id });
const tab5 = mockTab({ id: 7, windowId: win2.id });
const win2 = mockWindow({ id: 2, state: "normal" });
const tab4 = mockTab({ id: 20, windowId: win2.id });
const tab5 = mockTab({ id: 21, windowId: win2.id });

const win3 = mockWindow({ id: 8, state: "minimized" });
const tab6 = mockTab({ id: 9, windowId: win3.id });
const win3 = mockWindow({ id: 3, state: "minimized" });
const tab6 = mockTab({ id: 30, windowId: win3.id });

chromeMock.windows.getAll.mockResolvedValue([win1, win2, win3]);
chromeMock.tabGroups.query.mockResolvedValue([group1]);
Expand Down Expand Up @@ -56,10 +56,10 @@ describe("Windows Tab", () => {
const user = userEvent.setup();

const win1 = mockWindow({ id: 1, state: "normal" });
const group1 = mockTabGroup({ id: 10, windowId: win1.id });
const tab1 = mockTab({ id: 11, title: "meow 1", windowId: win1.id, groupId: group1.id });
const tab2 = mockTab({ id: 12, title: "meow 2", windowId: win1.id, groupId: group1.id });
const tab3 = mockTab({ id: 13, title: "no", windowId: win1.id });
const group1 = mockTabGroup({ id: 111, windowId: win1.id });
const tab1 = mockTab({ id: 10, title: "meow 1", windowId: win1.id, groupId: group1.id });
const tab2 = mockTab({ id: 11, title: "meow 2", windowId: win1.id, groupId: group1.id });
const tab3 = mockTab({ id: 12, title: "no", windowId: win1.id });

const win2 = mockWindow({ id: 2, state: "normal" });
const tab4 = mockTab({ id: 21, windowId: win2.id });
Expand Down
34 changes: 17 additions & 17 deletions src/tabViews/window/__tests__/Windows.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import Windows from "../Windows";
describe("Windows", () => {
test("should render windows", async () => {
const win1 = mockWindow({ id: 1, state: "normal" });
const group1 = mockTabGroup({ id: 11, windowId: win1.id });
const group2 = mockTabGroup({ id: 12, windowId: win1.id });
const tab1 = mockTab({ id: 2, windowId: win1.id, groupId: group1.id });
const tab2 = mockTab({ id: 3, windowId: win1.id, groupId: group2.id });
const group1 = mockTabGroup({ id: 111, windowId: win1.id });
const group2 = mockTabGroup({ id: 112, windowId: win1.id });
const tab1 = mockTab({ id: 10, windowId: win1.id, groupId: group1.id });
const tab2 = mockTab({ id: 11, windowId: win1.id, groupId: group2.id });

const win2 = mockWindow({ id: 4, state: "normal" });
const tab3 = mockTab({ id: 5, windowId: win2.id });
const tab4 = mockTab({ id: 6, windowId: win2.id });
const win2 = mockWindow({ id: 2, state: "normal" });
const tab3 = mockTab({ id: 20, windowId: win2.id });
const tab4 = mockTab({ id: 21, windowId: win2.id });

chromeMock.windows.getAll.mockResolvedValue([win1, win2]);
chromeMock.tabGroups.query.mockResolvedValue([group1, group2]);
Expand All @@ -34,18 +34,18 @@ describe("Windows", () => {

test("should render windows divided by state", async () => {
const win1 = mockWindow({ id: 1, state: "normal" });
const group1 = mockTabGroup({ id: 11, windowId: win1.id });
const group2 = mockTabGroup({ id: 12, windowId: win1.id });
const tab1 = mockTab({ id: 2, windowId: win1.id, groupId: group1.id });
const tab2 = mockTab({ id: 3, windowId: win1.id, groupId: group2.id });
const group1 = mockTabGroup({ id: 111, windowId: win1.id });
const group2 = mockTabGroup({ id: 112, windowId: win1.id });
const tab1 = mockTab({ id: 10, windowId: win1.id, groupId: group1.id });
const tab2 = mockTab({ id: 11, windowId: win1.id, groupId: group2.id });

const win2 = mockWindow({ id: 4, state: "normal" });
const tab3 = mockTab({ id: 5, windowId: win2.id });
const tab4 = mockTab({ id: 6, windowId: win2.id });
const win2 = mockWindow({ id: 2, state: "normal" });
const tab3 = mockTab({ id: 20, windowId: win2.id });
const tab4 = mockTab({ id: 21, windowId: win2.id });

const win3 = mockWindow({ id: 7, state: "minimized" });
const tab5 = mockTab({ id: 8, windowId: win3.id });
const tab6 = mockTab({ id: 9, windowId: win3.id });
const win3 = mockWindow({ id: 3, state: "minimized" });
const tab5 = mockTab({ id: 30, windowId: win3.id });
const tab6 = mockTab({ id: 31, windowId: win3.id });

chromeMock.windows.getAll.mockResolvedValue([win1, win2, win3]);
chromeMock.tabGroups.query.mockResolvedValue([group1, group2]);
Expand Down
49 changes: 26 additions & 23 deletions src/tabViews/window/header/__tests__/WindowsHeader.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,16 @@ describe("Windows Header", () => {
test("should enable if there are tabs selected", async () => {
const user = userEvent.setup();
chromeMock.windows.getAll.mockResolvedValue([mockWindow(), mockWindow()]);
chromeMock.tabs.query.mockResolvedValue([
mockTab({ title: "meow" }),
mockTab({ title: "no", url: "no" }),
mockTab({ title: "no", url: "no" }),
mockTab({ title: "no", url: "no" }),
]);
const tab1 = mockTab({ title: "meow" });
const tab2 = mockTab({ title: "no", url: "no" });
const tab3 = mockTab({ title: "no", url: "no" });
const tab4 = mockTab({ title: "no", url: "no" });
chromeMock.tabs.query.mockResolvedValue([tab1, tab2, tab3, tab4]);
vi.stubGlobal("chrome", chromeMock);

const { getByLabelText, getByRole } = renderWithContext(<TestWrap selectedTabs={[1]} />);
const { getByLabelText, getByRole } = renderWithContext(
<TestWrap selectedTabs={[tab1.id]} />,
);
await waitFor(() => {
expect(getByLabelText("4 Tabs across 2 Windows")).toBeVisible();
});
Expand All @@ -101,46 +102,48 @@ describe("Windows Header", () => {
test("should handle group tabs", async () => {
const user = userEvent.setup();
chromeMock.windows.getAll.mockResolvedValue([mockWindow(), mockWindow()]);
chromeMock.tabs.query.mockResolvedValue([
mockTab({ id: 1, title: "meow" }),
mockTab({ id: 2, title: "meow" }),
mockTab({ id: 3, title: "no", url: "no" }),
mockTab({ id: 4, title: "no", url: "no" }),
]);
const tab1 = mockTab({ title: "meow" });
const tab2 = mockTab({ title: "meow" });
const tab3 = mockTab({ title: "no", url: "no" });
const tab4 = mockTab({ title: "no", url: "no" });
chromeMock.tabs.query.mockResolvedValue([tab1, tab2, tab3, tab4]);
vi.stubGlobal("chrome", chromeMock);

const { getByLabelText, getByRole } = renderWithContext(<TestWrap selectedTabs={[1, 2]} />);
const { getByLabelText, getByRole } = renderWithContext(
<TestWrap selectedTabs={[tab1.id, tab2.id]} />,
);
await waitFor(() => {
expect(getByLabelText("4 Tabs across 2 Windows")).toBeVisible();
});

await user.type(getByRole("textbox", { name: "Search" }), "meow");
await user.click(getByRole("button", { name: "Group Tabs" }));

expect(chrome.tabs.group).toHaveBeenCalledWith({ tabIds: [1, 2] });
expect(chrome.tabs.group).toHaveBeenCalledWith({ tabIds: [tab1.id, tab2.id] });
expect(chrome.tabGroups.update).toHaveBeenCalledWith(expect.any(Number), { title: "meow" });
});

test("should handle close tabs", async () => {
const user = userEvent.setup();
chromeMock.windows.getAll.mockResolvedValue([mockWindow(), mockWindow()]);
chromeMock.tabs.query.mockResolvedValue([
mockTab({ id: 1, title: "meow" }),
mockTab({ id: 2, title: "meow" }),
mockTab({ id: 3, title: "no", url: "no" }),
mockTab({ id: 4, title: "no", url: "no" }),
]);
const tab1 = mockTab({ title: "meow" });
const tab2 = mockTab({ title: "meow" });
const tab3 = mockTab({ title: "no", url: "no" });
const tab4 = mockTab({ title: "no", url: "no" });
chromeMock.tabs.query.mockResolvedValue([tab1, tab2, tab3, tab4]);
vi.stubGlobal("chrome", chromeMock);

const { getByLabelText, getByRole } = renderWithContext(<TestWrap selectedTabs={[1, 2]} />);
const { getByLabelText, getByRole } = renderWithContext(
<TestWrap selectedTabs={[tab1.id, tab2.id]} />,
);
await waitFor(() => {
expect(getByLabelText("4 Tabs across 2 Windows")).toBeVisible();
});

await user.type(getByRole("textbox", { name: "Search" }), "meow");
await user.click(getByRole("button", { name: "Close Tabs" }));

expect(chrome.tabs.remove).toHaveBeenCalledWith([1, 2]);
expect(chrome.tabs.remove).toHaveBeenCalledWith([tab1.id, tab2.id]);
});
});
});
24 changes: 12 additions & 12 deletions test-utils/chromeMock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ const chromeMock = {
storage: {
sync: {
onChanged: {
addListener: vi.fn(), //.mockResolvedValue(),
removeListener: vi.fn(), //.mockResolvedValue(),
addListener: vi.fn(),
removeListener: vi.fn(),
},
clear: vi.fn(),
get: vi.fn().mockImplementation((defaultValue) => Promise.resolve(defaultValue)),
Expand All @@ -19,16 +19,16 @@ const chromeMock = {
},
tabs: {
onCreated: {
addListener: vi.fn(), //.mockResolvedValue(),
removeListener: vi.fn(), //.mockResolvedValue(),
addListener: vi.fn(),
removeListener: vi.fn(),
},
onUpdated: {
addListener: vi.fn(), //.mockResolvedValue(),
removeListener: vi.fn(), //.mockResolvedValue(),
addListener: vi.fn(),
removeListener: vi.fn(),
},
onRemoved: {
addListener: vi.fn(), //.mockResolvedValue(),
removeListener: vi.fn(), //.mockResolvedValue(),
addListener: vi.fn(),
removeListener: vi.fn(),
},
group: vi.fn().mockReturnValue(Promise.resolve(faker.number.int())),
update: vi.fn(),
Expand All @@ -44,12 +44,12 @@ const chromeMock = {
id: "focused-window-id",
}),
onCreated: {
addListener: vi.fn(), //.mockResolvedValue(),
removeListener: vi.fn(), //.mockResolvedValue(),
addListener: vi.fn(),
removeListener: vi.fn(),
},
onRemoved: {
addListener: vi.fn(), //.mockResolvedValue(),
removeListener: vi.fn(), //.mockResolvedValue(),
addListener: vi.fn(),
removeListener: vi.fn(),
},
getAll: vi.fn<typeof chrome.windows.getAll>().mockResolvedValue([]),
update: vi.fn<typeof chrome.windows.update>(),
Expand Down