You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

179 lines
6.0 KiB

  1. /* Copyright 2017 LasLabs Inc.
  2. * License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html). */
  3. odoo.define_section('web_widget_slickroom', ['web.core', 'web.form_common'], function (test) {
  4. "use strict";
  5. function appendWidget (core, formCommon, $fix) {
  6. var fieldManager = new formCommon.DefaultFieldManager(null, {});
  7. var node = {'attrs': {}};
  8. var FieldSlickroomImages = core.form_widget_registry.get('slickroom');
  9. var widget = new FieldSlickroomImages(fieldManager, node);
  10. widget.appendTo($fix);
  11. return widget;
  12. }
  13. function imgHTML (id, attr) {
  14. return $(
  15. '<div><img data-record-id="' +id + '" ' + attr +
  16. '="/web/image/ir.attachment/' + id + '/datas"></div>'
  17. );
  18. }
  19. test('._openModal() should open a darkroom modal with provided options',
  20. function (assert, core, formCommon) {
  21. var $fix = $('#qunit-fixture');
  22. var widget = appendWidget(core, formCommon, $fix);
  23. var recordId = 1;
  24. widget.$slick.slick('slickAdd', imgHTML(recordId, 'src'));
  25. var modalAction = {};
  26. widget.do_action = function (action, options) {
  27. modalAction = action;
  28. };
  29. var expectedAction = {
  30. "type": "ir.actions.act_window",
  31. "res_model": "darkroom.modal",
  32. "name": "Darkroom",
  33. "views": [[false, "form"]],
  34. "target": "new",
  35. "context": {
  36. "active_field": widget.options.fieldName,
  37. "active_model": widget.options.modelName,
  38. "active_record_id": recordId
  39. }
  40. };
  41. widget.$('img').click();
  42. assert.deepEqual(modalAction, expectedAction);
  43. }
  44. );
  45. test('._openModal() should open a darkroom modal with on_close action ' +
  46. 'that calls ._updateImage()',
  47. function (assert, core, formCommon) {
  48. var $fix = $('#qunit-fixture');
  49. var widget = appendWidget(core, formCommon, $fix);
  50. var recordId = 1;
  51. widget.$slick.slick('slickAdd', imgHTML(recordId, 'src'));
  52. var modalOptions = {};
  53. widget.do_action = function (action, options) {
  54. modalOptions = options;
  55. };
  56. var $img = widget.$('img');
  57. $img.click();
  58. modalOptions.on_close();
  59. assert.notStrictEqual($img.attr('src').indexOf('?unique'), -1);
  60. }
  61. );
  62. test('._slickRender() should add data-record-id to images',
  63. function (assert, core, formCommon) {
  64. var $fix = $('#qunit-fixture');
  65. var widget = appendWidget(core, formCommon, $fix);
  66. var values = [1, 2, 3];
  67. _.each(values, function(recordId) {
  68. widget._slickRender('/web/image/ir.attachments/', recordId);
  69. });
  70. var slickImageIds = widget.$slick.find('img').map(function () {
  71. return $(this).data('record-id');
  72. }).get();
  73. assert.deepEqual(slickImageIds, values);
  74. }
  75. );
  76. test('._updateImage() should update source of matching/loaded slick images',
  77. function (assert, core, formCommon) {
  78. var $fix = $('#qunit-fixture');
  79. var widget = appendWidget(core, formCommon, $fix);
  80. var imgId = 1;
  81. for(var i = 0; i < 5; i++) {
  82. widget.$slick.slick('slickAdd', imgHTML(imgId, 'src'));
  83. }
  84. widget._updateImage(imgId);
  85. var $matches = widget.$slick.find(
  86. '[data-record-id="' + imgId + '"]'
  87. );
  88. $matches.each(function () {
  89. var newSrc = $(this).attr('src');
  90. assert.notStrictEqual(newSrc.indexOf('?unique'), -1);
  91. });
  92. }
  93. );
  94. test('._updateImage() should update lazy data attribute of matching/unloaded slick images',
  95. function (assert, core, formCommon) {
  96. var $fix = $('#qunit-fixture');
  97. var widget = appendWidget(core, formCommon, $fix);
  98. var imgId = 1;
  99. for(var i = 0; i < 5; i++) {
  100. widget.$slick.slick('slickAdd', imgHTML(imgId, 'data-lazy'));
  101. }
  102. widget._updateImage(1);
  103. var $matches = widget.$slick.find(
  104. '[data-record-id="' + imgId + '"]'
  105. );
  106. $matches.each(function () {
  107. var newSrc = $(this).attr('data-lazy');
  108. assert.notStrictEqual(newSrc.indexOf('?unique'), -1);
  109. });
  110. }
  111. );
  112. test('._updateImage() should not update source of non-matching/loaded slick images',
  113. function (assert, core, formCommon) {
  114. var $fix = $('#qunit-fixture');
  115. var widget = appendWidget(core, formCommon, $fix);
  116. var imgId = 1;
  117. var img2Id = 2;
  118. widget.$slick.slick('slickAdd', imgHTML(imgId, 'src'));
  119. widget.$slick.slick('slickAdd', imgHTML(img2Id, 'src'));
  120. widget._updateImage(1);
  121. var $notMatch = widget.$slick.find(
  122. '[data-record-id="' + img2Id + '"]'
  123. );
  124. assert.strictEqual($notMatch.attr('src').indexOf('?unique'), -1);
  125. }
  126. );
  127. test('._updateImage() should not update lazy data attribute of ' +
  128. 'non-matching/unloaded slick images',
  129. function (assert, core, formCommon) {
  130. var $fix = $('#qunit-fixture');
  131. var widget = appendWidget(core, formCommon, $fix);
  132. var imgId = 1;
  133. var img2Id = 2;
  134. widget.$slick.slick('slickAdd', imgHTML(imgId, 'data-lazy'));
  135. widget.$slick.slick('slickAdd', imgHTML(img2Id, 'data-lazy'));
  136. widget._updateImage(1);
  137. var $notMatch = widget.$slick.find(
  138. '[data-record-id="' + img2Id + '"]'
  139. );
  140. assert.strictEqual(
  141. $notMatch.attr('data-lazy').indexOf('?unique'), -1
  142. );
  143. }
  144. );
  145. });