From 192a835878c2a645bd7ade3b0383b67c8a5df3e9 Mon Sep 17 00:00:00 2001 From: "jake@costa.security" Date: Mon, 21 Oct 2024 10:01:58 -0700 Subject: [PATCH 1/3] test: add basic test to trigger GitHub CI on PR --- tests/basic.test.js | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/basic.test.js b/tests/basic.test.js index 7fe9508..3637952 100644 --- a/tests/basic.test.js +++ b/tests/basic.test.js @@ -1,3 +1,4 @@ test('Jest is working', () => { expect(1 + 1).toBe(2); + expect(true).toBe(true); }); \ No newline at end of file From 575c784c816d4f09f8afe8ec12365f793e24e27f Mon Sep 17 00:00:00 2001 From: "jake@costa.security" Date: Mon, 21 Oct 2024 12:11:20 -0700 Subject: [PATCH 2/3] test: add real puppeteer header tests --- tests/puppeteer.test.js | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 tests/puppeteer.test.js diff --git a/tests/puppeteer.test.js b/tests/puppeteer.test.js new file mode 100644 index 0000000..2d210f9 --- /dev/null +++ b/tests/puppeteer.test.js @@ -0,0 +1,29 @@ +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'); + }); + + it ('should visit gnuterrypratchett.com and check for the header', async () => { + await page.goto('http://www.gnuterrypratchett.com'); + const header = await page.evaluate(() => { + return document.querySelector('h1').innerText; + }); + expect(header).toBe('GNU Terry Pratchett'); + }); +}); \ No newline at end of file From a0b61c9e2199f590a6ab100a0dd4f6bf1acdbcda Mon Sep 17 00:00:00 2001 From: "jake@costa.security" Date: Tue, 22 Oct 2024 09:02:45 -0700 Subject: [PATCH 3/3] test: make sure icon illumination works --- mocks/chrome.js | 27 ++++++++++++++++++++++++++ package.json | 3 +++ src/background.js | 2 ++ tests/changeIcons.test.js | 41 +++++++++++++++++++++++++++++++++++++++ tests/puppeteer.test.js | 11 +++++++---- 5 files changed, 80 insertions(+), 4 deletions(-) create mode 100644 mocks/chrome.js create mode 100644 tests/changeIcons.test.js diff --git a/mocks/chrome.js b/mocks/chrome.js new file mode 100644 index 0000000..cda405b --- /dev/null +++ b/mocks/chrome.js @@ -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(), + }, + }, +}; diff --git a/package.json b/package.json index 6009408..0a57124 100644 --- a/package.json +++ b/package.json @@ -13,5 +13,8 @@ "jest": "^29.7.0", "jest-puppeteer": "^10.1.2", "puppeteer": "^23.6.0" + }, + "jest": { + "setupFiles": ["/mocks/chrome.js"] } } diff --git a/src/background.js b/src/background.js index cfee1e3..882fbac 100644 --- a/src/background.js +++ b/src/background.js @@ -122,3 +122,5 @@ chrome.tabs.onRemoved.addListener(function (tabId) { // } // return Object.keys(set); // } + +module.exports = { getClacks, extinguishClacksIcon, illuminateClacksIcon }; diff --git a/tests/changeIcons.test.js b/tests/changeIcons.test.js new file mode 100644 index 0000000..a21ef52 --- /dev/null +++ b/tests/changeIcons.test.js @@ -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" + }); + }); +}); diff --git a/tests/puppeteer.test.js b/tests/puppeteer.test.js index 2d210f9..144415f 100644 --- a/tests/puppeteer.test.js +++ b/tests/puppeteer.test.js @@ -1,3 +1,4 @@ +// Test of Puppeteer itself const puppeteer = require('puppeteer'); describe('Puppeteer with Jest', () => { @@ -19,11 +20,13 @@ describe('Puppeteer with Jest', () => { expect(title).toBe('Example Domain'); }); - it ('should visit gnuterrypratchett.com and check for the header', async () => { + // 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 header = await page.evaluate(() => { + const h1 = await page.evaluate(() => { return document.querySelector('h1').innerText; }); - expect(header).toBe('GNU Terry Pratchett'); + expect(h1).toBe('GNU Terry Pratchett'); }); -}); \ No newline at end of file +});