-
-
Notifications
You must be signed in to change notification settings - Fork 200
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ feat(core): Add
waitForExtensionOnLoadPage
function (#954)
- Loading branch information
1 parent
d488acf
commit 3f5b549
Showing
2 changed files
with
66 additions
and
0 deletions.
There are no files selected for viewing
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,13 @@ | ||
import { type BrowserContext, errors as playwrightErrors } from 'playwright-core' | ||
|
||
export async function waitForExtensionOnLoadPage(context: BrowserContext) { | ||
try { | ||
return await context.waitForEvent('page', { timeout: 5000 }) // TODO: Extract & Make this timeout configurable. | ||
} catch (e) { | ||
if (e instanceof playwrightErrors.TimeoutError) { | ||
throw new Error('[WaitForExtensionOnLoadPage] Extension did not load in time!') | ||
} | ||
|
||
throw e | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
packages/core/test/utils/waitForExtensionOnLoadPage.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,53 @@ | ||
import { errors as playwrightErrors } from 'playwright-core' | ||
import { afterAll, afterEach, describe, expect, it, vi } from 'vitest' | ||
import { waitForExtensionOnLoadPage } from '../../src/utils/waitForExtensionOnLoadPage' | ||
|
||
const createContext = vi.fn().mockReturnValue({ | ||
waitForEvent: vi.fn().mockResolvedValue(undefined) | ||
}) | ||
|
||
const createTimeoutContext = vi.fn().mockReturnValue({ | ||
waitForEvent: vi.fn().mockImplementation(() => { | ||
throw new playwrightErrors.TimeoutError('Timeout :)') | ||
}) | ||
}) | ||
|
||
const createThrowContext = vi.fn().mockReturnValue({ | ||
waitForEvent: vi.fn().mockImplementation(() => { | ||
throw new Error('Unknown Quack!') | ||
}) | ||
}) | ||
|
||
describe('waitForExtensionOnLoadPage', () => { | ||
afterAll(() => { | ||
vi.resetAllMocks() | ||
}) | ||
|
||
afterEach(() => { | ||
vi.clearAllMocks() | ||
}) | ||
|
||
it('calls context.waitForEvent with correct arguments', async () => { | ||
const context = createContext() | ||
const waitForEventSpy = vi.spyOn(context, 'waitForEvent') | ||
|
||
await waitForExtensionOnLoadPage(context) | ||
|
||
expect(waitForEventSpy).toHaveBeenCalledOnce() | ||
expect(waitForEventSpy).toHaveBeenCalledWith('page', { timeout: 5000 }) | ||
}) | ||
|
||
it('throws with custom error if waitForEvent throws due to timeout', async () => { | ||
const context = createTimeoutContext() | ||
|
||
await expect(waitForExtensionOnLoadPage(context)).rejects.toThrowError( | ||
'[WaitForExtensionOnLoadPage] Extension did not load in time!' | ||
) | ||
}) | ||
|
||
it('throws if unknown error', async () => { | ||
const context = createThrowContext() | ||
|
||
await expect(waitForExtensionOnLoadPage(context)).rejects.toThrowError('Unknown Quack!') | ||
}) | ||
}) |