/*jshint forin:true, noarg:true, noempty:true, eqeqeq:true, bitwise:true, strict:false, undef:true, unused:true, browser:true, jquery:true, maxerr:50, curly:false, multistr:true */ /*global Shiny*/ /*global describe, it, expect, beforeEach, afterEach*/ describe("Input Bindings", function() { // How to talk to an input binding (the long way) // $obj = $('.shiny-bound-input#in_text'); // $obj.data('shiny-input-binding').receiveMessage($obj[0], {value: "foo"}) var select_input_object = function(id) { return $('.shiny-bound-input#' + id); }; // Given the name of the input binding, return the corresponding inputBinding // object. var get_input_binding_name = function(name) { return Shiny.inputBindings.bindingNames['shiny.' + name].binding; }; // Given the id of an input object, return the corresponding inputBinding // object. var get_input_binding_id = function(id) { return select_input_object(id).data('shiny-input-binding'); }; var get_value = function(id) { var $obj = select_input_object(id); return get_input_binding_id(id).getValue($obj[0]); }; var set_value = function(id, value) { var $obj = select_input_object(id); return get_input_binding_id(id).setValue($obj[0], value); }; var receive_message = function(id, data) { var $obj = select_input_object(id); get_input_binding_id(id).receiveMessage($obj[0], data); }; var get_state = function(id) { var $obj = select_input_object(id); return get_input_binding_id(id).getState($obj[0]); }; function padZeros(n, digits) { var str = n.toString(); while (str.length < digits) str = "0" + str; return str; } // Get a date string with format yyyy-mm-dd (date is in local time) function local_date_string() { var date = new Date(); return date.getFullYear() + '-' + padZeros(date.getMonth()+1, 2) + '-' + padZeros(date.getDate(), 2); } // These functions are here to reduce repetition. They are exactly the same // across different input bindings. var common_tests = function(id, binding_name) { it("select the object by id", function() { var $obj = select_input_object(id); expect($obj.length).toBe(1); }); it("find the input binding", function() { // Should be able to find it by binding name expect(get_input_binding_name(binding_name)).not.toBeNull(); // Should be same as when we retrieve it using input object id expect(get_input_binding_name(binding_name)).toBe(get_input_binding_id(id)); }); it("find() works", function() { var $obj = select_input_object(id); var input_binding = get_input_binding_name(binding_name); var find_result = input_binding.find(document).filter('#' + id); expect(find_result.length).toBe(1); expect(find_result instanceof jQuery).toBe(true); // Need to extract first element for testing equality of jQuery objects expect(find_result[0]).toBe($obj[0]); }); it("getId() works", function() { var $obj = select_input_object(id); var input_binding = get_input_binding_name(binding_name); expect(input_binding.getId($obj[0])).toEqual(id); }); it("getRatePolicy() works", function() { var input_binding = get_input_binding_name(binding_name); var rate_policy = input_binding.getRatePolicy(); var valid_policies = ['direct', 'debounce', 'throttle']; var timed_policies = ['debounce', 'throttle']; // The policy can be null. If so, don't continue onto other expectations if (rate_policy === null) return; // If the policy is in valid_policies, then $.inArray should not return -1 expect($.inArray(rate_policy.policy, valid_policies)).not.toBe(-1); // If it's a policy that requires a specified delay, check for delay if ($.inArray(rate_policy.policy, timed_policies) !== -1) { expect(typeof rate_policy.delay).toBe('number'); } }); // it("subscribe() works", function() { // // TODO // }); // it("unsubscribe() works", function() { // // TODO // }); }; // =========================================================================== describe("textInputBinding", function() { var id = 'in_text'; // id of the DOM object var binding_name = 'textInput'; // Name of the input binding in the registry beforeEach(function(){ var htmlstring = '\ '; // Wrapper div for the htmlstring var el = $('
This is the first panel.
\This is the second panel.
\