aboutsummaryrefslogtreecommitdiffstats
path: root/test/react/PopupMenu.spec.js
blob: 50147288627e39f8484970aab208961c0c180fcd (plain)
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import React from 'react';
import PopupMenu from '../../src/react/PopupMenu.js';

import renderer from 'react-test-renderer';
import {mount} from 'enzyme';

class MyPopupForm extends React.Component {
	constructor(props) {
		super(props);
		this.state = {position: {}};
	}

	handleClick(newPos) {
		this.setState({position: newPos});
	}

	getPosition() {
		return this.state.position;
	}

	render() {
		return (
			<form onClick={event => this.handleClick({
				x: event.pageX - event.target.offsetLeft,
				y: event.pageY - event.target.offsetTop
			})}>
				<PopupMenu position={this.state.position} relative onMenuItemClick={() => {}} />
			</form>
		);
	}
}

describe('PopupMenu', () => {
	test('check static menu rendered', () => {
		const menu = renderer.create(<PopupMenu onMenuItemClick={() => {}}/>).toJSON();
		expect(menu).toMatchSnapshot();
	});

	test('check relative menu rendered', () => {
		const menu = renderer.create(<PopupMenu onMenuItemClick={()=> {}} position={{x: 10, y: 10}} relative/>).toJSON();
		expect(menu).toMatchSnapshot();
	});

	test('check separator rendered', () => {
		const separator = renderer.create(<PopupMenu.Separator/>).toJSON();
		expect(separator).toMatchSnapshot();
	});

	test('check position changed', () => {
		const menuForm = mount(<MyPopupForm />);
		const position = menuForm.instance().getPosition();
		expect(position).toEqual({});
		expect(position).toEqual(menuForm.find('ul').props().style);
		menuForm.find('form').simulate('click', {
			target: {offsetLeft: 10, offsetTop: 20},
			pageX: 30, pageY: 50
		});
		const newPosition = menuForm.instance().getPosition();
		expect(newPosition).toEqual({x:20, y:30});
		expect({left: newPosition.x, top: newPosition.y}).toEqual(menuForm.find('ul').props().style);
	});
});