Skip to content

Commit df69fc6

Browse files
committed
extract assert/assertDom to separate file
1 parent cca911b commit df69fc6

File tree

2 files changed

+133
-76
lines changed

2 files changed

+133
-76
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import $dom from '../../dom'
2+
import $ from 'jquery'
3+
4+
export const selectors = {
5+
visible: 'visible',
6+
hidden: 'hidden',
7+
selected: 'selected',
8+
checked: 'checked',
9+
enabled: 'enabled',
10+
disabled: 'disabled',
11+
focus: 'focused',
12+
focused: 'focused',
13+
} as const
14+
15+
export const accessors = {
16+
attr: 'attribute',
17+
css: 'CSS property',
18+
prop: 'property',
19+
} as const
20+
21+
export type Accessors = keyof typeof accessors
22+
23+
export type Selectors = keyof typeof selectors
24+
25+
export type Methods = Accessors | Selectors | 'data' | 'class' | 'empty' | 'id' | 'html' | 'text' | 'value' | 'descendants' | 'match'
26+
27+
export type PartialAssertionArgs = [Chai.Message, Chai.Message, any?, any?, boolean?]
28+
29+
export interface Callbacks {
30+
onInvalid: (method, obj) => void
31+
onError: (err, method, obj, negated) => void
32+
}
33+
34+
// reset the obj under test
35+
// to be re-wrapped in our own
36+
// jquery, so we can control
37+
// the methods on it
38+
export const wrap = (ctx) => $(ctx._obj)
39+
40+
export function assertDom (ctx: Chai.AssertionStatic, utils: Chai.ChaiUtils, callbacks: Callbacks, method: Methods, ...args: PartialAssertionArgs) {
41+
if (!$dom.isDom(ctx._obj) && !$dom.isJquery(ctx._obj)) {
42+
try {
43+
// always fail the assertion
44+
// if we aren't a DOM like object
45+
// depends on the "negate" flag
46+
const negate = utils.flag(ctx, 'negate')
47+
48+
return ctx.assert(!!negate, ...args)
49+
} catch (err) {
50+
return callbacks.onInvalid(method, ctx._obj)
51+
}
52+
}
53+
}
54+
55+
export function assert (ctx: Chai.AssertionStatic, utils: Chai.ChaiUtils, callbacks: Callbacks, method: Methods, ...[bool, ...args]: Chai.AssertionArgs) {
56+
assertDom(ctx, utils, callbacks, method, ...args)
57+
58+
try {
59+
// reset obj to wrapped
60+
const orig = ctx._obj
61+
const selector = ctx._obj.selector
62+
63+
ctx._obj = wrap(ctx)
64+
65+
if (ctx._obj.length === 0) {
66+
// From jQuery 3.x .selector API is deprecated. (https://api.jquery.com/selector/)
67+
// Because of that, wrap() above removes selector property.
68+
// That's why we're caching the value of selector above and using it here.
69+
ctx._obj = selector ?? 'subject'
70+
// if no element found, fail the existence check
71+
// depends on the negate flag
72+
ctx.assert(!!chai.util.flag(ctx, 'negate'), ...args)
73+
}
74+
75+
// apply the assertion
76+
const ret = ctx.assert(bool, ...args)
77+
78+
ctx._obj = orig
79+
80+
return ret
81+
} catch (err) {
82+
// send it up with the obj and whether it was negated
83+
return callbacks.onError(err, method, ctx._obj, utils.flag(ctx, 'negate'))
84+
}
85+
}

packages/driver/src/cypress/chai_jquery.ts

Lines changed: 48 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,8 @@
11
import _ from 'lodash'
2-
import $ from 'jquery'
32
import $dom from '../dom'
43
import $elements from '../dom/elements'
5-
6-
type Accessors = keyof typeof accessors
7-
type Selectors = keyof typeof selectors
8-
type Methods = Accessors | Selectors | 'data' | 'class' | 'empty' | 'id' | 'html' | 'text' | 'value' | 'descendants' | 'match'
9-
10-
const selectors = {
11-
visible: 'visible',
12-
hidden: 'hidden',
13-
selected: 'selected',
14-
checked: 'checked',
15-
enabled: 'enabled',
16-
disabled: 'disabled',
17-
focus: 'focused',
18-
focused: 'focused',
19-
} as const
20-
21-
const accessors = {
22-
attr: 'attribute',
23-
css: 'CSS property',
24-
prop: 'property',
25-
} as const
26-
27-
// reset the obj under test
28-
// to be re-wrapped in our own
29-
// jquery, so we can control
30-
// the methods on it
31-
const wrap = (ctx) => $(ctx._obj)
4+
import type { Methods, PartialAssertionArgs } from './assertions/assert'
5+
import { assert, assertDom, accessors, selectors, wrap } from './assertions/assert'
326

