1 7
from functools import partial
2

3 7
import param
4

5 7
from bokeh.models import Div
6 7
from panel.layout import Tabs, WidgetBox
7 7
from panel.reactive import Reactive
8 7
from panel.viewable import Layoutable
9 7
from panel.widgets import Checkbox, StaticText, TextInput
10

11

12 7
def test_link():
13
    "Link two Reactive objects"
14

15 7
    class ReactiveLink(Reactive):
16

17 7
        a = param.Parameter()
18

19 7
    obj = ReactiveLink()
20 7
    obj2 = ReactiveLink()
21 7
    obj.link(obj2, a='a')
22 7
    obj.a = 1
23

24 7
    assert obj.a == 1
25 7
    assert obj2.a == 1
26

27

28 7
def test_param_rename():
29
    "Test that Reactive renames params and properties"
30

31 7
    class ReactiveRename(Reactive):
32

33 7
        a = param.Parameter()
34
    
35 7
        _rename = {'a': 'b'}
36

37 7
    obj = ReactiveRename()
38

39 7
    params = obj._process_property_change({'b': 1})
40 7
    assert params == {'a': 1}
41

42 7
    properties = obj._process_param_change({'a': 1})
43 7
    assert properties == {'b': 1}
44

45

46 7
def test_link_properties_nb(document, comm):
47

48 7
    class ReactiveLink(Reactive):
49

50 7
        text = param.String(default='A')
51

52 7
    obj = ReactiveLink()
53 7
    div = Div()
54

55
    # Link property and check bokeh js property callback is defined
56 7
    obj._link_props(div, ['text'], document, div, comm)
57 7
    assert 'text' in div._callbacks
58

59
    # Assert callback is set up correctly
60 7
    cb = div._callbacks['text'][0]
61 7
    assert isinstance(cb, partial)
62 7
    assert cb.args == (document, div.ref['id'], comm)
63 7
    assert cb.func == obj._comm_change
64

65

66 7
def test_link_properties_server(document):
67

68 7
    class ReactiveLink(Reactive):
69

70 7
        text = param.String(default='A')
71

72 7
    obj = ReactiveLink()
73 7
    div = Div()
74

75
    # Link property and check bokeh callback is defined
76 7
    obj._link_props(div, ['text'], document, div)
77 7
    assert 'text' in div._callbacks
78

79
    # Assert callback is set up correctly
80 7
    cb = div._callbacks['text'][0]
81 7
    assert isinstance(cb, partial)
82 7
    assert cb.args == (document, div.ref['id'])
83 7
    assert cb.func == obj._server_change
84

85

86 7
def test_text_input_controls():
87 7
    text_input = TextInput()
88

89 7
    controls = text_input.controls()
90

91 7
    assert isinstance(controls, Tabs)
92 7
    assert len(controls) == 2
93 7
    wb1, wb2 = controls
94 7
    assert isinstance(wb1, WidgetBox)
95 7
    assert len(wb1) == 4
96 7
    name, disabled, text1, text2 = wb1
97 7
    if text1.name == 'Placeholder':
98 0
        placeholder, value = text1, text2
99
    else:
100 7
        placeholder, value = text2, text1
101

102 7
    assert isinstance(name, StaticText)
103 7
    assert isinstance(disabled, Checkbox)
104 7
    assert isinstance(value, TextInput)
105 7
    assert isinstance(placeholder, TextInput)
106

107 7
    text_input.disabled = True
108 7
    assert disabled.value
109

110 7
    text_input.placeholder = "Test placeholder..."
111 7
    assert placeholder.value == "Test placeholder..."
112

113 7
    text_input.value = "New value"
114 7
    assert value.value == "New value"
115

116 7
    assert isinstance(wb2, WidgetBox)
117 7
    assert len(wb2) == len(list(Layoutable.param)) + 1
118

119

120

121 7
def test_text_input_controls_explicit():
122 7
    text_input = TextInput()
123

124 7
    controls = text_input.controls(['placeholder', 'disabled'])
125

126 7
    assert isinstance(controls, WidgetBox)
127 7
    assert len(controls) == 3
128 7
    name, disabled, placeholder = controls
129
    
130 7
    assert isinstance(name, StaticText)
131 7
    assert isinstance(disabled, Checkbox)
132 7
    assert isinstance(placeholder, TextInput)
133

134 7
    text_input.disabled = True
135 7
    assert disabled.value
136

137 7
    text_input.placeholder = "Test placeholder..."
138 7
    assert placeholder.value == "Test placeholder..."

Read our documentation on viewing source code .

Loading