Skip to content

Commit

Permalink
Add tests (not working now)
Browse files Browse the repository at this point in the history
  • Loading branch information
parostatkiem-zz committed Oct 3, 2018
1 parent f99aa00 commit 0a0093b
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 42 deletions.
50 changes: 33 additions & 17 deletions core/src/services/routing.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const getLastNodeObject = pathData => {
return lastElement ? lastElement : {};
};

const getDefaultChildNode = function (pathData) {
const getDefaultChildNode = function(pathData) {
const lastElement =
pathData.navigationPath[pathData.navigationPath.length - 1];

Expand All @@ -30,7 +30,7 @@ const getDefaultChildNode = function (pathData) {
}
};

const isExistingRoute = function (path, pathData) {
const isExistingRoute = function(path, pathData) {
if (path === '') {
return true;
}
Expand Down Expand Up @@ -87,7 +87,9 @@ const removeElementChildren = node => {
export const isNotSameDomain = (config, component) => {
if (config.iframe) {
const componentData = component.get();
const previousUrl = getUrlWithoutHash(componentData.previousNodeValues.viewUrl);
const previousUrl = getUrlWithoutHash(
componentData.previousNodeValues.viewUrl
);
const nextUrl = getUrlWithoutHash(componentData.viewUrl);
return previousUrl != nextUrl;
}
Expand All @@ -96,8 +98,10 @@ export const isNotSameDomain = (config, component) => {

export const hasIframeIsolation = component => {
const componentData = component.get();
return componentData.isolateView || componentData.previousNodeValues.isolateView;
}
return (
componentData.isolateView || componentData.previousNodeValues.isolateView
);
};

export const getContentViewParamPrefix = () => {
return (
Expand Down Expand Up @@ -143,9 +147,10 @@ const navigateIframe = (config, component, node) => {
);
}

if (isNotSameDomain(config, component)
|| hasIframeIsolation(component)
|| Boolean(config.builderCompatibilityMode)
if (
isNotSameDomain(config, component) ||
hasIframeIsolation(component) ||
Boolean(config.builderCompatibilityMode)
) {
const componentData = component.get();
// preserveView, hide other frames, else remove
Expand Down Expand Up @@ -240,9 +245,12 @@ const getNodeParams = params => {

const getPathParams = nodes => {
const params = {};
nodes.filter(n => n.pathParam).map(n => (n.pathParam)).forEach(pp => {
params[pp.key.replace(':', '')] = pp.value;
});
nodes
.filter(n => n.pathParam)
.map(n => n.pathParam)
.forEach(pp => {
params[pp.key.replace(':', '')] = pp.value;
});
return params;
};

Expand Down Expand Up @@ -289,10 +297,12 @@ export const handleRouteChange = async (path, component, node, config) => {
nodeParams,
pathParams,
isolateView,
previousNodeValues: (previousCompData) ? {
viewUrl: previousCompData.viewUrl,
isolateView: previousCompData.isolateView
} : {}
previousNodeValues: previousCompData
? {
viewUrl: previousCompData.viewUrl,
isolateView: previousCompData.isolateView
}
: {}
});

navigateIframe(config, component, node);
Expand Down Expand Up @@ -381,8 +391,14 @@ export const handleRouteClick = (
windowElem = window,
documentElem = document
) => {
if(node.externalLinkUrl){
node.sameWindow ? location.href=node.externalLinkUrl : window.open(node.externalLinkUrl).focus();
if (node.externalLinkUrl) {
try {
node.sameWindow
? (window.location.href = node.externalLinkUrl)
: window.open(node.externalLinkUrl).focus();
} catch (err) {
console.error('Could not open external link', err);
}
// externalLinkUrl property is provided so there's no need to trigger routing mechanizm
return;
}
Expand Down
47 changes: 32 additions & 15 deletions core/test/navigation.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ const expect = chai.expect;
const assert = chai.assert;
const sinon = require('sinon');

const sampleNavPromise = new Promise(function (resolve) {
const sampleNavPromise = new Promise(function(resolve) {
const lazyLoadedChildrenNodesProviderFn = () => {
return new Promise(function (resolve) {
return new Promise(function(resolve) {
resolve([
{
pathSegment: 'b1',
Expand Down Expand Up @@ -46,18 +46,23 @@ const sampleNavPromise = new Promise(function (resolve) {
context: {
lazy: false
}
},
{
externalLinkUrl: 'http://www.com',
label: 'external link',
sameWindow: true
}
]);
});

describe('Navigation', function () {
describe('Navigation', function() {
afterEach(() => {
// reset
window.Luigi = {
config: {}
};
});
describe('getNavigationPath()', function () {
describe('getNavigationPath()', function() {
it('should not fail for undefined arguments', () => {
navigation.getNavigationPath(undefined, undefined);
});
Expand All @@ -68,8 +73,8 @@ describe('Navigation', function () {
const rootNode = navPath.navigationPath[0];
assert.equal(
rootNode.children.length,
2,
'Root node expected to have 2 children nodes'
3,
'Root node expected to have 3 children nodes'
);
assert.equal(rootNode.children[0].pathSegment, 'aaa');
const nodeWithLazyLoadedChildren = rootNode.children[1];
Expand Down Expand Up @@ -205,7 +210,11 @@ describe('Navigation', function () {
window.Luigi = {
config: {
navigation: {
nodeAccessibilityResolver: (nodeToCheckPermissionFor, currentNode, currentContext) => {
nodeAccessibilityResolver: (
nodeToCheckPermissionFor,
currentNode,
currentContext
) => {
if (nodeToCheckPermissionFor.constraints) {
return nodeToCheckPermissionFor.constraints === 'other_scope';
}
Expand All @@ -222,9 +231,7 @@ describe('Navigation', function () {
{ label: 'child2' }
]
};
const children = await navigation.getChildren(
nodeWithChildren
);
const children = await navigation.getChildren(nodeWithChildren);
expect(children.length).to.equal(1);
expect(children[0].label).to.equal('child2');
});
Expand Down Expand Up @@ -257,8 +264,9 @@ describe('Navigation', function () {
// truthy tests
// when
const resStaticOk = navigation.findMatchingNode('other', [staticNode()]);
const resDynamicOk = navigation.findMatchingNode('avengers', [dynamicNode()]);

const resDynamicOk = navigation.findMatchingNode('avengers', [
dynamicNode()
]);

// // then
expect(resStaticOk.pathSegment).to.equal('other');
Expand All @@ -271,11 +279,20 @@ describe('Navigation', function () {
expect(resNull).to.equal(null);
sinon.assert.notCalled(console.warn);

const resStaticWarning = navigation.findMatchingNode('avengers', [staticNode(), dynamicNode()]);
expect(resStaticWarning.pathSegment).to.equal('avengers', 'static warning pathSegment: ' + resStaticWarning.pathSegment);
const resStaticWarning = navigation.findMatchingNode('avengers', [
staticNode(),
dynamicNode()
]);
expect(resStaticWarning.pathSegment).to.equal(
'avengers',
'static warning pathSegment: ' + resStaticWarning.pathSegment
);
sinon.assert.calledOnce(console.warn);

const resMultipleDynamicError = navigation.findMatchingNode('twoDynamic', [dynamicNode(), dynamicNode()]);
const resMultipleDynamicError = navigation.findMatchingNode(
'twoDynamic',
[dynamicNode(), dynamicNode()]
);
expect(resMultipleDynamicError).to.equal(null);
sinon.assert.calledOnce(console.warn);
sinon.assert.calledOnce(console.error);
Expand Down
77 changes: 67 additions & 10 deletions core/test/routing.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,18 @@ import { afterEach } from 'mocha';

describe('Routing', () => {
let component;
const mockBrowser = new MockBrowser();
const window = mockBrowser.getWindow();
global.window = window;

beforeEach(() => {
component = {
set: obj => {
component.get = () => obj;
},
get: () => ({})
};
})
});
afterEach(() => {
if (document.createElement.restore) {
document.createElement.restore();
Expand Down Expand Up @@ -137,9 +141,6 @@ describe('Routing', () => {
// given
const path = '#/projects';
const expectedViewUrl = '/aaa.html';
const mockBrowser = new MockBrowser();
const window = mockBrowser.getWindow();
global.window = window;
const document = mockBrowser.getDocument();
global.document = document;

Expand Down Expand Up @@ -203,7 +204,13 @@ describe('Routing', () => {
.returns({ src: null })
.once();

await routing.handleRouteChange(path, componentSaved, node, config, window);
await routing.handleRouteChange(
path,
componentSaved,
node,
config,
window
);

// then
assert.equal(componentSaved.get().viewUrl, expectedViewUrl);
Expand Down Expand Up @@ -377,6 +384,19 @@ describe('Routing', () => {
pathSegment: 'projects'
};

const nodeWithExternalLink = {
currentTab: {
externalLinkUrl: 'http://www.com',
label: 'external link',
sameWindow: true
},
newTab: {
externalLinkUrl: 'http://www.com',
label: 'external link',
sameWindow: false
}
};

it('should set proper location hash with parent node', () => {
// given
const expectedRoute = '#/projects/project-one';
Expand Down Expand Up @@ -492,6 +512,28 @@ describe('Routing', () => {
assert.equal(singleStateWithPath.path, expectedRoute);
assert.equal(dispatchCallsNum + 1, expectedDispatchCallsNum);
});

it('should open external link in current tab', () => {
routing.handleRouteClick(
nodeWithExternalLink.currentTab,
window,
document
);
// then
assert.equal(
window.location.href,
nodeWithExternalLink.currentTab.externalLinkUrl
);
});

it('should open external link in a new tab', () => {
routing.handleRouteClick(nodeWithExternalLink.newTab, window, document);
// then
assert.equal(
window.location.href,
nodeWithExternalLink.newTab.externalLinkUrl
);
});
});

describe('setActiveIframeToPrevious', () => {
Expand Down Expand Up @@ -585,24 +627,39 @@ describe('Routing', () => {
src: 'http://url.com/app.html!#/prevUrl'
}
};
component.set({ viewUrl: 'http://url.com/app.html!#/someUrl', previousNodeValues: { viewUrl: config.iframe.src } });
component.set({
viewUrl: 'http://url.com/app.html!#/someUrl',
previousNodeValues: { viewUrl: config.iframe.src }
});
assert.isFalse(routing.isNotSameDomain(config, component));

component.set({ viewUrl: 'http://otherurl.de/app.html!#/someUrl', previousNodeValues: { viewUrl: config.iframe.src } });
component.set({
viewUrl: 'http://otherurl.de/app.html!#/someUrl',
previousNodeValues: { viewUrl: config.iframe.src }
});
assert.isTrue(routing.isNotSameDomain(config, component));
});

it('hasIframeIsolation', () => {
// no node is set to isolateView
component.set({ isolateView: false, previousNodeValues: { isolateView: false } });
component.set({
isolateView: false,
previousNodeValues: { isolateView: false }
});
assert.isFalse(routing.hasIframeIsolation(component));

// new node is set to isolateView
component.set({ isolateView: true, previousNodeValues: { isolateView: false } });
component.set({
isolateView: true,
previousNodeValues: { isolateView: false }
});
assert.isTrue(routing.hasIframeIsolation(component));

// current node is set to isolateView
component.set({ isolateView: false, previousNodeValues: { isolateView: true } });
component.set({
isolateView: false,
previousNodeValues: { isolateView: true }
});
assert.isTrue(routing.hasIframeIsolation(component));
});

Expand Down

0 comments on commit 0a0093b

Please sign in to comment.