-
Notifications
You must be signed in to change notification settings - Fork 272
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: toBeExpanded matcher * feat: toBeCollapsed matcher * chore: shorten syntax for isElementCollapsed check * refactor: clean up --------- Co-authored-by: Maciej Jastrzebski <[email protected]>
- Loading branch information
1 parent
d898a9d
commit a9f32a6
Showing
8 changed files
with
271 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import * as React from 'react'; | ||
import { View } from 'react-native'; | ||
import { render, screen } from '../..'; | ||
import '../extend-expect'; | ||
|
||
test('toBeCollapsed() basic case', () => { | ||
render( | ||
<> | ||
<View testID="expanded" accessibilityState={{ expanded: true }} /> | ||
<View testID="expanded-aria" aria-expanded /> | ||
<View testID="not-expanded" accessibilityState={{ expanded: false }} /> | ||
<View testID="not-expanded-aria" aria-expanded={false} /> | ||
<View testID="default" /> | ||
</> | ||
); | ||
|
||
expect(screen.getByTestId('expanded')).not.toBeCollapsed(); | ||
expect(screen.getByTestId('expanded-aria')).not.toBeCollapsed(); | ||
expect(screen.getByTestId('not-expanded')).toBeCollapsed(); | ||
expect(screen.getByTestId('not-expanded-aria')).toBeCollapsed(); | ||
expect(screen.getByTestId('default')).not.toBeCollapsed(); | ||
}); | ||
|
||
test('toBeCollapsed() error messages', () => { | ||
render( | ||
<> | ||
<View testID="expanded" accessibilityState={{ expanded: true }} /> | ||
<View testID="expanded-aria" aria-expanded /> | ||
<View testID="not-expanded" accessibilityState={{ expanded: false }} /> | ||
<View testID="not-expanded-aria" aria-expanded={false} /> | ||
<View testID="default" /> | ||
</> | ||
); | ||
|
||
expect(() => expect(screen.getByTestId('expanded')).toBeCollapsed()) | ||
.toThrowErrorMatchingInlineSnapshot(` | ||
"expect(element).toBeCollapsed() | ||
Received element is not collapsed: | ||
<View | ||
accessibilityState={ | ||
{ | ||
"expanded": true, | ||
} | ||
} | ||
testID="expanded" | ||
/>" | ||
`); | ||
|
||
expect(() => expect(screen.getByTestId('expanded-aria')).toBeCollapsed()) | ||
.toThrowErrorMatchingInlineSnapshot(` | ||
"expect(element).toBeCollapsed() | ||
Received element is not collapsed: | ||
<View | ||
aria-expanded={true} | ||
testID="expanded-aria" | ||
/>" | ||
`); | ||
|
||
expect(() => expect(screen.getByTestId('not-expanded')).not.toBeCollapsed()) | ||
.toThrowErrorMatchingInlineSnapshot(` | ||
"expect(element).not.toBeCollapsed() | ||
Received element is collapsed: | ||
<View | ||
accessibilityState={ | ||
{ | ||
"expanded": false, | ||
} | ||
} | ||
testID="not-expanded" | ||
/>" | ||
`); | ||
|
||
expect(() => | ||
expect(screen.getByTestId('not-expanded-aria')).not.toBeCollapsed() | ||
).toThrowErrorMatchingInlineSnapshot(` | ||
"expect(element).not.toBeCollapsed() | ||
Received element is collapsed: | ||
<View | ||
aria-expanded={false} | ||
testID="not-expanded-aria" | ||
/>" | ||
`); | ||
|
||
expect(() => expect(screen.getByTestId('default')).toBeCollapsed()) | ||
.toThrowErrorMatchingInlineSnapshot(` | ||
"expect(element).toBeCollapsed() | ||
Received element is not collapsed: | ||
<View | ||
testID="default" | ||
/>" | ||
`); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import * as React from 'react'; | ||
import { View } from 'react-native'; | ||
import { render, screen } from '../..'; | ||
import '../extend-expect'; | ||
|
||
test('toBeExpanded() basic case', () => { | ||
render( | ||
<> | ||
<View testID="expanded" accessibilityState={{ expanded: true }} /> | ||
<View testID="expanded-aria" aria-expanded /> | ||
<View testID="not-expanded" accessibilityState={{ expanded: false }} /> | ||
<View testID="not-expanded-aria" aria-expanded={false} /> | ||
<View testID="default" /> | ||
</> | ||
); | ||
|
||
expect(screen.getByTestId('expanded')).toBeExpanded(); | ||
expect(screen.getByTestId('expanded-aria')).toBeExpanded(); | ||
expect(screen.getByTestId('not-expanded')).not.toBeExpanded(); | ||
expect(screen.getByTestId('not-expanded-aria')).not.toBeExpanded(); | ||
expect(screen.getByTestId('default')).not.toBeExpanded(); | ||
}); | ||
|
||
test('toBeExpanded() error messages', () => { | ||
render( | ||
<> | ||
<View testID="expanded" accessibilityState={{ expanded: true }} /> | ||
<View testID="expanded-aria" aria-expanded /> | ||
<View testID="not-expanded" accessibilityState={{ expanded: false }} /> | ||
<View testID="not-expanded-aria" aria-expanded={false} /> | ||
<View testID="default" /> | ||
</> | ||
); | ||
|
||
expect(() => expect(screen.getByTestId('expanded')).not.toBeExpanded()) | ||
.toThrowErrorMatchingInlineSnapshot(` | ||
"expect(element).not.toBeExpanded() | ||
Received element is expanded: | ||
<View | ||
accessibilityState={ | ||
{ | ||
"expanded": true, | ||
} | ||
} | ||
testID="expanded" | ||
/>" | ||
`); | ||
|
||
expect(() => expect(screen.getByTestId('expanded-aria')).not.toBeExpanded()) | ||
.toThrowErrorMatchingInlineSnapshot(` | ||
"expect(element).not.toBeExpanded() | ||
Received element is expanded: | ||
<View | ||
aria-expanded={true} | ||
testID="expanded-aria" | ||
/>" | ||
`); | ||
|
||
expect(() => expect(screen.getByTestId('not-expanded')).toBeExpanded()) | ||
.toThrowErrorMatchingInlineSnapshot(` | ||
"expect(element).toBeExpanded() | ||
Received element is not expanded: | ||
<View | ||
accessibilityState={ | ||
{ | ||
"expanded": false, | ||
} | ||
} | ||
testID="not-expanded" | ||
/>" | ||
`); | ||
|
||
expect(() => expect(screen.getByTestId('not-expanded-aria')).toBeExpanded()) | ||
.toThrowErrorMatchingInlineSnapshot(` | ||
"expect(element).toBeExpanded() | ||
Received element is not expanded: | ||
<View | ||
aria-expanded={false} | ||
testID="not-expanded-aria" | ||
/>" | ||
`); | ||
|
||
expect(() => expect(screen.getByTestId('default')).toBeExpanded()) | ||
.toThrowErrorMatchingInlineSnapshot(` | ||
"expect(element).toBeExpanded() | ||
Received element is not expanded: | ||
<View | ||
testID="default" | ||
/>" | ||
`); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { ReactTestInstance } from 'react-test-renderer'; | ||
import { matcherHint } from 'jest-matcher-utils'; | ||
import { isElementCollapsed } from '../helpers/accessiblity'; | ||
import { checkHostElement, formatElement } from './utils'; | ||
|
||
export function toBeCollapsed( | ||
this: jest.MatcherContext, | ||
element: ReactTestInstance | ||
) { | ||
checkHostElement(element, toBeCollapsed, this); | ||
|
||
return { | ||
pass: isElementCollapsed(element), | ||
message: () => { | ||
const matcher = matcherHint( | ||
`${this.isNot ? '.not' : ''}.toBeCollapsed`, | ||
'element', | ||
'' | ||
); | ||
return [ | ||
matcher, | ||
'', | ||
`Received element is ${this.isNot ? '' : 'not '}collapsed:`, | ||
formatElement(element), | ||
].join('\n'); | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { ReactTestInstance } from 'react-test-renderer'; | ||
import { matcherHint } from 'jest-matcher-utils'; | ||
import { isElementExpanded } from '../helpers/accessiblity'; | ||
import { checkHostElement, formatElement } from './utils'; | ||
|
||
export function toBeExpanded( | ||
this: jest.MatcherContext, | ||
element: ReactTestInstance | ||
) { | ||
checkHostElement(element, toBeExpanded, this); | ||
|
||
return { | ||
pass: isElementExpanded(element), | ||
message: () => { | ||
const matcher = matcherHint( | ||
`${this.isNot ? '.not' : ''}.toBeExpanded`, | ||
'element', | ||
'' | ||
); | ||
return [ | ||
matcher, | ||
'', | ||
`Received element is ${this.isNot ? '' : 'not '}expanded:`, | ||
formatElement(element), | ||
].join('\n'); | ||
}, | ||
}; | ||
} |