From 6528226a80f3dc855360d260e19f9c45a99fc48a Mon Sep 17 00:00:00 2001 From: Rohitt Vashishtha Date: Sat, 16 May 2020 11:53:32 +0530 Subject: [PATCH] dropdown_list_widget: Add basic tests. --- .../node_tests/dropdown_list_widget.js | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 frontend_tests/node_tests/dropdown_list_widget.js diff --git a/frontend_tests/node_tests/dropdown_list_widget.js b/frontend_tests/node_tests/dropdown_list_widget.js new file mode 100644 index 0000000000..2cd50d85ab --- /dev/null +++ b/frontend_tests/node_tests/dropdown_list_widget.js @@ -0,0 +1,47 @@ +zrequire('dropdown_list_widget'); +zrequire('scroll_util'); +set_global('$', global.make_zjquery()); + +const noop = () => {}; +const _list_render = { + create: () => { + return { init: noop }; + }, +}; +set_global('list_render', _list_render); + +run_test('basic_functions', () => { + let updated_value; + const opts = { + setting_name: 'my_setting', + data: ['one', 'two', 'three'].map(x => ({name: x, value: x})), + value: 'one', + on_update: (val) => { updated_value = val; }, + default_text: i18n.t("not set"), + render_text: (text) => `rendered: ${text}`, + }; + + const input_group = $(".input_group"); + const reset_button = $('.dropdown_list_reset_button'); + input_group.set_find_results('.dropdown_list_reset_button', reset_button); + $("#my_setting_widget #my_setting_name").closest = () => input_group; + const $widget = $("#my_setting_widget #my_setting_name"); + + const widget = dropdown_list_widget(opts); + + assert.equal(widget.value(), 'one'); + assert.equal(updated_value, undefined); // We haven't 'updated' the widget yet. + assert(reset_button.visible()); + + widget.update('two'); + assert.equal($widget.text(), 'rendered: two'); + assert.equal(widget.value(), 'two'); + assert.equal(updated_value, 'two'); + assert(reset_button.visible()); + + widget.update(null); + assert.equal($widget.text(), 'translated: not set'); + assert.equal(widget.value(), ''); + assert.equal(updated_value, null); + assert(!reset_button.visible()); +});