Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
june07 committed Jul 6, 2024
1 parent 3289b86 commit ddce801
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 33 deletions.
8 changes: 5 additions & 3 deletions src/sw.js
Original file line number Diff line number Diff line change
Expand Up @@ -422,9 +422,11 @@ async function checkLicenseStatus() {

await chrome.tabs.update(existingTabId, { active: true })
} else {
if (ENV !== 'test') {
await new Promise(resolve => setTimeout(resolve, 7 * 60000))
}
const delay = 7 * 60000
const showAt = Date.now() + delay

await chrome.storage.session.set({ showingSubscriptionMessage: showAt })
await new Promise(resolve => setTimeout(resolve, delay))
await chrome.tabs.create({
url: 'https://june07.com/nim-subscription/?oUserId=' + oUserId,
active: true
Expand Down
14 changes: 0 additions & 14 deletions tests/fixtures.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,6 @@ module.exports = {
appName: JSON.parse(fs.readFileSync(join(process.cwd(), '_locales/en/messages.json'), 'utf-8')).appName.message,
appVersion: JSON.parse(fs.readFileSync(join(process.cwd(), 'package.json'), 'utf-8')).version,
basename,
patch: ({ restore = false } = {}) => {
const manifest = JSON.parse(fs.readFileSync(join(process.cwd(), 'manifest.json'), 'utf-8'))
const sw = fs.readFileSync(join(process.cwd(), manifest.background.service_worker)).toString()

if (restore) {
const restored = sw.replace(`const ENV = 'test'`, `const ENV = 'production'`)

fs.writeFileSync(join(process.cwd(), manifest.background.service_worker), restored)
} else {
const patched = sw.replace(`const ENV = 'production'`, `const ENV = 'test'`)

fs.writeFileSync(join(process.cwd(), manifest.background.service_worker), patched)
}
},
test: test.extend({
context: async ({ }, use, testInfo) => {
const pathToExtension = process.env?.PATH_TO_EXTENSION || process.cwd()
Expand Down
27 changes: 11 additions & 16 deletions tests/z-license.spec.js → tests/license.spec.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
const { spawn } = require('child_process')
const { test, expect, ids, randomPort, basename, patch } = require('./fixtures')
const { test, expect, ids, randomPort, basename } = require('./fixtures')

module.exports = (async () => {
test.beforeAll(async () => {
patch()
})
test.afterAll(async () => {
patch({ restore: true })
})
test.describe.configure({ mode: 'serial' })
test.describe(async () => {
test(`${basename(__filename)} - Should show license message`, async ({ page, context, serviceWorker }) => {
Expand Down Expand Up @@ -36,14 +30,15 @@ module.exports = (async () => {
await context.waitForEvent('page')

// Wait for the license browser popup to show
let subPage, tries = 0
while (!subPage && tries < 10) {
let showingSubscriptionMessage, tries = 0
while (!showingSubscriptionMessage && tries < 10) {
tries += 1
subPage = (await context.pages()).find(page => /https:\/\/june07.com\/nim-subscription/.test(page.url())) ? true : false
const storage = await page.evaluate(async () => await chrome.storage.session.get('showingSubscriptionMessage'))
showingSubscriptionMessage = storage.showingSubscriptionMessage
await new Promise(r => setTimeout(r, 500))
}

expect(subPage).toBeTruthy()
expect(showingSubscriptionMessage).not.toBeNull()
})

process.kill()
Expand Down Expand Up @@ -73,15 +68,15 @@ module.exports = (async () => {
await (await page.locator(ids.inputs.host)).press('Enter')
await context.waitForEvent('page')

let subPage, tries = 0
while (!subPage && tries < 10) {
let showingSubscriptionMessage, tries = 0
while (!showingSubscriptionMessage && tries < 10) {
tries += 1
subPage = (await context.pages()).find(page => /https:\/\/june07.com\/nim-subscription/.test(page.url())) ? true : false
const storage = await page.evaluate(async () => await chrome.storage.session.get('showingSubscriptionMessage'))
showingSubscriptionMessage = storage.showingSubscriptionMessage
await new Promise(r => setTimeout(r, 500))
}

// Assert that subPage is null or undefined
expect(subPage).toBeFalsy()
expect(showingSubscriptionMessage < (Date.now() + 7 * 60000)).toBeTruthy()
})

process.kill()
Expand Down

0 comments on commit ddce801

Please sign in to comment.