11import _ from 'lodash'
2- import $ from 'jquery'
32import $dom from '../dom'
43import $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
337const maybeCastNumberToString = ( num : number | string ) => {
348 // if this is a finite number (no Infinity or NaN)
@@ -44,55 +18,20 @@ interface Callbacks {
4418export 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