Skip to content

Commit 6d961fe

Browse files
committed
Patch global-jsdom
1 parent 3a28592 commit 6d961fe

File tree

2 files changed

+19
-17
lines changed

2 files changed

+19
-17
lines changed

s3file/static/s3file/js/s3file.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* @return {string} - Key from response.
66
*/
77
export function getKeyFromResponse(responseText) {
8-
const xml = new globalThis.DOMParser().parseFromString(responseText, "text/xml")
8+
const xml = new DOMParser().parseFromString(responseText, "text/xml")
99
return decodeURI(xml.querySelector("Key").innerHTML)
1010
}
1111

@@ -15,7 +15,7 @@ export function getKeyFromResponse(responseText) {
1515
*
1616
* @extends HTMLElement
1717
*/
18-
export class S3FileInput extends globalThis.HTMLElement {
18+
export class S3FileInput extends HTMLElement {
1919
static passThroughAttributes = ["accept", "required", "multiple", "class", "style"]
2020
constructor() {
2121
super()
@@ -288,7 +288,7 @@ export class S3FileInput extends globalThis.HTMLElement {
288288
async uploadFiles() {
289289
this.keys = []
290290
for (const file of this.files) {
291-
const s3Form = new globalThis.FormData()
291+
const s3Form = new FormData()
292292
for (const attr of this.attributes) {
293293
let name = attr.name
294294

@@ -339,4 +339,4 @@ export class S3FileInput extends globalThis.HTMLElement {
339339
}
340340
}
341341

342-
globalThis.customElements.define("s3-file", S3FileInput)
342+
customElements.define("s3-file", S3FileInput)

tests/__tests__/s3file.test.js

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ afterEach(() => {
77
mock.restoreAll()
88
})
99

10+
// https://github.com/modosc/global-jsdom/issues/440
11+
globalThis = globalThis.window
12+
1013
describe("getKeyFromResponse", () => {
1114
test("returns key", () => {
1215
const responseText = `<?xml version="1.0" encoding="UTF-8"?>
@@ -98,7 +101,7 @@ describe("S3FileInput", () => {
98101
const submitButton = document.createElement("button")
99102
form.appendChild(submitButton)
100103
submitButton.setAttribute("type", "submit")
101-
const event = new globalThis.SubmitEvent("submit", { submitter: submitButton })
104+
const event = new SubmitEvent("submit", { submitter: submitButton })
102105
const input = new s3file.S3FileInput()
103106
form.appendChild(input)
104107
await input.submitHandler(event)
@@ -112,18 +115,17 @@ describe("S3FileInput", () => {
112115
const input = new s3file.S3FileInput()
113116
form.appendChild(input)
114117
Object.defineProperty(input, "files", {
115-
get: () => [new globalThis.File([""], "file.txt")],
118+
get: () => [new File([""], "file.txt")],
116119
})
117120
assert(!input.upload)
118121
assert.strictEqual(input.files.length, 1)
119122
input.uploadHandler()
120-
console.info(input.upload)
121123
assert(input.upload)
122124
assert(form.pendingRequests)
123125
})
124126

125127
test("fromDataHandler", () => {
126-
const event = new globalThis.CustomEvent("formdata", { formData: new FormData() })
128+
const event = new CustomEvent("formdata", { formData: new FormData() })
127129
const form = document.createElement("form")
128130
document.body.appendChild(form)
129131
const input = new s3file.S3FileInput()
@@ -143,7 +145,7 @@ describe("S3FileInput", () => {
143145
input.setAttribute("data-fields-policy", "policy")
144146
form.appendChild(input)
145147
Object.defineProperty(input, "files", {
146-
get: () => [new globalThis.File([""], "file.txt")],
148+
get: () => [new File([""], "file.txt")],
147149
})
148150
const responseText = `<?xml version="1.0" encoding="UTF-8"?>
149151
<PostResponse>
@@ -153,10 +155,10 @@ describe("S3FileInput", () => {
153155
<ETag>"a38155039ec348f97dfd63da4cb2619d"</ETag>
154156
</PostResponse>`
155157
const response = { status: 201, text: async () => responseText }
156-
globalThis.fetch = mock.fn(async () => response)
158+
fetch = mock.fn(async () => response)
157159
assert(input.files.length === 1)
158160
await input.uploadFiles()
159-
assert(globalThis.fetch.mock.calls.length === 1)
161+
assert(fetch.mock.calls.length === 1)
160162
assert.deepStrictEqual(input.keys, ["tmp/s2file/some file.jpeg"])
161163
})
162164

@@ -166,13 +168,13 @@ describe("S3FileInput", () => {
166168
const input = new s3file.S3FileInput()
167169
form.appendChild(input)
168170
Object.defineProperty(input, "files", {
169-
get: () => [new globalThis.File([""], "file.txt")],
171+
get: () => [new File([""], "file.txt")],
170172
})
171173
const response = { status: 400, statusText: "Bad Request" }
172-
globalThis.fetch = mock.fn(async () => response)
174+
fetch = mock.fn(async () => response)
173175
assert(input.files.length === 1)
174176
await input.uploadFiles()
175-
assert(globalThis.fetch.mock.calls.length === 1)
177+
assert(fetch.mock.calls.length === 1)
176178
assert.deepStrictEqual(input.keys, [])
177179
assert.strictEqual(input.validationMessage, "Bad Request")
178180
})
@@ -183,14 +185,14 @@ describe("S3FileInput", () => {
183185
const input = new s3file.S3FileInput()
184186
form.appendChild(input)
185187
Object.defineProperty(input, "files", {
186-
get: () => [new globalThis.File([""], "file.txt")],
188+
get: () => [new File([""], "file.txt")],
187189
})
188-
globalThis.fetch = mock.fn(async () => {
190+
fetch = mock.fn(async () => {
189191
throw new Error("Network Error")
190192
})
191193
assert(input.files.length === 1)
192194
await input.uploadFiles()
193-
assert(globalThis.fetch.mock.calls.length === 1)
195+
assert(fetch.mock.calls.length === 1)
194196
assert.deepStrictEqual(input.keys, [])
195197
assert.strictEqual(input.validationMessage, "Error: Network Error")
196198
})

0 commit comments

Comments
 (0)