mrp_document_kanban_tests.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /** @odoo-module **/
  2. import testUtils from 'web.test_utils';
  3. import { registry } from "@web/core/registry";
  4. import {
  5. click,
  6. getFixture,
  7. nextTick,
  8. } from '@web/../tests/helpers/utils';
  9. import { setupViewRegistries } from "@web/../tests/views/helpers";
  10. import {
  11. start,
  12. startServer,
  13. } from '@mail/../tests/helpers/test_utils';
  14. import { fileUploadService } from "@web/core/file_upload/file_upload_service";
  15. import { addModelNamesToFetch } from '@bus/../tests/helpers/model_definitions_helpers';
  16. addModelNamesToFetch([
  17. 'mrp.document',
  18. ]);
  19. const serviceRegistry = registry.category("services");
  20. let target;
  21. let pyEnv;
  22. QUnit.module('Views', {}, function () {
  23. QUnit.module('MrpDocumentsKanbanView', {
  24. beforeEach: async function () {
  25. serviceRegistry.add("file_upload", fileUploadService);
  26. this.ORIGINAL_CREATE_XHR = fileUploadService.createXhr;
  27. this.patchDocumentXHR = (mockedXHRs, customSend) => {
  28. fileUploadService.createXhr = () => {
  29. const xhr = new window.EventTarget();
  30. Object.assign(xhr, {
  31. upload: new window.EventTarget(),
  32. open() {},
  33. send(data) { customSend && customSend(data); },
  34. });
  35. mockedXHRs.push(xhr);
  36. return xhr;
  37. };
  38. };
  39. pyEnv = await startServer();
  40. const irAttachment = pyEnv['ir.attachment'].create({
  41. mimetype: 'image/png',
  42. name: 'test.png',
  43. })
  44. pyEnv['mrp.document'].create([
  45. {name: 'test1', priority: 2, ir_attachment_id: irAttachment},
  46. {name: 'test2', priority: 1},
  47. {name: 'test3', priority: 3},
  48. ]);
  49. target = getFixture();
  50. setupViewRegistries();
  51. },
  52. afterEach() {
  53. fileUploadService.createXhr = this.ORIGINAL_CREATE_XHR;
  54. },
  55. }, function () {
  56. QUnit.test('MRP documents kanban basic rendering', async function (assert) {
  57. assert.expect(4);
  58. const views = {
  59. 'mrp.document,false,kanban':
  60. `<kanban js_class="mrp_documents_kanban" create="false"><templates><t t-name="kanban-box">
  61. <div>
  62. <field name="name"/>
  63. </div>
  64. </t></templates></kanban>`
  65. };
  66. const { openView } = await start({
  67. serverData: { views },
  68. });
  69. await openView({
  70. res_model: 'mrp.document',
  71. views: [[false, 'kanban']],
  72. });
  73. assert.ok(target.querySelector('.o_mrp_documents_kanban_upload'),
  74. "should have upload button in kanban buttons");
  75. assert.containsN(target, '.o_kanban_renderer .o_kanban_record:not(.o_kanban_ghost)', 3,
  76. "should have 3 records in the renderer");
  77. // check control panel buttons
  78. assert.containsN(target, '.o_cp_buttons .btn-primary', 1,
  79. "should have only 1 primary button i.e. Upload button");
  80. assert.equal(target.querySelector(".o_cp_buttons .btn-primary").innerText.trim().toUpperCase(), 'UPLOAD',
  81. "should have a primary 'Upload' button");
  82. });
  83. QUnit.test('mrp: upload multiple files', async function (assert) {
  84. assert.expect(4);
  85. const file1 = await testUtils.file.createFile({
  86. name: 'text1.txt',
  87. content: 'hello, world',
  88. contentType: 'text/plain',
  89. });
  90. const file2 = await testUtils.file.createFile({
  91. name: 'text2.txt',
  92. content: 'hello, world',
  93. contentType: 'text/plain',
  94. });
  95. const file3 = await testUtils.file.createFile({
  96. name: 'text3.txt',
  97. content: 'hello, world',
  98. contentType: 'text/plain',
  99. });
  100. const mockedXHRs = [];
  101. this.patchDocumentXHR(mockedXHRs, data => assert.step('xhrSend'));
  102. const views = {
  103. 'mrp.document,false,kanban':
  104. `<kanban js_class="mrp_documents_kanban" create="false"><templates><t t-name="kanban-box">
  105. <div>
  106. <field name="name"/>
  107. </div>
  108. </t></templates></kanban>`
  109. };
  110. const { openView } = await start({
  111. serverData: { views },
  112. });
  113. await openView({
  114. res_model: 'mrp.document',
  115. views: [[false, 'kanban']],
  116. });
  117. const fileInput = target.querySelector(".o_input_file");
  118. let dataTransfer = new DataTransfer();
  119. dataTransfer.items.add(file1);
  120. fileInput.files = dataTransfer.files;
  121. fileInput.dispatchEvent(new Event('change', { bubbles: true }));
  122. assert.verifySteps(['xhrSend']);
  123. dataTransfer = new DataTransfer();
  124. dataTransfer.items.add(file2);
  125. dataTransfer.items.add(file3);
  126. fileInput.files = dataTransfer.files;
  127. fileInput.dispatchEvent(new Event('change', { bubbles: true }));
  128. assert.verifySteps(['xhrSend']);
  129. });
  130. QUnit.test('mrp: upload progress bars', async function (assert) {
  131. assert.expect(4);
  132. const file1 = await testUtils.file.createFile({
  133. name: 'text1.txt',
  134. content: 'hello, world',
  135. contentType: 'text/plain',
  136. });
  137. const mockedXHRs = [];
  138. this.patchDocumentXHR(mockedXHRs, data => assert.step('xhrSend'));
  139. const views = {
  140. 'mrp.document,false,kanban':
  141. `<kanban js_class="mrp_documents_kanban" create="false"><templates><t t-name="kanban-box">
  142. <div>
  143. <field name="name"/>
  144. </div>
  145. </t></templates></kanban>`
  146. };
  147. const { openView } = await start({
  148. serverData: { views },
  149. });
  150. await openView({
  151. res_model: 'mrp.document',
  152. views: [[false, 'kanban']],
  153. });
  154. const fileInput = target.querySelector(".o_input_file");
  155. let dataTransfer = new DataTransfer();
  156. dataTransfer.items.add(file1);
  157. fileInput.files = dataTransfer.files;
  158. fileInput.dispatchEvent(new Event('change', { bubbles: true }));
  159. assert.verifySteps(['xhrSend']);
  160. const progressEvent = new Event('progress', { bubbles: true });
  161. progressEvent.loaded = 250000000;
  162. progressEvent.total = 500000000;
  163. progressEvent.lengthComputable = true;
  164. mockedXHRs[0].upload.dispatchEvent(progressEvent);
  165. await nextTick();
  166. assert.strictEqual(
  167. target.querySelector('.o_file_upload_progress_text_left').innerText,
  168. "Uploading... (50%)",
  169. "the current upload progress should be at 50%"
  170. );
  171. progressEvent.loaded = 350000000;
  172. mockedXHRs[0].upload.dispatchEvent(progressEvent);
  173. await nextTick();
  174. assert.strictEqual(
  175. target.querySelector('.o_file_upload_progress_text_right').innerText,
  176. "(350/500MB)",
  177. "the current upload progress should be at (350/500Mb)"
  178. );
  179. });
  180. QUnit.test("mrp: click on image opens attachment viewer", async function (assert) {
  181. assert.expect(4);
  182. const views = {
  183. 'mrp.document,false,kanban':
  184. `<kanban js_class="mrp_documents_kanban" create="false"><templates><t t-name="kanban-box">
  185. <div class="o_kanban_image" t-if="record.ir_attachment_id.raw_value">
  186. <div class="o_kanban_previewer">
  187. <field name="ir_attachment_id" invisible="1"/>
  188. <img t-attf-src="/web/image/#{record.ir_attachment_id.raw_value}" width="100" height="100" alt="Document" class="o_attachment_image"/>
  189. </div>
  190. </div>
  191. <div>
  192. <field name="name"/>
  193. </div>
  194. </t></templates></kanban>`
  195. };
  196. const { openView } = await start({
  197. serverData: { views },
  198. });
  199. await openView({
  200. res_model: 'mrp.document',
  201. views: [[false, 'kanban']],
  202. });
  203. assert.containsOnce(target, ".o_kanban_previewer");
  204. await click(target.querySelector(".o_kanban_previewer"));
  205. await nextTick();
  206. assert.containsOnce(target, '.o_AttachmentViewer',
  207. "should have a document preview");
  208. assert.containsOnce(target, '.o_AttachmentViewer_headerItemButtonClose',
  209. "should have a close button");
  210. await click(target, '.o_AttachmentViewer_headerItemButtonClose');
  211. assert.containsNone(target, '.o_AttachmentViewer',
  212. "should not have a document preview");
  213. });
  214. });
  215. });