@@ -81,6 +81,7 @@ clear to read and to maintain.
8181 - [ ` toBePartiallyChecked ` ] ( #tobepartiallychecked )
8282 - [ ` toHaveRole ` ] ( #tohaverole )
8383 - [ ` toHaveErrorMessage ` ] ( #tohaveerrormessage )
84+ - [ ` toHaveSelection ` ] ( #tohaveselection )
8485 - [ ` toBePressed ` ] ( #tobepressed )
8586 - [ ` toBePartiallyPressed ` ] ( #tobepartiallypressed )
8687 - [ ` toAppearBefore ` ] ( #toappearbefore )
@@ -89,7 +90,6 @@ clear to read and to maintain.
8990 - [ ` toBeEmpty ` ] ( #tobeempty )
9091 - [ ` toBeInTheDOM ` ] ( #tobeinthedom )
9192 - [ ` toHaveDescription ` ] ( #tohavedescription )
92- - [ ` toHaveSelection ` ] ( #tohaveselection )
9393- [ Inspiration] ( #inspiration )
9494- [ Other Solutions] ( #other-solutions )
9595- [ Guiding Principles] ( #guiding-principles )
@@ -1312,6 +1312,73 @@ expect(timeInput).not.toHaveErrorMessage('Pikachu!')
13121312
13131313<hr />
13141314
1315+ ### ` toHaveSelection `
1316+
1317+ This allows to assert that an element has a
1318+ [ text selection] ( https://developer.mozilla.org/en-US/docs/Web/API/Selection ) .
1319+
1320+ This is useful to check if text or part of the text is selected within an
1321+ element. The element can be either an input of type text, a textarea, or any
1322+ other element that contains text, such as a paragraph, span, div etc.
1323+
1324+ NOTE: the expected selection is a string, it does not allow to check for
1325+ selection range indices.
1326+
1327+ ``` typescript
1328+ toHaveSelection (expectedSelection ?: string )
1329+ ```
1330+
1331+ #### Examples
1332+
1333+ ``` html
1334+ <div >
1335+ <input type =" text" value =" text selected text" data-testid =" text" />
1336+ <textarea data-testid =" textarea" >text selected text</textarea >
1337+ <p data-testid =" prev" >prev</p >
1338+ <p data-testid =" parent" >
1339+ text <span data-testid =" child" >selected</span > text
1340+ </p >
1341+ <p data-testid =" next" >next</p >
1342+ </div >
1343+ ```
1344+
1345+ ``` javascript
1346+ getByTestId (' text' ).setSelectionRange (5 , 13 )
1347+ expect (getByTestId (' text' )).toHaveSelection (' selected' )
1348+
1349+ getByTestId (' textarea' ).setSelectionRange (0 , 5 )
1350+ expect (getByTestId (' textarea' )).toHaveSelection (' text ' )
1351+
1352+ const selection = document .getSelection ()
1353+ const range = document .createRange ()
1354+ selection .removeAllRanges ()
1355+ selection .empty ()
1356+ selection .addRange (range)
1357+
1358+ // selection of child applies to the parent as well
1359+ range .selectNodeContents (getByTestId (' child' ))
1360+ expect (getByTestId (' child' )).toHaveSelection (' selected' )
1361+ expect (getByTestId (' parent' )).toHaveSelection (' selected' )
1362+
1363+ // selection that applies from prev all, parent text before child, and part child.
1364+ range .setStart (getByTestId (' prev' ), 0 )
1365+ range .setEnd (getByTestId (' child' ).childNodes [0 ], 3 )
1366+ expect (queryByTestId (' prev' )).toHaveSelection (' prev' )
1367+ expect (queryByTestId (' child' )).toHaveSelection (' sel' )
1368+ expect (queryByTestId (' parent' )).toHaveSelection (' text sel' )
1369+ expect (queryByTestId (' next' )).not .toHaveSelection ()
1370+
1371+ // selection that applies from part child, parent text after child and part next.
1372+ range .setStart (getByTestId (' child' ).childNodes [0 ], 3 )
1373+ range .setEnd (getByTestId (' next' ).childNodes [0 ], 2 )
1374+ expect (queryByTestId (' child' )).toHaveSelection (' ected' )
1375+ expect (queryByTestId (' parent' )).toHaveSelection (' ected text' )
1376+ expect (queryByTestId (' prev' )).not .toHaveSelection ()
1377+ expect (queryByTestId (' next' )).toHaveSelection (' ne' )
1378+ ```
1379+
1380+ <hr />
1381+
13151382### ` toBePressed `
13161383
13171384This allows to check whether given element is
@@ -1570,71 +1637,6 @@ expect(deleteButton).not.toHaveDescription()
15701637expect (deleteButton).toHaveDescription (' ' ) // Missing or empty description always becomes a blank string
15711638```
15721639
1573- <hr />
1574-
1575- ### ` toHaveSelection `
1576-
1577- This allows to assert that an element has a
1578- [ text selection] ( https://developer.mozilla.org/en-US/docs/Web/API/Selection ) .
1579-
1580- This is useful to check if text or part of the text is selected within an
1581- element. The element can be either an input of type text, a textarea, or any
1582- other element that contains text, such as a paragraph, span, div etc.
1583-
1584- NOTE: the expected selection is a string, it does not allow to check for
1585- selection range indeces.
1586-
1587- ``` typescript
1588- toHaveSelection (expectedSelection ?: string )
1589- ```
1590-
1591- ``` html
1592- <div >
1593- <input type =" text" value =" text selected text" data-testid =" text" />
1594- <textarea data-testid =" textarea" >text selected text</textarea >
1595- <p data-testid =" prev" >prev</p >
1596- <p data-testid =" parent" >
1597- text <span data-testid =" child" >selected</span > text
1598- </p >
1599- <p data-testid =" next" >next</p >
1600- </div >
1601- ```
1602-
1603- ``` javascript
1604- getByTestId (' text' ).setSelectionRange (5 , 13 )
1605- expect (getByTestId (' text' )).toHaveSelection (' selected' )
1606-
1607- getByTestId (' textarea' ).setSelectionRange (0 , 5 )
1608- expect (' textarea' ).toHaveSelection (' text ' )
1609-
1610- const selection = document .getSelection ()
1611- const range = document .createRange ()
1612- selection .removeAllRanges ()
1613- selection .empty ()
1614- selection .addRange (range)
1615-
1616- // selection of child applies to the parent as well
1617- range .selectNodeContents (getByTestId (' child' ))
1618- expect (getByTestId (' child' )).toHaveSelection (' selected' )
1619- expect (getByTestId (' parent' )).toHaveSelection (' selected' )
1620-
1621- // selection that applies from prev all, parent text before child, and part child.
1622- range .setStart (getByTestId (' prev' ), 0 )
1623- range .setEnd (getByTestId (' child' ).childNodes [0 ], 3 )
1624- expect (queryByTestId (' prev' )).toHaveSelection (' prev' )
1625- expect (queryByTestId (' child' )).toHaveSelection (' sel' )
1626- expect (queryByTestId (' parent' )).toHaveSelection (' text sel' )
1627- expect (queryByTestId (' next' )).not .toHaveSelection ()
1628-
1629- // selection that applies from part child, parent text after child and part next.
1630- range .setStart (getByTestId (' child' ).childNodes [0 ], 3 )
1631- range .setEnd (getByTestId (' next' ).childNodes [0 ], 2 )
1632- expect (queryByTestId (' child' )).toHaveSelection (' ected' )
1633- expect (queryByTestId (' parent' )).toHaveSelection (' ected text' )
1634- expect (queryByTestId (' prev' )).not .toHaveSelection ()
1635- expect (queryByTestId (' next' )).toHaveSelection (' ne' )
1636- ```
1637-
16381640## Inspiration
16391641
16401642This whole library was extracted out of Kent C. Dodds' [ DOM Testing
0 commit comments