|
| 1 | +import {useAccessibilityInfo} from './useAccessibilityInfo' |
| 2 | +import {act, renderHook} from '@testing-library/react-hooks' |
| 3 | +import {AccessibilityChangeEventName, AccessibilityInfo} from 'react-native' |
| 4 | + |
| 5 | +describe('useAccessibilityInfo', () => { |
| 6 | + const mockAddEventListener = AccessibilityInfo.addEventListener as jest.Mock |
| 7 | + |
| 8 | + const mockIsBoldTextEnabled = AccessibilityInfo.isBoldTextEnabled as jest.Mock |
| 9 | + const mockIsGrayscaleEnabled = |
| 10 | + AccessibilityInfo.isGrayscaleEnabled as jest.Mock |
| 11 | + const mockIsInvertColorsEnabled = |
| 12 | + AccessibilityInfo.isInvertColorsEnabled as jest.Mock |
| 13 | + const mockIsReduceMotionEnabled = |
| 14 | + AccessibilityInfo.isReduceMotionEnabled as jest.Mock |
| 15 | + const mockIsReduceTransparencyEnabled = |
| 16 | + AccessibilityInfo.isReduceTransparencyEnabled as jest.Mock |
| 17 | + const mockIsScreenReaderEnabled = |
| 18 | + AccessibilityInfo.isScreenReaderEnabled as jest.Mock |
| 19 | + |
| 20 | + const createEmitChangeEvent = (event: AccessibilityChangeEventName) => { |
| 21 | + let handler: (value: boolean) => void |
| 22 | + |
| 23 | + mockAddEventListener.mockImplementation((eventName, fn) => { |
| 24 | + if (eventName === event) { |
| 25 | + handler = fn |
| 26 | + } |
| 27 | + }) |
| 28 | + |
| 29 | + return (value: boolean) => handler(value) |
| 30 | + } |
| 31 | + |
| 32 | + beforeAll(() => { |
| 33 | + mockIsBoldTextEnabled.mockResolvedValue(false) |
| 34 | + mockIsGrayscaleEnabled.mockResolvedValue(false) |
| 35 | + mockIsInvertColorsEnabled.mockResolvedValue(false) |
| 36 | + mockIsReduceMotionEnabled.mockResolvedValue(false) |
| 37 | + mockIsReduceTransparencyEnabled.mockResolvedValue(false) |
| 38 | + mockIsScreenReaderEnabled.mockResolvedValue(false) |
| 39 | + }) |
| 40 | + |
| 41 | + describe('screenReaderEnabled', () => { |
| 42 | + it('should return undefined until promise will be resolved', async () => { |
| 43 | + const {result} = renderHook( |
| 44 | + () => useAccessibilityInfo().screenReaderEnabled, |
| 45 | + ) |
| 46 | + |
| 47 | + expect(result.current).toBeUndefined() |
| 48 | + }) |
| 49 | + |
| 50 | + it('should return default value', async () => { |
| 51 | + const defaultValue = true |
| 52 | + |
| 53 | + mockIsScreenReaderEnabled.mockResolvedValueOnce(defaultValue) |
| 54 | + |
| 55 | + const {result, waitForNextUpdate} = renderHook( |
| 56 | + () => useAccessibilityInfo().screenReaderEnabled, |
| 57 | + ) |
| 58 | + |
| 59 | + await waitForNextUpdate() // wait when promise will be resolved |
| 60 | + |
| 61 | + expect(result.current).toBe(defaultValue) |
| 62 | + }) |
| 63 | + |
| 64 | + it('should update value when it change', async () => { |
| 65 | + const newValue = true |
| 66 | + const emit = createEmitChangeEvent('screenReaderChanged') |
| 67 | + const {result, waitForNextUpdate} = renderHook( |
| 68 | + () => useAccessibilityInfo().screenReaderEnabled, |
| 69 | + ) |
| 70 | + await waitForNextUpdate() // wait when promise will be resolved |
| 71 | + |
| 72 | + const {current: initial} = result |
| 73 | + |
| 74 | + act(() => emit(newValue)) |
| 75 | + |
| 76 | + const {current: afterUpdate} = result |
| 77 | + |
| 78 | + expect({initial, afterUpdate}).toEqual({ |
| 79 | + initial: false, |
| 80 | + afterUpdate: newValue, |
| 81 | + }) |
| 82 | + }) |
| 83 | + }) |
| 84 | + |
| 85 | + describe('grayscaleEnabled', () => { |
| 86 | + it('should return undefined until promise will be resolved', async () => { |
| 87 | + const {result} = renderHook(() => useAccessibilityInfo().grayscaleEnabled) |
| 88 | + |
| 89 | + expect(result.current).toBeUndefined() |
| 90 | + }) |
| 91 | + |
| 92 | + it('should return default value', async () => { |
| 93 | + const defaultValue = true |
| 94 | + |
| 95 | + mockIsGrayscaleEnabled.mockResolvedValueOnce(defaultValue) |
| 96 | + |
| 97 | + const {result, waitForNextUpdate} = renderHook( |
| 98 | + () => useAccessibilityInfo().grayscaleEnabled, |
| 99 | + ) |
| 100 | + |
| 101 | + await waitForNextUpdate() // wait when promise will be resolved |
| 102 | + |
| 103 | + expect(result.current).toBe(defaultValue) |
| 104 | + }) |
| 105 | + |
| 106 | + it('should update value when it change', async () => { |
| 107 | + const newValue = true |
| 108 | + const emit = createEmitChangeEvent('grayscaleChanged') |
| 109 | + const {result, waitForNextUpdate} = renderHook( |
| 110 | + () => useAccessibilityInfo().grayscaleEnabled, |
| 111 | + ) |
| 112 | + await waitForNextUpdate() // wait when promise will be resolved |
| 113 | + |
| 114 | + const {current: initial} = result |
| 115 | + |
| 116 | + act(() => emit(newValue)) |
| 117 | + |
| 118 | + const {current: afterUpdate} = result |
| 119 | + |
| 120 | + expect({initial, afterUpdate}).toEqual({ |
| 121 | + initial: false, |
| 122 | + afterUpdate: newValue, |
| 123 | + }) |
| 124 | + }) |
| 125 | + }) |
| 126 | + |
| 127 | + describe('invertColorsEnabled', () => { |
| 128 | + it('should return undefined until promise will be resolved', async () => { |
| 129 | + const {result} = renderHook( |
| 130 | + () => useAccessibilityInfo().invertColorsEnabled, |
| 131 | + ) |
| 132 | + |
| 133 | + expect(result.current).toBeUndefined() |
| 134 | + }) |
| 135 | + |
| 136 | + it('should return default value', async () => { |
| 137 | + const defaultValue = true |
| 138 | + |
| 139 | + mockIsInvertColorsEnabled.mockResolvedValueOnce(defaultValue) |
| 140 | + |
| 141 | + const {result, waitForNextUpdate} = renderHook( |
| 142 | + () => useAccessibilityInfo().invertColorsEnabled, |
| 143 | + ) |
| 144 | + |
| 145 | + await waitForNextUpdate() // wait when promise will be resolved |
| 146 | + |
| 147 | + expect(result.current).toBe(defaultValue) |
| 148 | + }) |
| 149 | + |
| 150 | + it('should update value when it change', async () => { |
| 151 | + const newValue = true |
| 152 | + const emit = createEmitChangeEvent('invertColorsChanged') |
| 153 | + const {result, waitForNextUpdate} = renderHook( |
| 154 | + () => useAccessibilityInfo().invertColorsEnabled, |
| 155 | + ) |
| 156 | + await waitForNextUpdate() // wait when promise will be resolved |
| 157 | + |
| 158 | + const {current: initial} = result |
| 159 | + |
| 160 | + act(() => emit(newValue)) |
| 161 | + |
| 162 | + const {current: afterUpdate} = result |
| 163 | + |
| 164 | + expect({initial, afterUpdate}).toEqual({ |
| 165 | + initial: false, |
| 166 | + afterUpdate: newValue, |
| 167 | + }) |
| 168 | + }) |
| 169 | + }) |
| 170 | + |
| 171 | + describe('reduceMotionEnabled', () => { |
| 172 | + it('should return undefined until promise will be resolved', async () => { |
| 173 | + const {result} = renderHook( |
| 174 | + () => useAccessibilityInfo().reduceMotionEnabled, |
| 175 | + ) |
| 176 | + |
| 177 | + expect(result.current).toBeUndefined() |
| 178 | + }) |
| 179 | + |
| 180 | + it('should return default value', async () => { |
| 181 | + const defaultValue = true |
| 182 | + |
| 183 | + mockIsReduceMotionEnabled.mockResolvedValueOnce(defaultValue) |
| 184 | + |
| 185 | + const {result, waitForNextUpdate} = renderHook( |
| 186 | + () => useAccessibilityInfo().reduceMotionEnabled, |
| 187 | + ) |
| 188 | + |
| 189 | + await waitForNextUpdate() // wait when promise will be resolved |
| 190 | + |
| 191 | + expect(result.current).toBe(defaultValue) |
| 192 | + }) |
| 193 | + |
| 194 | + it('should update value when it change', async () => { |
| 195 | + const newValue = true |
| 196 | + const emit = createEmitChangeEvent('reduceMotionChanged') |
| 197 | + const {result, waitForNextUpdate} = renderHook( |
| 198 | + () => useAccessibilityInfo().reduceMotionEnabled, |
| 199 | + ) |
| 200 | + await waitForNextUpdate() // wait when promise will be resolved |
| 201 | + |
| 202 | + const {current: initial} = result |
| 203 | + |
| 204 | + act(() => emit(newValue)) |
| 205 | + |
| 206 | + const {current: afterUpdate} = result |
| 207 | + |
| 208 | + expect({initial, afterUpdate}).toEqual({ |
| 209 | + initial: false, |
| 210 | + afterUpdate: newValue, |
| 211 | + }) |
| 212 | + }) |
| 213 | + }) |
| 214 | + |
| 215 | + describe('reduceTransparencyEnabled', () => { |
| 216 | + it('should return undefined until promise will be resolved', async () => { |
| 217 | + const {result} = renderHook( |
| 218 | + () => useAccessibilityInfo().reduceTransparencyEnabled, |
| 219 | + ) |
| 220 | + |
| 221 | + expect(result.current).toBeUndefined() |
| 222 | + }) |
| 223 | + |
| 224 | + it('should return default value', async () => { |
| 225 | + const defaultValue = true |
| 226 | + |
| 227 | + mockIsReduceTransparencyEnabled.mockResolvedValueOnce(defaultValue) |
| 228 | + |
| 229 | + const {result, waitForNextUpdate} = renderHook( |
| 230 | + () => useAccessibilityInfo().reduceTransparencyEnabled, |
| 231 | + ) |
| 232 | + |
| 233 | + await waitForNextUpdate() // wait when promise will be resolved |
| 234 | + |
| 235 | + expect(result.current).toBe(defaultValue) |
| 236 | + }) |
| 237 | + |
| 238 | + it('should update value when it change', async () => { |
| 239 | + const newValue = true |
| 240 | + const emit = createEmitChangeEvent('reduceTransparencyChanged') |
| 241 | + const {result, waitForNextUpdate} = renderHook( |
| 242 | + () => useAccessibilityInfo().reduceTransparencyEnabled, |
| 243 | + ) |
| 244 | + await waitForNextUpdate() // wait when promise will be resolved |
| 245 | + |
| 246 | + const {current: initial} = result |
| 247 | + |
| 248 | + act(() => emit(newValue)) |
| 249 | + |
| 250 | + const {current: afterUpdate} = result |
| 251 | + |
| 252 | + expect({initial, afterUpdate}).toEqual({ |
| 253 | + initial: false, |
| 254 | + afterUpdate: newValue, |
| 255 | + }) |
| 256 | + }) |
| 257 | + }) |
| 258 | + |
| 259 | + describe('boldTextEnabled', () => { |
| 260 | + it('should return undefined until promise will be resolved', async () => { |
| 261 | + const {result} = renderHook(() => useAccessibilityInfo().boldTextEnabled) |
| 262 | + |
| 263 | + expect(result.current).toBeUndefined() |
| 264 | + }) |
| 265 | + |
| 266 | + it('should return default value', async () => { |
| 267 | + const defaultValue = true |
| 268 | + |
| 269 | + mockIsBoldTextEnabled.mockResolvedValueOnce(defaultValue) |
| 270 | + |
| 271 | + const {result, waitForNextUpdate} = renderHook( |
| 272 | + () => useAccessibilityInfo().boldTextEnabled, |
| 273 | + ) |
| 274 | + |
| 275 | + await waitForNextUpdate() // wait when promise will be resolved |
| 276 | + |
| 277 | + expect(result.current).toBe(defaultValue) |
| 278 | + }) |
| 279 | + |
| 280 | + it('should update value when it change', async () => { |
| 281 | + const newValue = true |
| 282 | + const emit = createEmitChangeEvent('boldTextChanged') |
| 283 | + const {result, waitForNextUpdate} = renderHook( |
| 284 | + () => useAccessibilityInfo().boldTextEnabled, |
| 285 | + ) |
| 286 | + await waitForNextUpdate() // wait when promise will be resolved |
| 287 | + |
| 288 | + const {current: initial} = result |
| 289 | + |
| 290 | + act(() => emit(newValue)) |
| 291 | + |
| 292 | + const {current: afterUpdate} = result |
| 293 | + |
| 294 | + expect({initial, afterUpdate}).toEqual({ |
| 295 | + initial: false, |
| 296 | + afterUpdate: newValue, |
| 297 | + }) |
| 298 | + }) |
| 299 | + }) |
| 300 | +}) |
0 commit comments