winzik/skeleton-chrome/tests/extension.test.cjs

61 lines
2.8 KiB
JavaScript

// The extension, tested in a real Chrome: `bin/jumphp chrome:test <folder>` (Node's test runner, Puppeteer).
// One test per feature, named as a sentence; add yours as you write the feature.
'use strict';
const { describe, test, before, after } = require('node:test');
const assert = require('node:assert/strict');
const { launch } = require('./helpers.cjs');
let ext;
describe('extension', () => {
before(async () => { ext = await launch(); });
after(async () => { await ext?.browser.close(); });
test('the popup shows its title and the word to highlight', async () => {
const page = await ext.browser.newPage();
await page.goto(ext.url('popup/popup.html'));
await page.waitForFunction(() => document.getElementById('term').value !== '');
assert.equal(await page.$eval('h1 span', (n) => n.textContent), 'Highlighter');
assert.equal(await page.$eval('#term', (n) => n.value), 'example');
await page.close();
});
test('the options page saves into chrome.storage.sync', async () => {
const page = await ext.browser.newPage();
await page.goto(ext.url('options/options.html'));
await page.waitForFunction(() => document.getElementById('term').value !== '');
await page.$eval('#term', (n) => { n.value = ''; });
await page.type('#term', 'domain');
await page.click('#save');
await page.waitForFunction(() => document.getElementById('status').textContent !== '');
const stored = await ext.worker.evaluate(() => chrome.storage.sync.get('term'));
assert.equal(stored.term, 'domain');
await ext.worker.evaluate(() => chrome.storage.sync.clear());
await page.close();
});
test('the content script highlights the word on a matching page', async () => {
const page = await ext.fixturePage();
await page.waitForSelector('mark.xh-mark');
const marks = await page.$$eval('mark.xh-mark', (nodes) => nodes.length);
assert.ok(marks >= 3, `expected matches, got ${marks}`);
assert.match(await page.$eval('#xh-badge', (n) => n.textContent), /\d+ match/);
await page.close();
});
test('the popup counts the matches of a tab and highlights a new word', async () => {
const site = await ext.fixturePage();
await site.waitForSelector('mark.xh-mark');
const tab = await ext.tabId(site);
const popup = await ext.browser.newPage();
await popup.goto(ext.url(`popup/popup.html?tab=${tab}`));
await popup.waitForFunction(() => /\d/.test(document.getElementById('count').textContent));
await popup.$eval('#term', (n) => { n.value = 'permission'; });
await popup.click('#highlight');
await popup.waitForFunction(() => document.getElementById('count').textContent.startsWith('1 '));
assert.equal(await site.$$eval('mark.xh-mark', (nodes) => nodes.length), 1);
await ext.worker.evaluate(() => chrome.storage.sync.clear());
await popup.close();
await site.close();
});
});