-
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
Open
ryanbas21
wants to merge
1
commit into
main
Choose a base branch
from
iframe-tests
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
275 changes: 275 additions & 0 deletions
275
packages/sdk-effects/iframe-manager/src/lib/iframe-manager.effects.test.ts
This file contains hidden or 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,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'); | ||
| }); | ||
| }); | ||
|
|
||
| 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(); | ||
| }); | ||
| }); | ||
| }); | ||
Binary file not shown.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Test name is misleading and the
errorParams: undefinedcase is missing.The description says
"successParams or errorParams is undefined"but the test body only exercisessuccessParams: undefined.errorParams: undefinedis never passed, so the guard for that branch is untested.🛠️ Proposed fix: cover both `undefined` cases
📝 Committable suggestion
🤖 Prompt for AI Agents