/* Copyright 2017 LasLabs Inc. * License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html). */ odoo.define_section('web_widget_slick', ['web.core', 'web.form_common'], function(test) { "use strict"; function appendWidget (core, formCommon, $fix) { var fieldManager = new formCommon.DefaultFieldManager(null, {}); var node = {'attrs': {}}; var FieldSlickImages = core.form_widget_registry.get('one2many_slick_images'); var widget = new FieldSlickImages(fieldManager, node); widget.appendTo($fix); return widget; } function imageUrl (modelName, fieldName, id) { return '/web/image/' + modelName + '/' + id + '/' + fieldName; } test('It should add a slick widget', function(assert, core, formCommon) { var $fix = $('#qunit-fixture'); var widget = appendWidget(core, formCommon, $fix); var slickContainerCount = $fix.find('.slick-container').length; assert.strictEqual(slickContainerCount, 1); } ); test('.init() should add defaults to options', function(assert, core, formCommon) { var $fix = $('#qunit-fixture'); var fieldManager = new formCommon.DefaultFieldManager(null, {}); var node = {'attrs': {}}; var FieldSlickImages = core.form_widget_registry.get('one2many_slick_images'); var widget = new FieldSlickImages(fieldManager, node); widget.appendTo($fix); widget.defaults.testing = 'tested'; widget.init(fieldManager, node); assert.strictEqual(widget.options.testing, 'tested'); } ); test('.destroy_content() should remove images', function(assert, core, formCommon) { var $fix = $('#qunit-fixture'); var widget = appendWidget(core, formCommon, $fix); var $slickImage = $('
'); widget.$slick.slick('slickAdd', $slickImage); widget.destroy_content(); var slickImageCount = widget.$slick.find('img').length; assert.strictEqual(slickImageCount, 0); } ); test('.destroy_content() should remove carousel', function (assert, core, formCommon) { var $fix = $('#qunit-fixture'); var widget = appendWidget(core, formCommon, $fix); widget.destroy_content(); var slickChildren = widget.$slick.children().length; assert.strictEqual(slickChildren, 0); } ); test('.render_value() should add images corresponding to field value', function(assert, core, formCommon) { var $fix = $('#qunit-fixture'); var widget = appendWidget(core, formCommon, $fix); var fieldValues = [1, 2]; widget.set({'value': fieldValues}); widget.render_value(); var slickImages = widget.$slick.find('img'); var slickImageUrls = slickImages.map(function() { return $(this).data('lazy'); }).get(); var modelName = widget.options.modelName; var fieldName = widget.options.fieldName; var expectedUrls = fieldValues.map(function(id) { return '/web/image/' + modelName + '/' + id + '/' + fieldName; }); assert.deepEqual(slickImageUrls, expectedUrls); } ); test('._resizeCarousel() should resize the widget', function (assert, core, formCommon) { var $fix = $('#qunit-fixture'); var $nosheet = $(''); $fix.append($nosheet); var widget = appendWidget(core, formCommon, $nosheet); var setWidth = 50; widget.$slick.outerWidth(setWidth); widget._resizeCarousel(); assert.notStrictEqual(widget.$slick.outerWidth(), setWidth); } ); test('._resizeCarousel() should be called when container is resized', function (assert, core, formCommon) { var $fix = $('#qunit-fixture'); var $nosheet = $(''); $fix.append($nosheet); var widget = appendWidget(core, formCommon, $nosheet); var setWidth = 50; widget.$slick.outerWidth(setWidth); core.bus.trigger('resize'); assert.notStrictEqual(widget.$slick.outerWidth(), setWidth); } ); test('._resizeLabelWidth() should return the width of the preceding ' + 'sibling label cell if it exists', function (assert, core, formCommon) { var $fix = $('#qunit-fixture'); var widget = appendWidget(core, formCommon, $fix); var width = 100; var $cell = $('