Skip to content
Draft
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
27 changes: 27 additions & 0 deletions mocks/chrome.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
global.chrome = {
action: {
setIcon: jest.fn(),
setTitle: jest.fn(),
},
runtime: {
getURL: jest.fn((path) => path),
onMessage: {
addListener: jest.fn(),
},
},
tabs: {
onRemoved: {
addListener: jest.fn(),
},
},
webRequest: {
onHeadersReceived: {
addListener: jest.fn(),
},
},
webNavigation: {
onCommitted: {
addListener: jest.fn(),
},
},
};
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,8 @@
"jest": "^29.7.0",
"jest-puppeteer": "^10.1.2",
"puppeteer": "^23.6.0"
},
"jest": {
"setupFiles": ["<rootDir>/mocks/chrome.js"]
}
}
2 changes: 2 additions & 0 deletions src/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,5 @@ chrome.tabs.onRemoved.addListener(function (tabId) {
// }
// return Object.keys(set);
// }

module.exports = { getClacks, extinguishClacksIcon, illuminateClacksIcon };
1 change: 1 addition & 0 deletions tests/basic.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
test('Jest is working', () => {
expect(1 + 1).toBe(2);
expect(true).toBe(true);
});
41 changes: 41 additions & 0 deletions tests/changeIcons.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const { getClacks, extinguishClacksIcon, illuminateClacksIcon } = require('../src/background');

describe('Clacks Overhead Icon state', () => {
beforeEach(() => {
jest.clearAllMocks();
});

it('should set the clacks icon to active if illuminated', () => {
illuminateClacksIcon(1);

expect(chrome.action.setIcon).toHaveBeenCalledWith({
tabId: 1,
path: {
"19": "images/melanie_icon19.png",
"38": "images/melanie_icon38.png"
}
});

expect(chrome.action.setTitle).toHaveBeenCalledWith({
tabId: 1,
title: "Clacks icon is active"
});
});

it('should set the clacks icon to disabled if disabled', () => {
extinguishClacksIcon(1);

expect(chrome.action.setIcon).toHaveBeenCalledWith({
tabId: 1,
path: {
"19": "images/melanie_icon19_disabled.png",
"38": "images/melanie_icon38_disabled.png"
}
});

expect(chrome.action.setTitle).toHaveBeenCalledWith({
tabId: 1,
title: "Clacks icon is disabled"
});
});
});
32 changes: 32 additions & 0 deletions tests/puppeteer.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Test of Puppeteer itself
const puppeteer = require('puppeteer');

describe('Puppeteer with Jest', () => {
let browser;
let page;

beforeAll(async () => {
browser = await puppeteer.launch();
page = await browser.newPage();
});

afterAll(async () => {
await browser.close();
});

it('should navigate to example.com and check title', async () => {
await page.goto('https://example.com');
const title = await page.title();
expect(title).toBe('Example Domain');
});

// This relies on the h1 tag being present on the page, which is not
// guaranteed, but has been consistent for years.
it ('should visit gnuterrypratchett.com and check for the h1', async () => {
await page.goto('http://www.gnuterrypratchett.com');
const h1 = await page.evaluate(() => {
return document.querySelector('h1').innerText;
});
expect(h1).toBe('GNU Terry Pratchett');
});
});