Debian Codegraph @

addons/web/static/lib/hoot/tests/mock/network.test.js

raw
1 /** @odoo-module */
2
3 import { after, describe, expect, mockFetch, test } from "@odoo/hoot";
4 import { parseUrl } from "../local_helpers";
5
6 /**
7 * @param {Blob | MediaSource} obj
8 */
9 function createObjectURL(obj) {
10 const url = URL.createObjectURL(obj);
11 after(() => URL.revokeObjectURL(url));
12 return url;
13 }
14
15 describe(parseUrl(import.meta.url), () => {
16 test("setup network values", async () => {
17 expect(document.cookie).toBe("");
18
19 document.cookie = "cids=4";
20 document.title = "kek";
21
22 expect(document.cookie).toBe("cids=4");
23 expect(document.title).toBe("kek");
24 });
25
26 test("values are reset between test", async () => {
27 expect(document.cookie).toBe("");
28 expect(document.title).toBe("");
29 });
30
31 test("fetch with internal URLs works without mocking fetch", async () => {
32 const blob = new Blob([JSON.stringify({ name: "coucou" })], {
33 type: "application/json",
34 });
35 const blobUrl = createObjectURL(blob);
36 const blobResponse = await fetch(blobUrl).then((res) => res.json());
37 const dataResponse = await fetch("data:text/html,<body></body>").then((res) => res.text());
38
39 expect(blobResponse).toEqual({ name: "coucou" });
40 expect(dataResponse).toBe("<body></body>");
41
42 await expect(fetch("http://some.url")).rejects.toThrow(/fetch is not mocked/);
43 });
44
45 test("fetch with internal URLs should return default value", async () => {
46 mockFetch(expect.step);
47
48 const external = await fetch("http://some.url").then((res) => res.text());
49 const internal = await fetch("/odoo").then((res) => res.text());
50 const data = await fetch("data:text/html,<body></body>").then((res) => res.text());
51
52 expect(external).toBe("null");
53 expect(internal).toBe("null");
54 expect(data).toBe("<body></body>");
55
56 expect.verifySteps(["http://some.url", "/odoo", "data:text/html,<body></body>"]);
57 });
58
59 test("fetch JSON with blob URLs", async () => {
60 mockFetch(expect.step);
61
62 const blob = new Blob([JSON.stringify({ name: "coucou" })], {
63 type: "application/json",
64 });
65 const blobUrl = createObjectURL(blob);
66 const response = await fetch(blobUrl);
67 const json = await response.json();
68
69 expect(json).toEqual({ name: "coucou" });
70
71 expect.verifySteps([blobUrl]);
72 });
73
74 test("fetch with mocked blob URLs", async () => {
75 mockFetch((input) => {
76 expect.step(input);
77 return "Some other content";
78 });
79
80 const blob = new Blob([JSON.stringify({ name: "coucou" })], {
81 type: "application/json",
82 });
83 const blobUrl = createObjectURL(blob);
84 const response = await fetch(blobUrl);
85
86 expect(response.headers).toEqual(new Headers([["Content-Type", "text/plain"]]));
87
88 const text = await response.text();
89
90 expect(text).toBe("Some other content");
91
92 expect.verifySteps([blobUrl]);
93 });
94
95 test("mock response with nested blobs", async () => {
96 mockFetch(
97 () =>
98 new Blob(["some blob", new Blob([" with nested content"], { type: "text/plain" })])
99 );
100
101 const response = await fetch("/nestedBlob");
102 const blob = await response.blob();
103 const result = await blob.text();
104
105 expect(result).toBe("some blob with nested content");
106 });
107
108 test("mock responses: array buffer", async () => {
109 mockFetch(() => "some text");
110
111 const response = await fetch("/arrayBuffer");
112 const result = await response.arrayBuffer();
113
114 expect(result).toBeInstanceOf(ArrayBuffer);
115 expect(new TextDecoder("utf-8").decode(result)).toBe("some text");
116 });
117
118 test("mock responses: blob", async () => {
119 mockFetch(() => "blob content");
120
121 const response = await fetch("/blob");
122 const result = await response.blob();
123
124 expect(result).toBeInstanceOf(Blob);
125 expect(result.size).toBe(12);
126
127 const buffer = await result.arrayBuffer();
128 expect(new TextDecoder("utf-8").decode(buffer)).toBe("blob content");
129 });
130
131 test("mock responses: bytes", async () => {
132 mockFetch(() => "some text");
133
134 const response = await fetch("/bytes");
135 const result = await response.bytes();
136
137 expect(result).toBeInstanceOf(Uint8Array);
138 expect(new TextDecoder("utf-8").decode(result)).toBe("some text");
139 });
140
141 test("mock responses: formData", async () => {
142 mockFetch(() => {
143 const data = new FormData();
144 data.append("name", "Frodo");
145 return data;
146 });
147
148 const response = await fetch("/formData");
149 const result = await response.formData();
150
151 expect(result).toBeInstanceOf(FormData);
152 expect(result.get("name")).toBe("Frodo");
153 });
154
155 test("mock responses: json", async () => {
156 mockFetch(() => ({ json: "content" }));
157
158 const response = await fetch("/json");
159 const result = await response.json();
160
161 expect(result).toEqual({ json: "content" });
162 });
163
164 test("mock responses: text", async () => {
165 mockFetch(() => "some text");
166
167 const response = await fetch("/text");
168 const result = await response.text();
169
170 expect(result).toBe("some text");
171 });
172
173 test("mock responses: error handling after reading body", async () => {
174 mockFetch(() => "some text");
175
176 const response = await fetch("/text");
177 const responseClone = response.clone();
178 const result = await response.text(); // read once
179
180 expect(result).toBe("some text");
181
182 // Rejects for every reader after body is used
183 await expect(response.arrayBuffer()).rejects.toThrow(TypeError);
184 await expect(response.blob()).rejects.toThrow(TypeError);
185 await expect(response.bytes()).rejects.toThrow(TypeError);
186 await expect(response.formData()).rejects.toThrow(TypeError);
187 await expect(response.json()).rejects.toThrow(TypeError);
188 await expect(response.text()).rejects.toThrow(TypeError);
189
190 const cloneResult = await responseClone.text(); // read clone
191
192 expect(cloneResult).toBe(result);
193
194 // Clone rejects reader as well
195 await expect(responseClone.text()).rejects.toThrow(TypeError);
196 });
197 });