-
Notifications
You must be signed in to change notification settings - Fork 273
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement toHaveAccessibilityValue matcher (#1496)
* feat: add toHaveAccessibilityValue matcher * refactor: clenup * refactor: clean up tests * refactor: self review --------- Co-authored-by: Maciej Jastrzebski <[email protected]>
- Loading branch information
1 parent
798100b
commit 28c8729
Showing
7 changed files
with
232 additions
and
21 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
166 changes: 166 additions & 0 deletions
166
src/matchers/__tests__/to-have-accessibility-value.test.tsx
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,166 @@ | ||
import * as React from 'react'; | ||
import { View } from 'react-native'; | ||
import { render, screen } from '../..'; | ||
import '../extend-expect'; | ||
|
||
describe('toHaveAccessibilityValue', () => { | ||
it('supports "accessibilityValue.min"', () => { | ||
render(<View accessibilityValue={{ min: 0 }} />); | ||
expect(screen.root).toHaveAccessibilityValue({ min: 0 }); | ||
expect(screen.root).not.toHaveAccessibilityValue({ min: 1 }); | ||
}); | ||
|
||
it('supports "accessibilityValue.max"', () => { | ||
render(<View accessibilityValue={{ max: 100 }} />); | ||
expect(screen.root).toHaveAccessibilityValue({ max: 100 }); | ||
expect(screen.root).not.toHaveAccessibilityValue({ max: 99 }); | ||
}); | ||
|
||
it('supports "accessibilityValue.now"', () => { | ||
render(<View accessibilityValue={{ now: 33 }} />); | ||
expect(screen.root).toHaveAccessibilityValue({ now: 33 }); | ||
expect(screen.root).not.toHaveAccessibilityValue({ now: 34 }); | ||
}); | ||
|
||
it('supports "accessibilityValue.text"', () => { | ||
render(<View testID="view" accessibilityValue={{ text: 'Hello' }} />); | ||
expect(screen.root).toHaveAccessibilityValue({ text: 'Hello' }); | ||
expect(screen.root).toHaveAccessibilityValue({ text: /He/ }); | ||
expect(screen.root).not.toHaveAccessibilityValue({ text: 'Hi' }); | ||
expect(screen.root).not.toHaveAccessibilityValue({ text: /Hi/ }); | ||
}); | ||
|
||
it('supports "aria-valuemin"', () => { | ||
render(<View testID="view" aria-valuemin={0} />); | ||
expect(screen.root).toHaveAccessibilityValue({ min: 0 }); | ||
expect(screen.root).not.toHaveAccessibilityValue({ min: 1 }); | ||
}); | ||
|
||
it('supports "aria-valuemax"', () => { | ||
render(<View testID="view" aria-valuemax={100} />); | ||
expect(screen.root).toHaveAccessibilityValue({ max: 100 }); | ||
expect(screen.root).not.toHaveAccessibilityValue({ max: 99 }); | ||
}); | ||
|
||
it('supports "aria-valuenow"', () => { | ||
render(<View testID="view" aria-valuenow={33} />); | ||
expect(screen.root).toHaveAccessibilityValue({ now: 33 }); | ||
expect(screen.root).not.toHaveAccessibilityValue({ now: 34 }); | ||
}); | ||
|
||
it('supports "aria-valuetext"', () => { | ||
render(<View testID="view" aria-valuetext="Hello" />); | ||
expect(screen.root).toHaveAccessibilityValue({ text: 'Hello' }); | ||
expect(screen.root).toHaveAccessibilityValue({ text: /He/ }); | ||
expect(screen.root).not.toHaveAccessibilityValue({ text: 'Hi' }); | ||
expect(screen.root).not.toHaveAccessibilityValue({ text: /Hi/ }); | ||
}); | ||
|
||
it('supports multi-argument matching', () => { | ||
render( | ||
<View accessibilityValue={{ min: 1, max: 10, now: 5, text: '5/10' }} /> | ||
); | ||
|
||
expect(screen.root).toHaveAccessibilityValue({ now: 5 }); | ||
expect(screen.root).toHaveAccessibilityValue({ now: 5, min: 1 }); | ||
expect(screen.root).toHaveAccessibilityValue({ now: 5, max: 10 }); | ||
expect(screen.root).toHaveAccessibilityValue({ now: 5, min: 1, max: 10 }); | ||
expect(screen.root).toHaveAccessibilityValue({ text: '5/10' }); | ||
expect(screen.root).toHaveAccessibilityValue({ now: 5, text: '5/10' }); | ||
expect(screen.root).toHaveAccessibilityValue({ | ||
now: 5, | ||
min: 1, | ||
max: 10, | ||
text: '5/10', | ||
}); | ||
|
||
expect(screen.root).not.toHaveAccessibilityValue({ now: 6 }); | ||
expect(screen.root).not.toHaveAccessibilityValue({ now: 5, min: 0 }); | ||
expect(screen.root).not.toHaveAccessibilityValue({ now: 5, max: 9 }); | ||
expect(screen.root).not.toHaveAccessibilityValue({ | ||
now: 5, | ||
min: 1, | ||
max: 10, | ||
text: '5 of 10', | ||
}); | ||
}); | ||
|
||
it('gives precedence to ARIA values', () => { | ||
render( | ||
<View | ||
testID="view" | ||
aria-valuemin={0} | ||
aria-valuemax={100} | ||
aria-valuenow={33} | ||
aria-valuetext="Hello" | ||
accessibilityValue={{ min: 10, max: 90, now: 30, text: 'Hi' }} | ||
/> | ||
); | ||
|
||
expect(screen.root).toHaveAccessibilityValue({ min: 0 }); | ||
expect(screen.root).toHaveAccessibilityValue({ max: 100 }); | ||
expect(screen.root).toHaveAccessibilityValue({ now: 33 }); | ||
expect(screen.root).toHaveAccessibilityValue({ text: 'Hello' }); | ||
|
||
expect(screen.root).not.toHaveAccessibilityValue({ min: 10 }); | ||
expect(screen.root).not.toHaveAccessibilityValue({ max: 90 }); | ||
expect(screen.root).not.toHaveAccessibilityValue({ now: 30 }); | ||
expect(screen.root).not.toHaveAccessibilityValue({ text: 'Hi' }); | ||
}); | ||
|
||
it('shows errors in expected format', () => { | ||
render( | ||
<View | ||
testID="view" | ||
aria-valuemin={0} | ||
aria-valuemax={100} | ||
aria-valuenow={33} | ||
aria-valuetext="Hello" | ||
/> | ||
); | ||
|
||
expect(() => expect(screen.root).toHaveAccessibilityValue({ min: 10 })) | ||
.toThrowErrorMatchingInlineSnapshot(` | ||
"expect(element).toHaveAccessibilityValue({"min": 10}) | ||
Expected the element to have accessibility value: | ||
{"min": 10} | ||
Received element with accessibility value: | ||
{"max": 100, "min": 0, "now": 33, "text": "Hello"}" | ||
`); | ||
|
||
expect(() => expect(screen.root).not.toHaveAccessibilityValue({ min: 0 })) | ||
.toThrowErrorMatchingInlineSnapshot(` | ||
"expect(element).not.toHaveAccessibilityValue({"min": 0}) | ||
Expected the element not to have accessibility value: | ||
{"min": 0} | ||
Received element with accessibility value: | ||
{"max": 100, "min": 0, "now": 33, "text": "Hello"}" | ||
`); | ||
}); | ||
|
||
it('shows errors in expected format with partial value', () => { | ||
render(<View testID="view" aria-valuenow={33} aria-valuetext="Hello" />); | ||
|
||
expect(() => expect(screen.root).toHaveAccessibilityValue({ min: 30 })) | ||
.toThrowErrorMatchingInlineSnapshot(` | ||
"expect(element).toHaveAccessibilityValue({"min": 30}) | ||
Expected the element to have accessibility value: | ||
{"min": 30} | ||
Received element with accessibility value: | ||
{"now": 33, "text": "Hello"}" | ||
`); | ||
|
||
expect(() => expect(screen.root).not.toHaveAccessibilityValue({ now: 33 })) | ||
.toThrowErrorMatchingInlineSnapshot(` | ||
"expect(element).not.toHaveAccessibilityValue({"now": 33}) | ||
Expected the element not to have accessibility value: | ||
{"now": 33} | ||
Received element with accessibility value: | ||
{"now": 33, "text": "Hello"}" | ||
`); | ||
}); | ||
}); |
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,39 @@ | ||
import type { ReactTestInstance } from 'react-test-renderer'; | ||
import { matcherHint, stringify } from 'jest-matcher-utils'; | ||
import { getAccessibilityValue } from '../helpers/accessiblity'; | ||
import { | ||
AccessibilityValueMatcher, | ||
matchAccessibilityValue, | ||
} from '../helpers/matchers/accessibilityValue'; | ||
import { removeUndefinedKeys } from '../helpers/object'; | ||
import { checkHostElement, formatMessage } from './utils'; | ||
|
||
export function toHaveAccessibilityValue( | ||
this: jest.MatcherContext, | ||
element: ReactTestInstance, | ||
expectedValue: AccessibilityValueMatcher | ||
) { | ||
checkHostElement(element, toHaveAccessibilityValue, this); | ||
|
||
const receivedValue = getAccessibilityValue(element); | ||
|
||
return { | ||
pass: matchAccessibilityValue(element, expectedValue), | ||
message: () => { | ||
const matcher = matcherHint( | ||
`${this.isNot ? '.not' : ''}.toHaveAccessibilityValue`, | ||
'element', | ||
stringify(expectedValue) | ||
); | ||
return formatMessage( | ||
matcher, | ||
`Expected the element ${ | ||
this.isNot ? 'not to' : 'to' | ||
} have accessibility value`, | ||
stringify(expectedValue), | ||
'Received element with accessibility value', | ||
stringify(removeUndefinedKeys(receivedValue)) | ||
); | ||
}, | ||
}; | ||
} |