|
| 1 | +import * as React from 'react'; |
| 2 | +import { View } from 'react-native'; |
| 3 | +import { render, screen } from '../..'; |
| 4 | +import '../extend-expect'; |
| 5 | + |
| 6 | +describe('toHaveAccessibilityValue', () => { |
| 7 | + it('supports "accessibilityValue.min"', () => { |
| 8 | + render(<View accessibilityValue={{ min: 0 }} />); |
| 9 | + expect(screen.root).toHaveAccessibilityValue({ min: 0 }); |
| 10 | + expect(screen.root).not.toHaveAccessibilityValue({ min: 1 }); |
| 11 | + }); |
| 12 | + |
| 13 | + it('supports "accessibilityValue.max"', () => { |
| 14 | + render(<View accessibilityValue={{ max: 100 }} />); |
| 15 | + expect(screen.root).toHaveAccessibilityValue({ max: 100 }); |
| 16 | + expect(screen.root).not.toHaveAccessibilityValue({ max: 99 }); |
| 17 | + }); |
| 18 | + |
| 19 | + it('supports "accessibilityValue.now"', () => { |
| 20 | + render(<View accessibilityValue={{ now: 33 }} />); |
| 21 | + expect(screen.root).toHaveAccessibilityValue({ now: 33 }); |
| 22 | + expect(screen.root).not.toHaveAccessibilityValue({ now: 34 }); |
| 23 | + }); |
| 24 | + |
| 25 | + it('supports "accessibilityValue.text"', () => { |
| 26 | + render(<View testID="view" accessibilityValue={{ text: 'Hello' }} />); |
| 27 | + expect(screen.root).toHaveAccessibilityValue({ text: 'Hello' }); |
| 28 | + expect(screen.root).toHaveAccessibilityValue({ text: /He/ }); |
| 29 | + expect(screen.root).not.toHaveAccessibilityValue({ text: 'Hi' }); |
| 30 | + expect(screen.root).not.toHaveAccessibilityValue({ text: /Hi/ }); |
| 31 | + }); |
| 32 | + |
| 33 | + it('supports "aria-valuemin"', () => { |
| 34 | + render(<View testID="view" aria-valuemin={0} />); |
| 35 | + expect(screen.root).toHaveAccessibilityValue({ min: 0 }); |
| 36 | + expect(screen.root).not.toHaveAccessibilityValue({ min: 1 }); |
| 37 | + }); |
| 38 | + |
| 39 | + it('supports "aria-valuemax"', () => { |
| 40 | + render(<View testID="view" aria-valuemax={100} />); |
| 41 | + expect(screen.root).toHaveAccessibilityValue({ max: 100 }); |
| 42 | + expect(screen.root).not.toHaveAccessibilityValue({ max: 99 }); |
| 43 | + }); |
| 44 | + |
| 45 | + it('supports "aria-valuenow"', () => { |
| 46 | + render(<View testID="view" aria-valuenow={33} />); |
| 47 | + expect(screen.root).toHaveAccessibilityValue({ now: 33 }); |
| 48 | + expect(screen.root).not.toHaveAccessibilityValue({ now: 34 }); |
| 49 | + }); |
| 50 | + |
| 51 | + it('supports "aria-valuetext"', () => { |
| 52 | + render(<View testID="view" aria-valuetext="Hello" />); |
| 53 | + expect(screen.root).toHaveAccessibilityValue({ text: 'Hello' }); |
| 54 | + expect(screen.root).toHaveAccessibilityValue({ text: /He/ }); |
| 55 | + expect(screen.root).not.toHaveAccessibilityValue({ text: 'Hi' }); |
| 56 | + expect(screen.root).not.toHaveAccessibilityValue({ text: /Hi/ }); |
| 57 | + }); |
| 58 | + |
| 59 | + it('supports multi-argument matching', () => { |
| 60 | + render( |
| 61 | + <View accessibilityValue={{ min: 1, max: 10, now: 5, text: '5/10' }} /> |
| 62 | + ); |
| 63 | + |
| 64 | + expect(screen.root).toHaveAccessibilityValue({ now: 5 }); |
| 65 | + expect(screen.root).toHaveAccessibilityValue({ now: 5, min: 1 }); |
| 66 | + expect(screen.root).toHaveAccessibilityValue({ now: 5, max: 10 }); |
| 67 | + expect(screen.root).toHaveAccessibilityValue({ now: 5, min: 1, max: 10 }); |
| 68 | + expect(screen.root).toHaveAccessibilityValue({ text: '5/10' }); |
| 69 | + expect(screen.root).toHaveAccessibilityValue({ now: 5, text: '5/10' }); |
| 70 | + expect(screen.root).toHaveAccessibilityValue({ |
| 71 | + now: 5, |
| 72 | + min: 1, |
| 73 | + max: 10, |
| 74 | + text: '5/10', |
| 75 | + }); |
| 76 | + |
| 77 | + expect(screen.root).not.toHaveAccessibilityValue({ now: 6 }); |
| 78 | + expect(screen.root).not.toHaveAccessibilityValue({ now: 5, min: 0 }); |
| 79 | + expect(screen.root).not.toHaveAccessibilityValue({ now: 5, max: 9 }); |
| 80 | + expect(screen.root).not.toHaveAccessibilityValue({ |
| 81 | + now: 5, |
| 82 | + min: 1, |
| 83 | + max: 10, |
| 84 | + text: '5 of 10', |
| 85 | + }); |
| 86 | + }); |
| 87 | + |
| 88 | + it('gives precedence to ARIA values', () => { |
| 89 | + render( |
| 90 | + <View |
| 91 | + testID="view" |
| 92 | + aria-valuemin={0} |
| 93 | + aria-valuemax={100} |
| 94 | + aria-valuenow={33} |
| 95 | + aria-valuetext="Hello" |
| 96 | + accessibilityValue={{ min: 10, max: 90, now: 30, text: 'Hi' }} |
| 97 | + /> |
| 98 | + ); |
| 99 | + |
| 100 | + expect(screen.root).toHaveAccessibilityValue({ min: 0 }); |
| 101 | + expect(screen.root).toHaveAccessibilityValue({ max: 100 }); |
| 102 | + expect(screen.root).toHaveAccessibilityValue({ now: 33 }); |
| 103 | + expect(screen.root).toHaveAccessibilityValue({ text: 'Hello' }); |
| 104 | + |
| 105 | + expect(screen.root).not.toHaveAccessibilityValue({ min: 10 }); |
| 106 | + expect(screen.root).not.toHaveAccessibilityValue({ max: 90 }); |
| 107 | + expect(screen.root).not.toHaveAccessibilityValue({ now: 30 }); |
| 108 | + expect(screen.root).not.toHaveAccessibilityValue({ text: 'Hi' }); |
| 109 | + }); |
| 110 | + |
| 111 | + it('shows errors in expected format', () => { |
| 112 | + render( |
| 113 | + <View |
| 114 | + testID="view" |
| 115 | + aria-valuemin={0} |
| 116 | + aria-valuemax={100} |
| 117 | + aria-valuenow={33} |
| 118 | + aria-valuetext="Hello" |
| 119 | + /> |
| 120 | + ); |
| 121 | + |
| 122 | + expect(() => expect(screen.root).toHaveAccessibilityValue({ min: 10 })) |
| 123 | + .toThrowErrorMatchingInlineSnapshot(` |
| 124 | + "expect(element).toHaveAccessibilityValue({"min": 10}) |
| 125 | +
|
| 126 | + Expected the element to have accessibility value: |
| 127 | + {"min": 10} |
| 128 | + Received element with accessibility value: |
| 129 | + {"max": 100, "min": 0, "now": 33, "text": "Hello"}" |
| 130 | + `); |
| 131 | + |
| 132 | + expect(() => expect(screen.root).not.toHaveAccessibilityValue({ min: 0 })) |
| 133 | + .toThrowErrorMatchingInlineSnapshot(` |
| 134 | + "expect(element).not.toHaveAccessibilityValue({"min": 0}) |
| 135 | +
|
| 136 | + Expected the element not to have accessibility value: |
| 137 | + {"min": 0} |
| 138 | + Received element with accessibility value: |
| 139 | + {"max": 100, "min": 0, "now": 33, "text": "Hello"}" |
| 140 | + `); |
| 141 | + }); |
| 142 | + |
| 143 | + it('shows errors in expected format with partial value', () => { |
| 144 | + render(<View testID="view" aria-valuenow={33} aria-valuetext="Hello" />); |
| 145 | + |
| 146 | + expect(() => expect(screen.root).toHaveAccessibilityValue({ min: 30 })) |
| 147 | + .toThrowErrorMatchingInlineSnapshot(` |
| 148 | + "expect(element).toHaveAccessibilityValue({"min": 30}) |
| 149 | +
|
| 150 | + Expected the element to have accessibility value: |
| 151 | + {"min": 30} |
| 152 | + Received element with accessibility value: |
| 153 | + {"now": 33, "text": "Hello"}" |
| 154 | + `); |
| 155 | + |
| 156 | + expect(() => expect(screen.root).not.toHaveAccessibilityValue({ now: 33 })) |
| 157 | + .toThrowErrorMatchingInlineSnapshot(` |
| 158 | + "expect(element).not.toHaveAccessibilityValue({"now": 33}) |
| 159 | +
|
| 160 | + Expected the element not to have accessibility value: |
| 161 | + {"now": 33} |
| 162 | + Received element with accessibility value: |
| 163 | + {"now": 33, "text": "Hello"}" |
| 164 | + `); |
| 165 | + }); |
| 166 | +}); |
0 commit comments