337
const maybeCastNumberToString = (num: number | string) => {
348
// if this is a finite number (no Infinity or NaN)
@@ -44,55 +18,20 @@ interface Callbacks {
4418
export const $chaiJquery = (chai: Chai.ChaiStatic, chaiUtils: Chai.ChaiUtils, callbacks: Callbacks) => {
4519
const { inspect, flag } = chaiUtils
4620

47-
const assertDom = (ctx, method: Methods, ...args) => {
48-
if (!$dom.isDom(ctx._obj) && !$dom.isJquery(ctx._obj)) {
49-
try {
50-
// always fail the assertion
51-
// if we aren't a DOM like object
52-
// depends on the "negate" flag
53-
return ctx.assert(!!ctx.__flags.negate, ...args)
54-
} catch (err) {
55-
return callbacks.onInvalid(method, ctx._obj)
56-
}
57-
}
58-
}
59-
60-
const assert = (ctx, method: Methods, bool: boolean, ...args) => {
61-
assertDom(ctx, method, ...args)
62-
63-
try {
64-
// reset obj to wrapped
65-
const orig = ctx._obj
66-
const selector = ctx._obj.selector
67-
68-
ctx._obj = wrap(ctx)
69-
70-
if (ctx._obj.length === 0) {
71-
// From jQuery 3.x .selector API is deprecated. (https://api.jquery.com/selector/)
72-
// Because of that, wrap() above removes selector property.
73-
// That's why we're caching the value of selector above and using it here.
74-
ctx._obj = selector ?? 'subject'
75-
// if no element found, fail the existence check
76-
// depends on the negate flag
77-
ctx.assert(!!ctx.__flags.negate, ...args)
78-
}
79-
80-
// apply the assertion
81-
const ret = ctx.assert(bool, ...args)
82-
83-
ctx._obj = orig
84-
85-
return ret
86-
} catch (err) {
87-
// send it up with the obj and whether it was negated
88-
return callbacks.onError(err, method, ctx._obj, flag(ctx, 'negate'))
89-
}
90-
}
91-
92-
const assertPartial = (ctx, method: Methods, actual, expected, message: string, notMessage: string, ...args) => {
93-
if (ctx.__flags.contains || ctx.__flags.includes) {
21+
const assertPartial = (
22+
ctx: Chai.AssertionStatic,
23+
chaiUtils: Chai.ChaiUtils,
24+
callbacks: Callbacks,
25+
method: Methods,
26+
actual: any,
27+
expected: any,
28+
...[message, notMessage, ...args]: PartialAssertionArgs
29+
) => {
30+
if (chaiUtils.flag(ctx, 'contains') || chaiUtils.flag(ctx, 'includes')) {
9431
return assert(
9532
ctx,
33+
chaiUtils,
34+
callbacks,
9635
method,
9736
_.includes(actual, expected),
9837
`expected #{this} to contain ${message}`,
@@ -103,6 +42,8 @@ export const $chaiJquery = (chai: Chai.ChaiStatic, chaiUtils: Chai.ChaiUtils, ca
10342

10443
return assert(
10544
ctx,
45+
chaiUtils,
46+
callbacks,
10647
method,
10748
actual === expected,
10849
`expected #{this} to have ${message}`,
@@ -112,7 +53,8 @@ export const $chaiJquery = (chai: Chai.ChaiStatic, chaiUtils: Chai.ChaiUtils, ca
11253
}
11354

11455
chai.Assertion.addMethod('data', function (...args) {
115-
assertDom(this, 'data')
56+
// @ts-expect-error - Custom assertions expect messages for failures
57+
assertDom(this, chaiUtils, callbacks, 'data')
11658

11759
let a = new chai.Assertion(wrap(this).data())
11860

@@ -127,6 +69,8 @@ export const $chaiJquery = (chai: Chai.ChaiStatic, chaiUtils: Chai.ChaiUtils, ca
12769
chai.Assertion.addMethod('class', function (className: string) {
12870
return assert(
12971
this,
72+
chaiUtils,
73+
callbacks,
13074
'class',
13175
wrap(this).hasClass(className),
13276
'expected #{this} to have class #{exp}',
@@ -140,6 +84,8 @@ export const $chaiJquery = (chai: Chai.ChaiStatic, chaiUtils: Chai.ChaiUtils, ca
14084

14185
return assert(
14286
this,
87+
chaiUtils,
88+
callbacks,
14389
'id',
14490
wrap(this).prop('id') === id,
14591
'expected #{this} to have id #{exp}',
@@ -151,6 +97,8 @@ export const $chaiJquery = (chai: Chai.ChaiStatic, chaiUtils: Chai.ChaiUtils, ca
15197
chai.Assertion.addMethod('html', function (html: string) {
15298
assertDom(
15399
this,
100+
chaiUtils,
101+
callbacks,
154102
'html',
155103
'expected #{this} to have HTML #{exp}',
156104
'expected #{this} not to have HTML #{exp}',
@@ -161,6 +109,8 @@ export const $chaiJquery = (chai: Chai.ChaiStatic, chaiUtils: Chai.ChaiUtils, ca
161109

162110
return assertPartial(
163111
this,
112+
chaiUtils,
113+
callbacks,
164114
'html',
165115
actual,
166116
html,
@@ -176,6 +126,8 @@ export const $chaiJquery = (chai: Chai.ChaiStatic, chaiUtils: Chai.ChaiUtils, ca
176126

177127
assertDom(
178128
this,
129+
chaiUtils,
130+
callbacks,
179131
'text',
180132
'expected #{this} to have text #{exp}',
181133
'expected #{this} not to have text #{exp}',
@@ -186,6 +138,8 @@ export const $chaiJquery = (chai: Chai.ChaiStatic, chaiUtils: Chai.ChaiUtils, ca
186138

187139
return assertPartial(
188140
this,
141+
chaiUtils,
142+
callbacks,
189143
'text',
190144
actual,
191145
text,
@@ -207,6 +161,8 @@ export const $chaiJquery = (chai: Chai.ChaiStatic, chaiUtils: Chai.ChaiUtils, ca
207161

208162
assertDom(
209163
this,
164+
chaiUtils,
165+
callbacks,
210166
'value',
211167
'expected #{this} to have value #{exp}',
212168
'expected #{this} not to have value #{exp}',
@@ -217,6 +173,8 @@ export const $chaiJquery = (chai: Chai.ChaiStatic, chaiUtils: Chai.ChaiUtils, ca
217173

218174
return assertPartial(
219175
this,
176+
chaiUtils,
177+
callbacks,
220178
'value',
221179
actual,
222180
value,
@@ -230,6 +188,8 @@ export const $chaiJquery = (chai: Chai.ChaiStatic, chaiUtils: Chai.ChaiUtils, ca
230188
chai.Assertion.addMethod('descendants', function (selector: string) {
231189
return assert(
232190
this,
191+
chaiUtils,
192+
callbacks,
233193
'descendants',
234194
wrap(this).has(selector).length > 0,
235195
'expected #{this} to have descendants #{exp}',
@@ -243,6 +203,8 @@ export const $chaiJquery = (chai: Chai.ChaiStatic, chaiUtils: Chai.ChaiUtils, ca
243203
if ($dom.isDom(this._obj)) {
244204
return assert(
245205
this,
206+
chaiUtils,
207+
callbacks,
246208
'empty',
247209
wrap(this).is(':empty'),
248210
'expected #{this} to be #{exp}',
@@ -262,6 +224,8 @@ export const $chaiJquery = (chai: Chai.ChaiStatic, chaiUtils: Chai.ChaiUtils, ca
262224
if ($dom.isDom(this._obj)) {
263225
return assert(
264226
this,
227+
chaiUtils,
228+
callbacks,
265229
'match',
266230
wrap(this).is(selector),
267231
'expected #{this} to match #{exp}',
@@ -280,6 +244,8 @@ export const $chaiJquery = (chai: Chai.ChaiStatic, chaiUtils: Chai.ChaiUtils, ca
280244
return chai.Assertion.addProperty(sel, function () {
281245
return assert(
282246
this,
247+
chaiUtils,
248+
callbacks,
283249
sel,
284250
wrap(this).is(`:${sel}`),
285251
'expected #{this} to be #{exp}',
@@ -295,6 +261,8 @@ export const $chaiJquery = (chai: Chai.ChaiStatic, chaiUtils: Chai.ChaiUtils, ca
295261
return chai.Assertion.addMethod(acc, function (name, val) {
296262
assertDom(
297263
this,
264+
chaiUtils,
265+
callbacks,
298266
acc,
299267
`expected #{this} to have ${description} #{exp}`,
300268
`expected #{this} not to have ${description} #{exp}`,
@@ -307,6 +275,8 @@ export const $chaiJquery = (chai: Chai.ChaiStatic, chaiUtils: Chai.ChaiUtils, ca
307275
if (arguments.length === 1) {
308276
assert(
309277
this,
278+
chaiUtils,
279+
callbacks,
310280
acc,
311281
actual !== undefined,
312282
`expected #{this} to have ${description} #{exp}`,
@@ -342,6 +312,8 @@ export const $chaiJquery = (chai: Chai.ChaiStatic, chaiUtils: Chai.ChaiUtils, ca
342312

343313
assert(
344314
this,
315+
chaiUtils,
316+
callbacks,
345317
acc,
346318
(actual != null) && (actual === val),
347319
message,

0 commit comments

Comments
 (0)