-
Notifications
You must be signed in to change notification settings - Fork 3
test(iframe-manager): add unit tests with jsdom #625
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,275 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /* | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Copyright (c) 2025 Ping Identity Corporation. All rights reserved. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * This software may be modified and distributed under the terms | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * of the MIT license. See the LICENSE file for details. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { iFrameManager } from './iframe-manager.effects.js'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Patches an iframe's contentWindow.location.href to simulate navigation, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * then fires a 'load' event so the onLoadHandler runs. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| function simulateIframeLoad(iframe: HTMLIFrameElement, href: string): void { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Object.defineProperty(iframe, 'contentWindow', { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| value: { location: { href } }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| writable: true, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| configurable: true, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| iframe.dispatchEvent(new Event('load')); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| describe('iFrameManager', () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| describe('getParamsByRedirect – input validation', () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| it('throws synchronously when successParams is empty', () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const manager = iFrameManager(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expect(() => | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| manager.getParamsByRedirect({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| url: 'https://example.com', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| timeout: 1000, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| successParams: [], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| errorParams: ['error'], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ).toThrow('successParams and errorParams must be provided'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| it('throws synchronously when errorParams is empty', () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const manager = iFrameManager(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expect(() => | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| manager.getParamsByRedirect({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| url: 'https://example.com', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| timeout: 1000, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| successParams: ['code'], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| errorParams: [], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ).toThrow('successParams and errorParams must be provided'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| it('throws synchronously when successParams or errorParams is undefined', () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const manager = iFrameManager(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expect(() => | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| manager.getParamsByRedirect({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| url: 'https://example.com', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| timeout: 1000, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| successParams: undefined as any, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| errorParams: ['error'], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ).toThrow('successParams and errorParams must be provided'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+50
to
+61
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Test name is misleading and the The description says 🛠️ Proposed fix: cover both `undefined` cases- it('throws synchronously when successParams or errorParams is undefined', () => {
- const manager = iFrameManager();
- expect(() =>
- manager.getParamsByRedirect({
- url: 'https://example.com',
- timeout: 1000,
- // eslint-disable-next-line `@typescript-eslint/no-explicit-any`
- successParams: undefined as any,
- errorParams: ['error'],
- }),
- ).toThrow('successParams and errorParams must be provided');
- });
+ it('throws synchronously when successParams is undefined', () => {
+ const manager = iFrameManager();
+ expect(() =>
+ manager.getParamsByRedirect({
+ url: 'https://example.com',
+ timeout: 1000,
+ // eslint-disable-next-line `@typescript-eslint/no-explicit-any`
+ successParams: undefined as any,
+ errorParams: ['error'],
+ }),
+ ).toThrow('successParams and errorParams must be provided');
+ });
+
+ it('throws synchronously when errorParams is undefined', () => {
+ const manager = iFrameManager();
+ expect(() =>
+ manager.getParamsByRedirect({
+ url: 'https://example.com',
+ timeout: 1000,
+ successParams: ['code'],
+ // eslint-disable-next-line `@typescript-eslint/no-explicit-any`
+ errorParams: undefined as any,
+ }),
+ ).toThrow('successParams and errorParams must be provided');
+ });📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| describe('getParamsByRedirect – iframe lifecycle', () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| beforeEach(() => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| vi.useFakeTimers(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| afterEach(() => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| vi.useRealTimers(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| document.body.replaceChildren(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| it('creates a hidden iframe with display:none and appends it to document.body', () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const manager = iFrameManager(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| manager.getParamsByRedirect({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| url: 'https://example.com', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| timeout: 5000, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| successParams: ['code'], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| errorParams: ['error'], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const iframe = document.querySelector('iframe'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expect(iframe).not.toBeNull(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expect(iframe?.style.display).toBe('none'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| it('sets iframe.src to the provided URL', () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const manager = iFrameManager(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| manager.getParamsByRedirect({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| url: 'https://example.com/start', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| timeout: 5000, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| successParams: ['code'], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| errorParams: ['error'], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const iframe = document.querySelector('iframe') as HTMLIFrameElement; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expect(iframe.src).toBe('https://example.com/start'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| it('rejects with timeout error when iframe never resolves', async () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const manager = iFrameManager(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const promise = manager.getParamsByRedirect({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| url: 'https://example.com', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| timeout: 3000, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| successParams: ['code'], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| errorParams: ['error'], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| vi.advanceTimersByTime(3000); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| await expect(promise).rejects.toEqual({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| type: 'internal_error', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| message: 'iframe timed out', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| it('removes the iframe from the DOM after timeout', async () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const manager = iFrameManager(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const promise = manager.getParamsByRedirect({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| url: 'https://example.com', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| timeout: 3000, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| successParams: ['code'], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| errorParams: ['error'], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| vi.advanceTimersByTime(3000); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| await promise.catch(vi.fn()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expect(document.querySelector('iframe')).toBeNull(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| it('resolves with all query params when any successParam key is present in the redirect URL', async () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const manager = iFrameManager(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const promise = manager.getParamsByRedirect({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| url: 'https://example.com/start', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| timeout: 5000, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| successParams: ['code'], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| errorParams: ['error'], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const iframe = document.querySelector('iframe') as HTMLIFrameElement; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| simulateIframeLoad(iframe, 'https://app.example.com/callback?code=abc123&state=xyz'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const result = await promise; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expect(result).toEqual({ code: 'abc123', state: 'xyz' }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| it('removes the iframe from the DOM after resolving with success params', async () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const manager = iFrameManager(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const promise = manager.getParamsByRedirect({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| url: 'https://example.com/start', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| timeout: 5000, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| successParams: ['code'], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| errorParams: ['error'], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const iframe = document.querySelector('iframe') as HTMLIFrameElement; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| simulateIframeLoad(iframe, 'https://app.example.com/callback?code=abc123'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| await promise; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expect(document.querySelector('iframe')).toBeNull(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| it('resolves (not rejects) with all query params when an errorParam key is present', async () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const manager = iFrameManager(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const promise = manager.getParamsByRedirect({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| url: 'https://example.com/start', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| timeout: 5000, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| successParams: ['code'], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| errorParams: ['error', 'error_description'], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const iframe = document.querySelector('iframe') as HTMLIFrameElement; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| simulateIframeLoad( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| iframe, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 'https://app.example.com/callback?error=access_denied&error_description=User+cancelled', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const result = await promise; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expect(result).toEqual({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| error: 'access_denied', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| error_description: 'User cancelled', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| it('ignores the initial about:blank load event and keeps waiting', async () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const manager = iFrameManager(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const promise = manager.getParamsByRedirect({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| url: 'https://example.com/start', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| timeout: 5000, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| successParams: ['code'], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| errorParams: ['error'], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const iframe = document.querySelector('iframe') as HTMLIFrameElement; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| simulateIframeLoad(iframe, 'about:blank'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| vi.advanceTimersByTime(100); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| simulateIframeLoad(iframe, 'https://app.example.com/callback?code=abc123'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const result = await promise; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expect(result).toEqual({ code: 'abc123' }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| it('waits through intermediate redirects before resolving', async () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const manager = iFrameManager(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const promise = manager.getParamsByRedirect({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| url: 'https://example.com/start', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| timeout: 5000, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| successParams: ['code'], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| errorParams: ['error'], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const iframe = document.querySelector('iframe') as HTMLIFrameElement; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| simulateIframeLoad(iframe, 'https://example.com/authorize'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| simulateIframeLoad(iframe, 'https://app.example.com/callback?code=final'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const result = await promise; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expect(result).toEqual({ code: 'final' }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| it('rejects with internal_error when contentWindow access throws (cross-origin)', async () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const manager = iFrameManager(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const promise = manager.getParamsByRedirect({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| url: 'https://example.com/start', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| timeout: 5000, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| successParams: ['code'], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| errorParams: ['error'], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const iframe = document.querySelector('iframe') as HTMLIFrameElement; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Object.defineProperty(iframe, 'contentWindow', { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| get() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| throw new DOMException('Blocked a frame with origin', 'SecurityError'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| configurable: true, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| iframe.dispatchEvent(new Event('load')); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| await expect(promise).rejects.toEqual({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| type: 'internal_error', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| message: 'unexpected failure', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| it('removes the iframe from the DOM after cross-origin rejection', async () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const manager = iFrameManager(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const promise = manager.getParamsByRedirect({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| url: 'https://example.com/start', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| timeout: 5000, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| successParams: ['code'], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| errorParams: ['error'], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const iframe = document.querySelector('iframe') as HTMLIFrameElement; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Object.defineProperty(iframe, 'contentWindow', { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| get() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| throw new DOMException('Blocked a frame with origin', 'SecurityError'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| configurable: true, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| iframe.dispatchEvent(new Event('load')); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| await promise.catch(vi.fn()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expect(document.querySelector('iframe')).toBeNull(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Public API rename is breaking without a compatibility bridge.
Renaming
polltopollStatuschanges the exported client contract and will break existing integrations unless you provide a transitional alias (or explicitly gate this behind a major-version release plan).Suggested compatibility bridge (in source, then regenerate API report)
🤖 Prompt for AI Agents