Skip to content

Commit

Permalink
Expand browser tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mtusnio committed Aug 24, 2024
1 parent c1bca53 commit 89fd1ec
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 9 deletions.
12 changes: 11 additions & 1 deletion .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ on:
- 'manifest-v3'

jobs:
build:
unit-tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
Expand All @@ -17,3 +17,13 @@ jobs:
node-version: '20.x'
- run: npm ci
- run: npm test
browser-tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Use Node.js
uses: actions/setup-node@v4
with:
node-version: '20.x'
- run: npm ci
- run: npm test-browser
59 changes: 51 additions & 8 deletions browser_tests/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,65 @@ afterEach(async () => {
workerTarget = null
});

async function getExtensionStatus() {
const storage = await worker.evaluate(async () => {
return await chrome.storage.local.get(["enabled"])
})
const badgeData = await worker.evaluate(async () => {
return {
"text": await chrome.action.getBadgeText({}),
"color": await chrome.action.getBadgeBackgroundColor({}),
}
})

test("clicking the extension button activates the extension", async () => {
const someOtherPage = await browser.newPage();
await someOtherPage.goto("https://www.google.com/", { waitUntil: ['domcontentloaded', "networkidle2"] });
await someOtherPage.bringToFront();
return {
storage,
badgeData
}
}

async function toggleExtension() {
await worker.evaluate(async () => {
const tabs = await chrome.tabs.query({ active: true })
await chrome.action.onClicked.dispatch(tabs[0]);
});
}
test("extension is disabled by default", async () => {
const someOtherPage = await browser.newPage();
await someOtherPage.goto("https://www.google.com/", { waitUntil: ['domcontentloaded', "networkidle2"] });
await someOtherPage.bringToFront();

const status = await getExtensionStatus()
expect(status.storage).toEqual({})
expect(status.badgeData).toEqual({
"text": "", "color": [0, 0, 0, 0]
})
})

const storage = await worker.evaluate(async () => {
return await chrome.storage.local.get(["enabled"])
test("toggling the extension on/off two times works", async () => {
const someOtherPage = await browser.newPage();
await someOtherPage.goto("https://www.google.com/", { waitUntil: ['domcontentloaded', "networkidle2"] });
await someOtherPage.bringToFront();

})
for (let i = 0; i < 2; i++) {
// On
await toggleExtension()
// Need a slight wait for the extension to trigger
await new Promise((r) => setTimeout(r, 200));
let status = await getExtensionStatus()
expect(status.storage).toEqual({ "enabled": true })
expect(status.badgeData).toEqual({
"text": "On", "color": [255, 0, 0, 255]
})

// Off
await toggleExtension()
await new Promise((r) => setTimeout(r, 200));
status = await getExtensionStatus()
expect(status.storage).toEqual({ "enabled": false })
expect(status.badgeData).toEqual({
"text": "", "color": [0, 0, 0, 0]
})
}

expect(storage).toEqual({ "enabled": true })
});

0 comments on commit 89fd1ec

Please sign in to comment.