-
Notifications
You must be signed in to change notification settings - Fork 2
/
AssertionComponent.tsx
619 lines (546 loc) · 29.1 KB
/
AssertionComponent.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
import { AnyFunction, ClassUnion, Mixin } from "typescript-mixin-class/index.js"
import { TextJSX } from "../../../jsx/TextJSX.js"
import { WaitForResult } from "../../../util/TimeHelpers.js"
import { isFunction, isString } from "../../../util/Typeguards.js"
import { isElementPointReachable } from "../../../util_browser/Dom.js"
import { WaitForOptions } from "../../test/assertion/AssertionAsync.js"
import { ExtComponent, TestSenchaPre } from "../TestSenchaPre.js"
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
/**
* This type excludes the `condition` property from the [[WaitForOptions]] type and adds `target`
*
* It is used in the [[waitForComponentVisible]], [[waitForComponentNotVisible]] methods.
*/
export type WaitForComponentOptions = Omit<WaitForOptions<ExtComponent>, 'condition'> & {
/**
* Either an `Ext.Component` instance or a component query string.
*/
target : string | ExtComponent
/**
* The optional root component in which the query should be resolved
*/
root : ExtComponent
}
/**
* This type excludes the `condition` property from the [[WaitForOptions]] type and adds `target`
*
* It is used in the [[waitForComponentVisible]] method.
*/
export type WaitForComponentQueryOptions = Omit<WaitForOptions<ExtComponent>, 'condition'> & {
/**
* A component query string to wait for
*/
target : string
/**
* The optional root component in which the query should be resolved
*/
root : ExtComponent
}
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
export class AssertionComponent extends Mixin(
[ TestSenchaPre ],
(base : ClassUnion<typeof TestSenchaPre>) =>
class AssertionComponent extends base {
/**
* Waits until the main element of the passed component becomes "reachable" in the DOM.
* This means at least some part of the element should be within the currently visible viewport
* and the center of that part should be directly reachable by user (ie not hidden by some other
* element).
*
* Returns a promise, which is resolved to the passed component instance (or result of the component query).
*
* This method may throw or issue a warning if passed component query is resolved to multiple elements and
* [[onAmbiguousQuery]] config is set to the appropriate value.
*
* @param options Either a component query string, Ext.Component instance or [[WaitForComponentOptions]] options.
* If provided as a component query, this method will additionally wait for the query to resolve to at least one
* component.
*
* @category Sencha: Wait for
*/
async waitForComponentVisible (
options : string | ExtComponent | Partial<WaitForComponentOptions>, callback? : AnyFunction, scope? : object, timeout? : number
)
: Promise<ExtComponent>
{
const target = isString(options) || this.isExtComponent(options) ? options : options.target
const opts = isString(options) || this.isExtComponent(options) ? {} : options
if (!isString(target) && !this.isExtComponent(target)) throw new Error("Invalid input for `waitForComponentVisible")
let warned : boolean = false
return await this.waitFor(Object.assign(opts, {
condition : () => {
const components = this.resolveExtComponentAll(target, opts.root)
if (!warned) warned = this.warnAmbiguousComponentQuery(components)
if (components.length === 0) return null
const el = this.compToEl(components[ 0 ])
return el && isElementPointReachable(el, undefined, true).reachable ? components[ 0 ] : null
},
reporting : {
assertionName : 'waitForComponentVisible',
onTimeout : (waitRes : WaitForResult<ExtComponent>, waitOptions : WaitForOptions<ExtComponent>) =>
<div>
Waited too long for the element of component { target } to become reachable
</div>,
onConditionMet : (waitRes : WaitForResult<ExtComponent>, waitOptions : WaitForOptions<ExtComponent>) =>
<div>
Waited { waitRes.elapsedTime }ms for the element of component { target } to become reachable
</div>,
onException : (waitRes : WaitForResult<ExtComponent>, waitOptions : WaitForOptions<ExtComponent>) =>
<div>
<div>Exception thrown while checking for component visibility</div>
<div>{ String(waitRes.exception) }</div>
</div>
}
} as WaitForOptions<ExtComponent>, timeout != null ? { timeout } as WaitForOptions<ExtComponent> : null))
}
/**
* Waits until the passed component becomes hidden, as defined by its `hidden` property.
*
* Returns a promise, which is resolved to the passed component instance (or result of the component query).
*
* This method may throw or issue a warning if passed component query is resolved to multiple elements and
* [[onAmbiguousQuery]] config is set to the appropriate value.
*
* @param options Either a component query string, Ext.Component instance or [[WaitForComponentOptions]] options.
* If provided as a component query, this method will additionally wait for the query to resolve to at least one
* component.
*
* @category Sencha: Wait for
*/
async waitForComponentNotVisible (
options : string | ExtComponent | Partial<WaitForComponentOptions>, callback? : AnyFunction, scope? : object, timeout? : number
)
: Promise<ExtComponent>
{
const target = isString(options) || this.isExtComponent(options) ? options : options.target
const opts = isString(options) || this.isExtComponent(options) ? {} : options
if (!isString(target) && !this.isExtComponent(target)) throw new Error("Invalid input for `waitForComponentVisible")
let warned : boolean = false
return await this.waitFor(Object.assign(opts, {
condition : () => {
const components = this.resolveExtComponentAll(target, opts.root)
if (!warned) warned = this.warnAmbiguousComponentQuery(components)
if (components.length === 0) return null
return this.componentIsHidden(components[ 0 ]) ? components[ 0 ] : null
},
reporting : {
assertionName : 'waitForComponentNotVisible',
onTimeout : (waitRes : WaitForResult<ExtComponent>, waitOptions : WaitForOptions<ExtComponent>) =>
<div>
Waited too long for the element of component { target } to become not visible
</div>,
onConditionMet : (waitRes : WaitForResult<ExtComponent>, waitOptions : WaitForOptions<ExtComponent>) =>
<div>
Waited { waitRes.elapsedTime }ms for the element of component { target } to become not visible
</div>,
onException : (waitRes : WaitForResult<ExtComponent>, waitOptions : WaitForOptions<ExtComponent>) =>
<div>
<div>Exception thrown while checking for component visibility</div>
<div>{ String(waitRes.exception) }</div>
</div>
}
} as WaitForOptions<ExtComponent>, timeout != null ? { timeout } as WaitForOptions<ExtComponent> : null))
}
/**
* Waits until the passed component query is resolved to at least one component.
*
* Returns a promise, which is resolved to the result of the query.
*
* The "root" argument of this method can be omitted.
*
* @param options The component query selector or [[WaitForComponentQueryOptions]] object
* @param root The container to start a component query from. Optional
*
* @category Sencha: Wait for
*/
async waitForComponentQuery (
options : string | Partial<WaitForComponentQueryOptions>, root? : ExtComponent, callback? : AnyFunction, scope? : object, timeout? : number
)
: Promise<ExtComponent[]>
{
// no `root` supplied
if (isFunction(root)) {
// @ts-ignore
timeout = scope
scope = callback
callback = root
root = undefined
}
const target = isString(options) ? options : options.target
const opts = isString(options) ? {} as Partial<WaitForComponentQueryOptions> : options
if (!isString(target)) throw new Error("Invalid input for `waitForComponentQuery")
return await this.waitFor(Object.assign(opts, {
condition : () => {
const result = this.componentQuery(target, root ?? opts.root)
return result.length > 0 ? result : null
},
reporting : {
assertionName : 'waitForComponentQuery',
onTimeout : (waitRes : WaitForResult<ExtComponent[]>, waitOptions : WaitForOptions<ExtComponent[]>) =>
<div>
Waited too long for the component query { target } to return at least one component
</div>,
onConditionMet : (waitRes : WaitForResult<ExtComponent[]>, waitOptions : WaitForOptions<ExtComponent[]>) =>
<div>
Waited { waitRes.elapsedTime }ms for the component query { target } to return at least one component
</div>,
onException : (waitRes : WaitForResult<ExtComponent[]>, waitOptions : WaitForOptions<ExtComponent[]>) =>
<div>
<div>Exception thrown while resolving the component query { target }</div>
<div>{ String(waitRes.exception) }</div>
</div>
}
} as WaitForOptions<ExtComponent[]>, timeout != null ? { timeout } as WaitForOptions<ExtComponent[]> : null))
}
/**
* Waits until the passed component query is resolved to empty array.
*
* Returns a promise, which is resolved to the result of the query.
*
* The "root" argument of this method can be omitted.
*
* @param options The component query selector or [[WaitForComponentQueryOptions]] object
* @param root The container to start a component query from. Optional
*
* @category Sencha: Wait for
*/
async waitForComponentQueryNotFound (
options : string | Partial<WaitForComponentQueryOptions>, root? : ExtComponent, callback? : AnyFunction, scope? : object, timeout? : number
)
: Promise<ExtComponent[]>
{
// no `root` supplied
if (isFunction(root)) {
// @ts-ignore
timeout = scope
scope = callback
callback = root
root = undefined
}
const target = isString(options) ? options : options.target
const opts = isString(options) ? {} as Partial<WaitForComponentQueryOptions> : options
if (!isString(target)) throw new Error("Invalid input for `waitForComponentQuery")
return await this.waitFor(Object.assign(opts, {
condition : () => {
const result = this.componentQuery(target, root ?? opts.root)
return result.length === 0 ? result : null
},
reporting : {
assertionName : 'waitForComponentQueryNotFound',
onTimeout : (waitRes : WaitForResult<ExtComponent[]>, waitOptions : WaitForOptions<ExtComponent[]>) =>
<div>
Waited too long for the component query { target } to return empty array
</div>,
onConditionMet : (waitRes : WaitForResult<ExtComponent[]>, waitOptions : WaitForOptions<ExtComponent[]>) =>
<div>
Waited { waitRes.elapsedTime }ms for the component query { target } to return empty array
</div>,
onException : (waitRes : WaitForResult<ExtComponent[]>, waitOptions : WaitForOptions<ExtComponent[]>) =>
<div>
<div>Exception thrown while resolving the component query { target }</div>
<div>{ String(waitRes.exception) }</div>
</div>
}
} as WaitForOptions<ExtComponent[]>, timeout != null ? { timeout } as WaitForOptions<ExtComponent[]> : null))
}
/**
* Waits until the passed composite query is resolved to at least one DOM element.
*
* Returns a promise, which is resolved to the result of the query.
*
* The "root" argument of this method can be omitted.
*
* @param options The component query selector or [[WaitForComponentQueryOptions]] object
* @param root The container to start a component query from. Optional
*
* @category Sencha: Wait for
*/
async waitForCompositeQuery (
options : string | Partial<WaitForComponentQueryOptions>, root? : ExtComponent, callback? : AnyFunction, scope? : object, timeout? : number
)
: Promise<Element[]>
{
// no `root` supplied
if (isFunction(root)) {
// @ts-ignore
timeout = scope
scope = callback
callback = root
root = undefined
}
const target = isString(options) ? options : options.target
const opts = isString(options) ? {} as Partial<WaitForComponentQueryOptions> : options
if (!isString(target)) throw new Error("Invalid input for `waitForComponentQuery")
return await this.waitFor(Object.assign(opts, {
condition : () => {
const result = this.compositeQuery(target, root ?? opts.root)
return result.length > 0 ? result : null
},
reporting : {
assertionName : 'waitForCompositeQuery',
onTimeout : (waitRes : WaitForResult<Element[]>, waitOptions : WaitForOptions<Element[]>) =>
<div>
Waited too long for the composite query { target } to return at least one DOM element
</div>,
onConditionMet : (waitRes : WaitForResult<Element[]>, waitOptions : WaitForOptions<Element[]>) =>
<div>
Waited { waitRes.elapsedTime }ms for the composite query { target } to return at least one DOM element
</div>,
onException : (waitRes : WaitForResult<Element[]>, waitOptions : WaitForOptions<Element[]>) =>
<div>
<div>Exception thrown while resolving the composite query { target }</div>
<div>{ String(waitRes.exception) }</div>
</div>
}
} as WaitForOptions<Element[]>, timeout != null ? { timeout } as WaitForOptions<Element[]> : null))
}
/**
* Waits until the passed composite query is resolved to empty array.
*
* Returns a promise, which is resolved to the result of the query.
*
* The "root" argument of this method can be omitted.
*
* @param options The composite query selector or [[WaitForComponentQueryOptions]] object
* @param root The container to start a composite query from. Optional
*
* @category Sencha: Wait for
*/
async waitForCompositeQueryNotFound (
options : string | Partial<WaitForComponentQueryOptions>, root? : ExtComponent, callback? : AnyFunction, scope? : object, timeout? : number
)
: Promise<Element[]>
{
// no `root` supplied
if (isFunction(root)) {
// @ts-ignore
timeout = scope
scope = callback
callback = root
root = undefined
}
const target = isString(options) ? options : options.target
const opts = isString(options) ? {} as Partial<WaitForComponentQueryOptions> : options
if (!isString(target)) throw new Error("Invalid input for `waitForComponentQuery")
return await this.waitFor(Object.assign(opts, {
condition : () => {
const result = this.compositeQuery(target, root ?? opts.root)
return result.length === 0 ? result : null
},
reporting : {
assertionName : 'waitForCompositeQueryNotFound',
onTimeout : (waitRes : WaitForResult<Element[]>, waitOptions : WaitForOptions<Element[]>) =>
<div>
Waited too long for the component query { target } to return empty array
</div>,
onConditionMet : (waitRes : WaitForResult<Element[]>, waitOptions : WaitForOptions<Element[]>) =>
<div>
Waited { waitRes.elapsedTime }ms for the component query { target } to return empty array
</div>,
onException : (waitRes : WaitForResult<Element[]>, waitOptions : WaitForOptions<Element[]>) =>
<div>
<div>Exception thrown while resolving the component query { target }</div>
<div>{ String(waitRes.exception) }</div>
</div>
}
} as WaitForOptions<Element[]>, timeout != null ? { timeout } as WaitForOptions<Element[]> : null))
}
/**
* Alias for [[waitForComponentQuery]]
*
* @param options
* @param root
* @param callback
* @param scope
* @param timeout
*
* @category Sencha: Wait for
*/
async waitForCQ (
options : string | Partial<WaitForComponentQueryOptions>, root? : ExtComponent, callback? : AnyFunction, scope? : object, timeout? : number
)
: Promise<ExtComponent[]>
{
return this.waitForComponentQuery(options, root, callback, scope, timeout)
}
/**
* Alias for [[waitForComponentQueryNotFound]]
*
* @param options
* @param root
* @param callback
* @param scope
* @param timeout
*
* @category Sencha: Wait for
*/
async waitForCQNotFound (
options : string | Partial<WaitForComponentQueryOptions>, root? : ExtComponent, callback? : AnyFunction, scope? : object, timeout? : number
) {
return this.waitForComponentQueryNotFound(options, root, callback, scope, timeout)
}
/**
* Alias for [[waitForComponentQueryVisible]]
*
* @param options
* @param root
* @param callback
* @param scope
* @param timeout
*
* @category Sencha: Wait for
*/
async waitForCQVisible (
options : string | Partial<WaitForComponentQueryOptions>, root? : ExtComponent, callback? : AnyFunction, scope? : object, timeout? : number
)
: Promise<ExtComponent[]>
{
return this.waitForComponentQueryVisible(options, root, callback, scope, timeout)
}
/**
* Alias for [[waitForComponentQueryNotVisible]]
*
* @param options
* @param root
* @param callback
* @param scope
* @param timeout
*
* @category Sencha: Wait for
*/
async waitForCQNotVisible (
options : string | Partial<WaitForComponentQueryOptions>, root? : ExtComponent, callback? : AnyFunction, scope? : object, timeout? : number
)
: Promise<ExtComponent[]>
{
return this.waitForComponentQueryNotVisible(options, root, callback, scope, timeout)
}
/**
* Waits until all results of the `Ext.ComponentQuery` are detected and visible.
* The visibility criteria in this assertion is that component should be rendered and its main element
* has non-empty bounding rectangle (as returned by the [getBoundingClientRect](https://developer.mozilla.org/en-US/docs/Web/API/Element/getBoundingClientRect) method.
* Note, that element still may be not visible on the screen, because it can be scrolled out of the viewport.
* This is different from the [[waitForComponentVisible]] method.
*
* This assertion is useful, when you need to determine that many components are rendered and physically
* present in the DOM.
*
* Returns a promise, which is resolved to the result of the query.
*
* The "root" argument of this method can be omitted.
*
* @param options The component query selector or [[WaitForComponentQueryOptions]] object
* @param root The container to start a component query from. Optional
*
* @category Sencha: Wait for
*/
async waitForComponentQueryVisible (
options : string | Partial<WaitForComponentQueryOptions>, root? : ExtComponent, callback? : AnyFunction, scope? : object, timeout? : number
)
: Promise<ExtComponent[]>
{
// no `root` supplied
if (isFunction(root)) {
// @ts-ignore
timeout = scope
scope = callback
callback = root
root = undefined
}
const target = isString(options) ? options : options.target
const opts = isString(options) ? {} as Partial<WaitForComponentQueryOptions> : options
if (!isString(target)) throw new Error("Invalid input for `waitForComponentQuery")
let firstNonVisibleId
let resultsLen : number
return await this.waitFor(Object.assign(opts, {
condition : () => {
const result = this.componentQuery(target, root ?? opts.root)
resultsLen = result.length
for (const comp of result)
if (this.componentIsHidden(comp)) { firstNonVisibleId = comp.id; return null }
return resultsLen === 0 ? null : result
},
reporting : {
assertionName : 'waitForComponentQueryVisible',
onTimeout : (waitRes : WaitForResult<ExtComponent[]>, waitOptions : WaitForOptions<ExtComponent[]>) => {
if (resultsLen === 0)
return <div>
Waited too long for the component query { target } to return non-empty array
</div>
else
return <div>
Waited too long for the component query { target } to return array of visible components.
The matching component [id={ firstNonVisibleId }] is not visible
</div>
},
onConditionMet : (waitRes : WaitForResult<ExtComponent[]>, waitOptions : WaitForOptions<ExtComponent[]>) =>
<div>
Waited { waitRes.elapsedTime }ms for the component query { target } to return non-empty array of visible components
</div>,
onException : (waitRes : WaitForResult<ExtComponent[]>, waitOptions : WaitForOptions<ExtComponent[]>) =>
<div>
<div>Exception thrown while resolving and checking for visibility the component query { target }</div>
<div>{ String(waitRes.exception) }</div>
</div>
}
} as WaitForOptions<ExtComponent[]>, timeout != null ? { timeout } as WaitForOptions<ExtComponent[]> : null))
}
/**
* `Ext.ComponentQuery` is either empty, or the found component(s) is not visible.
* See the [[waitForComponentQueryVisible]] for the visibility criteria.
*
* Returns a promise, which is resolved to the result of the query.
*
* The "root" argument of this method can be omitted.
*
* @param options The component query selector or [[WaitForComponentQueryOptions]] object
* @param root The container to start a component query from. Optional
*
* @category Sencha: Wait for
*/
async waitForComponentQueryNotVisible (
options : string | Partial<WaitForComponentQueryOptions>, root? : ExtComponent, callback? : AnyFunction, scope? : object, timeout? : number
)
: Promise<ExtComponent[]>
{
// no `root` supplied
if (isFunction(root)) {
// @ts-ignore
timeout = scope
scope = callback
callback = root
root = undefined
}
const target = isString(options) ? options : options.target
const opts = isString(options) ? {} as Partial<WaitForComponentQueryOptions> : options
if (!isString(target)) throw new Error("Invalid input for `waitForComponentQuery")
let firstVisibleId
return await this.waitFor(Object.assign(opts, {
condition : () => {
const result = this.componentQuery(target, root ?? opts.root)
for (const comp of result)
if (!this.componentIsHidden(comp)) { firstVisibleId = comp.id; return null }
return result
},
reporting : {
assertionName : 'waitForComponentQueryNotVisible',
onTimeout : (waitRes : WaitForResult<ExtComponent[]>, waitOptions : WaitForOptions<ExtComponent[]>) => {
return <div>
Waited too long for the component query { target } to return array of visible components.
The matching component [id={ firstVisibleId }] is visible
</div>
},
onConditionMet : (waitRes : WaitForResult<ExtComponent[]>, waitOptions : WaitForOptions<ExtComponent[]>) =>
<div>
Waited { waitRes.elapsedTime }ms for the component query { target } to return non-empty array of visible components
</div>,
onException : (waitRes : WaitForResult<ExtComponent[]>, waitOptions : WaitForOptions<ExtComponent[]>) =>
<div>
<div>Exception thrown while resolving and checking for visibility the component query { target }</div>
<div>{ String(waitRes.exception) }</div>
</div>
}
} as WaitForOptions<ExtComponent[]>, timeout != null ? { timeout } as WaitForOptions<ExtComponent[]> : null))
}
}
) {}