Skip to content

Commit 4d6f9ca

Browse files
committed
Added 3 new tests for promise handling
1 parent 83f53d4 commit 4d6f9ca

File tree

3 files changed

+115
-4
lines changed

3 files changed

+115
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
All notable changes to `dash` will be documented in this file.
33
This project adheres to [Semantic Versioning](https://semver.org/).
44

5-
## [2.3.2]
5+
## [Unreleased]
66

77
### Added
88

tests/integration/clientside/assets/clientside.js

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ window.dash_clientside.clientside = {
5353
'side effect'
5454
);
5555
}, 100);
56-
resolve('output');
56+
resolve('foo');
5757
}, 1);
5858
});
5959
},
@@ -98,5 +98,27 @@ window.dash_clientside.clientside = {
9898
}
9999
window.callCount += 1;
100100
return inputValue.toString();
101-
}
101+
},
102+
103+
chained_promise: function (inputValue) {
104+
return new Promise(function (resolve) {
105+
setTimeout(function () {
106+
resolve(inputValue + "-chained");
107+
}, 100);
108+
});
109+
},
110+
111+
delayed_promise: function (inputValue) {
112+
return new Promise(function (resolve) {
113+
window.callbackDone = function (deferredValue) {
114+
resolve("clientside-" + inputValue + "-" + deferredValue);
115+
};
116+
});
117+
},
118+
119+
non_delayed_promise: function (inputValue) {
120+
return new Promise(function (resolve) {
121+
resolve("clientside-" + inputValue);
122+
});
123+
},
102124
};

tests/integration/clientside/test_clientside.py

Lines changed: 90 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# -*- coding: UTF-8 -*-
2-
from multiprocessing import Value
2+
from multiprocessing import Value, Lock
3+
import pytest
34

45
from dash import Dash, Input, Output, State, ClientsideFunction, ALL, html, dcc
56
from selenium.webdriver.common.keys import Keys
@@ -223,6 +224,7 @@ def test_clsd004_clientside_multiple_outputs(dash_duo):
223224
dash_duo.wait_for_text_to_equal(selector, expected)
224225

225226

227+
@pytest.mark.xfail(reason="Promises are now handled within Dash-Renderer")
226228
def test_clsd005_clientside_fails_when_returning_a_promise(dash_duo):
227229
app = Dash(__name__, assets_folder="assets")
228230

@@ -716,3 +718,90 @@ def test_clsd014_input_output_callback(dash_duo):
716718
assert call_count == 2, "initial + changed once"
717719

718720
assert dash_duo.get_logs() == []
721+
722+
723+
def test_clsd015_clientside_chained_callbacks_returning_promise(dash_duo):
724+
app = Dash(__name__, assets_folder="assets")
725+
726+
app.layout = html.Div(
727+
[
728+
html.Div(id="input", children=["initial"]),
729+
html.Div(id="div-1"),
730+
html.Div(id="div-2"),
731+
]
732+
)
733+
734+
app.clientside_callback(
735+
ClientsideFunction(namespace="clientside", function_name="chained_promise"),
736+
Output("div-1", "children"),
737+
Input("input", "children"),
738+
)
739+
740+
@app.callback(Output("div-2", "children"), Input("div-1", "children"))
741+
def callback(value):
742+
return value + "-twice"
743+
744+
dash_duo.start_server(app)
745+
746+
dash_duo.wait_for_text_to_equal("#div-1", "initial-chained")
747+
dash_duo.wait_for_text_to_equal("#div-2", "initial-chained-twice")
748+
749+
750+
def test_clsd016_serverside_clientside_shared_input_with_promise(dash_duo):
751+
app = Dash(__name__, assets_folder="assets")
752+
753+
app.layout = html.Div(
754+
[
755+
html.Div(id="input", children=["initial"]),
756+
html.Div(id="clientside-div"),
757+
html.Div(id="serverside-div"),
758+
]
759+
)
760+
761+
app.clientside_callback(
762+
ClientsideFunction(namespace="clientside", function_name="delayed_promise"),
763+
Output("clientside-div", "children"),
764+
Input("input", "children"),
765+
)
766+
767+
@app.callback(Output("serverside-div", "children"), Input("input", "children"))
768+
def callback(value):
769+
return "serverside-" + value[0]
770+
771+
dash_duo.start_server(app)
772+
773+
dash_duo.wait_for_text_to_equal("#serverside-div", "serverside-initial")
774+
dash_duo.driver.execute_script("window.callbackDone('deferred')")
775+
dash_duo.wait_for_text_to_equal("#clientside-div", "clientside-initial-deferred")
776+
777+
778+
def test_clsd017_clientside_serverside_shared_input_with_promise(dash_duo):
779+
lock = Lock()
780+
lock.acquire()
781+
782+
app = Dash(__name__, assets_folder="assets")
783+
784+
app.layout = html.Div(
785+
[
786+
html.Div(id="input", children=["initial"]),
787+
html.Div(id="clientside-div"),
788+
html.Div(id="serverside-div"),
789+
]
790+
)
791+
792+
app.clientside_callback(
793+
ClientsideFunction(namespace="clientside", function_name="non_delayed_promise"),
794+
Output("clientside-div", "children"),
795+
Input("input", "children"),
796+
)
797+
798+
@app.callback(Output("serverside-div", "children"), Input("input", "children"))
799+
def callback(value):
800+
with lock:
801+
return "serverside-" + value[0] + "-deferred"
802+
803+
dash_duo.start_server(app)
804+
805+
dash_duo.wait_for_text_to_equal("#clientside-div", "clientside-initial")
806+
lock.release()
807+
dash_duo.wait_for_text_to_equal("#serverside-div", "serverside-initial-deferred")

0 commit comments

Comments
 (0)