Debian Codegraph @

addons/web/static/tests/views/fields/pdf_viewer_field.test.js

raw
1 import {
2 clickSave,
3 defineModels,
4 fields,
5 mockService,
6 models,
7 mountView,
8 onRpc,
9 patchWithCleanup,
10 } from "@web/../tests/web_test_helpers";
11 import { test, expect } from "@odoo/hoot";
12 import { click, setInputFiles, queryOne, waitFor } from "@odoo/hoot-dom";
13 import { browser } from "@web/core/browser/browser";
14
15 const getIframeSrc = () => queryOne(".o_field_widget iframe.o_pdfview_iframe").dataset.src;
16
17 const getIframeProtocol = () => getIframeSrc().match(/\?file=(\w+)%3A/)[1];
18
19 const getIframeViewerParams = () =>
20 decodeURIComponent(getIframeSrc().match(/%2Fweb%2Fcontent%3F(.*)#page/)[1]);
21
22 class Partner extends models.Model {
23 document = fields.Binary({ string: "Binary" });
24 _records = [
25 {
26 document: "coucou==\n",
27 },
28 ];
29 }
30
31 defineModels([Partner]);
32
33 test("PdfViewerField without data", async () => {
34 await mountView({
35 type: "form",
36 resModel: "partner",
37 arch: '<form><field name="document" widget="pdf_viewer"/></form>',
38 });
39 expect(".o_field_widget").toHaveClass("o_field_pdf_viewer");
40 expect(".o_select_file_button:not(.o_hidden)").toHaveCount(1);
41 expect(".o_pdfview_iframe").toHaveCount(0);
42 expect(`input[type="file"]`).toHaveCount(1);
43 });
44
45 test("PdfViewerField: basic rendering", async () => {
46 await mountView({
47 type: "form",
48 resModel: "partner",
49 resId: 1,
50 arch: '<form><field name="document" widget="pdf_viewer"/></form>',
51 });
52
53 expect(".o_field_widget").toHaveClass("o_field_pdf_viewer");
54 expect(".o_select_file_button").toHaveCount(1);
55 expect(".o_field_widget iframe.o_pdfview_iframe").toHaveCount(1);
56 expect(getIframeProtocol()).toBe("https");
57 expect(getIframeViewerParams()).toBe("model=partner&field=document&id=1");
58 });
59
60 test("PdfViewerField: upload rendering", async () => {
61 expect.assertions(4);
62
63 onRpc("web_save", ({ args }) => {
64 expect(args[1]).toEqual({ document: btoa("test") });
65 });
66
67 await mountView({
68 type: "form",
69 resModel: "partner",
70 arch: '<form><field name="document" widget="pdf_viewer"/></form>',
71 });
72
73 expect("iframe.o_pdfview_iframe").toHaveCount(0);
74 const file = new File(["test"], "test.pdf", { type: "application/pdf" });
75 await click(".o_field_pdf_viewer input[type=file]");
76 await setInputFiles(file);
77 await waitFor("iframe.o_pdfview_iframe");
78 expect(getIframeProtocol()).toBe("blob");
79 await clickSave();
80 expect(getIframeProtocol()).toBe("blob");
81 });
82
83 test("PdfViewerField: upload file and download it", async () => {
84 await mountView({
85 type: "form",
86 resModel: "partner",
87 resId: 1,
88 arch: '<form><field name="document" widget="pdf_viewer"/></form>',
89 });
90 mockService("action", {
91 doAction({ type }) {
92 expect.step(type);
93 return super.doAction(...arguments);
94 },
95 });
96 patchWithCleanup(browser, {
97 open: (_url, type) => {
98 expect.step(`browser_open:${type}`);
99 },
100 });
101 expect("iframe.o_pdfview_iframe").toHaveCount(1);
102 const file = new File(["test"], "test.pdf", { type: "application/pdf" });
103 await click(".o_field_pdf_viewer input[type=file]");
104 await setInputFiles(file);
105 await waitFor("iframe.o_pdfview_iframe");
106 await clickSave();
107 await click(".fa-download");
108 expect.verifySteps(["ir.actions.act_url", "browser_open:_blank"]);
109 });