|
1 | 1 | // @flow
|
2 | 2 | import * as React from 'react';
|
3 |
| -import { View, Text, TouchableOpacity } from 'react-native'; |
4 |
| -import { render, fireEvent, waitFor } from '..'; |
| 3 | +import { Text, TouchableOpacity, View } from 'react-native'; |
| 4 | +import { fireEvent, render, waitFor } from '..'; |
| 5 | +import { TimerMode } from './timerUtils'; |
5 | 6 |
|
6 | 7 | class Banana extends React.Component<any> {
|
7 | 8 | changeFresh = () => {
|
@@ -76,39 +77,63 @@ test('waits for element with custom interval', async () => {
|
76 | 77 | // suppress
|
77 | 78 | }
|
78 | 79 |
|
79 |
| - expect(mockFn).toHaveBeenCalledTimes(3); |
| 80 | + expect(mockFn).toHaveBeenCalledTimes(2); |
80 | 81 | });
|
81 | 82 |
|
82 |
| -test('works with legacy fake timers', async () => { |
83 |
| - jest.useFakeTimers('legacy'); |
| 83 | +test.each([TimerMode.Legacy, TimerMode.Modern])( |
| 84 | + 'waits for element until it stops throwing using %s fake timers', |
| 85 | + async (fakeTimerType) => { |
| 86 | + jest.useFakeTimers(fakeTimerType); |
| 87 | + const { getByText, queryByText } = render(<BananaContainer />); |
84 | 88 |
|
85 |
| - const mockFn = jest.fn(() => { |
86 |
| - throw Error('test'); |
87 |
| - }); |
| 89 | + fireEvent.press(getByText('Change freshness!')); |
| 90 | + expect(queryByText('Fresh')).toBeNull(); |
88 | 91 |
|
89 |
| - try { |
90 |
| - waitFor(() => mockFn(), { timeout: 400, interval: 200 }); |
91 |
| - } catch (e) { |
92 |
| - // suppress |
| 92 | + jest.advanceTimersByTime(300); |
| 93 | + const freshBananaText = await waitFor(() => getByText('Fresh')); |
| 94 | + |
| 95 | + expect(freshBananaText.props.children).toBe('Fresh'); |
93 | 96 | }
|
94 |
| - jest.advanceTimersByTime(400); |
| 97 | +); |
95 | 98 |
|
96 |
| - expect(mockFn).toHaveBeenCalledTimes(3); |
97 |
| -}); |
| 99 | +test.each([TimerMode.Legacy, TimerMode.Modern])( |
| 100 | + 'waits for assertion until timeout is met with %s fake timers', |
| 101 | + async (fakeTimerType) => { |
| 102 | + jest.useFakeTimers(fakeTimerType); |
98 | 103 |
|
99 |
| -test('works with fake timers', async () => { |
100 |
| - jest.useFakeTimers('modern'); |
| 104 | + const mockFn = jest.fn(() => { |
| 105 | + throw Error('test'); |
| 106 | + }); |
101 | 107 |
|
102 |
| - const mockFn = jest.fn(() => { |
103 |
| - throw Error('test'); |
104 |
| - }); |
| 108 | + try { |
| 109 | + await waitFor(() => mockFn(), { timeout: 400, interval: 200 }); |
| 110 | + } catch (error) { |
| 111 | + // suppress |
| 112 | + } |
105 | 113 |
|
106 |
| - try { |
107 |
| - waitFor(() => mockFn(), { timeout: 400, interval: 200 }); |
108 |
| - } catch (e) { |
109 |
| - // suppress |
| 114 | + expect(mockFn).toHaveBeenCalledTimes(3); |
110 | 115 | }
|
111 |
| - jest.advanceTimersByTime(400); |
112 |
| - |
113 |
| - expect(mockFn).toHaveBeenCalledTimes(3); |
114 |
| -}); |
| 116 | +); |
| 117 | + |
| 118 | +test.each([TimerMode.Legacy, TimerMode.Legacy])( |
| 119 | + 'awaiting something that succeeds before timeout works with %s fake timers', |
| 120 | + async (fakeTimerType) => { |
| 121 | + jest.useFakeTimers(fakeTimerType); |
| 122 | + |
| 123 | + let calls = 0; |
| 124 | + const mockFn = jest.fn(() => { |
| 125 | + calls += 1; |
| 126 | + if (calls < 3) { |
| 127 | + throw Error('test'); |
| 128 | + } |
| 129 | + }); |
| 130 | + |
| 131 | + try { |
| 132 | + await waitFor(() => mockFn(), { timeout: 400, interval: 200 }); |
| 133 | + } catch (error) { |
| 134 | + // suppress |
| 135 | + } |
| 136 | + |
| 137 | + expect(mockFn).toHaveBeenCalledTimes(3); |
| 138 | + } |
| 139 | +); |
0 commit comments