Skip to content

Commit 0b63a69

Browse files
committed
WIP: scroll elements into view before asserting fast visibility
1 parent df69fc6 commit 0b63a69

File tree

2 files changed

+83
-1
lines changed

2 files changed

+83
-1
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import { unwrap, isJquery } from '../../dom/jquery'
2+
import { assert, wrap } from './assert'
3+
import type { Callbacks } from './assert'
4+
import { scrollBehaviorOptionsMap } from '../../cy/actionability'
5+
import { fastIsHidden } from '../../dom/visibility/fastIsHidden'
6+
7+
function getSubjects (ref: Chai.AssertionStatic['_obj']): HTMLElement[] {
8+
const unwrapped = isJquery(ref) ? unwrap(ref) : ref
9+
10+
if (Array.isArray(unwrapped)) {
11+
return unwrapped
12+
}
13+
14+
return [unwrapped]
15+
}
16+
17+
function checkSubjects (subjects: HTMLElement[], checkVisibility: (subject: HTMLElement) => boolean): boolean {
18+
return subjects.some((subject) => {
19+
const configuredScrollBehavior = Cypress.config('scrollBehavior')
20+
21+
if (configuredScrollBehavior !== false) {
22+
const block: ScrollLogicalPosition = scrollBehaviorOptionsMap[configuredScrollBehavior]
23+
24+
subject.scrollIntoView({ behavior: 'instant', block })
25+
}
26+
27+
return checkVisibility(subject)
28+
})
29+
}
30+
31+
export function assertVisible (
32+
chaiUtils: Chai.ChaiUtils,
33+
callbacks: Callbacks,
34+
) {
35+
return function (
36+
this: Chai.AssertionStatic,
37+
) {
38+
const subjects = getSubjects(this._obj)
39+
40+
const isVisible = Cypress.config('experimentalFastVisibility') ? checkSubjects(subjects, (s) => !fastIsHidden(s)) : wrap(this).is(':visible')
41+
42+
return assert(
43+
this,
44+
chaiUtils,
45+
callbacks,
46+
'visible',
47+
isVisible,
48+
'expected #{this} to be visible',
49+
'expected #{this} not to be visible',
50+
'visible',
51+
)
52+
}
53+
}
54+
55+
export function assertHidden (
56+
chaiUtils: Chai.ChaiUtils,
57+
callbacks: Callbacks,
58+
) {
59+
return function (this: Chai.AssertionStatic) {
60+
const subjects = getSubjects(this._obj)
61+
const isHidden = Cypress.config('experimentalFastVisibility') ? checkSubjects(subjects, (s) => fastIsHidden(s)) : wrap(this).is(':hidden')
62+
63+
return assert(
64+
this,
65+
chaiUtils,
66+
callbacks,
67+
'hidden',
68+
isHidden,
69+
'expected #{this} to be hidden',
70+
'expected #{this} not to be hidden',
71+
'hidden',
72+
)
73+
}
74+
}

packages/driver/src/cypress/chai_jquery.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import $dom from '../dom'
33
import $elements from '../dom/elements'
44
import type { Methods, PartialAssertionArgs } from './assertions/assert'
55
import { assert, assertDom, accessors, selectors, wrap } from './assertions/assert'
6-
6+
import { assertVisible, assertHidden } from './assertions/visible'
77
const maybeCastNumberToString = (num: number | string) => {
88
// if this is a finite number (no Infinity or NaN)
99
// cast to a string
@@ -241,6 +241,14 @@ export const $chaiJquery = (chai: Chai.ChaiStatic, chaiUtils: Chai.ChaiUtils, ca
241241
_.each(selectors, (selectorName, selector) => {
242242
const sel = selector as keyof typeof selectors
243243

244+
if (sel === 'visible') {
245+
return chai.Assertion.addProperty(sel, assertVisible(chaiUtils, callbacks))
246+
}
247+
248+
if (sel === 'hidden') {
249+
return chai.Assertion.addProperty(sel, assertHidden(chaiUtils, callbacks))
250+
}
251+
244252
return chai.Assertion.addProperty(sel, function () {
245253
return assert(
246254
this,

0 commit comments

Comments
 (0)