def test_session_registering_model_classes(): from flexx import ui store = AssetStore() store.update_modules() s = SessionAssets(store) commands = [] s._send_command = lambda x: commands.append(x) assert not s._used_modules # Register button, pulls in all dependent modules s.register_model_class(ui.Button) assert ui.Button.__jsmodule__ in s._used_modules assert ui.BaseButton.__jsmodule__ in s._used_modules assert ui.Widget.__jsmodule__ in s._used_modules assert 'flexx.app.model' in s._used_modules # Get assets, level 9 js_assets, css_assets = s.get_assets_in_order(bundle_level=9) names = [a.name for a in js_assets] assert 'flexx.app.model.js' in names assert 'flexx.ui.widgets._button.js' in names # Get assets, level 2 js_assets, css_assets = s.get_assets_in_order(bundle_level=2) names = [a.name for a in js_assets] assert 'flexx.app.js' in names assert 'flexx.ui.js' in names assert 'flexx.ui.widgets._button.js' not in names # Get assets, level 1 js_assets, css_assets = s.get_assets_in_order(bundle_level=1) names = [a.name for a in js_assets] assert 'flexx.js' in names assert 'flexx.ui.js' not in names assert 'flexx.ui.widgets._button.js' not in names # Get page code = s.get_page() assert '<html>' in code code = s.get_page_for_export([]) assert '<html>' in code # dynamic loading ... # No commands so far assert not commands s.register_model_class(ui.html.ul) assert commands
def test_session_registering_model_classes(): store = AssetStore() s = SessionAssets(store) s._send_command = lambda x: None store.create_module_assets('flexx.ui.layouts') raises(ValueError, s.register_model_class, 4) # must be a Model class s.register_model_class(ui.Slider) assert len(s._known_classes) == 3 # Slider, Widget, and Model s.register_model_class(ui.Slider) # no duplicates! assert len(s._known_classes) == 3 s.register_model_class(ui.BoxLayout) s.register_model_class(ui.Button) # Get result js = s.get_js_only() assert js.count('.Button = function ') == 1 assert js.count('.Slider = function ') == 1 assert js.count('.Widget = function ') == 1 assert js.count('.BoxLayout = function ') == 1 # Check that module indeed only has layout widgets jsmodule = store.load_asset('flexx-ui-layouts.js').decode() assert jsmodule.count('.BoxLayout = function ') == 1 assert jsmodule.count('.Button = function ') == 0 assert jsmodule.count('.Widget = function ') == 0 # Check that page contains the rest page = s.get_page() assert page.count('.BoxLayout = function ') == 0 assert page.count('.Button = function ') == 1 assert page.count('.Widget = function ') == 1 # Check that a single page export has it all export = s.get_page_for_export([], True) assert export.count('.BoxLayout = function ') == 1 assert export.count('.Button = function ') == 1 assert export.count('.Widget = function ') == 1 # Patch - this func is normally provided by the Session subclass commands = [] s._send_command = lambda x: commands.append(x) # Dynamic s.register_model_class(ui.BoxLayout) assert len(commands) == 0 # already known s.register_model_class(ui.FormLayout) assert len(commands) == 0 # already in module asset # s.register_model_class(ui.Label) assert '.Label = function' in commands[0] # JS assert 'flx-' in commands[1] # CSS