Skip to content

Commit

Permalink
fix: Configure JSDOM to load script tags injected into the dom by get…
Browse files Browse the repository at this point in the history
…Scripts
  • Loading branch information
lucasnetau committed Oct 19, 2023
1 parent b34c8bf commit 41c0b20
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 37 deletions.
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,10 @@
"tests/"
],
"testEnvironment": "jsdom",
"testEnvironmentOptions": {
"resources": "usable",
"runScripts": "dangerously"
},
"setupFiles": [
"./tests/setup-jest.js"
],
Expand Down
35 changes: 3 additions & 32 deletions tests/setup-jest.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,8 @@


import $ from 'jquery'
global.$ = $
global.jQuery = $

const langFiles = require('formbuilder-languages')
global.FB_EN_US = langFiles['en-US']

const xhrMockClass = () => ({
open : jest.fn(), //(method, url) => {console.log(method + ':' + url)},
send : jest.fn().mockImplementation(function(){ this.onload() }),
setRequestHeader: jest.fn(),
abort: jest.fn(),
addEventListener: jest.fn(),
dispatchEvent: jest.fn(),
error: jest.fn(),
getAllResponseHeaders: jest.fn(),
getResponseHeader: jest.fn(),
//load: jest.fn(),
loadend: jest.fn(),
loadstart: jest.fn(),
onreadystatechange: jest.fn(),
progress: jest.fn(),
readyState: jest.fn(),
removeEventListener: jest.fn(),
response: jest.fn(),
responseText: jest.fn(),
responseType: jest.fn(),
responseURL: jest.fn(),
responseXML: jest.fn(),
status: jest.fn(),
statusText: jest.fn(),
timeout: jest.fn(),
upload: jest.fn(),
withCredentials: jest.fn(),
})

window.XMLHttpRequest = jest.fn().mockImplementation(xhrMockClass)
global.FB_EN_US = langFiles['en-US']
9 changes: 4 additions & 5 deletions tests/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,12 +267,11 @@ describe('Test util Asset inclusion', () => {
expect(window.fbLoaded.css).toContain('test2')
})

test.failing('include js', () => {
return getScripts('http://example.com/test.js').then(data => {
expect(data).toBe('peanut butter')
expect(window.fbLoaded.js).toContain('/test.js')
test('include js', async () => {
await getScripts('https://formbuilder.online/assets/js/form-builder.min.js').then(() => {
expect(window.fbLoaded.js).toContain('https://formbuilder.online/assets/js/form-builder.min.js')
})
}, 1)
})
})

describe('enhancedBootstrap feature utils', () => {
Expand Down

0 comments on commit 41c0b20

Please sign in to comment.