1
+ import HTTP, JSON2
2
+ using Test
3
+ using Dash
4
+
5
+ @testset " callid" begin
6
+ id = callid " id1.prop1 => id2.prop2"
7
+ @test id isa CallbackId
8
+ @test length (id. state) == 0
9
+ @test length (id. input) == 1
10
+ @test length (id. output) == 1
11
+ @test id. input[1 ] == (:id1 , :prop1 )
12
+ @test id. output[1 ] == (:id2 , :prop2 )
13
+
14
+ id = callid " {state1.prop1, state2.prop2} input1.prop1, input2.prop2 => output1.prop1, output2.prop2"
15
+ @test id isa CallbackId
16
+ @test length (id. state) == 2
17
+ @test length (id. input) == 2
18
+ @test length (id. output) == 2
19
+ @test id. input[1 ] == (:input1 , :prop1 )
20
+ @test id. input[2 ] == (:input2 , :prop2 )
21
+ @test id. output[1 ] == (:output1 , :prop1 )
22
+ @test id. output[2 ] == (:output2 , :prop2 )
23
+ @test id. state[1 ] == (:state1 , :prop1 )
24
+ @test id. state[2 ] == (:state2 , :prop2 )
25
+ end
26
+
27
+ @testset " callback!" begin
28
+ app = dash ()
29
+ app. layout = html_div () do
30
+ dcc_input (id = " my-id" , value= " initial value" , type = " text" ),
31
+ html_div (id = " my-div" )
32
+ end
33
+
34
+ callback! (app, callid " my-id.value => my-div.children" ) do value
35
+ return value
36
+ end
37
+ @test length (app. callbacks) == 1
38
+ @test haskey (app. callbacks, Symbol (" my-div.children" ))
39
+ @test app. callbacks[Symbol (" my-div.children" )]. func (" test" ) == " test"
40
+
41
+ handler = make_handler (app)
42
+ request = HTTP. Request (" GET" , " /_dash-dependencies" )
43
+ resp = HTTP. handle (handler, request)
44
+ deps = JSON2. read (String (resp. body))
45
+
46
+ @test length (deps) == 1
47
+ cb = deps[1 ]
48
+ @test cb. output == " my-div.children"
49
+ @test cb. inputs[1 ]. id == " my-id"
50
+ @test cb. inputs[1 ]. property == " value"
51
+ @test :clientside_function in keys (cb)
52
+ @test isnothing (cb. clientside_function)
53
+
54
+ app = dash ()
55
+ app. layout = html_div () do
56
+ dcc_input (id = " my-id" , value= " initial value" , type = " text" ),
57
+ html_div (id = " my-div" ),
58
+ html_div (id = " my-div2" )
59
+ end
60
+ callback! (app, callid " {my-id.type} my-id.value => my-div.children, my-div2.children" ) do state, value
61
+ return state, value
62
+ end
63
+ @test length (app. callbacks) == 1
64
+ @test haskey (app. callbacks, Symbol (" ..my-div.children...my-div2.children.." ))
65
+ @test app. callbacks[Symbol (" ..my-div.children...my-div2.children.." )]. func (" state" , " value" ) == (" state" , " value" )
66
+
67
+ app = dash ()
68
+
69
+ app. layout = html_div () do
70
+ dcc_input (id = " my-id" , value= " initial value" , type = " text" ),
71
+ html_div (id = " my-div" ),
72
+ html_div (id = " my-div2" )
73
+ end
74
+
75
+ callback! (app, callid " my-id.value => my-div.children" ) do value
76
+ return value
77
+ end
78
+ callback! (app, callid " my-id.value => my-div2.children" ) do value
79
+ return " v_$(value) "
80
+ end
81
+
82
+ @test length (app. callbacks) == 2
83
+ @test haskey (app. callbacks, Symbol (" my-div.children" ))
84
+ @test haskey (app. callbacks, Symbol (" my-div2.children" ))
85
+ @test app. callbacks[Symbol (" my-div.children" )]. func (" value" ) == " value"
86
+ @test app. callbacks[Symbol (" my-div2.children" )]. func (" value" ) == " v_value"
87
+
88
+ @test_throws ErrorException callback! (app, callid " my-id.value => my-div2.children" ) do value
89
+ return " v_$(value) "
90
+ end
91
+
92
+ @test_throws ErrorException callback! (app, callid " {my-id.value} my-id.value => my-id.value" ) do value
93
+ return " v_$(value) "
94
+ end
95
+
96
+ @test_throws ErrorException callback! (app, callid " my-div.children, my-id.value => my-id.value" ) do value
97
+ return " v_$(value) "
98
+ end
99
+ @test_throws ErrorException callback! (app, callid " my-id.value => my-div.children, my-id.value" ) do value
100
+ return " v_$(value) "
101
+ end
102
+
103
+ @test_throws ErrorException callback! (app, callid " => my-div2.title, my-id.value" ) do value
104
+ return " v_$(value) "
105
+ end
106
+
107
+ @test_throws ErrorException callback! (app, callid " my-id.value => my-div2.title, my-div2.title" ) do value
108
+ return " v_$(value) "
109
+ end
110
+
111
+ @test_throws ErrorException callback! (app, callid " my-id.value => my-div2.title" ) do
112
+ return " v_"
113
+ end
114
+
115
+
116
+ app = dash ()
117
+ app. layout = html_div () do
118
+ dcc_input (id = " my-id" , value= " initial value" , type = " text" ),
119
+ html_div (" test2" , id = " my-div" ),
120
+ html_div (id = " my-div2" ) do
121
+ html_h1 (" gggg" , id = " my-h" )
122
+ end
123
+ end
124
+ callback! (app, callid " {my-id.type} my-id.value => my-div.children, my-h.children" ) do state, value
125
+ return state, value
126
+ end
127
+ @test length (app. callbacks) == 1
128
+ end
129
+
130
+ @testset " clientside callbacks function" begin
131
+ app = dash ()
132
+ app. layout = html_div () do
133
+ dcc_input (id = " my-id" , value= " initial value" , type = " text" ),
134
+ html_div (id = " my-div" )
135
+ end
136
+
137
+ callback! (ClientsideFunction (" namespace" , " func_name" ),app, callid " my-id.value => my-div.children" )
138
+
139
+ @test length (app. callbacks) == 1
140
+ @test haskey (app. callbacks, Symbol (" my-div.children" ))
141
+ @test app. callbacks[Symbol (" my-div.children" )]. func isa ClientsideFunction
142
+ @test app. callbacks[Symbol (" my-div.children" )]. func. namespace == " namespace"
143
+ @test app. callbacks[Symbol (" my-div.children" )]. func. function_name == " func_name"
144
+
145
+ @test_throws ErrorException callback! (app, callid " my-id.value => my-div.children" ) do value
146
+ return " v_$(value) "
147
+ end
148
+
149
+ handler = make_handler (app)
150
+ request = HTTP. Request (" GET" , " /_dash-dependencies" )
151
+ resp = HTTP. handle (handler, request)
152
+ deps = JSON2. read (String (resp. body))
153
+
154
+ @test length (deps) == 1
155
+ cb = deps[1 ]
156
+ @test cb. output == " my-div.children"
157
+ @test cb. inputs[1 ]. id == " my-id"
158
+ @test cb. inputs[1 ]. property == " value"
159
+ @test :clientside_function in keys (cb)
160
+ @test cb. clientside_function. namespace == " namespace"
161
+ @test cb. clientside_function. function_name == " func_name"
162
+ end
163
+ @testset " clientside callbacks string" begin
164
+ app = dash ()
165
+ app. layout = html_div () do
166
+ dcc_input (id = " my-id" , value= " initial value" , type = " text" ),
167
+ html_div (id = " my-div" )
168
+ end
169
+
170
+ callback! (
171
+ """
172
+ function(input_value) {
173
+ return (
174
+ parseFloat(input_value_1, 10)
175
+ );
176
+ }
177
+ """
178
+ , app, callid " my-id.value => my-div.children"
179
+ )
180
+
181
+ @test length (app. callbacks) == 1
182
+ @test haskey (app. callbacks, Symbol (" my-div.children" ))
183
+ @test app. callbacks[Symbol (" my-div.children" )]. func isa ClientsideFunction
184
+ @test app. callbacks[Symbol (" my-div.children" )]. func. namespace == " _dashprivate_my-div"
185
+ @test app. callbacks[Symbol (" my-div.children" )]. func. function_name == " children"
186
+ @test length (app. inline_scripts) == 1
187
+ @test occursin (" clientside[\" _dashprivate_my-div\" ]" , app. inline_scripts[1 ])
188
+ @test occursin (" ns[\" children\" ]" , app. inline_scripts[1 ])
189
+
190
+ handler = make_handler (app)
191
+ request = HTTP. Request (" GET" , " /_dash-dependencies" )
192
+ resp = HTTP. handle (handler, request)
193
+ deps = JSON2. read (String (resp. body))
194
+
195
+ @test length (deps) == 1
196
+ cb = deps[1 ]
197
+ @test cb. output == " my-div.children"
198
+ @test cb. inputs[1 ]. id == " my-id"
199
+ @test cb. inputs[1 ]. property == " value"
200
+ @test :clientside_function in keys (cb)
201
+ @test cb. clientside_function. namespace == " _dashprivate_my-div"
202
+ @test cb. clientside_function. function_name == " children"
203
+ request = HTTP. Request (" GET" , " /" )
204
+ resp = HTTP. handle (handler, request)
205
+ body = String (resp. body)
206
+ @test occursin (" clientside[\" _dashprivate_my-div\" ]" , body)
207
+ @test occursin (" ns[\" children\" ]" , body)
208
+ end
0 commit comments