Skip to content

Commit

Permalink
Add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
philippjfr committed Oct 30, 2018
1 parent 338b817 commit c793b5e
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 4 deletions.
28 changes: 28 additions & 0 deletions panel/tests/test_widgets.py
Expand Up @@ -5,6 +5,8 @@

from bokeh.layouts import WidgetBox
from bokeh.models import Div as BkDiv, Slider as BkSlider
from panel.util import block_comm
from panel.viewable import Viewable
from panel.widgets import (
TextInput, StaticText, FloatSlider, IntSlider, RangeSlider,
LiteralInput, Checkbox, Select, MultiSelect, Button, Toggle,
Expand Down Expand Up @@ -34,6 +36,32 @@ def test_text_input(document, comm):
assert repr(text) == "TextInput(name='Text:', value='A')"


def test_widget_triggers_events(document, comm):
"""
Ensure widget events don't get swallowed in comm mode
"""
text = TextInput(value='ABC', name='Text:')

box = text._get_root(document, comm=comm)
widget = box.children[0]
document.add_root(box)
document.hold()

# Simulate client side change
widget.value = '123'
document._held_events = document._held_events[:-1]

# Set new value
with block_comm():
text.value = '123'

assert len(document._held_events) == 1
event = document._held_events[0]
assert event.model is widget
assert event.attr == 'value'
assert event.new == '123'


def test_static_text(document, comm):

text = StaticText(value='ABC', name='Text:')
Expand Down
18 changes: 16 additions & 2 deletions panel/util.py
Expand Up @@ -10,6 +10,7 @@

from collections import defaultdict, MutableSequence, MutableMapping, OrderedDict
from datetime import datetime
from contextlib import contextmanager

import param
import bokeh
Expand All @@ -29,8 +30,10 @@
except:
basestring = unicode = str


# Global variables
CUSTOM_MODELS = {}
BLOCKED = False


def load_compiled_models(custom_model, implementation):
"""
Expand Down Expand Up @@ -195,13 +198,24 @@ def diff(doc, binary=True, events=None):
the latest plot data.
"""
events = list(doc._held_events) if events is None else events
if not events:
if not events or BLOCKED:
return None
msg = Protocol("1.0").create("PATCH-DOC", events, use_buffers=binary)
doc._held_events = [e for e in doc._held_events if e not in events]
return msg


@contextmanager
def block_comm():
"""
Context manager to temporarily block comm push
"""
global BLOCKED
BLOCKED = True
yield
BLOCKED = False


def push(doc, comm, binary=True):
"""
Pushes events stored on the document across the provided comm.
Expand Down
4 changes: 2 additions & 2 deletions panel/viewable.py
Expand Up @@ -15,7 +15,7 @@

from bokeh.application.handlers import FunctionHandler
from bokeh.application import Application
from bokeh.document import Document
from bokeh.document.document import Document, _combine_document_events
from bokeh.document.events import ModelChangedEvent
from bokeh.io import curdoc, show
from bokeh.models import CustomJS
Expand Down Expand Up @@ -365,7 +365,7 @@ def update_model():
old = getattr(model, attr)
serializable_new = model.lookup(attr).serializable_value(model)
event = ModelChangedEvent(doc, model, attr, old, new, serializable_new)
doc._held_events.append(event)
_combine_document_events(event, doc._held_events)
else:
model.update(**update)

Expand Down

0 comments on commit c793b5e

Please sign in to comment.