Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Call miqCheckForChanges in breadcrumbs #6003

Merged
merged 1 commit into from Aug 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 6 additions & 3 deletions app/javascript/components/breadcrumbs/index.js
Expand Up @@ -3,21 +3,23 @@ import PropTypes from 'prop-types';
import { Breadcrumb } from 'patternfly-react';
import { unescape } from 'lodash';

import { onClickTree, onClick } from './on-click-functions';

const parsedText = text => unescape(text).replace(/<[/]{0,1}strong>/g, '');

class Breadcrumbs extends Component {
renderItems = () => {
const {
items, controllerName,
} = this.props;
return items.filter((item, index) => index !== (items.length - 1)).map((item, index) => {
return items.filter((_item, index) => index !== (items.length - 1)).map((item, index) => {
const text = parsedText(item.title);
if ((item.url || item.key) && !item.action) {
if (item.key) {
return (
<Breadcrumb.Item
key={`${item.key}-${index}`} // eslint-disable-line react/no-array-index-key
onClick={() => sendDataWithRx({ breadcrumbSelect: { path: `/${controllerName}/tree_select`, key: item.key } })}
onClick={() => onClickTree(controllerName, item)}
>
{text}
</Breadcrumb.Item>
Expand All @@ -27,6 +29,7 @@ class Breadcrumbs extends Component {
<Breadcrumb.Item
key={item.url || index}
href={item.url}
onClick={e => onClick(e, item.url)}
>
{text}
</Breadcrumb.Item>
Expand All @@ -38,7 +41,7 @@ class Breadcrumbs extends Component {

render() {
const {
items, title, controllerName, ...rest
items, title, controllerName, ...rest // eslint-disable-line no-unused-vars
} = this.props;

return (
Expand Down
10 changes: 10 additions & 0 deletions app/javascript/components/breadcrumbs/on-click-functions.js
@@ -0,0 +1,10 @@
export const onClickTree = (controllerName, item) => (
window.miqCheckForChanges()
? sendDataWithRx({ breadcrumbSelect: { path: `/${controllerName}/tree_select`, key: item.key } }) : null
);

export const onClick = (e) => {
if (!window.miqCheckForChanges()) {
e.preventDefault();
}
};
Expand Up @@ -55,13 +55,15 @@ exports[`Breadcrumbs component is correctly rendered 1`] = `
active={false}
href="/providers"
key="/providers"
onClick={[Function]}
>
<li
className=""
>
<SafeAnchor
componentClass="a"
href="/providers"
onClick={[Function]}
>
<a
href="/providers"
Expand Down
36 changes: 36 additions & 0 deletions app/javascript/spec/breadcrumbs/breadcrumbs.spec.js
@@ -1,6 +1,8 @@
import React from 'react';
import { mount } from 'enzyme';
import toJson from 'enzyme-to-json';

import * as clickFunctions from '../../components/breadcrumbs/on-click-functions';
import Breadcrumbs from '../../components/breadcrumbs';

describe('Breadcrumbs component', () => {
Expand All @@ -19,4 +21,38 @@ describe('Breadcrumbs component', () => {
const wrapper = mount(<Breadcrumbs {...props} />);
expect(toJson(wrapper)).toMatchSnapshot();
});

it('clicked on breadcrumb in tree', () => {
clickFunctions.onClickTree = jest.fn();

const initialProps = {
...props,
items: [
{ key: 'xx-11', title: 'Item 11' },
{ title: 'Header' },
],
};
const wrapper = mount(<Breadcrumbs {...initialProps} />);

wrapper.find('a').first().simulate('click');

expect(clickFunctions.onClickTree).toHaveBeenCalledWith(initialProps.controllerName, initialProps.items[0]);
});

it('clicked on breadcrumb', () => {
clickFunctions.onClick = jest.fn();

const initialProps = {
...props,
items: [
{ url: 'xx-11', title: 'Item 11' },
{ title: 'Header' },
],
};
const wrapper = mount(<Breadcrumbs {...initialProps} />);

wrapper.find('a').first().simulate('click');

expect(clickFunctions.onClick).toHaveBeenCalledWith(expect.any(Object), initialProps.items[0].url);
});
});
52 changes: 52 additions & 0 deletions app/javascript/spec/breadcrumbs/on-click-functions.spec.js
@@ -0,0 +1,52 @@
import { onClickTree, onClick } from '../../components/breadcrumbs/on-click-functions';

describe('Breadcrumbs onClick functions', () => {
describe('onClick', () => {
it('calls e.prevent default', () => {
const mockFn = jest.fn();
window.miqCheckForChanges = jest.fn(() => false);

onClick({ preventDefault: mockFn }, '/url');

expect(mockFn).toHaveBeenCalled();
expect(window.miqCheckForChanges).toHaveBeenCalled();

window.miqCheckForChanges.mockClear();
});

it('change location', () => {
const mockFn = jest.fn();
window.miqCheckForChanges = () => true;

onClick({ preventDefault: mockFn }, '/url');

expect(mockFn).not.toHaveBeenCalled();
});
});

describe('onClickTree', () => {
it('returns null if no changes', () => {
window.miqCheckForChanges = () => false;
jest.spyOn(window, 'sendDataWithRx');

const result = onClickTree();

expect(result).toEqual(null);
expect(window.sendDataWithRx).not.toHaveBeenCalled();
});

it('calls rxjs', () => {
window.miqCheckForChanges = () => true;
jest.spyOn(window, 'sendDataWithRx');

onClickTree('pxe', { key: 'xx-11' });

expect(window.sendDataWithRx).toHaveBeenCalledWith({
breadcrumbSelect: {
key: 'xx-11',
path: '/pxe/tree_select',
},
});
});
});
});