brut-js 0.0.2 → 0.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +5 -2
- package/specs/AjaxSubmit.spec.js +8 -5
- package/specs/Autosubmit.spec.js +1 -1
- package/specs/ConfirmSubmit.spec.js +1 -1
- package/specs/ConstraintViolationMessage.spec.js +1 -1
- package/specs/ConstraintViolationMessages.spec.js +1 -1
- package/specs/Form.spec.js +45 -1
- package/specs/I18nTranslation.spec.js +1 -1
- package/specs/LocaleDetection.spec.js +2 -2
- package/specs/Message.spec.js +1 -1
- package/specs/SpecHelper.js +23 -0
- package/specs/Tabs.spec.js +1 -1
- package/specs/public/js/bundle.js +17 -2
- package/specs/public/js/bundle.js.map +2 -2
- package/src/AjaxSubmit.js +2 -2
- package/src/CopyToClipboard.js +92 -0
- package/src/Form.js +16 -2
- package/src/testing/AssetMetadata.js +35 -0
- package/src/testing/AssetMetadataLoader.js +25 -0
- package/src/testing/CustomElementTest.js +235 -0
- package/src/testing/DOMCreator.js +45 -0
- package/src/testing/index.js +13 -313
package/src/testing/index.js
CHANGED
|
@@ -21,328 +21,28 @@
|
|
|
21
21
|
*
|
|
22
22
|
* @module testing
|
|
23
23
|
*/
|
|
24
|
-
import
|
|
25
|
-
import
|
|
26
|
-
import path from "node:path"
|
|
27
|
-
import assert from "assert"
|
|
28
|
-
|
|
29
|
-
const __dirname = import.meta.dirname
|
|
30
|
-
|
|
31
|
-
const appRoot = path.resolve(__dirname,"..","..","specs")
|
|
32
|
-
const publicRoot = path.resolve(appRoot,"public")
|
|
33
|
-
|
|
34
|
-
class AssetMetadata {
|
|
35
|
-
constructor(parsedJSON, publicRoot) {
|
|
36
|
-
this.assetMetadata = parsedJSON.asset_metadata
|
|
37
|
-
this.publicRoot = publicRoot
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
scriptURLs() {
|
|
41
|
-
return Object.entries(this.assetMetadata[".js"]).map( (entry) => {
|
|
42
|
-
return entry[0]
|
|
43
|
-
})
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
fileContainingScriptURL(scriptURL) {
|
|
47
|
-
const file = Object.entries(this.assetMetadata[".js"]).find( (entry) => {
|
|
48
|
-
return entry[0] == scriptURL
|
|
49
|
-
})
|
|
50
|
-
if (!file || !file[1]) {
|
|
51
|
-
return null
|
|
52
|
-
}
|
|
53
|
-
let relativePath = file[1]
|
|
54
|
-
if (relativePath[0] == "/") {
|
|
55
|
-
relativePath = relativePath.slice(1)
|
|
56
|
-
}
|
|
57
|
-
return fs.readFileSync(path.resolve(this.publicRoot,relativePath))
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
const assetMetadata = new AssetMetadata(
|
|
62
|
-
JSON.parse(fs.readFileSync(path.resolve(appRoot,"config","asset_metadata.json"))),
|
|
63
|
-
publicRoot,
|
|
64
|
-
)
|
|
65
|
-
|
|
66
|
-
class AssetMetadataLoader extends ResourceLoader {
|
|
67
|
-
constructor(assetMetadata) {
|
|
68
|
-
super()
|
|
69
|
-
this.assetMetadata = assetMetadata
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
fetch(url,options) {
|
|
73
|
-
const parsedURL = new URL(url)
|
|
74
|
-
const jsContents = this.assetMetadata.fileContainingScriptURL(parsedURL.pathname)
|
|
75
|
-
if (jsContents) {
|
|
76
|
-
return Promise.resolve(jsContents)
|
|
77
|
-
}
|
|
78
|
-
else {
|
|
79
|
-
return super.fetch(url,options)
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
const resourceLoader = new AssetMetadataLoader(assetMetadata)
|
|
85
|
-
|
|
86
|
-
const createDOM = (html, queryString) => {
|
|
87
|
-
|
|
88
|
-
const url = "http://example.com" + ( queryString ? `?${queryString}` : "" )
|
|
89
|
-
|
|
90
|
-
const scripts = assetMetadata.scriptURLs().map( (url) => `<script src="${url}"></script>` )
|
|
91
|
-
const virtualConsole = new VirtualConsole()
|
|
92
|
-
virtualConsole.sendTo(console);
|
|
93
|
-
return new JSDOM(
|
|
94
|
-
`<!DOCTYPE html>
|
|
95
|
-
<html>
|
|
96
|
-
<head>
|
|
97
|
-
${scripts}
|
|
98
|
-
</head>
|
|
99
|
-
<body>
|
|
100
|
-
${html}
|
|
101
|
-
</body>
|
|
102
|
-
</html>
|
|
103
|
-
`,{
|
|
104
|
-
resources: "usable",
|
|
105
|
-
runScripts: "dangerously",
|
|
106
|
-
includeNodeLocations: true,
|
|
107
|
-
resources: resourceLoader,
|
|
108
|
-
url: url
|
|
109
|
-
}
|
|
110
|
-
)
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
/**
|
|
114
|
-
* Returned by {@link withHTML} as the basis for writing tests for custom elements. You won't create
|
|
115
|
-
* instances of this class. Rather, you'll be given an instance from `withHTML` and likely call
|
|
116
|
-
* `test` on it.
|
|
117
|
-
*
|
|
118
|
-
* @see withHTML
|
|
119
|
-
*/
|
|
120
|
-
class CustomElementTest {
|
|
121
|
-
constructor(html, queryString) {
|
|
122
|
-
this.html = html
|
|
123
|
-
this.queryString = queryString
|
|
124
|
-
this.fetchBehavior = {}
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
/**
|
|
128
|
-
* Configure a query string to be present when the custom elements are defined and connected.
|
|
129
|
-
*/
|
|
130
|
-
andQueryString(queryString) {
|
|
131
|
-
this.queryString = queryString
|
|
132
|
-
return this
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
/**
|
|
136
|
-
* Configure behavior when #{link external:fetch} is called, since it's not implemented by JSDom or NodeJS.
|
|
137
|
-
*
|
|
138
|
-
* @param {String} url - the URL that is expected. This should be a relative URL, as that is all that is currently supported. This
|
|
139
|
-
* URL must match exactly to a `fetch` call.
|
|
140
|
-
* @param {Object} behavior - an object describing what you want to happen when `url` is fetched. This currently supports only a
|
|
141
|
-
* few rudimentary behaviors:
|
|
142
|
-
* * `{then: { ok: { text: "some text" } } }` - This will return an "ok" response whose body is the given text, available only as
|
|
143
|
-
* text.
|
|
144
|
-
* * `{then: { status: XXX, text: "some text" }}` - This will return the given http status, with `ok` as true if it's 2xx or 3xx. If `text` is given, that will be available as text only.
|
|
145
|
-
*/
|
|
146
|
-
onFetch(url,behavior) {
|
|
147
|
-
if (!this.fetchBehavior[url]) {
|
|
148
|
-
this.fetchBehavior[url] = {
|
|
149
|
-
numCalls: 0,
|
|
150
|
-
responses: [],
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
if (behavior instanceof Array) {
|
|
154
|
-
behavior.forEach( (b) => {
|
|
155
|
-
this.fetchBehavior[url].responses.push(b)
|
|
156
|
-
})
|
|
157
|
-
}
|
|
158
|
-
else {
|
|
159
|
-
this.fetchBehavior[url].responses.push(behavior)
|
|
160
|
-
}
|
|
161
|
-
return this
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
/** Comment out a test without using code comments */
|
|
165
|
-
xtest() {
|
|
166
|
-
return this
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
/** Declare a test to run with the previously-defined HTML, query string, and fetch behavior.
|
|
171
|
-
*
|
|
172
|
-
* @param {String} description - a description of the test.
|
|
173
|
-
* @param {testCodeCallback} testCode - a function containing the code for your test.
|
|
174
|
-
*/
|
|
175
|
-
test(description,testCode) {
|
|
176
|
-
it(description, () => {
|
|
177
|
-
const dom = createDOM(this.html,this.queryString)
|
|
178
|
-
const fetchRequests = []
|
|
179
|
-
|
|
180
|
-
dom.window.Request = Request
|
|
181
|
-
dom.window.fetch = (request) => {
|
|
182
|
-
const url = new URL(request.url)
|
|
183
|
-
const path = url.pathname + url.search
|
|
184
|
-
const behaviors = this.fetchBehavior[path]
|
|
185
|
-
if (!behaviors) {
|
|
186
|
-
throw `fetch() called with ${path}, which was not configured`
|
|
187
|
-
}
|
|
188
|
-
if (behaviors.numCalls > behaviors.responses.length) {
|
|
189
|
-
throw `fetch() called ${behaviors.numCalls} times, but we only have ${behaviors.response.length} responses configured`
|
|
190
|
-
}
|
|
191
|
-
const behavior = behaviors.responses[behaviors.numCalls]
|
|
192
|
-
behaviors.numCalls++
|
|
193
|
-
|
|
194
|
-
let promise = null
|
|
195
|
-
|
|
196
|
-
if (behavior.then) {
|
|
197
|
-
if (behavior.then.ok) {
|
|
198
|
-
if (behavior.then.ok.text) {
|
|
199
|
-
const response = {
|
|
200
|
-
ok: true,
|
|
201
|
-
text: () => {
|
|
202
|
-
return Promise.resolve(behavior.then.ok.text)
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
promise = Promise.resolve(response)
|
|
206
|
-
}
|
|
207
|
-
else {
|
|
208
|
-
throw `unknown fetch behavior: expected then.ok.text: ${JSON.stringify(behavior)}`
|
|
209
|
-
}
|
|
210
|
-
}
|
|
211
|
-
else if (behavior.then.status) {
|
|
212
|
-
let ok = false
|
|
213
|
-
if ((behavior.then.status >= 200) && (behavior.then.status < 400) ){
|
|
214
|
-
ok = true
|
|
215
|
-
}
|
|
216
|
-
const response = {
|
|
217
|
-
ok: ok,
|
|
218
|
-
status: behavior.then.status,
|
|
219
|
-
text: () => {
|
|
220
|
-
return Promise.resolve(behavior.then.text)
|
|
221
|
-
}
|
|
222
|
-
}
|
|
223
|
-
promise = Promise.resolve(response)
|
|
224
|
-
}
|
|
225
|
-
else {
|
|
226
|
-
throw `unknown fetch behavior: expected then.ok or then.status: ${JSON.stringify(behavior)}`
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
|
-
else {
|
|
230
|
-
throw `unknown fetch behavior: expected then: ${JSON.stringify(behavior)}`
|
|
231
|
-
}
|
|
232
|
-
request.promiseReturned = promise
|
|
233
|
-
fetchRequests.push(request)
|
|
234
|
-
return promise
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
const window = dom.window
|
|
238
|
-
const document = window.document
|
|
239
|
-
let returnValue = null
|
|
240
|
-
return new Promise( (resolve, reject) => {
|
|
241
|
-
dom.window.addEventListener("load", () => {
|
|
242
|
-
try {
|
|
243
|
-
returnValue = testCode({window,document,assert,fetchRequests})
|
|
244
|
-
if (returnValue) {
|
|
245
|
-
resolve(returnValue)
|
|
246
|
-
}
|
|
247
|
-
else {
|
|
248
|
-
resolve()
|
|
249
|
-
}
|
|
250
|
-
} catch (e) {
|
|
251
|
-
reject(e)
|
|
252
|
-
}
|
|
253
|
-
})
|
|
254
|
-
})
|
|
255
|
-
})
|
|
256
|
-
return this
|
|
257
|
-
}
|
|
258
|
-
}
|
|
24
|
+
import AssetMetadata from "./AssetMetadata.js"
|
|
25
|
+
import CustomElementTest from "./CustomElementTest.js"
|
|
259
26
|
|
|
260
27
|
/**
|
|
261
|
-
*
|
|
262
|
-
*
|
|
28
|
+
* Bootstraps a test based on some HTML and configuration about where the bundled custom elements are. It's recommended that you
|
|
29
|
+
* create the method `withHTML` in your `SpecHelper.js` file to set up the asset metadata stuff.
|
|
263
30
|
*
|
|
264
31
|
* This returns a {@link module:testing~CustomElementTest}, on which you can call additional setup methods, or start defining tests with the
|
|
265
32
|
* {@link module:testing~CustomElementTest#test} method.
|
|
266
33
|
*
|
|
267
|
-
* @
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
* Given a fetch request (available via `fetchRequests` passed to your test), turn the body into JSON
|
|
273
|
-
* and return a promise with the JSON. To use this in a test, you must include your assertions inside
|
|
274
|
-
* the `then` of the returned promise and you *must* return that from your test.
|
|
275
|
-
*
|
|
276
|
-
* @example
|
|
277
|
-
* withHTML(
|
|
278
|
-
* "<div-makes-a-fetch>"
|
|
279
|
-
* ).onFetch("/foo", { then: { status: 200 }}
|
|
280
|
-
* ).test("a test",({assert,fetchRequests}) => {
|
|
281
|
-
* assert.equal(fetchRequests.length,1)
|
|
282
|
-
* return readRequestBodyIntoJSON(fetchRequests[0]).then( (json) => {
|
|
283
|
-
* assert.equal(json["foo"],"bar")
|
|
284
|
-
* })
|
|
285
|
-
* })
|
|
286
|
-
*/
|
|
287
|
-
const readRequestBodyIntoJSON = (request) => {
|
|
288
|
-
return readRequestBodyIntoString(request).then( (string) => {
|
|
289
|
-
try {
|
|
290
|
-
const json = JSON.parse(string)
|
|
291
|
-
return Promise.resolve(json)
|
|
292
|
-
}
|
|
293
|
-
catch (e) {
|
|
294
|
-
assert(false,`'${string}' could not be parsed as JSON`)
|
|
295
|
-
}
|
|
296
|
-
return Promise.resolve()
|
|
297
|
-
})
|
|
298
|
-
}
|
|
299
|
-
|
|
300
|
-
/**
|
|
301
|
-
* Given a fetch request (available via `fetchRequests` passed to your test), turn the body into a string
|
|
302
|
-
* and return a promise with the string. To use this in a test, you must include your assertions inside
|
|
303
|
-
* the `then` of the returned promise and you *must* return that from your test.
|
|
34
|
+
* @param {String} html - HTML that should be in the document for the test
|
|
35
|
+
* @param {Object} assetMetadata - a JSON object describing where the bundles are. This is needed to allow JSDOM to load the custom
|
|
36
|
+
* elements as if it were served up by a webserver
|
|
37
|
+
* @param {String} publicRoot - The root to where JS files. When using this in a BrutRB web app, this would be where your bundled
|
|
38
|
+
* files are placed for serving.
|
|
304
39
|
*
|
|
305
|
-
* @
|
|
306
|
-
* withHTML(
|
|
307
|
-
* "<div-makes-a-fetch>"
|
|
308
|
-
* ).onFetch("/foo", { then: { status: 200 }}
|
|
309
|
-
* ).test("a test",({assert,fetchRequests}) => {
|
|
310
|
-
* assert.equal(fetchRequests.length,1)
|
|
311
|
-
* return readRequestBodyIntoString(fetchRequests[0]).then( (string) => {
|
|
312
|
-
* assert.equal(string,"foo")
|
|
313
|
-
* })
|
|
314
|
-
* })
|
|
315
|
-
*/
|
|
316
|
-
const readRequestBodyIntoString = (request) => {
|
|
317
|
-
const reader = request.body.getReader()
|
|
318
|
-
const utf8Decoder = new TextDecoder("utf-8");
|
|
319
|
-
let string = ""
|
|
320
|
-
const parse = ({value,done}) => {
|
|
321
|
-
if (done) {
|
|
322
|
-
return Promise.resolve(string)
|
|
323
|
-
}
|
|
324
|
-
else {
|
|
325
|
-
if (value) {
|
|
326
|
-
string = string + utf8Decoder.decode(value, { stream: true })
|
|
327
|
-
}
|
|
328
|
-
return reader.read().then(parse)
|
|
329
|
-
}
|
|
330
|
-
}
|
|
331
|
-
return reader.read().then(parse)
|
|
332
|
-
}
|
|
333
|
-
|
|
334
|
-
/** Used to wait for a few milliseconds before performing further assertions.
|
|
335
|
-
* This can be useful when a custom element has `setTimeout` calls
|
|
40
|
+
* @see module:testing~CustomElementTest
|
|
336
41
|
*/
|
|
337
|
-
const
|
|
338
|
-
return new
|
|
339
|
-
setTimeout(resolve,ms)
|
|
340
|
-
})
|
|
42
|
+
const createTestBasedOnHTML = ({html,assetMetadata,publicRoot}) => {
|
|
43
|
+
return new CustomElementTest(html,null,new AssetMetadata(assetMetadata,publicRoot))
|
|
341
44
|
}
|
|
342
45
|
|
|
343
46
|
export {
|
|
344
|
-
|
|
345
|
-
readRequestBodyIntoJSON,
|
|
346
|
-
readRequestBodyIntoString,
|
|
347
|
-
waitForSetTimeout,
|
|
47
|
+
createTestBasedOnHTML
|
|
348
48
|
}
|