2017-06-13 01:02:59 +05:30
|
|
|
import sinon from 'sinon';
|
2016-07-30 16:14:43 +05:30
|
|
|
import expect from 'unexpected';
|
|
|
|
|
2016-05-01 23:20:55 +05:30
|
|
|
import React from 'react';
|
|
|
|
|
2016-07-26 10:10:45 +05:30
|
|
|
import { shallow, mount } from 'enzyme';
|
2016-05-01 23:20:55 +05:30
|
|
|
|
|
|
|
import { PopupStack } from 'components/ui/popup/PopupStack';
|
2016-07-26 10:10:45 +05:30
|
|
|
import styles from 'components/ui/popup/popup.scss';
|
2016-05-01 23:20:55 +05:30
|
|
|
|
2016-07-26 10:10:45 +05:30
|
|
|
function DummyPopup() {return null;}
|
2016-05-01 23:20:55 +05:30
|
|
|
|
|
|
|
describe('<PopupStack />', () => {
|
|
|
|
it('renders all popup components', () => {
|
|
|
|
const props = {
|
|
|
|
destroy: () => {},
|
|
|
|
popups: [
|
|
|
|
{
|
2016-07-26 10:10:45 +05:30
|
|
|
Popup: DummyPopup
|
2016-05-01 23:20:55 +05:30
|
|
|
},
|
|
|
|
{
|
2016-07-26 10:10:45 +05:30
|
|
|
Popup: DummyPopup
|
2016-05-01 23:20:55 +05:30
|
|
|
}
|
|
|
|
]
|
|
|
|
};
|
|
|
|
const component = shallow(<PopupStack {...props} />);
|
|
|
|
|
2016-07-30 16:14:43 +05:30
|
|
|
expect(component.find(DummyPopup), 'to satisfy', {length: 2});
|
2016-05-01 23:20:55 +05:30
|
|
|
});
|
|
|
|
|
2016-07-26 10:10:45 +05:30
|
|
|
it('should pass onClose as props', () => {
|
2016-05-01 23:20:55 +05:30
|
|
|
const expectedProps = {
|
|
|
|
foo: 'bar'
|
|
|
|
};
|
|
|
|
|
|
|
|
const props = {
|
|
|
|
destroy: () => {},
|
|
|
|
popups: [
|
|
|
|
{
|
2016-07-26 10:10:45 +05:30
|
|
|
Popup: (props = {}) => {
|
2017-08-23 00:27:35 +05:30
|
|
|
// eslint-disable-next-line
|
2016-07-30 16:14:43 +05:30
|
|
|
expect(props.onClose, 'to be a', 'function');
|
2016-07-26 10:10:45 +05:30
|
|
|
|
|
|
|
return <DummyPopup {...expectedProps} />;
|
|
|
|
}
|
2016-05-01 23:20:55 +05:30
|
|
|
}
|
|
|
|
]
|
|
|
|
};
|
2016-07-26 10:10:45 +05:30
|
|
|
const component = mount(<PopupStack {...props} />);
|
2016-05-01 23:20:55 +05:30
|
|
|
|
2016-07-26 10:10:45 +05:30
|
|
|
const popup = component.find(DummyPopup);
|
2016-07-30 16:14:43 +05:30
|
|
|
expect(popup, 'to satisfy', {length: 1});
|
|
|
|
expect(popup.props(), 'to equal', expectedProps);
|
2016-05-01 23:20:55 +05:30
|
|
|
});
|
|
|
|
|
2016-07-26 10:10:45 +05:30
|
|
|
it('should hide popup, when onClose called', () => {
|
|
|
|
const props = {
|
|
|
|
popups: [
|
|
|
|
{
|
|
|
|
Popup: DummyPopup
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Popup: DummyPopup
|
|
|
|
}
|
|
|
|
],
|
2016-07-30 16:14:43 +05:30
|
|
|
destroy: sinon.stub().named('props.destroy')
|
2016-05-01 23:20:55 +05:30
|
|
|
};
|
2016-07-26 10:10:45 +05:30
|
|
|
const component = shallow(<PopupStack {...props} />);
|
|
|
|
|
|
|
|
component.find(DummyPopup).last().prop('onClose')();
|
2016-05-01 23:20:55 +05:30
|
|
|
|
2016-07-30 16:14:43 +05:30
|
|
|
expect(props.destroy, 'was called once');
|
|
|
|
expect(props.destroy, 'to have a call satisfying', [
|
|
|
|
expect.it('to be', props.popups[1])
|
|
|
|
]);
|
2016-07-26 10:10:45 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
it('should hide popup, when overlay clicked', () => {
|
2016-07-30 16:14:43 +05:30
|
|
|
const preventDefault = sinon.stub().named('event.preventDefault');
|
2016-05-01 23:20:55 +05:30
|
|
|
const props = {
|
2016-07-30 16:14:43 +05:30
|
|
|
destroy: sinon.stub().named('props.destroy'),
|
2016-05-01 23:20:55 +05:30
|
|
|
popups: [
|
|
|
|
{
|
2016-07-26 10:10:45 +05:30
|
|
|
Popup: DummyPopup
|
2016-05-01 23:20:55 +05:30
|
|
|
}
|
|
|
|
]
|
|
|
|
};
|
|
|
|
const component = shallow(<PopupStack {...props} />);
|
|
|
|
|
2016-07-26 10:10:45 +05:30
|
|
|
const overlay = component.find(`.${styles.overlay}`);
|
|
|
|
overlay.simulate('click', {target: 1, currentTarget: 1, preventDefault});
|
|
|
|
|
2016-07-30 16:14:43 +05:30
|
|
|
expect(props.destroy, 'was called once');
|
|
|
|
expect(preventDefault, 'was called once');
|
2016-05-01 23:20:55 +05:30
|
|
|
});
|
|
|
|
|
2016-07-26 10:10:45 +05:30
|
|
|
it('should hide popup on overlay click if disableOverlayClose', () => {
|
2016-05-01 23:20:55 +05:30
|
|
|
const props = {
|
2016-07-30 16:14:43 +05:30
|
|
|
destroy: sinon.stub().named('props.destroy'),
|
2016-05-01 23:20:55 +05:30
|
|
|
popups: [
|
|
|
|
{
|
2016-07-26 10:10:45 +05:30
|
|
|
Popup: DummyPopup,
|
|
|
|
disableOverlayClose: true
|
2016-05-01 23:20:55 +05:30
|
|
|
}
|
2016-07-26 10:10:45 +05:30
|
|
|
]
|
2016-05-01 23:20:55 +05:30
|
|
|
};
|
|
|
|
const component = shallow(<PopupStack {...props} />);
|
|
|
|
|
2016-07-26 10:10:45 +05:30
|
|
|
const overlay = component.find(`.${styles.overlay}`);
|
|
|
|
overlay.simulate('click', {target: 1, currentTarget: 1, preventDefault() {}});
|
2016-05-01 23:20:55 +05:30
|
|
|
|
2016-07-30 16:14:43 +05:30
|
|
|
expect(props.destroy, 'was not called');
|
2016-05-01 23:20:55 +05:30
|
|
|
});
|
2016-07-26 10:50:37 +05:30
|
|
|
|
|
|
|
it('should hide popup, when esc pressed', () => {
|
|
|
|
const props = {
|
2016-07-30 16:14:43 +05:30
|
|
|
destroy: sinon.stub().named('props.destroy'),
|
2016-07-26 10:50:37 +05:30
|
|
|
popups: [
|
|
|
|
{
|
|
|
|
Popup: DummyPopup
|
|
|
|
}
|
|
|
|
]
|
|
|
|
};
|
|
|
|
mount(<PopupStack {...props} />);
|
|
|
|
|
|
|
|
const event = new Event('keyup');
|
|
|
|
event.which = 27;
|
|
|
|
document.dispatchEvent(event);
|
|
|
|
|
2016-07-30 16:14:43 +05:30
|
|
|
expect(props.destroy, 'was called once');
|
2016-07-26 10:50:37 +05:30
|
|
|
});
|
|
|
|
|
2016-07-27 00:58:58 +05:30
|
|
|
it('should hide first popup in stack if esc pressed', () => {
|
2016-07-26 10:50:37 +05:30
|
|
|
const props = {
|
2016-07-30 16:14:43 +05:30
|
|
|
destroy: sinon.stub().named('props.destroy'),
|
2016-07-26 10:50:37 +05:30
|
|
|
popups: [
|
|
|
|
{
|
|
|
|
Popup() {return null;}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Popup: DummyPopup
|
|
|
|
}
|
|
|
|
]
|
|
|
|
};
|
|
|
|
mount(<PopupStack {...props} />);
|
|
|
|
|
|
|
|
const event = new Event('keyup');
|
|
|
|
event.which = 27;
|
|
|
|
document.dispatchEvent(event);
|
|
|
|
|
2016-07-30 16:14:43 +05:30
|
|
|
expect(props.destroy, 'was called once');
|
|
|
|
expect(props.destroy, 'to have a call satisfying', [
|
|
|
|
expect.it('to be', props.popups[1])
|
|
|
|
]);
|
2016-07-26 10:50:37 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
it('should NOT hide popup on esc pressed if disableOverlayClose', () => {
|
|
|
|
const props = {
|
2016-07-30 16:14:43 +05:30
|
|
|
destroy: sinon.stub().named('props.destroy'),
|
2016-07-26 10:50:37 +05:30
|
|
|
popups: [
|
|
|
|
{
|
|
|
|
Popup: DummyPopup,
|
|
|
|
disableOverlayClose: true
|
|
|
|
}
|
|
|
|
]
|
|
|
|
};
|
|
|
|
mount(<PopupStack {...props} />);
|
|
|
|
|
|
|
|
const event = new Event('keyup');
|
|
|
|
event.which = 27;
|
|
|
|
document.dispatchEvent(event);
|
|
|
|
|
2016-07-30 16:14:43 +05:30
|
|
|
expect(props.destroy, 'was not called');
|
2016-07-26 10:50:37 +05:30
|
|
|
});
|
2016-05-01 23:20:55 +05:30
|
|
|
});
|