@@ -147,6 +147,21 @@ declare global {
147147 ```
148148 * */
149149 ( chainer : "not.be.activeDescendant" ) : Chainable < Subject > ;
150+
151+ /**
152+ * Checks if there is a text selection in effect that
153+ * matches the expectation.
154+ *
155+ * @example
156+ ```
157+ cy.findByTestId('test-input).should('have.selectionRange',0, 2)
158+ ```
159+ * */
160+ (
161+ chainer : "have.selectionRange" ,
162+ from : number ,
163+ to : number ,
164+ ) : Chainable < Subject > ;
150165 }
151166 }
152167}
@@ -163,7 +178,7 @@ const isHighlighted: ChaiPlugin = (_chai) => {
163178 // make sure it's an Element
164179 new _chai . Assertion (
165180 root . nodeType ,
166- `Expected an Element but got '${ String ( root ) } '`
181+ `Expected an Element but got '${ String ( root ) } '` ,
167182 ) . to . equal ( 1 ) ;
168183
169184 const className = this . _obj . attr ( "class" ) ;
@@ -173,7 +188,7 @@ const isHighlighted: ChaiPlugin = (_chai) => {
173188 `expected root to include CSS class #{exp}, got #{act} instead.` ,
174189 `expected root not to have class #{exp}.` ,
175190 "saltHighlighted" ,
176- className
191+ className ,
177192 ) ;
178193 }
179194
@@ -195,7 +210,7 @@ const hasFocusVisible: ChaiPlugin = (_chai) => {
195210 // make sure it's an Element
196211 new _chai . Assertion (
197212 root . nodeType ,
198- `Expected an Element but got '${ String ( root ) } '`
213+ `Expected an Element but got '${ String ( root ) } '` ,
199214 ) . to . equal ( 1 ) ;
200215
201216 const className = this . _obj . attr ( "class" ) ;
@@ -205,7 +220,7 @@ const hasFocusVisible: ChaiPlugin = (_chai) => {
205220 `expected root to include CSS class #{exp}, got #{act} instead.` ,
206221 `expected root not to have class #{exp}.` ,
207222 "saltFocusVisible" ,
208- className
223+ className ,
209224 ) ;
210225 }
211226
@@ -227,7 +242,7 @@ const hasAriaSelected: ChaiPlugin = (_chai) => {
227242 // make sure it's an Element
228243 new _chai . Assertion (
229244 root . nodeType ,
230- `Expected an Element but got '${ String ( root ) } '`
245+ `Expected an Element but got '${ String ( root ) } '` ,
231246 ) . to . equal ( 1 ) ;
232247
233248 const ariaSelected = this . _obj . attr ( "aria-selected" ) ;
@@ -237,7 +252,7 @@ const hasAriaSelected: ChaiPlugin = (_chai) => {
237252 `expected root to have aria-selected #{exp}, got #{act} instead.` ,
238253 `expected root to have aria-selected = #{exp}, got #{act} instead` ,
239254 "true" ,
240- ariaSelected
255+ ariaSelected ,
241256 ) ;
242257 }
243258
@@ -259,7 +274,7 @@ const isInTheViewport: ChaiPlugin = (_chai, utils) => {
259274 // make sure it's an Element
260275 new _chai . Assertion (
261276 root . nodeType ,
262- `Expected an Element but got '${ String ( root ) } '`
277+ `Expected an Element but got '${ String ( root ) } '` ,
263278 ) . to . equal ( 1 ) ;
264279
265280 const viewportHeight = Cypress . config ( `viewportHeight` ) ;
@@ -269,7 +284,7 @@ const isInTheViewport: ChaiPlugin = (_chai, utils) => {
269284 ! ( rect . bottom < 0 || rect . top - viewportHeight >= 0 ) ,
270285 `expected \n${ elementToString ( root ) } to be in the viewport.` ,
271286 `expected \n${ elementToString ( root ) } to not be in the viewport` ,
272- null
287+ null ,
273288 ) ;
274289 }
275290
@@ -293,7 +308,7 @@ const isViewportSize: ChaiPlugin = (_chai, utils) => {
293308 // make sure it's an Element
294309 new _chai . Assertion (
295310 root . nodeType ,
296- `Expected an Element but got '${ String ( root ) } '`
311+ `Expected an Element but got '${ String ( root ) } '` ,
297312 ) . to . equal ( 1 ) ;
298313
299314 const viewportHeight = Cypress . config ( `viewportHeight` ) ;
@@ -303,12 +318,12 @@ const isViewportSize: ChaiPlugin = (_chai, utils) => {
303318 this . assert (
304319 rect . height === viewportHeight && rect . width === viewportWidth ,
305320 `expected \n${ elementToString (
306- root
321+ root ,
307322 ) } to be sized to fill viewport (${ viewportWidth } x ${ viewportHeight } ), actual height ${
308323 rect . height
309324 } , width: ${ rect . width } .`,
310325 `expected \n${ elementToString ( root ) } to not be sized to fill viewport.` ,
311- null
326+ null ,
312327 ) ;
313328 }
314329
@@ -331,7 +346,7 @@ const isActiveDescendant: ChaiPlugin = (_chai) => {
331346 // make sure it's an Element
332347 new _chai . Assertion (
333348 root . nodeType ,
334- `Expected an Element but got '${ String ( root ) } '`
349+ `Expected an Element but got '${ String ( root ) } '` ,
335350 ) . to . equal ( 1 ) ;
336351
337352 const id = root . id ;
@@ -340,7 +355,7 @@ const isActiveDescendant: ChaiPlugin = (_chai) => {
340355 $focused . attr ( "aria-activedescendant" ) === id ,
341356 "expected #{this} to be #{exp}" ,
342357 "expected #{this} not to be #{exp}" ,
343- "active descendant"
358+ "active descendant" ,
344359 ) ;
345360 } ) ;
346361 }
@@ -351,4 +366,39 @@ const isActiveDescendant: ChaiPlugin = (_chai) => {
351366// registers our assertion function "isFocused" with Chai
352367chai . use ( isActiveDescendant ) ;
353368
369+ /**
370+ * Checks if the element text includes a text selection range
371+ *
372+ * @example
373+ * cy.findByTestId('test-input).should('have.selectionRange',0,3)
374+ */
375+ const hasSelectionRange : ChaiPlugin = ( _chai , utils ) => {
376+ function assertHasSelectedRange (
377+ this : AssertionStatic ,
378+ rangeFrom : number ,
379+ rangeTo : number ,
380+ ) {
381+ const root = this . _obj . get ( 0 ) ;
382+ // make sure it's an Element
383+ new _chai . Assertion (
384+ root . nodeType ,
385+ `Expected an Element but got '${ String ( root ) } '` ,
386+ ) . to . equal ( 1 ) ;
387+
388+ const { selectionStart, selectionEnd } = root ;
389+
390+ this . assert (
391+ rangeFrom === selectionStart && rangeTo === selectionEnd ,
392+ `expected root to have selectionRange from:${ rangeFrom } to: ${ rangeTo } , got from: ${ selectionStart } to: ${ selectionEnd } instead.` ,
393+ `expected root not to have selectionRange from:${ rangeFrom } to: ${ rangeTo } ` ,
394+ "selectedRange" ,
395+ ) ;
396+ }
397+
398+ _chai . Assertion . addMethod ( "selectionRange" , assertHasSelectedRange ) ;
399+ } ;
400+
401+ // registers our assertion function "hasSelectedRange" with Chai
402+ chai . use ( hasSelectionRange ) ;
403+
354404export { } ;
0 commit comments