-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ab69d84
commit f6b4000
Showing
7 changed files
with
113 additions
and
23 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
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
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
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
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
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,50 @@ | ||
import React from 'react'; | ||
import { render, screen } from '@testing-library/react'; | ||
import ModalIframe from './ModalIframe'; | ||
|
||
import { IFRAME_FEATURE_POLICY, SANDBOX_OPTIONS } from '../../constants'; | ||
|
||
describe('ModalIframe', () => { | ||
it('renders correctly with required props', () => { | ||
render(<ModalIframe title="Test Modal" />); | ||
|
||
const iframe = screen.getByTitle('Test Modal') as HTMLIFrameElement; | ||
|
||
expect(iframe).toBeInTheDocument(); | ||
expect(iframe.title).toBe('Test Modal'); | ||
expect(iframe.className).toContain('modal-iframe'); | ||
expect(iframe.getAttribute('allow')).toBe(IFRAME_FEATURE_POLICY); | ||
expect(iframe.getAttribute('referrerpolicy')).toBe('origin'); | ||
expect(iframe.getAttribute('sandbox')).toBe(SANDBOX_OPTIONS); | ||
expect(iframe.getAttribute('frameborder')).toBe('0'); | ||
expect(iframe.getAttribute('scrolling')).toBe('no'); | ||
}); | ||
|
||
it('applies custom className', () => { | ||
const { getByTitle } = render( | ||
<ModalIframe title="Test Modal" className="custom-class" />, | ||
); | ||
|
||
const iframe = getByTitle('Test Modal') as HTMLIFrameElement; | ||
|
||
expect(iframe.className).toContain('modal-iframe'); | ||
expect(iframe.className).toContain('custom-class'); | ||
}); | ||
|
||
it('passes additional props to iframe', () => { | ||
const { getByTitle } = render( | ||
<ModalIframe title="Test Modal" src="https://example.com" />, | ||
); | ||
|
||
const iframe = getByTitle('Test Modal') as HTMLIFrameElement; | ||
|
||
expect(iframe.src).toBe('https://example.com/'); | ||
}); | ||
|
||
it('forwards ref to iframe element', () => { | ||
const ref = React.createRef<HTMLIFrameElement>(); | ||
render(<ModalIframe title="Test Modal" ref={ref} />); | ||
|
||
expect(ref.current).toBeInstanceOf(HTMLIFrameElement); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -1,44 +1,42 @@ | ||
import { forwardRef, ForwardedRef, IframeHTMLAttributes } from 'react'; | ||
import classNames from 'classnames'; | ||
|
||
import { IFRAME_FEATURE_POLICY, SANDBOX_OPTIONS } from '../../constants'; | ||
|
||
interface ModalIframeProps extends IframeHTMLAttributes<HTMLIFrameElement> { | ||
title: string; | ||
className?: string; | ||
labelledBy?: string; | ||
describedBy?: string; | ||
} | ||
|
||
const SANDBOX_OPTIONS = [ | ||
'allow-forms', | ||
'allow-modals', | ||
'allow-popups', | ||
'allow-popups-to-escape-sandbox', | ||
'allow-presentation', | ||
'allow-same-origin', | ||
'allow-scripts', | ||
'allow-top-navigation-by-user-activation', | ||
].join(' '); | ||
|
||
export const IFRAME_FEATURE_POLICY = ( | ||
'microphone *; camera *; midi *; geolocation *; encrypted-media *, clipboard-write *' | ||
); | ||
|
||
const ModalIframe = forwardRef<HTMLIFrameElement, ModalIframeProps>( | ||
({ title, className, ...props }, ref: ForwardedRef<HTMLIFrameElement>) => ( | ||
({ | ||
title, className, labelledBy, describedBy, ...props | ||
}, ref: ForwardedRef<HTMLIFrameElement>) => ( | ||
<iframe | ||
title={title} | ||
className={classNames('modal-iframe', className)} | ||
data-testid="modal-iframe" | ||
allow={IFRAME_FEATURE_POLICY} | ||
referrerPolicy="origin" | ||
frameBorder="0" | ||
scrolling="no" | ||
ref={ref} | ||
sandbox={SANDBOX_OPTIONS} | ||
aria-modal="true" | ||
role="dialog" | ||
aria-labelledby={labelledBy} | ||
aria-describedby={describedBy} | ||
{...props} | ||
/> | ||
), | ||
); | ||
|
||
ModalIframe.defaultProps = { | ||
className: 'modal-iframe', | ||
labelledBy: 'modal-title', | ||
describedBy: 'modal-description', | ||
}; | ||
|
||
export default ModalIframe; |