-
Notifications
You must be signed in to change notification settings - Fork 264
Expand file tree
/
Copy pathIIIFIFrameCommunication.test.jsx
More file actions
45 lines (40 loc) · 1.56 KB
/
IIIFIFrameCommunication.test.jsx
File metadata and controls
45 lines (40 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import { render, screen } from '@tests/utils/test-utils';
import { IIIFIFrameCommunication } from '../../../src/components/IIIFIFrameCommunication';
/** */
function createWrapper(props) {
render(
<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={() => {}}
{...props}
/>,
);
}
describe('IIIFIFrameCommunication', () => {
it('should render an iframe', () => {
createWrapper();
expect(screen.getByTitle('AccessTokenSender', { hidden: true })).toBeInTheDocument();
});
});
describe('Register event listener', () => {
afterEach(() => {
vi.restoreAllMocks();
});
it('should call handleReceiveMessage on message event', () => {
const events = {};
vi.spyOn(window, 'addEventListener').mockImplementation((event, onReceiveMessage) => {
events[event] = onReceiveMessage;
});
vi.spyOn(window, 'removeEventListener').mockImplementation((event, onReceiveMessage) => {
events[event] = undefined;
});
const props = { handleReceiveMessage: vi.fn() };
const view = render(<IIIFIFrameCommunication {...props} />);
events.message();
expect(props.handleReceiveMessage).toBeCalledTimes(1);
expect(window.addEventListener).toBeCalledWith('message', expect.any(Function));
view.unmount();
expect(window.removeEventListener).toBeCalledWith('message', expect.any(Function), false);
});
});