-
Notifications
You must be signed in to change notification settings - Fork 256
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace IComCom library with custom component (#3636)
Co-authored-by: Chris Beer <chris@cbeer.info>
- Loading branch information
1 parent
f42b0e1
commit 2c2f553
Showing
7 changed files
with
106 additions
and
17 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { shallow, mount } from 'enzyme'; | ||
import { IIIFIFrameCommunication } from '../../../src/components/IIIFIFrameCommunication'; | ||
|
||
/** */ | ||
function createWrapper(props) { | ||
return shallow( | ||
<IIIFIFrameCommunication | ||
src="https://iiifauth.digtest.co.uk/auth/token/login/01_Icarus_Breughel.jpg?origin=http://localhost:4444&messageId=https://iiifauth.digtest.co.uk/auth/token/login/01_Icarus_Breughel.jpg" | ||
title="AccessTokenSender" | ||
handleReceiveMessage={() => {}} | ||
/>, | ||
); | ||
} | ||
|
||
describe('IIIFIFrameCommunication', () => { | ||
it('should render an iframe', () => { | ||
const wrapper = createWrapper(); | ||
expect(wrapper.find('iframe')).toHaveLength(1); | ||
}); | ||
}); | ||
|
||
describe('Register event listener', () => { | ||
afterEach(() => { | ||
jest.restoreAllMocks(); | ||
}); | ||
it('should call handleReceiveMessage on message event', () => { | ||
const events = {}; | ||
jest.spyOn(window, 'addEventListener').mockImplementation((event, onReceiveMessage) => { | ||
events[event] = onReceiveMessage; | ||
}); | ||
jest.spyOn(window, 'removeEventListener').mockImplementation((event, onReceiveMessage) => { | ||
events[event] = undefined; | ||
}); | ||
const props = { handleReceiveMessage: jest.fn() }; | ||
const wrapper = mount(<IIIFIFrameCommunication {...props} />); | ||
events.message(); | ||
|
||
expect(props.handleReceiveMessage).toBeCalledTimes(1); | ||
expect(window.addEventListener).toBeCalledWith('message', expect.any(Function)); | ||
|
||
wrapper.unmount(); | ||
expect(window.removeEventListener).toBeCalledWith('message', expect.any(Function), false); | ||
}); | ||
}); |
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,48 @@ | ||
import { useEffect } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
|
||
/** | ||
* Handle IIIF Auth token validation using iframe message events | ||
*/ | ||
export function IIIFIFrameCommunication({ handleReceiveMessage, ...props }) { | ||
// Attaches the 'message' event listener to the window. | ||
useEffect(() => { | ||
if (!handleReceiveMessage) return undefined; | ||
|
||
window.addEventListener('message', handleReceiveMessage); | ||
|
||
// cleanup function | ||
return () => window.removeEventListener('message', handleReceiveMessage, false); | ||
}, [handleReceiveMessage]); | ||
|
||
return ( | ||
// iframe "title" attribute is passed in via props for accessibility | ||
// eslint-disable-next-line jsx-a11y/iframe-has-title | ||
<iframe | ||
{...props} | ||
/> | ||
); | ||
} | ||
|
||
IIIFIFrameCommunication.propTypes = { | ||
ariaHidden: PropTypes.bool, | ||
frameBorder: PropTypes.oneOfType([PropTypes.string, PropTypes.number]), | ||
handleReceiveMessage: PropTypes.func, | ||
height: PropTypes.oneOfType([PropTypes.string, PropTypes.number]), | ||
name: PropTypes.string, | ||
scrolling: PropTypes.string, | ||
src: PropTypes.string.isRequired, | ||
style: PropTypes.shape({}), | ||
width: PropTypes.oneOfType([PropTypes.string, PropTypes.number]), | ||
}; | ||
|
||
IIIFIFrameCommunication.defaultProps = { | ||
ariaHidden: true, | ||
frameBorder: 0, | ||
handleReceiveMessage: undefined, | ||
height: 1, | ||
name: undefined, | ||
scrolling: undefined, | ||
style: { visibility: 'hidden' }, | ||
width: 1, | ||
}; |
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