|
| 1 | +import test from "ava"; |
| 2 | +import { getComponent } from "../utils.js"; |
| 3 | +import { send } from "../../../src/django_unicorn/static/unicorn/js/messageSender.js"; |
| 4 | + |
| 5 | +function makeResponse() { |
| 6 | + return { |
| 7 | + ok: true, |
| 8 | + json: async () => ({ |
| 9 | + id: "", |
| 10 | + dom: "", |
| 11 | + data: {}, |
| 12 | + errors: {}, |
| 13 | + redirect: {}, |
| 14 | + return: {}, |
| 15 | + }), |
| 16 | + }; |
| 17 | +} |
| 18 | + |
| 19 | +test("send uses JSON body when no files are present", async (t) => { |
| 20 | + const component = getComponent(); |
| 21 | + component.actionQueue.push({ |
| 22 | + type: "callMethod", |
| 23 | + payload: { name: "save" }, |
| 24 | + }); |
| 25 | + |
| 26 | + let capturedOptions = null; |
| 27 | + global.fetch = async (_url, options) => { |
| 28 | + capturedOptions = options; |
| 29 | + return makeResponse(); |
| 30 | + }; |
| 31 | + |
| 32 | + await send(component); |
| 33 | + |
| 34 | + t.false(capturedOptions.body instanceof FormData); |
| 35 | + t.is(capturedOptions.headers["Content-Type"], "application/json"); |
| 36 | +}); |
| 37 | + |
| 38 | +test("send uses FormData when component.data contains a File", async (t) => { |
| 39 | + const file = new File(["content"], "photo.jpg", { type: "image/jpeg" }); |
| 40 | + const component = getComponent(undefined, undefined, undefined, { |
| 41 | + photo: file, |
| 42 | + }); |
| 43 | + component.actionQueue.push({ |
| 44 | + type: "callMethod", |
| 45 | + payload: { name: "save" }, |
| 46 | + }); |
| 47 | + |
| 48 | + let capturedOptions = null; |
| 49 | + global.fetch = async (_url, options) => { |
| 50 | + capturedOptions = options; |
| 51 | + return makeResponse(); |
| 52 | + }; |
| 53 | + |
| 54 | + await send(component); |
| 55 | + |
| 56 | + t.true(capturedOptions.body instanceof FormData); |
| 57 | +}); |
| 58 | + |
| 59 | +test("send omits Content-Type header when using FormData", async (t) => { |
| 60 | + const file = new File(["content"], "photo.jpg", { type: "image/jpeg" }); |
| 61 | + const component = getComponent(undefined, undefined, undefined, { |
| 62 | + photo: file, |
| 63 | + }); |
| 64 | + component.actionQueue.push({ |
| 65 | + type: "callMethod", |
| 66 | + payload: { name: "save" }, |
| 67 | + }); |
| 68 | + |
| 69 | + let capturedOptions = null; |
| 70 | + global.fetch = async (_url, options) => { |
| 71 | + capturedOptions = options; |
| 72 | + return makeResponse(); |
| 73 | + }; |
| 74 | + |
| 75 | + await send(component); |
| 76 | + |
| 77 | + t.false("Content-Type" in capturedOptions.headers); |
| 78 | +}); |
| 79 | + |
| 80 | +test("send uses FormData when a syncInput action payload contains a File", async (t) => { |
| 81 | + const file = new File(["content"], "photo.jpg", { type: "image/jpeg" }); |
| 82 | + const component = getComponent(); |
| 83 | + component.actionQueue.push({ |
| 84 | + type: "syncInput", |
| 85 | + payload: { name: "photo", value: file }, |
| 86 | + }); |
| 87 | + |
| 88 | + let capturedOptions = null; |
| 89 | + global.fetch = async (_url, options) => { |
| 90 | + capturedOptions = options; |
| 91 | + return makeResponse(); |
| 92 | + }; |
| 93 | + |
| 94 | + await send(component); |
| 95 | + |
| 96 | + t.true(capturedOptions.body instanceof FormData); |
| 97 | + t.false("Content-Type" in capturedOptions.headers); |
| 98 | +}); |
| 99 | + |
| 100 | +test("send FormData includes body field as JSON-stringified payload", async (t) => { |
| 101 | + const file = new File(["content"], "photo.jpg", { type: "image/jpeg" }); |
| 102 | + const component = getComponent(undefined, undefined, undefined, { |
| 103 | + photo: file, |
| 104 | + }); |
| 105 | + component.actionQueue.push({ |
| 106 | + type: "callMethod", |
| 107 | + payload: { name: "save" }, |
| 108 | + }); |
| 109 | + |
| 110 | + let capturedBody = null; |
| 111 | + global.fetch = async (_url, options) => { |
| 112 | + capturedBody = options.body; |
| 113 | + return makeResponse(); |
| 114 | + }; |
| 115 | + |
| 116 | + await send(component); |
| 117 | + |
| 118 | + const bodyJson = capturedBody.get("body"); |
| 119 | + t.is(typeof bodyJson, "string"); |
| 120 | + |
| 121 | + const parsed = JSON.parse(bodyJson); |
| 122 | + t.is(parsed.id, component.id); |
| 123 | + t.truthy(parsed.actionQueue); |
| 124 | +}); |
| 125 | + |
| 126 | +test("send FormData appends File from component.data", async (t) => { |
| 127 | + const file = new File(["hello"], "photo.jpg", { type: "image/jpeg" }); |
| 128 | + const component = getComponent(undefined, undefined, undefined, { |
| 129 | + photo: file, |
| 130 | + }); |
| 131 | + component.actionQueue.push({ |
| 132 | + type: "callMethod", |
| 133 | + payload: { name: "save" }, |
| 134 | + }); |
| 135 | + |
| 136 | + let capturedBody = null; |
| 137 | + global.fetch = async (_url, options) => { |
| 138 | + capturedBody = options.body; |
| 139 | + return makeResponse(); |
| 140 | + }; |
| 141 | + |
| 142 | + await send(component); |
| 143 | + |
| 144 | + const uploadedFile = capturedBody.get("photo"); |
| 145 | + t.true(uploadedFile instanceof File); |
| 146 | + t.is(uploadedFile.name, "photo.jpg"); |
| 147 | +}); |
| 148 | + |
| 149 | +test("send FormData appends File from syncInput action payload", async (t) => { |
| 150 | + const file = new File(["content"], "upload.pdf", { type: "application/pdf" }); |
| 151 | + const component = getComponent(); |
| 152 | + component.actionQueue.push({ |
| 153 | + type: "syncInput", |
| 154 | + payload: { name: "document", value: file }, |
| 155 | + }); |
| 156 | + |
| 157 | + let capturedBody = null; |
| 158 | + global.fetch = async (_url, options) => { |
| 159 | + capturedBody = options.body; |
| 160 | + return makeResponse(); |
| 161 | + }; |
| 162 | + |
| 163 | + await send(component); |
| 164 | + |
| 165 | + const uploadedFile = capturedBody.get("document"); |
| 166 | + t.true(uploadedFile instanceof File); |
| 167 | + t.is(uploadedFile.name, "upload.pdf"); |
| 168 | +}); |
| 169 | + |
| 170 | +test("send does not call fetch when action queue is empty", async (t) => { |
| 171 | + const component = getComponent(); |
| 172 | + |
| 173 | + let fetchCalled = false; |
| 174 | + global.fetch = async () => { |
| 175 | + fetchCalled = true; |
| 176 | + return makeResponse(); |
| 177 | + }; |
| 178 | + |
| 179 | + await send(component); |
| 180 | + |
| 181 | + t.false(fetchCalled); |
| 182 | +}); |
| 183 | + |
| 184 | +test("send uses JSON body when only callMethod actions with no files", async (t) => { |
| 185 | + const component = getComponent(); |
| 186 | + component.actionQueue.push({ |
| 187 | + type: "callMethod", |
| 188 | + payload: { name: "processData" }, |
| 189 | + }); |
| 190 | + component.actionQueue.push({ |
| 191 | + type: "syncInput", |
| 192 | + payload: { name: "name", value: "hello" }, |
| 193 | + }); |
| 194 | + |
| 195 | + let capturedOptions = null; |
| 196 | + global.fetch = async (_url, options) => { |
| 197 | + capturedOptions = options; |
| 198 | + return makeResponse(); |
| 199 | + }; |
| 200 | + |
| 201 | + await send(component); |
| 202 | + |
| 203 | + t.false(capturedOptions.body instanceof FormData); |
| 204 | + t.is(capturedOptions.headers["Content-Type"], "application/json"); |
| 205 | +}); |
0 commit comments