This repository was archived by the owner on Nov 24, 2024. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathtests_react.py
More file actions
115 lines (95 loc) · 3.32 KB
/
Copy pathtests_react.py
File metadata and controls
115 lines (95 loc) · 3.32 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
try:
from unittest.mock import patch
except ImportError:
from mock import patch
import unittest
import logging
import collections
import six
import react
from react.react import _DEFINE_GLOBALS_SCRIPT
logging.disable(logging.CRITICAL)
class ReactTest(unittest.TestCase):
def setUp(self):
pass
def tearDown(self):
pass
def test_set_up(self):
"""
It should call utils.setup
"""
with patch('react.utils.set_up', autospec=True) as s:
with patch('react.utils.run_script', autospec=True) as r:
react.set_up()
s.assert_called_once_with()
r.assert_called_once_with(_DEFINE_GLOBALS_SCRIPT)
def test_set_up_globals(self):
"""
It should register globals
"""
react.utils.run_script('global.foo = "foo"')
self.assertEqual(react.utils.run_script('global.foo'), 'foo')
self.assertEqual(react.utils.run_script('delete global.foo'), 'true')
self.assertEqual(react.utils.run_script('console.warn()'), 'undefined')
self.assertEqual(react.utils.run_script('console.error()'), 'undefined')
self.assertEqual(react.utils.run_script('console.info()'), 'undefined')
self.assertEqual(react.utils.run_script('console.log()'), 'undefined')
def test_build_js_script(self):
"""
It should build the react call
"""
data = collections.OrderedDict((
('foo', 'foo'),
('bar', 1)))
react_ = react.React(data)
self.assertEqual(
'global.RenderToString({"foo": "foo", "bar": 1})',
react_.build_js_script())
def test_render(self):
"""
It should call react
"""
data = collections.OrderedDict((
('foo', 'foo'),
('bar', 1)))
react_ = react.React(data)
with patch('react.utils.run_script', autospec=True) as r:
r.return_value = 'foo'
self.assertEqual('foo', react_.render())
r.assert_called_once_with(
'global.RenderToString({"foo": "foo", "bar": 1})')
def test_render_exception(self):
"""
It should propagate V8 exceptions
"""
react_ = react.React({})
with patch(
'react.utils.run_script',
autospec=True,
side_effect=react.excepts.V8Error) as r:
self.assertRaises(react.excepts.V8Error, react_.render)
r.assert_called_once_with(
'global.RenderToString({})')
def test_to_json(self):
"""
It should convert data to json
"""
self.assertEqual(
'{"foo": "foo", "bar": 1}',
react.React.to_json(collections.OrderedDict(
(('foo', 'foo'), ('bar', 1)))))
self.assertEqual(
'{"foo": "foo", "bar": 1}',
react.React.to_json('{"foo": "foo", "bar": 1}'))
self.assertIsInstance(
react.React.to_json('{"foo": "foo", "bar": 1}'),
six.text_type)
def test_to_dict(self):
"""
It should convert json to dict
"""
self.assertDictEqual(
{"foo": "foo", "bar": 1},
react.React.to_dict('{"foo": "foo", "bar": 1}'))