|
| 1 | +/* global expect, ReactDOM, React, ReactCytoscape, cy */ |
| 2 | + |
| 3 | +(function() { |
| 4 | + const defaults = { |
| 5 | + global: 'cy', |
| 6 | + id: 'cy', |
| 7 | + style: { width: '500px', height: '500px' }, |
| 8 | + zoom: 1, |
| 9 | + pan: { |
| 10 | + x: 0, |
| 11 | + y: 0 |
| 12 | + }, |
| 13 | + elements: [ |
| 14 | + { |
| 15 | + data: { id: 'a', label: 'apple' }, |
| 16 | + position: { x: 0, y: 0 }, |
| 17 | + scratch: { _test: 1 }, |
| 18 | + classes: 'foo bar' |
| 19 | + }, |
| 20 | + { |
| 21 | + data: { id: 'b', label: 'banana' }, |
| 22 | + position: { x: 0, y: 0 }, |
| 23 | + scratch: { _test: 2 }, |
| 24 | + classes: 'foo bar' |
| 25 | + }, |
| 26 | + { |
| 27 | + data: { id: 'c', label: 'cherry' }, |
| 28 | + position: { x: 0, y: 0 }, |
| 29 | + scratch: { _test: 3 }, |
| 30 | + classes: 'foo bar' |
| 31 | + } |
| 32 | + ], |
| 33 | + stylesheet: [ |
| 34 | + { |
| 35 | + selector: 'node', |
| 36 | + style: { |
| 37 | + 'width': 100 |
| 38 | + } |
| 39 | + } |
| 40 | + ] |
| 41 | + }; |
| 42 | + |
| 43 | + const cloneDefaults = () => JSON.parse(JSON.stringify(defaults)); |
| 44 | + |
| 45 | + /** |
| 46 | + * The TestComponent drives updates to the ReactCytoscape component. Updates |
| 47 | + * to the state of the TestComponent trigger updates to the props of |
| 48 | + * ReactCytoscape via componentDidUpdate(props). |
| 49 | + */ |
| 50 | + class TestComponent extends React.Component { |
| 51 | + constructor(props) { |
| 52 | + super(props); |
| 53 | + |
| 54 | + props.setStateRef(this.setState.bind(this)); |
| 55 | + |
| 56 | + this.state = props.defaults; |
| 57 | + } |
| 58 | + |
| 59 | + render() { |
| 60 | + return React.createElement(ReactCytoscape, this.state); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + describe('Rendering hints', function() { |
| 65 | + let root, setState, json; |
| 66 | + |
| 67 | + let updateCyProps = props => |
| 68 | + new Promise(resolve => setState(Object.assign({}, json, props), resolve)); |
| 69 | + |
| 70 | + afterEach(function() { |
| 71 | + ReactDOM.unmountComponentAtNode(root); |
| 72 | + document.body.removeChild(root); |
| 73 | + }); |
| 74 | + |
| 75 | + let createComponent = props => { |
| 76 | + json = Object.assign(cloneDefaults(), props); |
| 77 | + |
| 78 | + root = document.createElement('div'); |
| 79 | + |
| 80 | + document.body.appendChild(root); |
| 81 | + |
| 82 | + ReactDOM.render( |
| 83 | + React.createElement(TestComponent, { |
| 84 | + setStateRef: ref => (setState = ref), |
| 85 | + defaults: json |
| 86 | + }), |
| 87 | + root |
| 88 | + ); |
| 89 | + }; |
| 90 | + |
| 91 | + it('allows headless:true', function(){ |
| 92 | + createComponent({ headless: true }); |
| 93 | + |
| 94 | + expect(cy.style()).to.not.exist; |
| 95 | + |
| 96 | + // TODO add another assertion here if/when there is a public getter for headless state |
| 97 | + |
| 98 | + expect(cy.nodes().length).to.equal(3); // sanity check |
| 99 | + }); |
| 100 | + |
| 101 | + it('allows headless:true styleEnabled:true', function(){ |
| 102 | + createComponent({ headless: true, styleEnabled: true }); |
| 103 | + |
| 104 | + expect(cy.style()).to.exist; |
| 105 | + |
| 106 | + expect(cy.nodes().length).to.equal(3); // sanity check |
| 107 | + |
| 108 | + expect(cy.nodes().first().width()).to.equal(100); |
| 109 | + }); |
| 110 | + }); |
| 111 | +})(); |
0 commit comments