-
Notifications
You must be signed in to change notification settings - Fork 173
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(content-script-context): Fix initialization logic for Firefox (#895)
- Loading branch information
Showing
2 changed files
with
70 additions
and
5 deletions.
There are no files selected for viewing
59 changes: 59 additions & 0 deletions
59
packages/wxt/src/client/content-scripts/__tests__/content-script-context.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/** @vitest-environment happy-dom */ | ||
|
||
import { describe, it, expect, vi, beforeEach } from 'vitest'; | ||
import { ContentScriptContext } from '..'; | ||
import { fakeBrowser } from '@webext-core/fake-browser'; | ||
import { sleep } from '../../../core/utils/time'; | ||
|
||
describe('Content Script Context', () => { | ||
beforeEach(() => { | ||
vi.useRealTimers(); | ||
fakeBrowser.runtime.id = 'anything'; | ||
}); | ||
|
||
it("should recognize when the content script has lost it's connection to the extension API", () => { | ||
const ctx = new ContentScriptContext('test'); | ||
const onInvalidated = vi.fn(); | ||
|
||
ctx.onInvalidated(onInvalidated); | ||
// @ts-expect-error | ||
delete fakeBrowser.runtime.id; | ||
const isValid = ctx.isValid; | ||
|
||
expect(onInvalidated).toBeCalled(); | ||
expect(isValid).toBe(false); | ||
}); | ||
|
||
it('should invalidate the current content script when a new context is created', async () => { | ||
const name = 'test'; | ||
const onInvalidated = vi.fn(); | ||
const ctx = new ContentScriptContext(name); | ||
ctx.onInvalidated(onInvalidated); | ||
|
||
// Wait for events to run before next tick next tick | ||
await sleep(0); | ||
|
||
// Create a new context after first is initialized, and wait for it to initialize | ||
new ContentScriptContext(name); | ||
await sleep(0); | ||
|
||
expect(onInvalidated).toBeCalled(); | ||
expect(ctx.isValid).toBe(false); | ||
}); | ||
|
||
it('should not invalidate the current content script when a new context is created with a different name', async () => { | ||
const onInvalidated = vi.fn(); | ||
const ctx = new ContentScriptContext('test1'); | ||
ctx.onInvalidated(onInvalidated); | ||
|
||
// Wait for events to run before next tick next tick | ||
await sleep(0); | ||
|
||
// Create a new context after first is initialized, and wait for it to initialize | ||
new ContentScriptContext('test2'); | ||
await sleep(0); | ||
|
||
expect(onInvalidated).not.toBeCalled(); | ||
expect(ctx.isValid).toBe(true); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters