zjquery: Rename add_child() to set_find_results().

Hopefully this will make it more explicit that zjquery does
not truly simulate DOM, but it instead allows you to dynamically
set what you want the results of $('foo').find(some_selector)
to be.
This commit is contained in:
Steve Howell
2017-07-08 08:31:18 -04:00
parent 70407e080d
commit ccd821e29b
7 changed files with 49 additions and 44 deletions

View File

@@ -290,8 +290,8 @@ presence.presence_info[norbert.user_id] = { status: activity.ACTIVE };
var count = $('alice-count'); var count = $('alice-count');
var pm_key = alice.user_id.toString(); var pm_key = alice.user_id.toString();
var li = $("li.user_sidebar_entry[data-user-id='" + pm_key + "']"); var li = $("li.user_sidebar_entry[data-user-id='" + pm_key + "']");
count.add_child('.value', value); count.set_find_results('.value', value);
li.add_child('.count', count); li.set_find_results('.count', count);
count.set_parent(li); count.set_parent(li);
var counts = new Dict(); var counts = new Dict();
@@ -315,8 +315,8 @@ presence.presence_info[norbert.user_id] = { status: activity.ACTIVE };
var pm_key = alice.user_id.toString() + "," + fred.user_id.toString(); var pm_key = alice.user_id.toString() + "," + fred.user_id.toString();
var li_selector = "li.group-pms-sidebar-entry[data-user-ids='" + pm_key + "']"; var li_selector = "li.group-pms-sidebar-entry[data-user-ids='" + pm_key + "']";
var li = $(li_selector); var li = $(li_selector);
count.add_child('.value', value); count.set_find_results('.value', value);
li.add_child('.count', count); li.set_find_results('.count', count);
count.set_parent(li); count.set_parent(li);
var counts = new Dict(); var counts = new Dict();
@@ -416,7 +416,7 @@ presence.presence_info[jill.user_id] = { status: activity.ACTIVE };
var alice_li = $('alice-li'); var alice_li = $('alice-li');
// These selectors are here to avoid some short-circuit logic. // These selectors are here to avoid some short-circuit logic.
$('#user_presences').add_child('[data-user-id="1"]', alice_li); $('#user_presences').set_find_results('[data-user-id="1"]', alice_li);
var appended_html; var appended_html;
$('#user_presences').append = function (html) { $('#user_presences').append = function (html) {
@@ -437,7 +437,7 @@ presence.presence_info[jill.user_id] = { status: activity.ACTIVE };
var fred_li = $('fred-li'); var fred_li = $('fred-li');
// These selectors are here to avoid some short-circuit logic. // These selectors are here to avoid some short-circuit logic.
$('#user_presences').add_child('[data-user-id="2"]', fred_li); $('#user_presences').set_find_results('[data-user-id="2"]', fred_li);
var appended_html; var appended_html;
$('#user_presences').append = function (html) { $('#user_presences').append = function (html) {
@@ -466,7 +466,7 @@ presence.presence_info[jill.user_id] = { status: activity.ACTIVE };
var fred_li = $('fred-li'); var fred_li = $('fred-li');
// These selectors are here to avoid some short-circuit logic. // These selectors are here to avoid some short-circuit logic.
$('#user_presences').add_child('[data-user-id="2"]', fred_li); $('#user_presences').set_find_results('[data-user-id="2"]', fred_li);
$('fake-dom-for-jill').attr = function (attr_name) { $('fake-dom-for-jill').attr = function (attr_name) {
assert.equal(attr_name, 'data-user-id'); assert.equal(attr_name, 'data-user-id');
@@ -576,8 +576,8 @@ $('.user-list-filter').is = function (sel) {
var pm_key = alice.user_id.toString() + "," + fred.user_id.toString(); var pm_key = alice.user_id.toString() + "," + fred.user_id.toString();
var li_selector = "li.group-pms-sidebar-entry[data-user-ids='" + pm_key + "']"; var li_selector = "li.group-pms-sidebar-entry[data-user-ids='" + pm_key + "']";
var li = $(li_selector); var li = $(li_selector);
count.add_child('.value', value); count.set_find_results('.value', value);
li.add_child('.count', count); li.set_find_results('.count', count);
count.set_parent(li); count.set_parent(li);
var real_get_huddles = activity.get_huddles; var real_get_huddles = activity.get_huddles;
@@ -609,7 +609,7 @@ $('.user-list-filter').is = function (sel) {
}; };
var alice_li = $('alice-li'); var alice_li = $('alice-li');
$('#user_presences').add_child('[data-user-id="1"]', alice_li); $('#user_presences').set_find_results('[data-user-id="1"]', alice_li);
$('#user_presences').append = function () {}; $('#user_presences').append = function () {};

View File

@@ -146,8 +146,8 @@ global.people.initialize_current_user(me.user_id);
var alice_li = $('alice-li-stub'); var alice_li = $('alice-li-stub');
var bob_li = $('bob-li-stub'); var bob_li = $('bob-li-stub');
private_li.add_child("li[data-user-ids-string='101']", alice_li); private_li.set_find_results("li[data-user-ids-string='101']", alice_li);
private_li.add_child("li[data-user-ids-string='102']", bob_li); private_li.set_find_results("li[data-user-ids-string='102']", bob_li);
var dom; var dom;
private_li.append = function (html) { private_li.append = function (html) {
@@ -237,15 +237,15 @@ global.people.initialize_current_user(me.user_id);
var total_value = $('total-value-stub'); var total_value = $('total-value-stub');
var total_count = $('total-count-stub'); var total_count = $('total-count-stub');
var private_li = $("#global_filters > li[data-name='private']"); var private_li = $("#global_filters > li[data-name='private']");
private_li.add_child('.count', total_count); private_li.set_find_results('.count', total_count);
total_count.add_child('.value', total_value); total_count.set_find_results('.value', total_value);
var child_value = $('child-value-stub'); var child_value = $('child-value-stub');
var child_count = $('child-count-stub'); var child_count = $('child-count-stub');
var child_li = $('child-li-stub'); var child_li = $('child-li-stub');
private_li.add_child("li[data-user-ids-string='101,102']", child_li); private_li.set_find_results("li[data-user-ids-string='101,102']", child_li);
child_li.add_child('.private_message_count', child_count); child_li.set_find_results('.private_message_count', child_count);
child_count.add_child('.value', child_value); child_count.set_find_results('.value', child_value);
var pm_count = new Dict(); var pm_count = new Dict();
var user_ids_string = '101,102'; var user_ids_string = '101,102';

View File

@@ -146,7 +146,7 @@ set_global('message_store', {
var count_element = $('count-stub'); var count_element = $('count-stub');
var reaction_element = $('reaction-stub'); var reaction_element = $('reaction-stub');
reaction_element.add_child('.message_reaction_count', count_element); reaction_element.set_find_results('.message_reaction_count', count_element);
reactions.set_reaction_count(reaction_element, 5); reactions.set_reaction_count(reaction_element, 5);
@@ -158,8 +158,8 @@ set_global('message_store', {
var message_row = $('some-message-row'); var message_row = $('some-message-row');
var message_reactions = $('our-reactions-section'); var message_reactions = $('our-reactions-section');
message_table.add_child("[zid='555']", message_row); message_table.set_find_results("[zid='555']", message_row);
message_row.add_child('.message_reactions', message_reactions); message_row.set_find_results('.message_reactions', message_reactions);
var section = reactions.get_reaction_section(555); var section = reactions.get_reaction_section(555);
@@ -222,7 +222,7 @@ set_global('message_store', {
var count_element = $('count-element'); var count_element = $('count-element');
var reaction_element = $('reaction-element'); var reaction_element = $('reaction-element');
reaction_element.add_child('.message_reaction_count', count_element); reaction_element.set_find_results('.message_reaction_count', count_element);
var title_set; var title_set;
reaction_element.prop = function (prop_name, value) { reaction_element.prop = function (prop_name, value) {

View File

@@ -69,14 +69,14 @@ set_global('ui_report', {
}); });
function simulate_auth_methods() { function simulate_auth_methods() {
$('#admin_auth_methods_table').add_child( $('#admin_auth_methods_table').set_find_results(
'tr.method_row', 'tr.method_row',
$('admin-tr-stub') $('admin-tr-stub')
); );
var controls = $('auth-methods-controls-stub'); var controls = $('auth-methods-controls-stub');
$(".organization-box [data-name='auth-methods']").add_child( $(".organization-box [data-name='auth-methods']").set_find_results(
'input, button, select, checked', 'input, button, select, checked',
controls controls
); );
@@ -87,7 +87,7 @@ function simulate_auth_methods() {
}; };
var non_editables = $('auth-methods-not-edit-stub'); var non_editables = $('auth-methods-not-edit-stub');
$('.organization-box').add_child( $('.organization-box').set_find_results(
'.settings-section:not(.can-edit)', '.settings-section:not(.can-edit)',
non_editables non_editables
); );
@@ -96,7 +96,7 @@ function simulate_auth_methods() {
} }
function simulate_realm_domains_table() { function simulate_realm_domains_table() {
$('#realm_domains_table tbody').add_child( $('#realm_domains_table tbody').set_find_results(
'tr', 'tr',
$('realm-tr-stub') $('realm-tr-stub')
); );
@@ -115,17 +115,17 @@ function simulate_realm_domains_table() {
function test_realms_domain_modal(add_realm_domain) { function test_realms_domain_modal(add_realm_domain) {
var info = $('domains-info-stub'); var info = $('domains-info-stub');
$('#realm_domains_modal').add_child( $('#realm_domains_modal').set_find_results(
'.realm_domains_info', '.realm_domains_info',
info info
); );
$('#add-realm-domain-widget').add_child( $('#add-realm-domain-widget').set_find_results(
'.new-realm-domain', '.new-realm-domain',
$('new-realm-domain-stub') $('new-realm-domain-stub')
); );
$('#add-realm-domain-widget').add_child( $('#add-realm-domain-widget').set_find_results(
'.new-realm-domain-allow-subdomains', '.new-realm-domain-allow-subdomains',
$('new-realm-domain-allow-subdomains-stub') $('new-realm-domain-allow-subdomains-stub')
); );
@@ -393,7 +393,7 @@ function test_change_allow_subdomains(change_allow_subdomains) {
error_callback = req.error; error_callback = req.error;
}; };
$('#realm_domains_modal').add_child( $('#realm_domains_modal').set_find_results(
'.realm_domains_info', '.realm_domains_info',
info info
); );
@@ -402,7 +402,7 @@ function test_change_allow_subdomains(change_allow_subdomains) {
$('elem-stub').parents = function () { $('elem-stub').parents = function () {
return $('parents-stub'); return $('parents-stub');
}; };
$('parents-stub').add_child('.domain', $('domain-stub')); $('parents-stub').set_find_results('.domain', $('domain-stub'));
$('elem-stub').prop('checked', allow); $('elem-stub').prop('checked', allow);
change_allow_subdomains.apply('elem-stub', [ev]); change_allow_subdomains.apply('elem-stub', [ev]);

View File

@@ -50,8 +50,8 @@ set_global('topic_list', {});
(function create_devel_sidebar_row() { (function create_devel_sidebar_row() {
var devel_value = $('devel-value'); var devel_value = $('devel-value');
var devel_count = $('devel-count'); var devel_count = $('devel-count');
$('devel-stub-html').add_child('.count', devel_count); $('devel-stub-html').set_find_results('.count', devel_count);
$('devel-count').add_child('.value', devel_value); $('devel-count').set_find_results('.value', devel_value);
devel_count.set_parent($('devel-stub-html')); devel_count.set_parent($('devel-stub-html'));
global.templates.render = function (template_name, data) { global.templates.render = function (template_name, data) {
@@ -67,8 +67,8 @@ set_global('topic_list', {});
(function create_social_sidebar_row() { (function create_social_sidebar_row() {
var social_value = $('social-value'); var social_value = $('social-value');
var social_count = $('social-count'); var social_count = $('social-count');
$('social-stub-html').add_child('.count', social_count); $('social-stub-html').set_find_results('.count', social_count);
$('social-count').add_child('.value', social_value); $('social-count').set_find_results('.value', social_value);
social_count.set_parent($('social-stub-html')); social_count.set_parent($('social-stub-html'));
global.templates.render = function (template_name, data) { global.templates.render = function (template_name, data) {
@@ -111,7 +111,7 @@ set_global('topic_list', {});
var stream_id = social.stream_id; var stream_id = social.stream_id;
var privacy_elem = $('privacy-stub'); var privacy_elem = $('privacy-stub');
social_li.add_child('.stream-privacy', privacy_elem); social_li.set_find_results('.stream-privacy', privacy_elem);
social.invite_only = true; social.invite_only = true;
social.color = '#222222'; social.color = '#222222';
@@ -362,8 +362,8 @@ function initialize_stream_data() {
var elem = $(elem_selector); var elem = $(elem_selector);
var count = $(count_selector); var count = $(count_selector);
var value = $(value_selector); var value = $(value_selector);
elem.add_child('.count', count); elem.set_find_results('.count', count);
count.add_child('.value', value); count.set_find_results('.value', value);
count.set_parent(elem); count.set_parent(elem);
return elem; return elem;

View File

@@ -58,7 +58,7 @@ set_global('$', global.make_zjquery());
assert.equal(widget.val(), '42'); assert.equal(widget.val(), '42');
}()); }());
(function test_parent_child_stuff() { (function test_finding_child_objects() {
// Let's say you have a function like the following: // Let's say you have a function like the following:
function update_message_emoji(emoji_src) { function update_message_emoji(emoji_src) {
$('#my-message').find('.emoji').attr('src', emoji_src); $('#my-message').find('.emoji').attr('src', emoji_src);
@@ -72,9 +72,9 @@ set_global('$', global.make_zjquery());
// But you can set up your tests to simulate DOM relationships. // But you can set up your tests to simulate DOM relationships.
// //
// We will use add_child(), which is a special zjquery helper. // We will use set_find_results(), which is a special zjquery helper.
var emoji = $('emoji-stub'); var emoji = $('emoji-stub');
$('#my-message').add_child('.emoji', emoji); $('#my-message').set_find_results('.emoji', emoji);
// And then calling the function produces the desired effect: // And then calling the function produces the desired effect:
update_message_emoji('foo.png'); update_message_emoji('foo.png');

View File

@@ -12,7 +12,7 @@ exports.make_zjquery = function () {
var value; var value;
var shown = false; var shown = false;
var focused = false; var focused = false;
var children = new Dict(); var find_results = new Dict();
var my_parent; var my_parent;
var properties = new Dict(); var properties = new Dict();
var attrs = new Dict(); var attrs = new Dict();
@@ -31,8 +31,10 @@ exports.make_zjquery = function () {
} }
var self = { var self = {
add_child: function (child_selector, child_elem) { add_child: function () {
children.set(child_selector, child_elem); // TODO: Remove this once some in-flight PRs
// get merged.
assert(false, 'Use set_find_results instead.');
}, },
addClass: function (class_name) { addClass: function (class_name) {
classes.set(class_name, true); classes.set(class_name, true);
@@ -69,7 +71,7 @@ exports.make_zjquery = function () {
}, },
fadeTo: noop, fadeTo: noop,
find: function (child_selector) { find: function (child_selector) {
var child = children.get(child_selector); var child = find_results.get(child_selector);
if (child) { if (child) {
return child; return child;
} }
@@ -187,6 +189,9 @@ exports.make_zjquery = function () {
generic_event('select', arg); generic_event('select', arg);
return self.wrapper; return self.wrapper;
}, },
set_find_results: function (find_selector, jquery_object) {
find_results.set(find_selector, jquery_object);
},
show: function () { show: function () {
shown = true; shown = true;
return self.wrapper; return self.wrapper;