def test_jsx(self): ans = dukpy.jsx_compile('var react_hello = <h1>Hello, world!</h1>;') expected = """"use strict"; var react_hello = React.createElement(\n "h1",\n null,\n "Hello, world!"\n);""" assert expected == ans, report_diff(expected, ans)
def input(self, _in, out, **kw): options = {'filename': os.path.basename(kw['source_path'])} if self.loader == 'systemjs': options['plugins'] = ['transform-es2015-modules-systemjs'] elif self.loader == 'umd': options['plugins'] = ['transform-es2015-modules-umd'] src = dukpy.jsx_compile(_in.read(), **options) out.write(src)
def test_jsx_mixed(self): code = ''' var React = require('react/react'), ReactDOM = require('react/react-dom-server'); ReactDOM.renderToStaticMarkup(<h1>Hello, world!</h1>, null); ''' jsx = dukpy.jsx_compile(code) res = dukpy.evaljs(jsx) assert res == '<h1>Hello, world!</h1>', res
def test_install_react(self): dukpy.install_jspackage('react', '0.14.8', self.tmpdir) dukpy.install_jspackage('react-dom', '0.14.8', self.tmpdir) jsx = dukpy.jsx_compile(TEST_CODE) jsi = dukpy.JSInterpreter() jsi.loader.register_path(self.tmpdir) res = jsi.evaljs(jsx, data={'id': 1, 'name': "Alessandro"}) assert res == '<div class="helloworld">Hello Alessandro</div>', res
def validateJavascript(self, text): ''' # Detect if all texts are Javascript ''' try: jsx = dukpy.jsx_compile(text) except Exception as e: #print('Invalid Javascript.') pass return False
def test_hello_world(self): jsx = dukpy.jsx_compile('var react_hello = <h1>Hello, world!</h1>;') jsi = dukpy.JSInterpreter() result = jsi.evaljs([ ''' var React = require('react/react'), ReactDOM = require('react/react-dom-server'); ''', jsx, 'ReactDOM.renderToStaticMarkup(react_hello, null);' ]) assert result == '<h1>Hello, world!</h1>', res
def test_jsx6(self): ans = dukpy.jsx_compile(''' import Component from 'react'; class HelloWorld extends Component { render() { return ( <div className="helloworld"> Hello {this.props.data.name} </div> ); } } ''') assert '_createClass(HelloWorld,' in ans, ans
def test_jsx6(self): code = ''' import React from 'react/react'; var ReactDOM = require('react/react-dom-server'); class HelloWorld extends React.Component { render() { return ( <div className="helloworld"> Hello {this.props.data.name} </div> ); } } ReactDOM.renderToStaticMarkup(<HelloWorld data={dukpy.data}/>, null); ''' jsx = dukpy.jsx_compile(code) res = dukpy.evaljs(jsx, data={'id': 1, 'name': "Alessandro"}) assert res == '<div class="helloworld">Hello Alessandro</div>', res
def test_react_binding(self): code = ''' var React = require('react/react'), ReactDOM = require('react/react-dom-server'); var HelloWorld = React.createClass({ render: function() { return ( <div className="helloworld"> Hello {this.props.data.name} </div> ); } }); ReactDOM.renderToStaticMarkup(<HelloWorld data={dukpy.data}/>, null); ''' jsx = dukpy.jsx_compile(code) res = dukpy.evaljs(jsx, data={'id': 1, 'name': "Alessandro"}) assert res == '<div class="helloworld">Hello Alessandro</div>', res