Skip to content

Commit

Permalink
Add tests for embedded iframe cases
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Brain committed Jan 25, 2019
1 parent cfd6d10 commit 3d5e8a2
Show file tree
Hide file tree
Showing 4 changed files with 231 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .eslintrc.js
Expand Up @@ -6,5 +6,10 @@ module.exports = {
'globals': {
__ZOID__: true,
__POST_ROBOT__: true
},

'rules': {
'react/display-name': 'off',
'react/prop-types': 'off'
}
};
90 changes: 90 additions & 0 deletions test/tests/happy.js → test/tests/happy.jsx
@@ -1,7 +1,9 @@
/* @flow */
/** @jsx node */

import { wrapPromise } from 'belter/src';
import { getParent, getOpener } from 'cross-domain-utils/src';
import { node, dom } from 'jsx-pragmatic/src';

import { onWindowOpen } from '../common';

Expand Down Expand Up @@ -291,4 +293,92 @@ describe('zoid happy cases', () => {
}).render(document.body);
});
});

it('should enter a component rendered as an iframe inside another iframe and call a prop', () => {
return wrapPromise(({ expect, avoid }) => {
const expectedValue = 'bar';

window.__component__ = () => {
return window.zoid.create({
tag: 'test-render-iframe-in-iframe-with-prop',
url: 'mock://www.child.com/base/test/windows/child/index.htm',
domain: 'mock://www.child.com',
containerTemplate: ({ doc, outlet }) => {
return (
<div>
<iframe>
<html>
<body>
<node el={ outlet } />
</body>
</html>
</iframe>
</div>
).render(dom({ doc }));
}
});
};

const component = window.__component__();

return component({

foo: expect('foo', bar => {
if (bar !== expectedValue) {
throw new Error(`Expected bar to be ${ JSON.stringify(expectedValue) }, got ${ JSON.stringify(bar) }`);
}
}),

onClose: avoid('onClose'),

run: `
window.xprops.foo(${ JSON.stringify(expectedValue) });
`
}).render(document.body);
});
});

it('should enter a component rendered as a popup inside another iframe and call a prop', () => {
return wrapPromise(({ expect, avoid }) => {
const expectedValue = 'bar';

window.__component__ = () => {
return window.zoid.create({
tag: 'test-render-popup-in-iframe-with-prop',
url: 'mock://www.child.com/base/test/windows/child/index.htm',
domain: 'mock://www.child.com',
containerTemplate: ({ doc, outlet }) => {
return (
<div>
<iframe>
<html>
<body>
<node el={ outlet } />
</body>
</html>
</iframe>
</div>
).render(dom({ doc }));
}
});
};

const component = window.__component__();

return component({

foo: expect('foo', bar => {
if (bar !== expectedValue) {
throw new Error(`Expected bar to be ${ JSON.stringify(expectedValue) }, got ${ JSON.stringify(bar) }`);
}
}),

onClose: avoid('onClose'),

run: `
window.xprops.foo(${ JSON.stringify(expectedValue) });
`
}).render(document.body, window.zoid.CONTEXT.POPUP);
});
});
});
114 changes: 114 additions & 0 deletions test/tests/renderto.js → test/tests/renderto.jsx
@@ -1,8 +1,10 @@
/* @flow */
/* eslint max-lines: off */
/** @jsx node */

import { onCloseWindow, getParent, getOpener, isWindowClosed } from 'cross-domain-utils/src';
import { wrapPromise } from 'belter/src';
import { node, dom } from 'jsx-pragmatic/src';

import { onWindowOpen } from '../common';

Expand Down Expand Up @@ -581,4 +583,116 @@ describe('zoid renderto cases', () => {
}).render(document.body);
});
});

it('should render a component to the parent as an iframe inside an iframe and call a prop', () => {
return wrapPromise(({ expect }) => {

window.__component__ = () => {
return {
simple: window.zoid.create({
tag: 'test-renderto-iframe-in-iframe-simple',
url: 'mock://www.child.com/base/test/windows/child/index.htm',
domain: 'mock://www.child.com'
}),

remote: window.zoid.create({
tag: 'test-renderto-iframe-in-iframe-remote',
url: 'mock://www.child.com/base/test/windows/child/index.htm',
domain: 'mock://www.child.com',
containerTemplate: ({ doc, outlet }) => {
return (
<div>
<iframe>
<html>
<body>
<node el={ outlet } />
</body>
</html>
</iframe>
</div>
).render(dom({ doc }));
}
})
};
};

return window.__component__().simple({
foo: expect('foo'),

run: () => {
onWindowOpen().then(expect('onWindowOpen', win => {
if (getParent(win) !== window) {
throw new Error(`Expected window parent to be current window`);
}
}));

return `
window.__component__().remote({
foo: window.xprops.foo,
run: \`
window.xprops.foo();
\`
}).renderTo(window.parent, 'body');
`;
}
}).render(document.body);
});
});

it('should render a component to the parent as an popup inside an iframe and call a prop', () => {
return wrapPromise(({ expect }) => {

window.__component__ = () => {
return {
simple: window.zoid.create({
tag: 'test-renderto-popup-in-iframe-simple',
url: 'mock://www.child.com/base/test/windows/child/index.htm',
domain: 'mock://www.child.com'
}),

remote: window.zoid.create({
tag: 'test-renderto-popup-in-iframe-remote',
url: 'mock://www.child.com/base/test/windows/child/index.htm',
domain: 'mock://www.child.com',
containerTemplate: ({ doc, outlet }) => {
return (
<div>
<iframe>
<html>
<body>
<node el={ outlet } />
</body>
</html>
</iframe>
</div>
).render(dom({ doc }));
}
})
};
};

return window.__component__().simple({
foo: expect('foo'),

run: () => {
onWindowOpen().then(expect('onWindowOpen', win => {
if (getParent(win) !== window) {
throw new Error(`Expected window parent to be current window`);
}
}));

return `
window.__component__().remote({
foo: window.xprops.foo,
run: \`
window.xprops.foo();
\`
}).renderTo(window.parent, 'body', zoid.CONTEXT.POPUP);
`;
}
}).render(document.body);
});
});
});
24 changes: 22 additions & 2 deletions test/windows/child/index.htm
Expand Up @@ -4,9 +4,29 @@
<script>
window.mockDomain = 'mock://www.child.com';

function getAncestor(win) {
if (win.opener) {
return win.opener;
}

if (win.parent !== win) {
return win.parent;
}
}

var parentWindow = window;
while ((parentWindow.opener && parentWindow.opener.zoid && parentWindow !== parentWindow.opener) || (parentWindow.parent && parentWindow.parent.zoid && parentWindow !== parentWindow.parent)) {
parentWindow = parentWindow.opener || parentWindow.parent;
while (true) {
const ancestor = getAncestor(parentWindow);

if (!ancestor) {
break;
}

if (ancestor.zoid || getAncestor(ancestor)) {
parentWindow = ancestor;
} else {
break;
}
}

window.console.karma = parentWindow.console.karma;
Expand Down

0 comments on commit 3d5e8a2

Please sign in to comment.