2018-03-07 20:46:13 +01:00
|
|
|
set_global('$', global.make_zjquery());
|
2018-07-11 18:29:48 +02:00
|
|
|
set_global('i18n', global.stub_i18n);
|
2018-03-07 20:46:13 +01:00
|
|
|
zrequire('input_pill');
|
|
|
|
|
2018-03-31 13:22:29 +02:00
|
|
|
zrequire('Handlebars', 'handlebars');
|
|
|
|
zrequire('templates');
|
|
|
|
global.compile_template('input_pill');
|
|
|
|
|
2018-04-24 01:07:22 +02:00
|
|
|
set_global('blueslip', global.make_zblueslip());
|
2018-03-07 20:46:13 +01:00
|
|
|
|
|
|
|
var noop = function () {};
|
2018-06-27 20:17:04 +02:00
|
|
|
var example_img_link = 'http://example.com/example.png';
|
2018-03-07 20:46:13 +01:00
|
|
|
|
|
|
|
set_global('ui_util', {
|
|
|
|
place_caret_at_end: noop,
|
|
|
|
});
|
|
|
|
|
|
|
|
var id_seq = 0;
|
2018-06-02 09:17:16 +02:00
|
|
|
run_test('set_up_ids', () => {
|
2018-03-07 20:46:13 +01:00
|
|
|
// just get coverage on a simple one-liner:
|
|
|
|
input_pill.random_id();
|
|
|
|
|
|
|
|
input_pill.random_id = function () {
|
|
|
|
id_seq += 1;
|
|
|
|
return 'some_id' + id_seq;
|
|
|
|
};
|
2018-06-02 09:17:16 +02:00
|
|
|
});
|
2018-03-07 20:46:13 +01:00
|
|
|
|
|
|
|
|
2018-06-27 20:17:04 +02:00
|
|
|
function pill_html(value, data_id, img_src) {
|
|
|
|
var has_image = img_src !== undefined;
|
|
|
|
|
2018-03-31 13:22:29 +02:00
|
|
|
var opts = {
|
|
|
|
id: data_id,
|
|
|
|
display_value: value,
|
2018-06-27 20:17:04 +02:00
|
|
|
has_image: has_image,
|
2018-03-31 13:22:29 +02:00
|
|
|
};
|
|
|
|
|
2018-06-27 20:17:04 +02:00
|
|
|
if (has_image) {
|
|
|
|
opts.img_src = img_src;
|
|
|
|
}
|
|
|
|
|
2018-03-31 13:22:29 +02:00
|
|
|
return templates.render('input_pill', opts);
|
2018-03-07 20:46:13 +01:00
|
|
|
}
|
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
run_test('basics', () => {
|
2018-03-07 20:46:13 +01:00
|
|
|
var config = {};
|
|
|
|
|
2018-04-24 01:07:22 +02:00
|
|
|
blueslip.set_test_data('error', 'Pill needs container.');
|
2018-03-07 20:46:13 +01:00
|
|
|
input_pill.create(config);
|
2018-04-24 01:07:22 +02:00
|
|
|
assert.equal(blueslip.get_test_logs('error').length, 1);
|
|
|
|
blueslip.clear_test_data();
|
2018-03-07 20:46:13 +01:00
|
|
|
|
|
|
|
var pill_input = $.create('pill_input');
|
|
|
|
var container = $.create('container');
|
|
|
|
container.set_find_results('.input', pill_input);
|
|
|
|
|
2018-04-24 01:07:22 +02:00
|
|
|
blueslip.set_test_data('error', 'Pill needs create_item_from_text');
|
2018-03-07 20:46:13 +01:00
|
|
|
config.container = container;
|
|
|
|
input_pill.create(config);
|
2018-04-24 01:07:22 +02:00
|
|
|
assert.equal(blueslip.get_test_logs('error').length, 1);
|
|
|
|
blueslip.clear_test_data();
|
2018-03-07 20:46:13 +01:00
|
|
|
|
2018-04-24 01:07:22 +02:00
|
|
|
blueslip.set_test_data('error', 'Pill needs get_text_from_item');
|
2018-03-07 20:46:13 +01:00
|
|
|
config.create_item_from_text = noop;
|
|
|
|
input_pill.create(config);
|
2018-04-24 01:07:22 +02:00
|
|
|
assert.equal(blueslip.get_test_logs('error').length, 1);
|
|
|
|
blueslip.clear_test_data();
|
2018-03-07 20:46:13 +01:00
|
|
|
|
|
|
|
config.get_text_from_item = noop;
|
|
|
|
var widget = input_pill.create(config);
|
|
|
|
|
|
|
|
var item = {
|
|
|
|
display_value: 'JavaScript',
|
|
|
|
language: 'js',
|
2018-06-27 20:17:04 +02:00
|
|
|
img_src: example_img_link,
|
2018-03-07 20:46:13 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
var inserted_before;
|
2018-06-27 20:17:04 +02:00
|
|
|
var expected_html = pill_html('JavaScript', 'some_id1', example_img_link);
|
2018-03-07 20:46:13 +01:00
|
|
|
|
|
|
|
pill_input.before = function (elem) {
|
|
|
|
inserted_before = true;
|
|
|
|
assert.equal(elem.html(), expected_html);
|
|
|
|
};
|
|
|
|
|
|
|
|
widget.appendValidatedData(item);
|
|
|
|
assert(inserted_before);
|
|
|
|
|
|
|
|
assert.deepEqual(widget.items(), [item]);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2018-03-07 20:46:13 +01:00
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
run_test('insert_remove', () => {
|
2018-03-07 20:46:13 +01:00
|
|
|
set_global('$', global.make_zjquery());
|
|
|
|
var items = {
|
|
|
|
blue: {
|
|
|
|
display_value: 'BLUE',
|
|
|
|
description: 'color of the sky',
|
2018-06-27 20:17:04 +02:00
|
|
|
img_src: example_img_link,
|
2018-03-07 20:46:13 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
red: {
|
|
|
|
display_value: 'RED',
|
|
|
|
description: 'color of stop signs',
|
|
|
|
},
|
|
|
|
|
|
|
|
yellow: {
|
|
|
|
display_value: 'YELLOW',
|
|
|
|
description: 'color of bananas',
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
var pill_input = $.create('pill_input');
|
|
|
|
|
|
|
|
var inserted_html = [];
|
|
|
|
pill_input.before = function (elem) {
|
|
|
|
inserted_html.push(elem.html());
|
|
|
|
};
|
|
|
|
|
|
|
|
var create_item_from_text = function (text) {
|
|
|
|
return items[text];
|
|
|
|
};
|
|
|
|
|
|
|
|
var container = $.create('container');
|
|
|
|
container.set_find_results('.input', pill_input);
|
|
|
|
|
|
|
|
var config = {
|
|
|
|
container: container,
|
|
|
|
create_item_from_text: create_item_from_text,
|
|
|
|
get_text_from_item: noop,
|
|
|
|
};
|
|
|
|
|
|
|
|
id_seq = 0;
|
|
|
|
|
|
|
|
|
|
|
|
// FINALLY CREATE THE WIDGET!!
|
|
|
|
var widget = input_pill.create(config);
|
|
|
|
|
|
|
|
var created;
|
|
|
|
var removed;
|
|
|
|
|
|
|
|
widget.onPillCreate(function () {
|
|
|
|
created = true;
|
|
|
|
});
|
|
|
|
|
|
|
|
widget.onPillRemove(function () {
|
|
|
|
removed = true;
|
|
|
|
});
|
|
|
|
|
|
|
|
widget.appendValue('blue,chartreuse,red,yellow,mauve');
|
|
|
|
|
|
|
|
assert(created);
|
|
|
|
assert(!removed);
|
|
|
|
|
|
|
|
assert.deepEqual(inserted_html, [
|
2018-06-27 20:17:04 +02:00
|
|
|
pill_html('BLUE', 'some_id1', example_img_link),
|
2018-03-07 20:46:13 +01:00
|
|
|
pill_html('RED', 'some_id2'),
|
|
|
|
pill_html('YELLOW', 'some_id3'),
|
|
|
|
]);
|
|
|
|
|
|
|
|
assert.deepEqual(widget.items(), [
|
|
|
|
items.blue,
|
|
|
|
items.red,
|
|
|
|
items.yellow,
|
|
|
|
]);
|
|
|
|
|
|
|
|
assert.equal(pill_input.text(), 'chartreuse, mauve');
|
|
|
|
|
|
|
|
widget.clear_text();
|
|
|
|
assert.equal(pill_input.text(), '');
|
|
|
|
|
|
|
|
var BACKSPACE = 8;
|
|
|
|
var key_handler = container.get_on_handler('keydown', '.input');
|
|
|
|
|
|
|
|
key_handler({
|
|
|
|
keyCode: BACKSPACE,
|
|
|
|
target: {
|
|
|
|
innerText: '',
|
|
|
|
},
|
|
|
|
preventDefault: noop,
|
|
|
|
});
|
|
|
|
|
|
|
|
assert(removed);
|
|
|
|
|
|
|
|
assert.deepEqual(widget.items(), [
|
|
|
|
items.blue,
|
|
|
|
items.red,
|
|
|
|
]);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|