Skip to content

Commit

Permalink
Adjust tests
Browse files Browse the repository at this point in the history
  • Loading branch information
klappradla committed Sep 20, 2024
1 parent 645fd3a commit a87faa0
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 17 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"bundle:chrome": "cross-env BUNDLE=true run-s build:chrome",
"bundle:firefox": "cross-env BUNDLE=true run-s build:firefox",
"lint": "eslint --ignore-path .gitignore --ext .js,.ts,.tsx .",
"format:js": "eslint --ignore-path .gitignore --fix --ext .js,.ts,.tsx .",
"stylelint": "stylelint --ignore-path .gitignore 'src/**/*.scss'",
"test": "jest",
"typecheck": "tsc --noEmit",
Expand Down
9 changes: 7 additions & 2 deletions src/background/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import browser from "webextension-polyfill";

import type { BackgroundMessage } from "../popup/types";

async function getTickets() {
const [tab] = await browser.tabs.query({ active: true, currentWindow: true });
const results = await browser.tabs.sendMessage(tab.id!, { tickets: true }); // eslint-disable-line @typescript-eslint/no-non-null-assertion
return results;
}

browser.runtime.onMessage.addListener((msg) => {
async function handleMessage(msg: BackgroundMessage) {
if (msg.getTickets) {
return getTickets();
}
});
return null;
}

browser.runtime.onMessage.addListener(handleMessage);
2 changes: 1 addition & 1 deletion src/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
}
],
"web_accessible_resources": [],
"permissions": ["activeTab", "clipboardWrite", "storage", "offscreen"],
"permissions": ["activeTab", "clipboardWrite", "storage"],
"action": {
"default_title": "Git Branch/Message",
"default_popup": "popup.html",
Expand Down
32 changes: 19 additions & 13 deletions src/popup/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,20 @@ import enhance from "../core/enhance";
import { deserialize } from "../errors";
import store from "../store";
import render from "./render";
import type { BackgroundPage } from "./types";
import type { BackgroundWorker } from "./types";

const mockBackgroundWorker: BackgroundWorker = { getTickets: jest.fn() };

jest
.mock("webextension-polyfill", () => {
const result = { tickets: [], errors: [] };
const background = { getTickets: jest.fn().mockResolvedValue(result) };
const extension = { getBackgroundPage: () => background };
return { extension };
const runtime = {
sendMessage: jest
.fn()
.mockImplementation((_msg, _sender) =>
mockBackgroundWorker.getTickets(),
),
};
return { runtime };
})
.mock("../core/enhance", () => jest.fn(() => jest.fn()))
.mock("../store", () => ({ get: jest.fn().mockResolvedValue({}) }))
Expand All @@ -27,11 +33,8 @@ jest
describe("popup", () => {
const initialize = window.onload as () => Promise<void>;

let background: BackgroundPage;

beforeEach(() => {
background = browser.extension.getBackgroundPage() as BackgroundPage;
(background.getTickets as jest.Mock).mockResolvedValue({
(mockBackgroundWorker.getTickets as jest.Mock).mockResolvedValue({
tickets: [],
errors: [],
});
Expand All @@ -40,7 +43,7 @@ describe("popup", () => {
});

afterEach(() => {
(background.getTickets as jest.Mock).mockReset();
(mockBackgroundWorker.getTickets as jest.Mock).mockReset();
(store.get as jest.Mock).mockReset();
(enhance as jest.Mock).mockReset();
(render as jest.Mock).mockReset();
Expand All @@ -53,7 +56,10 @@ describe("popup", () => {
it("fetches ticket information through the background page", async () => {
await initialize();

expect(background.getTickets).toHaveBeenCalled();
expect(browser.runtime.sendMessage).toHaveBeenCalledWith({
getTickets: true,
});
expect(mockBackgroundWorker.getTickets).toHaveBeenCalled();
});

it("loads settings from storage", async () => {
Expand All @@ -76,7 +82,7 @@ describe("popup", () => {
const tickets = ["uno", "dos"].map((title, id) =>
make({ id: id.toString(), title }),
);
(background.getTickets as jest.Mock).mockResolvedValue({
(mockBackgroundWorker.getTickets as jest.Mock).mockResolvedValue({
tickets,
errors: [],
});
Expand All @@ -98,7 +104,7 @@ describe("popup", () => {

it("renders the popup content with errors", async () => {
const errors = [{ message: "Test Error" }];
(background.getTickets as jest.Mock).mockResolvedValue({
(mockBackgroundWorker.getTickets as jest.Mock).mockResolvedValue({
tickets: [],
errors,
});
Expand Down
4 changes: 3 additions & 1 deletion src/popup/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import onmedia from "./observe-media";
import render from "./render";

async function load() {
const { tickets, errors } = await browser.runtime.sendMessage({getTickets: true})
const { tickets, errors } = await browser.runtime.sendMessage({
getTickets: true,
});
const { options = {}, templates } = await store.get(null);

const enhancer = await enhance(templates, options.autofmt);
Expand Down
8 changes: 8 additions & 0 deletions src/popup/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,11 @@ import type { Ticket } from "../types";
export type BackgroundPage = Window & {
getTickets: () => { tickets: Ticket[]; errors: ErrorObject[] };
};

export type BackgroundWorker = {
getTickets: () => { tickets: Ticket[]; errors: ErrorObject[] };
};

export type BackgroundMessage = {
getTickets?: boolean;
};

0 comments on commit a87faa0

Please sign in to comment.